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

手机短信编程

开发平台:

Visual C++

  1. // EsmeTransmitterCom.cpp : Implementation of CEsmeTransmitterCom
  2. #include "stdafx.h"
  3. #include "EsmeTransmitterCom.h"
  4. // CEsmeTransmitterCom
  5. STDMETHODIMP CEsmeTransmitterCom::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. CEsmeTransmitter::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 CEsmeTransmitterCom::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. CEsmeTransmitter::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 CEsmeTransmitterCom::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. CEsmeTransmitter::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 CEsmeTransmitterCom::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. CEsmeTransmitter::init(sip, port);
  93. return S_OK;
  94. }
  95. STDMETHODIMP CEsmeTransmitterCom::close(void)
  96. {
  97. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  98. // TODO: Add your implementation code here
  99. CEsmeTransmitter::close();
  100. return S_OK;
  101. }
  102. STDMETHODIMP CEsmeTransmitterCom::get_Connected(VARIANT_BOOL* pVal)
  103. {
  104. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  105. // TODO: Add your implementation code here
  106. if (CEsmeTransmitter::isConnected())
  107. {
  108. *pVal = VARIANT_TRUE;
  109. }
  110. else
  111. {
  112. *pVal = VARIANT_FALSE;
  113. }
  114. return S_OK;
  115. }
  116. STDMETHODIMP CEsmeTransmitterCom::submitMessage(ISubmitSMCom* isubmit, BSTR* pMsgid, VARIANT_BOOL* pret)
  117. {
  118. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  119. // TODO: Add your implementation code here
  120. CSubmitSM sm;
  121. USES_CONVERSION;
  122. BSTR svrtype;
  123. isubmit->get_ServiceType(&svrtype);
  124. LPSTR stype = OLE2A(svrtype);
  125. sm.setServiceType(stype);
  126. SHORT dc, esm, repif, priflag, proid, defmsg, regdel;
  127. isubmit->get_dataCoding(&dc);
  128. isubmit->get_esmClass(&esm);
  129. isubmit->get_replaceIfPresent(&repif);
  130. isubmit->get_priorityFlag(&priflag);
  131. isubmit->get_protocolID(&proid);
  132. isubmit->get_smDefaultMsgId(&defmsg);
  133. isubmit->get_registeredDelivery(&regdel);
  134. sm.setDataCoding(dc);
  135. sm.setEsmClass(esm);
  136. sm.setReplaceIfPresent(repif);
  137. sm.setPriorityFlag(priflag);
  138. sm.setProtocolId(proid);
  139. sm.setSmDefaultMsgId(defmsg);
  140. sm.setRegisteredDelivery(regdel);
  141. SHORT ton, npi;
  142. BSTR addr;
  143. ISmppAddressCom *iaddr;
  144. //getting source
  145. isubmit->get_Source(&iaddr);
  146. iaddr->get_TON(&ton);
  147. iaddr->get_NPI(&npi);
  148. iaddr->get_Address(&addr);
  149. iaddr->Release();
  150. LPSTR paddr = OLE2A(addr);
  151. SysFreeString(addr);
  152. CSmppAddress src(ton, npi, paddr);
  153. //getting destination
  154. isubmit->get_Destination(&iaddr);
  155. iaddr->get_TON(&ton);
  156. iaddr->get_NPI(&npi);
  157. iaddr->get_Address(&addr);
  158. iaddr->Release();
  159. paddr = OLE2A(addr);
  160. SysFreeString(addr);
  161. CSmppAddress dst(ton, npi, paddr);
  162. //assing source and destination
  163. sm.setSource(src);
  164. sm.setDestination(dst);
  165. ISmppDateCom *idate;
  166. BSTR strdate;
  167. //getting ScheduledDelivery
  168. isubmit->get_scheduledDelivery(&idate);
  169. idate->toString(&strdate);
  170. LPSTR sdt = OLE2A(strdate);
  171. idate->Release();
  172. CSmppDate schdel;
  173. schdel.setDate(sdt);
  174. //getting ValidityPeriod
  175. isubmit->get_validityPeriod(&idate);
  176. idate->toString(&strdate);
  177. sdt = OLE2A(strdate);
  178. idate->Release();
  179. CSmppDate valprd;
  180. valprd.setDate(sdt);
  181. //assign scheduledDelivery and validityPeriod to sm
  182. sm.setScheduledDelivery(schdel);
  183. sm.setValidityPeriod(valprd);
  184. VARIANT var;
  185. isubmit->getMessage(&var);
  186. SAFEARRAY *psa = var.parray;
  187. BYTE *padata;
  188. int nsize = psa->rgsabound->cElements;
  189. SafeArrayAccessData(psa, (void **) &padata);
  190. sm.setMessage(padata, nsize);
  191. SafeArrayUnaccessData(psa);
  192. //
  193. //okay, we've setting up the SubmitSM, send it
  194. //
  195. EnterCriticalSection(&m_cs); //enter m_cs
  196. ResetEvent(m_response_event);
  197. CEsmeTransmitter::submitMessage(sm);
  198. HANDLE hwait[] = {m_response_event, m_hDisconnectEvent, m_hKillEvent};
  199. DWORD ret = WaitForMultipleObjects(3, hwait, FALSE, 30000);
  200. if (ret == WAIT_OBJECT_0)
  201. {
  202. if (m_last_error)
  203. *pret = VARIANT_FALSE;
  204. else
  205. {
  206. *pret = VARIANT_TRUE;
  207. *pMsgid = m_last_msg_id.AllocSysString();
  208. }
  209. }
  210. else
  211. *pret = VARIANT_FALSE;
  212. ResetEvent(m_response_event);
  213. LeaveCriticalSection(&m_cs); //leave m_cs
  214. return S_OK;
  215. }
  216. //registered callback to handle SMPP packets sent from SMSC
  217. //(must be static class method or global procedure)
  218. void __stdcall CEsmeTransmitterCom::processPacketProc(CPacketBase *pak, LPVOID param)
  219. {
  220. CEsmeTransmitterCom *pTrans = (CEsmeTransmitterCom *) param;
  221. //route to instance method, so it can access instance attributes
  222. pTrans->processPacket(pak);
  223. }
  224. //actuallly SMPP packets are in turns handled here 
  225. void CEsmeTransmitterCom::processPacket(CPacketBase *pak)
  226. {
  227. switch (pak->getCommandId())
  228. {
  229. case SMPP_ENQUIRE_LINK:
  230. {
  231. //SMSC requested us to send querylink response
  232. CEnquireLink *pPak;
  233. pPak = static_cast<CEnquireLink *>(pak);
  234. //automatic reponse for enquery link
  235. CEnquireLinkResp elresp(*pPak);
  236. sendPacket(elresp);
  237. }
  238. break;
  239. case SMPP_BIND_TRANSMITTER_RESP:
  240. {
  241. //bind transmitter response
  242. CBindTransmitterResp *pPak;
  243. pPak = static_cast<CBindTransmitterResp *>(pak);
  244. if (pPak->getCommandStatus() == 0)
  245. {
  246. m_last_error = false;
  247. }
  248. else
  249. {
  250. m_last_error = true;
  251. }
  252. SetEvent(m_response_event);
  253. }
  254. break;
  255. case SMPP_UNBIND_RESP:
  256. {
  257. //unbind response
  258. CUnbindResp *pPak;
  259. pPak = static_cast<CUnbindResp *>(pak);
  260. if (pPak->getCommandStatus() == 0)
  261. {
  262. m_last_error = false;
  263. }
  264. else
  265. {
  266. m_last_error = true;
  267. }
  268. SetEvent(m_response_event);
  269. }
  270. break;
  271. case SMPP_SUBMIT_SM_RESP:
  272. {
  273. //Submit Short Message Response
  274. CSubmitSMResp *pPak;
  275. pPak = static_cast<CSubmitSMResp *>(pak);
  276. if (pPak->getCommandStatus() == 0)
  277. {
  278. m_last_msg_id = pPak->getMessageId();
  279. m_last_error = false;
  280. }
  281. else
  282. {
  283. m_last_error = true;
  284. }
  285. SetEvent(m_response_event);
  286. }
  287. break;
  288. case SMPP_ENQUIRE_LINK_RESP:
  289. {
  290. //Previous EnquireLink is responsed
  291. CEnquireLinkResp *pPak;
  292. pPak = static_cast<CEnquireLinkResp *>(pak);
  293. if (pPak->getCommandStatus() == 0)
  294. {
  295. m_last_error = false;
  296. SetEvent(m_response_event);
  297. }
  298. else
  299. {
  300. m_last_error = true;
  301. }
  302. SetEvent(m_response_event);
  303. }
  304. break;
  305. default:
  306. break;
  307. }
  308. }