MyUdpThread.cpp
上传用户:deligs
上传日期:2007-01-08
资源大小:43k
文件大小:4k
源码类别:

网络编程

开发平台:

Visual C++

  1. // MyUdpThread.cpp : implementation of the MyUdp communication thread
  2. //
  3. #include "stdafx.h"
  4. #include "MyMonitor.h"
  5. #include "MyUdpThread.h"
  6. #include "MyUdpSocket.h"
  7. #define LOG(x) { if((MyUdpSocket.m_pLogFile->m_uLogLevel) > 0) MyUdpSocket.m_pLogFile->WriteLog(csModuleName+x); }
  8. #define INFO(x) { if((MyUdpSocket.m_pLogFile->m_uLogLevel) > 1) MyUdpSocket.m_pLogFile->WriteLog(csModuleName+x); }
  9. #define NOTE(x) { if((MyUdpSocket.m_pLogFile->m_uLogLevel) > 2) MyUdpSocket.m_pLogFile->WriteLog(csModuleName+x); }
  10. UINT ThreadMessageBox(LPVOID pParam /* msgInfo ptr */)
  11. {
  12. MessageInfo* pMsgInfo = (MessageInfo*)pParam;
  13. MessageInfo MsgInfo;
  14. MsgInfo.hwndShowMsg = pMsgInfo->hwndShowMsg;
  15. MsgInfo.pbMsgShowing = pMsgInfo->pbMsgShowing;
  16. MsgInfo.strMsgText = pMsgInfo->strMsgText;
  17. MsgInfo.strMsgTitle = pMsgInfo->strMsgTitle;
  18. MsgInfo.uMsgStyle = pMsgInfo->uMsgStyle;
  19. // Absorb parameters OK
  20. // set event to enable next message thread
  21. SetEvent(pMsgInfo->hEvent_GetParametersOK);
  22. // Don't show duplicate message
  23. if(*(MsgInfo.pbMsgShowing)) return 1;
  24. *MsgInfo.pbMsgShowing = TRUE;
  25. ::MessageBox(MsgInfo.hwndShowMsg, MsgInfo.strMsgText, MsgInfo.strMsgTitle, MsgInfo.uMsgStyle);
  26. *MsgInfo.pbMsgShowing = FALSE;
  27. return 0;
  28. }
  29. UINT MyUdpThreadProc(LPVOID pParam)
  30. {
  31. // This MyUdp thread runs in an infinite loop, waiting to communicate with
  32. // a USHA whenever the main application thread sets the "start MyUdp" event.
  33. // The MyUdp thread exits the loop only when the main application sets the
  34. // "kill MyUdp" event.
  35. CMyUdpInfo* pMyUdpInfo = (CMyUdpInfo*)pParam;
  36. ResetEvent(pMyUdpInfo->m_hEvent_Killed);
  37. CString csModuleName = LOG_STR_THREAD_NAME;
  38. CMyUdpSocket MyUdpSocket;
  39. MyUdpSocket.m_pMyUdpInfo = pMyUdpInfo;
  40. MyUdpSocket.m_hwndNotify = pMyUdpInfo->m_hwndNotify;
  41. MyUdpSocket.m_pLogFile = pMyUdpInfo->m_pLogFile;
  42. MyUdpSocket.m_csAddress = pMyUdpInfo->m_csAddress;
  43. MyUdpSocket.m_uRemotePort = pMyUdpInfo->m_uRemotePort;
  44. MyUdpSocket.m_csClientName = pMyUdpInfo->m_csClientName;
  45. // Absorb parameters OK
  46. // set event to enable next message thread
  47. SetEvent(pMyUdpInfo->m_hEvent_GetParametersDone);
  48. // For use to peek Socket's OnReceive Message
  49. MSG msg;
  50. if(!pMyUdpInfo->AddressValid())
  51. {
  52. SetEvent(pMyUdpInfo->m_hEvent_Killed);
  53. return 1;
  54. }
  55. // Initial the socket
  56. if(!MyUdpSocket.Initial())
  57. {
  58. ::MessageBox(pMyUdpInfo->m_hwndNotify, 
  59. "Socket Initial Error : "+pMyUdpInfo->m_csAddress, 
  60. "UDP test MessageBox", 
  61. MB_ICONWARNING | MB_TOPMOST | MB_SETFOREGROUND);
  62. MyUdpSocket.Close();
  63. SetEvent(pMyUdpInfo->m_hEvent_Killed);
  64. return 2;
  65. }
  66. // To start trying to connect
  67. MyUdpSocket.m_dwState = STAT_MYUDPC_SENDFIRST;
  68. // Test Main program events
  69. while(WaitForSingleObject(pMyUdpInfo->m_hEvent_Kill,0) != WAIT_OBJECT_0)
  70. {
  71. // Test MyUdpSocket state
  72. switch (MyUdpSocket.m_dwState)
  73. {
  74. case STAT_MYUDPC_SENDFIRST :
  75. MyUdpSocket.SendFirstPacket();
  76. break;
  77. case STAT_MYUDPC_SENDSECOND :
  78. // You can change m_dwState to STAT_MYUDPC_SENDSECOND
  79. // when you receive correct info after you send the first packet
  80. //...
  81. // Notify the main thread of it
  82. ::PostMessage(pMyUdpInfo->m_hwndNotify, WM_MYUDP_CALLBACK, EVENT_UDP_CONNECTED, (long) pParam);
  83. break;
  84. default :
  85. LOG(LOG_STR_UNKNOWN + MyUdpSocket.m_csAddress);
  86. break;
  87. }// of switch
  88. Sleep(TIME_BETWEEN_RECEIVE);
  89. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  90. // !!! Important !!!
  91. // To Enable Socket OnReceive Message while looping
  92. if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  93. {
  94. TranslateMessage(&msg);         
  95. DispatchMessage(&msg);
  96. }
  97. // !!! Important !!!
  98. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  99. }// of while
  100. // Close the socket
  101. MyUdpSocket.Close();
  102. // Notify Main of exiting ok
  103. SetEvent(pMyUdpInfo->m_hEvent_Killed);
  104. return 0;
  105. }