SmppLibTest.cpp
上传用户:hkcoast
上传日期:2007-01-12
资源大小:979k
文件大小:5k
源码类别:

手机短信编程

开发平台:

Visual C++

  1. // SmppLibTest.cpp: implementation of the CSmppLibTest class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "TestServer.h"
  6. #include "SmppLibTest.h"
  7. #include "IOCPServer.h"
  8. #include "..common.h"
  9. #include "..smpp.h"
  10. #include "..smpppacket.h"
  11. #ifdef _DEBUG
  12. #undef THIS_FILE
  13. static char THIS_FILE[]=__FILE__;
  14. #define new DEBUG_NEW
  15. #endif
  16. //////////////////////////////////////////////////////////////////////
  17. // Construction/Destruction
  18. //////////////////////////////////////////////////////////////////////
  19. CSmppLibTest::CSmppLibTest(CIOCPServer *pIOCPServer)
  20. {
  21. m_pIOCPServer = pIOCPServer;
  22. }
  23. CSmppLibTest::~CSmppLibTest()
  24. {
  25. }
  26. //create the packet here, and pass to processpacket
  27. void CSmppLibTest::parsePacket(ClientContext *pContext, PBYTE pby, int nsz)
  28. {
  29. TRACE1("parse packet %dn", nsz);
  30. if (nsz < 16)
  31. return;
  32. uint32 cmdId = readInt(pby);
  33. TCHAR buff[100];
  34. sprintf(buff, "MessageId is %xn", cmdId);
  35. TRACE(buff);
  36. int cmdStatus = readInt(pby+4);
  37. int seqNum = readInt(pby+8);
  38. switch (cmdId)
  39. {
  40. case SMPP_BIND_TRANSMITTER:
  41. {
  42. CBindTransmitter* ppak;
  43. ppak = new CBindTransmitter();
  44. ppak->loadPacket(pby, nsz);
  45. CBindTransmitterResp pak;
  46. sendPacket(pContext, pak);
  47. delete ppak;
  48. }
  49. break;
  50. case SMPP_BIND_TRANSMITTER_RESP:
  51. {
  52. CBindTransmitterResp* ppak;
  53. ppak = new CBindTransmitterResp();
  54. ppak->loadPacket(pby, nsz);
  55. delete ppak;
  56. }
  57. break;
  58. case SMPP_BIND_RECEIVER:
  59. {
  60. CBindReceiver* ppak;
  61. ppak = new CBindReceiver();
  62. ppak->loadPacket(pby, nsz);
  63. CBindReceiverResp pak;
  64. sendPacket(pContext, pak);
  65. delete ppak;
  66. }
  67. break;
  68. case SMPP_BIND_RECEIVER_RESP:
  69. {
  70. CBindReceiverResp* ppak;
  71. ppak = new CBindReceiverResp();
  72. ppak->loadPacket(pby, nsz);
  73. delete ppak;
  74. }
  75. break;
  76. case SMPP_BIND_TRANSCEIVER:
  77. {
  78. CBindTransceiver* ppak;
  79. ppak = new CBindTransceiver();
  80. ppak->loadPacket(pby, nsz);
  81. TRACE(ppak->getSystemId());
  82. TRACE("n");
  83. TRACE(ppak->getSystemType());
  84. TRACE("n");
  85. TRACE(ppak->getPassword());
  86. TRACE("n");
  87. CSmppAddress src_adr;
  88. src_adr = ppak->getSourceRange();
  89. TRACE(src_adr.m_addr);
  90. TRACE("n");
  91. //reply
  92. CBindTransceiverResp pak;
  93. pak.setSystemId("KOTY-AB");
  94. pak.setCommandStatus(0);
  95. sendPacket(pContext, pak);
  96. delete ppak;
  97. }
  98. break;
  99. case SMPP_BIND_TRANSCEIVER_RESP:
  100. {
  101. CBindTransceiverResp* ppak;
  102. ppak = new CBindTransceiverResp();
  103. ppak->loadPacket(pby, nsz);
  104. delete ppak;
  105. }
  106. break;
  107. case SMPP_SUBMIT_SM:
  108. {
  109. CSubmitSM* ppak;
  110. ppak = new CSubmitSM();
  111. ppak->loadPacket(pby, nsz);
  112. CSmppAddress src = ppak->getSource();
  113. CSmppAddress dst = ppak->getDestination();
  114. TRACE("Source address is:n");
  115. TRACE(src.m_addr);
  116. TRACE("n");
  117. TRACE("Destination address is:n");
  118. TRACE(dst.m_addr);
  119. TRACE("n");
  120. TRACE("Message is:n");
  121. PBYTE msg;
  122. uint32 nsize;
  123. ppak->getMessage(msg, nsize);
  124. LPSTR pmsg = new char[nsize+1];
  125. memcpy(pmsg, msg, nsize);
  126. pmsg[nsize] = 0;
  127. TRACE1("%sn", pmsg);
  128. delete pmsg;
  129. delete msg;
  130. //reply
  131. CSubmitSMResp pak;
  132. pak.setMessageId("EABC3141414521265254562553");
  133. pak.setCommandStatus(0);
  134. sendPacket(pContext, pak);
  135. delete ppak;
  136. }
  137. break;
  138. case SMPP_SUBMIT_SM_RESP:
  139. {
  140. CSubmitSMResp* ppak;
  141. ppak = new CSubmitSMResp();
  142. ppak->loadPacket(pby, nsz);
  143. delete ppak;
  144. }
  145. break;
  146. case SMPP_DELIVER_SM:
  147. {
  148. CDeliverSM* ppak;
  149. ppak = new CDeliverSM();
  150. ppak->loadPacket(pby, nsz);
  151. delete ppak;
  152. }
  153. break;
  154. case SMPP_DELIVER_SM_RESP:
  155. {
  156. CDeliverSMResp* ppak;
  157. ppak = new CDeliverSMResp();
  158. ppak->loadPacket(pby, nsz);
  159. delete ppak;
  160. }
  161. break;
  162. case SMPP_UNBIND:
  163. {
  164. TRACE("Unbind Received");
  165. CUnbind* ppak;
  166. ppak = new CUnbind();
  167. ppak->loadPacket(pby, nsz);
  168. //reply
  169. CUnbindResp pak;
  170. pak.setCommandStatus(0);
  171. sendPacket(pContext, pak);
  172. delete ppak;
  173. }
  174. break;
  175. case SMPP_UNBIND_RESP:
  176. {
  177. CUnbindResp* ppak;
  178. ppak = new CUnbindResp();
  179. ppak->loadPacket(pby, nsz);
  180. delete ppak;
  181. }
  182. break;
  183. case SMPP_ENQUIRE_LINK:
  184. {
  185. CEnquireLink* ppak;
  186. ppak = new CEnquireLink();
  187. ppak->loadPacket(pby, nsz);
  188. CEnquireLinkResp pak;
  189. pak.setCommandStatus(0);
  190. sendPacket(pContext, pak);
  191. delete ppak;
  192. //testing deliversm
  193. CDeliverSM dsm;
  194. CString msg = "Hello, how are you?";
  195. CSmppAddress adr(1, 2, "567890");
  196. dsm.setDataCoding(4);
  197. dsm.setSource(adr);
  198. dsm.setDestination(adr);
  199. dsm.setMessage((PBYTE)msg.GetBuffer(0), msg.GetLength());
  200. sendPacket(pContext, dsm);
  201. }
  202. break;
  203. case SMPP_QUERY_SM:
  204. {
  205. CQuerySM* ppak;
  206. ppak = new CQuerySM();
  207. ppak->loadPacket(pby, nsz);
  208. CQuerySMResp pak;
  209. pak.setErrorCode(11);
  210. pak.setMessageId("BCAAA213");
  211. sendPacket(pContext, pak);
  212. }
  213. break;
  214. default:
  215. break;
  216. } //switch
  217. } //parse packet
  218. bool CSmppLibTest::sendPacket(ClientContext *pContext, CPacketBase &pak)
  219. {
  220. bool ret = true;
  221. PBYTE pby;
  222. int nsz;
  223. pak.encodeBody(pby, nsz);
  224. m_pIOCPServer->Send(pContext, pby, nsz);
  225. delete [] pby;
  226. return ret;
  227. }