MyUdpThread.cpp
上传用户:deligs
上传日期:2007-01-08
资源大小:43k
文件大小:4k
- // MyUdpThread.cpp : implementation of the MyUdp communication thread
- //
- #include "stdafx.h"
- #include "MyMonitor.h"
- #include "MyUdpThread.h"
- #include "MyUdpSocket.h"
- #define LOG(x) { if((MyUdpSocket.m_pLogFile->m_uLogLevel) > 0) MyUdpSocket.m_pLogFile->WriteLog(csModuleName+x); }
- #define INFO(x) { if((MyUdpSocket.m_pLogFile->m_uLogLevel) > 1) MyUdpSocket.m_pLogFile->WriteLog(csModuleName+x); }
- #define NOTE(x) { if((MyUdpSocket.m_pLogFile->m_uLogLevel) > 2) MyUdpSocket.m_pLogFile->WriteLog(csModuleName+x); }
- UINT ThreadMessageBox(LPVOID pParam /* msgInfo ptr */)
- {
- MessageInfo* pMsgInfo = (MessageInfo*)pParam;
- MessageInfo MsgInfo;
- MsgInfo.hwndShowMsg = pMsgInfo->hwndShowMsg;
- MsgInfo.pbMsgShowing = pMsgInfo->pbMsgShowing;
- MsgInfo.strMsgText = pMsgInfo->strMsgText;
- MsgInfo.strMsgTitle = pMsgInfo->strMsgTitle;
- MsgInfo.uMsgStyle = pMsgInfo->uMsgStyle;
- // Absorb parameters OK
- // set event to enable next message thread
- SetEvent(pMsgInfo->hEvent_GetParametersOK);
- // Don't show duplicate message
- if(*(MsgInfo.pbMsgShowing)) return 1;
- *MsgInfo.pbMsgShowing = TRUE;
- ::MessageBox(MsgInfo.hwndShowMsg, MsgInfo.strMsgText, MsgInfo.strMsgTitle, MsgInfo.uMsgStyle);
- *MsgInfo.pbMsgShowing = FALSE;
- return 0;
- }
- UINT MyUdpThreadProc(LPVOID pParam)
- {
- // This MyUdp thread runs in an infinite loop, waiting to communicate with
- // a USHA whenever the main application thread sets the "start MyUdp" event.
- // The MyUdp thread exits the loop only when the main application sets the
- // "kill MyUdp" event.
- CMyUdpInfo* pMyUdpInfo = (CMyUdpInfo*)pParam;
- ResetEvent(pMyUdpInfo->m_hEvent_Killed);
- CString csModuleName = LOG_STR_THREAD_NAME;
-
- CMyUdpSocket MyUdpSocket;
- MyUdpSocket.m_pMyUdpInfo = pMyUdpInfo;
- MyUdpSocket.m_hwndNotify = pMyUdpInfo->m_hwndNotify;
- MyUdpSocket.m_pLogFile = pMyUdpInfo->m_pLogFile;
- MyUdpSocket.m_csAddress = pMyUdpInfo->m_csAddress;
- MyUdpSocket.m_uRemotePort = pMyUdpInfo->m_uRemotePort;
- MyUdpSocket.m_csClientName = pMyUdpInfo->m_csClientName;
- // Absorb parameters OK
- // set event to enable next message thread
- SetEvent(pMyUdpInfo->m_hEvent_GetParametersDone);
- // For use to peek Socket's OnReceive Message
- MSG msg;
- if(!pMyUdpInfo->AddressValid())
- {
- SetEvent(pMyUdpInfo->m_hEvent_Killed);
- return 1;
- }
- // Initial the socket
- if(!MyUdpSocket.Initial())
- {
- ::MessageBox(pMyUdpInfo->m_hwndNotify,
- "Socket Initial Error : "+pMyUdpInfo->m_csAddress,
- "UDP test MessageBox",
- MB_ICONWARNING | MB_TOPMOST | MB_SETFOREGROUND);
- MyUdpSocket.Close();
- SetEvent(pMyUdpInfo->m_hEvent_Killed);
- return 2;
- }
- // To start trying to connect
- MyUdpSocket.m_dwState = STAT_MYUDPC_SENDFIRST;
- // Test Main program events
- while(WaitForSingleObject(pMyUdpInfo->m_hEvent_Kill,0) != WAIT_OBJECT_0)
- {
- // Test MyUdpSocket state
- switch (MyUdpSocket.m_dwState)
- {
- case STAT_MYUDPC_SENDFIRST :
- MyUdpSocket.SendFirstPacket();
- break;
- case STAT_MYUDPC_SENDSECOND :
- // You can change m_dwState to STAT_MYUDPC_SENDSECOND
- // when you receive correct info after you send the first packet
- //...
- // Notify the main thread of it
- ::PostMessage(pMyUdpInfo->m_hwndNotify, WM_MYUDP_CALLBACK, EVENT_UDP_CONNECTED, (long) pParam);
- break;
- default :
- LOG(LOG_STR_UNKNOWN + MyUdpSocket.m_csAddress);
- break;
- }// of switch
- Sleep(TIME_BETWEEN_RECEIVE);
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- // !!! Important !!!
- // To Enable Socket OnReceive Message while looping
- if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- // !!! Important !!!
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- }// of while
- // Close the socket
- MyUdpSocket.Close();
- // Notify Main of exiting ok
- SetEvent(pMyUdpInfo->m_hEvent_Killed);
- return 0;
- }