SmsTraffic.cpp
上传用户:hcymiss
上传日期:2009-09-17
资源大小:121k
文件大小:7k
源码类别:

手机短信编程

开发平台:

Visual C++

  1. // SmsTraffic.cpp: implementation of the CSmsTraffic class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "SmsTest.h"
  6. #include "SmsTraffic.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CSmsTraffic::CSmsTraffic()
  16. {
  17. m_nSendIn = 0;
  18. m_nSendOut = 0;
  19. m_nRecvIn = 0;
  20. m_nRecvOut = 0;
  21. m_hKillThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  22. m_hThreadKilledEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  23. InitializeCriticalSection(&m_csSend);
  24. InitializeCriticalSection(&m_csRecv);
  25. // 启动子线程
  26. AfxBeginThread(SmThread, this, THREAD_PRIORITY_NORMAL);
  27. }
  28. CSmsTraffic::~CSmsTraffic()
  29. {
  30. SetEvent(m_hKillThreadEvent); // 发出关闭子线程的信号
  31. WaitForSingleObject(m_hThreadKilledEvent, INFINITE); // 等待子线程关闭
  32. DeleteCriticalSection(&m_csSend);
  33. DeleteCriticalSection(&m_csRecv);
  34. CloseHandle(m_hKillThreadEvent);
  35. CloseHandle(m_hThreadKilledEvent);
  36. }
  37. // 将一条短消息放入发送队列
  38. void CSmsTraffic::PutSendMessage(SM_PARAM* pparam)
  39. {
  40. EnterCriticalSection(&m_csSend);
  41. memcpy(&m_SmSend[m_nSendIn], pparam, sizeof(SM_PARAM));
  42. m_nSendIn++;
  43. if (m_nSendIn >= MAX_SM_SEND)  m_nSendIn = 0;
  44. LeaveCriticalSection(&m_csSend);
  45. }
  46. // 从发送队列中取一条短消息
  47. BOOL CSmsTraffic::GetSendMessage(SM_PARAM* pparam)
  48. {
  49. BOOL fSuccess = FALSE;
  50. EnterCriticalSection(&m_csSend);
  51. if (m_nSendOut != m_nSendIn)
  52. {
  53. memcpy(pparam, &m_SmSend[m_nSendOut], sizeof(SM_PARAM));
  54. m_nSendOut++;
  55. if (m_nSendOut >= MAX_SM_SEND)  m_nSendOut = 0;
  56. fSuccess = TRUE;
  57. }
  58. LeaveCriticalSection(&m_csSend);
  59. return fSuccess;
  60. }
  61. // 将短消息放入接收队列
  62. void CSmsTraffic::PutRecvMessage(SM_PARAM* pparam, int nCount)
  63. {
  64. EnterCriticalSection(&m_csRecv);
  65. for (int i = 0; i < nCount; i++)
  66. {
  67. memcpy(&m_SmRecv[m_nRecvIn], pparam, sizeof(SM_PARAM));
  68. m_nRecvIn++;
  69. if (m_nRecvIn >= MAX_SM_RECV)  m_nRecvIn = 0;
  70. pparam++;
  71. }
  72. LeaveCriticalSection(&m_csRecv);
  73. }
  74. // 从接收队列中取一条短消息
  75. BOOL CSmsTraffic::GetRecvMessage(SM_PARAM* pparam)
  76. {
  77. BOOL fSuccess = FALSE;
  78. EnterCriticalSection(&m_csRecv);
  79. if (m_nRecvOut != m_nRecvIn)
  80. {
  81. memcpy(pparam, &m_SmRecv[m_nRecvOut], sizeof(SM_PARAM));
  82. m_nRecvOut++;
  83. if (m_nRecvOut >= MAX_SM_RECV)  m_nRecvOut = 0;
  84. fSuccess = TRUE;
  85. }
  86. LeaveCriticalSection(&m_csRecv);
  87. return fSuccess;
  88. }
  89. UINT CSmsTraffic::SmThread(LPVOID lParam)
  90. {
  91. CSmsTraffic* p=(CSmsTraffic *)lParam; // this
  92. int nMsg; // 收到短消息条数
  93. int nDelete; // 目前正在删除的短消息编号
  94. SM_BUFF buff; // 接收短消息列表的缓冲区
  95. SM_PARAM param[256]; // 发送/接收短消息缓冲区
  96. CTime tmOrg, tmNow; // 上次和现在的时间,计算超时用
  97. enum {
  98. stBeginRest, // 开始休息/延时
  99. stContinueRest, // 继续休息/延时
  100. stSendMessageRequest, // 发送短消息
  101. stSendMessageResponse, // 读取短消息列表到缓冲区
  102. stSendMessageWaitIdle, // 发送不成功,等待GSM就绪
  103. stReadMessageRequest, // 发送读取短消息列表的命令
  104. stReadMessageResponse, // 读取短消息列表到缓冲区
  105. stDeleteMessageRequest, // 删除短消息
  106. stDeleteMessageResponse, // 删除短消息
  107. stDeleteMessageWaitIdle, // 删除不成功,等待GSM就绪
  108. stExitThread // 退出
  109. } nState; // 处理过程的状态
  110. // 初始状态
  111. nState = stBeginRest;
  112. // 发送和接收处理的状态循环
  113. while (nState != stExitThread)
  114. {
  115. switch(nState)
  116. {
  117. case stBeginRest:
  118. // TRACE("State=stBeginRestn");
  119. tmOrg = CTime::GetCurrentTime();
  120. nState = stContinueRest;
  121. break;
  122. case stContinueRest:
  123. // TRACE("State=stContinueRestn");
  124. Sleep(300);
  125. tmNow = CTime::GetCurrentTime();
  126. if (p->GetSendMessage(&param[0]))
  127. {
  128. nState = stSendMessageRequest; // 有待发短消息,就不休息了
  129. }
  130. else if (tmNow - tmOrg >= 5) // 待发短消息队列空,休息5秒
  131. {
  132. nState = stReadMessageRequest; // 转到读取短消息状态
  133. }
  134. break;
  135. case stSendMessageRequest:
  136. // TRACE("State=stSendMessageRequestn");
  137. gsmSendMessage(&param[0]);
  138. memset(&buff, 0, sizeof(buff));
  139. tmOrg = CTime::GetCurrentTime();
  140. nState = stSendMessageResponse;
  141. break;
  142. case stSendMessageResponse:
  143. // TRACE("State=stSendMessageResponsen");
  144. Sleep(100);
  145. tmNow = CTime::GetCurrentTime();
  146. switch (gsmGetResponse(&buff))
  147. {
  148. case GSM_OK: 
  149. // TRACE("  GSM_OK %dn", tmNow - tmOrg);
  150. nState = stBeginRest;
  151. break;
  152. case GSM_ERR:
  153. // TRACE("  GSM_ERR %dn", tmNow - tmOrg);
  154. nState = stSendMessageWaitIdle;
  155. break;
  156. default:
  157. // TRACE("  GSM_WAIT %dn", tmNow - tmOrg);
  158. if (tmNow - tmOrg >= 10) // 10秒超时
  159. {
  160. // TRACE("  Timeout!n");
  161. nState = stSendMessageWaitIdle;
  162. }
  163. }
  164. break;
  165. case stSendMessageWaitIdle:
  166. Sleep(500);
  167. nState = stSendMessageRequest; // 直到发送成功为止
  168. break;
  169. case stReadMessageRequest:
  170. // TRACE("State=stReadMessageRequestn");
  171. gsmReadMessageList();
  172. memset(&buff, 0, sizeof(buff));
  173. tmOrg = CTime::GetCurrentTime();
  174. nState = stReadMessageResponse;
  175. break;
  176. case stReadMessageResponse:
  177. // TRACE("State=stReadMessageResponsen");
  178. Sleep(100);
  179. tmNow = CTime::GetCurrentTime();
  180. switch (gsmGetResponse(&buff))
  181. {
  182. case GSM_OK: 
  183. // TRACE("  GSM_OK %dn", tmNow - tmOrg);
  184. nMsg = gsmParseMessageList(param, &buff);
  185. if (nMsg > 0)
  186. {
  187. p->PutRecvMessage(param, nMsg);
  188. nDelete = 0;
  189. nState = stDeleteMessageRequest;
  190. }
  191. else
  192. {
  193. nState = stBeginRest;
  194. }
  195. break;
  196. case GSM_ERR:
  197. // TRACE("  GSM_ERR %dn", tmNow - tmOrg);
  198. nState = stBeginRest;
  199. break;
  200. default:
  201. // TRACE("  GSM_WAIT %dn", tmNow - tmOrg);
  202. if (tmNow - tmOrg >= 15) // 15秒超时
  203. {
  204. // TRACE("  Timeout!n");
  205. nState = stBeginRest;
  206. }
  207. }
  208. break;
  209. case stDeleteMessageRequest:
  210. // TRACE("State=stDeleteMessageRequestn");
  211. if (nDelete < nMsg)
  212. {
  213. gsmDeleteMessage(param[nDelete].index);
  214. memset(&buff, 0, sizeof(buff));
  215. tmOrg = CTime::GetCurrentTime();
  216. nState = stDeleteMessageResponse;
  217. }
  218. else
  219. {
  220. nState = stBeginRest;
  221. }
  222. break;
  223. case stDeleteMessageResponse:
  224. // TRACE("State=stDeleteMessageResponsen");
  225. Sleep(100);
  226. tmNow = CTime::GetCurrentTime();
  227. switch (gsmGetResponse(&buff))
  228. {
  229. case GSM_OK: 
  230. // TRACE("  GSM_OK %dn", tmNow - tmOrg);
  231. nDelete++;
  232. nState = stDeleteMessageRequest;
  233. break;
  234. case GSM_ERR:
  235. // TRACE("  GSM_ERR %dn", tmNow - tmOrg);
  236. nState = stDeleteMessageWaitIdle;
  237. break;
  238. default:
  239. // TRACE("  GSM_WAIT %dn", tmNow - tmOrg);
  240. if (tmNow - tmOrg >= 5) // 5秒超时
  241. {
  242. // TRACE("  Timeout!n");
  243. nState = stBeginRest;
  244. }
  245. }
  246. break;
  247. case stDeleteMessageWaitIdle:
  248. // TRACE("State=stDeleteMessageWaitIdlen");
  249. Sleep(500);
  250. nState = stDeleteMessageRequest; // 直到删除成功为止
  251. break;
  252. }
  253. // 检测是否有关闭本线程的信号
  254. DWORD dwEvent = WaitForSingleObject(p->m_hKillThreadEvent, 20);
  255. if (dwEvent == WAIT_OBJECT_0)  nState = stExitThread;
  256. }
  257. // 置该线程结束标志
  258. SetEvent(p->m_hThreadKilledEvent);
  259. return 9999;
  260. }