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

手机短信编程

开发平台:

Visual C++

  1. // EsmeReceiverCom.cpp : Implementation of CEsmeReceiverCom
  2. #include "stdafx.h"
  3. #include "EsmeReceiverCom.h"
  4. // CEsmeReceiverCom
  5. STDMETHODIMP CEsmeReceiverCom::bind(BSTR sysid, BSTR passwd, BSTR systype, ISmppAddressCom* iaddr, VARIANT_BOOL* pret)
  6. {
  7. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  8. // TODO: Add your implementation code here
  9. USES_CONVERSION;
  10. LPSTR sid, pwd, stype;
  11. sid = OLE2A(sysid);
  12. pwd = OLE2A(passwd);
  13. stype = OLE2A(systype);
  14. SHORT npi;
  15. SHORT ton;
  16. BSTR addr;
  17. iaddr->get_NPI(&npi);
  18. iaddr->get_TON(&ton);
  19. iaddr->get_Address(&addr);
  20. LPSTR paddr = OLE2A(addr);
  21. CSmppAddress srange(ton, npi, paddr);
  22. SysFreeString(addr);
  23. //setting finished, going to bind to SMSC
  24. EnterCriticalSection(&m_cs); //enter m_cs
  25. ResetEvent(m_response_event);
  26. CEsmeReceiver::bind(sid, pwd, stype, srange);
  27. HANDLE hwait[] = {m_response_event, m_hDisconnectEvent, m_hKillEvent};
  28. DWORD ret = WaitForMultipleObjects(3, hwait, FALSE, 30000);
  29. if (ret == WAIT_OBJECT_0)
  30. {
  31. if (m_last_error)
  32. *pret = VARIANT_FALSE;
  33. else
  34. *pret = VARIANT_TRUE;
  35. }
  36. else
  37. *pret = VARIANT_FALSE;
  38. ResetEvent(m_response_event);
  39. LeaveCriticalSection(&m_cs); //leave m_cs
  40. return S_OK;
  41. }
  42. STDMETHODIMP CEsmeReceiverCom::unbind(VARIANT_BOOL* pret)
  43. {
  44. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  45. // TODO: Add your implementation code here
  46. EnterCriticalSection(&m_cs); //enter m_cs
  47. ResetEvent(m_response_event);
  48. CEsmeReceiver::unbind();
  49. HANDLE hwait[] = {m_response_event, m_hDisconnectEvent, m_hKillEvent};
  50. DWORD ret = WaitForMultipleObjects(3, hwait, FALSE, 30000);
  51. if (ret == WAIT_OBJECT_0)
  52. {
  53. if (m_last_error)
  54. *pret = VARIANT_FALSE;
  55. else
  56. *pret = VARIANT_TRUE;
  57. }
  58. else
  59. *pret = VARIANT_FALSE;
  60. ResetEvent(m_response_event);
  61. LeaveCriticalSection(&m_cs); //leave m_cs
  62. return S_OK;
  63. }
  64. STDMETHODIMP CEsmeReceiverCom::enquireLink(VARIANT_BOOL* pret)
  65. {
  66. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  67. // TODO: Add your implementation code here
  68. EnterCriticalSection(&m_cs); //enter m_cs
  69. ResetEvent(m_response_event);
  70. CEsmeReceiver::enquireLink();
  71. HANDLE hwait[] = {m_response_event, m_hDisconnectEvent, m_hKillEvent};
  72. DWORD ret = WaitForMultipleObjects(3, hwait, FALSE, 30000);
  73. if (ret == WAIT_OBJECT_0)
  74. {
  75. if (m_last_error)
  76. *pret = VARIANT_FALSE;
  77. else
  78. *pret = VARIANT_TRUE;
  79. }
  80. else
  81. *pret = VARIANT_FALSE;
  82. ResetEvent(m_response_event);
  83. LeaveCriticalSection(&m_cs); //leave m_cs
  84. return S_OK;
  85. }
  86. STDMETHODIMP CEsmeReceiverCom::init(BSTR svrip, LONG port)
  87. {
  88. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  89. // TODO: Add your implementation code here
  90. USES_CONVERSION;
  91. LPSTR sip = OLE2A(svrip);
  92. CEsmeReceiver::init(sip, port);
  93. return S_OK;
  94. }
  95. STDMETHODIMP CEsmeReceiverCom::close(void)
  96. {
  97. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  98. // TODO: Add your implementation code here
  99. CEsmeReceiver::close();
  100. return S_OK;
  101. }
  102. STDMETHODIMP CEsmeReceiverCom::get_Connected(VARIANT_BOOL* pVal)
  103. {
  104. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  105. // TODO: Add your implementation code here
  106. if (CEsmeReceiver::isConnected())
  107. {
  108. *pVal = VARIANT_TRUE;
  109. }
  110. else
  111. {
  112. *pVal = VARIANT_FALSE;
  113. }
  114. return S_OK;
  115. }
  116. //registered callback to handle SMPP packets sent from SMSC
  117. //(must be static class method or global procedure)
  118. void __stdcall CEsmeReceiverCom::processPacketProc(CPacketBase *pak, LPVOID param)
  119. {
  120. CEsmeReceiverCom *pRecv = (CEsmeReceiverCom *) param;
  121. //route to instance method, so it can access instance attributes
  122. pRecv->processPacket(pak);
  123. }
  124. //actuallly SMPP packets are in turns handled here 
  125. void CEsmeReceiverCom::processPacket(CPacketBase *pak)
  126. {
  127. switch (pak->getCommandId())
  128. {
  129. case SMPP_ENQUIRE_LINK:
  130. {
  131. //SMSC requested us to send querylink response
  132. CEnquireLink *pPak;
  133. pPak = static_cast<CEnquireLink *>(pak);
  134. //automatic reponse for enquery link
  135. CEnquireLinkResp elresp(*pPak);
  136. sendPacket(elresp);
  137. }
  138. break;
  139. case SMPP_DELIVER_SM:
  140. {
  141. //got a deliver SM
  142. CDeliverSM *pPak;
  143. pPak = static_cast<CDeliverSM *>(pak);
  144. //automatic response deliversm reponse
  145. CDeliverSMResp dresp (*pPak);
  146. sendPacket(dresp);
  147. //notify client for the deliver SM
  148. NotifyClientDeliverSM(pPak);
  149. }
  150. break;
  151. case SMPP_BIND_RECEIVER_RESP:
  152. {
  153. //bind receiver response
  154. CBindReceiverResp *pPak;
  155. pPak = static_cast<CBindReceiverResp *>(pak);
  156. if (pPak->getCommandStatus() == 0)
  157. {
  158. m_last_error = false;
  159. }
  160. else
  161. {
  162. m_last_error = true;
  163. }
  164. SetEvent(m_response_event);
  165. }
  166. break;
  167. case SMPP_UNBIND_RESP:
  168. {
  169. //unbind response
  170. CUnbindResp *pPak;
  171. pPak = static_cast<CUnbindResp *>(pak);
  172. if (pPak->getCommandStatus() == 0)
  173. {
  174. m_last_error = false;
  175. }
  176. else
  177. {
  178. m_last_error = true;
  179. }
  180. SetEvent(m_response_event);
  181. }
  182. break;
  183. case SMPP_ENQUIRE_LINK_RESP:
  184. {
  185. //Previous EnquireLink is responsed
  186. CEnquireLinkResp *pPak;
  187. pPak = static_cast<CEnquireLinkResp *>(pak);
  188. if (pPak->getCommandStatus() == 0)
  189. {
  190. m_last_error = false;
  191. SetEvent(m_response_event);
  192. }
  193. else
  194. {
  195. m_last_error = true;
  196. }
  197. SetEvent(m_response_event);
  198. }
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. void CEsmeReceiverCom::NotifyClientDeliverSM(CDeliverSM *pak)
  205. {
  206. IDeliverSMCom *pVal;
  207. //create an CDeliverSMCom instance to return
  208. CoCreateInstance( CLSID_DeliverSMCom,
  209. NULL,
  210. CLSCTX_ALL,
  211. IID_IDeliverSMCom,
  212. (void **) &pVal);
  213. USES_CONVERSION;
  214. pVal->put_dataCoding(pak->getDataCoding());
  215. pVal->put_esmClass(pak->getEsmClass());
  216. pVal->put_replaceIfPresent(pak->getReplaceIfPresent());
  217. pVal->put_priorityFlag(pak->getPriorityFlag());
  218. pVal->put_protocolID(pak->getProtocolId());
  219. pVal->put_smDefaultMsgId(pak->getSmDefaultMsgId());
  220. pVal->put_registeredDelivery(pak->getRegisteredDelivery());
  221. //create an CSmppAddressCom for setting addresses
  222. ISmppAddressCom *iaddr;
  223. CoCreateInstance( CLSID_SmppAddressCom,
  224. NULL,
  225. CLSCTX_ALL,
  226. IID_ISmppAddressCom,
  227. (void **) &iaddr);
  228. BSTR addrstr;
  229. //Setting source
  230. CSmppAddress source = pak->getSource();
  231. iaddr->put_TON(source.m_addr_ton);
  232. iaddr->put_NPI(source.m_addr_npi);
  233. addrstr = source.m_addr.AllocSysString();
  234. iaddr->put_Address(addrstr);
  235. SysFreeString(addrstr);
  236. pVal->put_Source(iaddr);
  237. //Setting Destination
  238. CSmppAddress destination = pak->getDestination();
  239. iaddr->put_TON(destination.m_addr_ton);
  240. iaddr->put_NPI(destination.m_addr_npi);
  241. addrstr = destination.m_addr.AllocSysString();
  242. iaddr->put_Address(addrstr);
  243. SysFreeString(addrstr);
  244. pVal->put_Destination(iaddr);
  245. iaddr->Release();
  246. //create an CSmppDate for setting dates
  247. ISmppDateCom *idate;
  248. CoCreateInstance( CLSID_SmppDateCom,
  249. NULL,
  250. CLSCTX_ALL,
  251. IID_ISmppDateCom,
  252. (void **) &idate);
  253. BSTR strdate;
  254. //setting scheduleddelivery
  255. CSmppDate schdate = pak->getScheduledDelivery();
  256. strdate = schdate.toString().AllocSysString();
  257. idate->setDate(strdate);
  258. pVal->put_scheduledDelivery(idate);
  259. SysFreeString(strdate);
  260. //setting validityperiod
  261. CSmppDate vldprd = pak->getValidityPeriod();
  262. strdate = vldprd.toString().AllocSysString();
  263. idate->setDate(strdate);
  264. pVal->put_validityPeriod(idate);
  265. SysFreeString(strdate);
  266. idate->Release();
  267. //setting message
  268. PBYTE pdata;
  269. uint32 nsize;
  270. pak->getMessage(pdata, nsize);
  271. VARIANT var;
  272. VariantInit(&var);
  273. var.vt = VT_ARRAY | VT_UI1;
  274. SAFEARRAY *psa;
  275. SAFEARRAYBOUND bounds = {nsize, 0};
  276. psa = SafeArrayCreate(VT_UI1, 1, &bounds);
  277. BYTE *padata;
  278. SafeArrayAccessData(psa, (void **) &padata);
  279. memcpy(padata, pdata, nsize);
  280. SafeArrayUnaccessData(psa);
  281. var.parray = psa;
  282. pVal->setMessage(var);
  283. //fire notification event
  284. Fire_OnDeliverSM(pVal);
  285. }