www.pudn.com > CtrlIPClt.rar > RunThread.cpp
// RunThread.cpp : implementation file
//
#include "stdafx.h"
#include "CtrlIPClt.h"
#include "RunThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CCtrlIPCltApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CRunThread
IMPLEMENT_DYNCREATE(CRunThread, CWinThread)
CRunThread::CRunThread()
{
}
CRunThread::~CRunThread()
{
}
BOOL CRunThread::InitInstance()
{
// TODO: perform and per-thread initialization here
m_pRunLog = &theApp.m_cRunLog;
if(!InitUDPSocket())
{
m_pRunLog->WriteErrorLog("初始化UDP socket失败!");
return FALSE;
}
// 发送广播消息
m_cUDPSocket.AsyncSelect(FD_WRITE | FD_CLOSE);
return TRUE;
}
int CRunThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
m_cUDPSocket.Close();
return CWinThread::ExitInstance();
}
BEGIN_MESSAGE_MAP(CRunThread, CWinThread)
//{{AFX_MSG_MAP(CRunThread)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRunThread message handlers
BOOL CRunThread::InitUDPSocket()
{
m_cUDPSocket.m_pRunLog = m_pRunLog;
m_cUDPSocket.Close();
if(!m_cUDPSocket.Create(0, SOCK_DGRAM, m_strIP))
{
m_pRunLog->WriteErrorLog("Create Socket Error!!!, errno=%ld", ::GetLastError());
return FALSE;
}
m_cUDPSocket.m_strConsoleSN = theApp.m_strConsoleSN;
// 设置广播属性
BOOL bEnable = TRUE;
if(!m_cUDPSocket.SetSockOpt(SO_BROADCAST, &bEnable, sizeof(BOOL)))
{
m_pRunLog->WriteErrorLog("设置广播属性错误!!!, errno=%ld", ::GetLastError());
m_cUDPSocket.Close();
return FALSE;
}
// 设置接收缓冲区的大小
int nRecvBufSize = 100 * sizeof(CTRLIPINFO);
if(!m_cUDPSocket.SetSockOpt(SO_RCVBUF, &nRecvBufSize, sizeof(int)))
{
m_pRunLog->WriteErrorLog("设置接收缓冲区大小失败!errno=%ld", ::GetLastError());
m_cUDPSocket.Close();
return FALSE;
}
// 设置线程ID
m_cUDPSocket.m_dwThreadID = this->m_nThreadID;
m_cUDPSocket.AsyncSelect(FD_READ | FD_CLOSE);
return TRUE;
}
void CRunThread::SetIPAddr(CString strIP)
{
m_strIP = strIP;
}