- // MyUdpSocket.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyMonitor.h"
- #include "MyUdpSocket.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define LOG(x) { if((m_pLogFile->m_uLogLevel) > 0) m_pLogFile->WriteLog(m_csModuleName+x); }
- #define INFO(x) { if((m_pLogFile->m_uLogLevel) > 1) m_pLogFile->WriteLog(m_csModuleName+x); }
- /////////////////////////////////////////////////////////////////////////////
- // CMyUdpSocket
- CMyUdpSocket::CMyUdpSocket()
- {
- m_csModuleName = LOG_STR_SOCKET_NAME;
- }
- CMyUdpSocket::~CMyUdpSocket()
- {
- }
- // Do not edit the following lines, which are needed by ClassWizard.
- #if 0
- BEGIN_MESSAGE_MAP(CMyUdpSocket, CAsyncSocket)
- //{{AFX_MSG_MAP(CMyUdpSocket)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- #endif // 0
- /////////////////////////////////////////////////////////////////////////////
- // CMyUdpSocket member functions
- BOOL CMyUdpSocket::ChecksumValid()
- {
- return TRUE;
- }
- void CMyUdpSocket::SendFirstPacket()
- {
- // Prepare the data packet here
- BYTE bData[8];
- //bData[0]=...
- //bData[1]=...
- //...
- Send((void*)&bData,8);
- }
- BOOL CMyUdpSocket::Initial()
- {
- int iResult = Create(0,SOCK_DGRAM); // Create as a UDP port
- if(!iResult)
- {
- LOG(LOG_STR_SOCKET_CREATE_ERROR + m_csAddress);
- return FALSE;
- }
- iResult = Connect(m_csAddress, m_uRemotePort);
- if(!iResult)
- {
- LOG(LOG_STR_SOCKET_ERROR + m_csAddress);
- return FALSE;
- }
- // Get the local port that the system assigned for you
- CString socketAddress;
- GetSockName(socketAddress, m_uLocalPort);
- return TRUE;
- }
- void CMyUdpSocket::OnReceive(int nErrorCode)
- {
- // TODO: Add your specialized code here and/or call the base class
- BYTE szBuf[100];
- int iReceived;
- // Receive the message
- iReceived = Receive(szBuf, sizeof(szBuf));
- // Did we receive anything?
- if(iReceived == 0)
- {
- // If nothing
- //...
- return;
- }
- else if(iReceived == SOCKET_ERROR)
- {
- // Get the error and handle it
- LOG(LOG_STR_SOCKET_RECEIVE_ERROR + m_csAddress);
- return;
- }
- else
- {
- // Else analyze the response data here
- //...
- if( ChecksumValid() )
- {
- if( m_dwState == STAT_MYUDPC_SENDFIRST )
- {
- m_dwState = STAT_MYUDPC_SENDSECOND;
- return;
- }
- }
- }
- CAsyncSocket::OnReceive(nErrorCode);
- }