www.pudn.com > CtrlIPClt.rar > UDPSocket.cpp
// UDPSocket.cpp : implementation file
//
#include "stdafx.h"
#include "CtrlIPClt.h"
#include "UDPSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUDPSocket
CUDPSocket::CUDPSocket()
{
ZeroMemory(m_byteRecvBuf, sizeof(m_byteRecvBuf));
m_nReceivedLen = 0;
ZeroMemory(m_byteSendBuf, sizeof(m_byteSendBuf));
m_nSendedLen = 0;
m_ConsoleInfoArray.RemoveAll();
}
CUDPSocket::~CUDPSocket()
{
int nSize = m_ConsoleInfoArray.GetSize();
for(int i=0; iWriteDebugLog("OnSend(nErrorCode=%d)", nErrorCode);
if(nErrorCode != 0)
{
return ;
}
if(m_nSendedLen == 0) // 上一次的已经发送完成
{
int nPos = 0;
CTRLIPINFO stCtrlIPInfo;
stCtrlIPInfo.nSize = sizeof(stCtrlIPInfo);
strcpy(stCtrlIPInfo.szConsoleSN, m_strConsoleSN);
// 复制到发送缓冲区
memcpy(m_byteSendBuf, &stCtrlIPInfo, stCtrlIPInfo.nSize);
m_pRunLog->WriteDebugLog("上一次的已经发送完成,本次发送开始!");
}
else
{
m_pRunLog->WriteDebugLog("上一次的发送未完成!总长度=%d,已发送的长度=%d", sizeof(CTRLIPINFO), m_nSendedLen);
}
int nRtn = SendTo(&m_byteSendBuf[m_nSendedLen], sizeof(CTRLIPINFO) - m_nSendedLen, CTRLIPINFO_UPD_PORT, NULL);
if(nRtn != SOCKET_ERROR)
{
m_nSendedLen += nRtn;
if(m_nSendedLen == sizeof(CTRLIPINFO)) // 发送完成
{
m_nSendedLen = 0;
ZeroMemory(m_byteSendBuf, sizeof(m_byteSendBuf));
m_pRunLog->WriteDebugLog("发送完成!");
CAsyncSocket::AsyncSelect(FD_READ | FD_CLOSE);
}
else // 发送未完成
{
m_pRunLog->WriteDebugLog("发送未完成!总长度=%d,已发送的长度=%d", sizeof(CTRLIPINFO), m_nSendedLen);
CAsyncSocket::AsyncSelect(FD_READ | FD_CLOSE | FD_WRITE);
}
}
else
{
m_pRunLog->WriteDebugLog("发送失败!errno=%ld", ::GetLastError());
}
CSocket::OnSend(nErrorCode);
}
void CUDPSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_pRunLog->WriteDebugLog("OnReceive(nErrorCode=%d)", nErrorCode);
if(nErrorCode != 0)
{
CAsyncSocket::OnReceive(nErrorCode);
return ;
}
CTRLIPINFO stCtrlIPInfo;
ZeroMemory(&stCtrlIPInfo, sizeof(stCtrlIPInfo));
stCtrlIPInfo.nSize = sizeof(stCtrlIPInfo);
CString strIP;
UINT uPort;
BYTE byteBuf[1024] = {0};
int nRtn = ReceiveFrom(byteBuf, stCtrlIPInfo.nSize, strIP, uPort);
if((nRtn != SOCKET_ERROR) && (nRtn != 0))
{
memcpy(&m_byteRecvBuf[m_nReceivedLen], byteBuf, nRtn);
m_nReceivedLen += nRtn;
if(m_nReceivedLen == stCtrlIPInfo.nSize)
{
m_pRunLog->WriteDebugLog("接收完成!");
// 接收成功
m_nReceivedLen = 0;
memcpy(&stCtrlIPInfo, m_byteRecvBuf, stCtrlIPInfo.nSize);
ZeroMemory(m_byteRecvBuf, sizeof(m_byteRecvBuf));
// 添加到控制台信息链表
AddCtrlIPInfo(&stCtrlIPInfo, strIP);
#ifndef _DEBUG // 如果是Release版本, 则立即结束线程
Close(); // 关闭socket
::PostThreadMessage(m_dwThreadID, WM_QUIT, 0, 0); // 终止线程
return ;
#endif
CAsyncSocket::AsyncSelect(FD_READ | FD_CLOSE);
}
else
{
m_pRunLog->WriteDebugLog("接收未完成!总长度=%d,当前接收完的长度=%d", stCtrlIPInfo.nSize, m_nReceivedLen);
}
}
else
{
m_pRunLog->WriteDebugLog("接收失败,errno=%ld", ::GetLastError());
}
CSocket::OnReceive(nErrorCode);
}
void CUDPSocket::AddCtrlIPInfo(LPCTRLIPINFO lpItem, CString strConsoleIP)
{
int nSize = m_ConsoleInfoArray.GetSize();
LPCONSOLEINFO lpCurItem = NULL;
for(int i=0; iszConsoleIP, strConsoleIP))
{
return ; // 存在相同的控制台IP,则忽略
}
}
LPCONSOLEINFO lpNewItem = new CONSOLEINFO;
if(lpItem != NULL)
{
memcpy(&lpNewItem->stCtrlIPInfo, lpItem, sizeof(CTRLIPINFO));
strcpy(lpNewItem->szConsoleIP, strConsoleIP);
m_pRunLog->WriteDebugLog("控制台信息:IP=%s, ConsolePort=%d, UpdateServerPort=%d", \
strConsoleIP, lpItem->uConsolePort, lpItem->uUpdateServerPort);
m_ConsoleInfoArray.Add(lpNewItem);
}
}