📄 network.cpp
字号:
// NetWork.cpp: implementation of the CNetWork class.
//
//////////////////////////////////////////////////////////////////////
#include "NetMsg.h"
#include "NetWork.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//CNetMsg* CNetwork::ProcessNetMsg()
//{
//
// MYASSERT (m_pSocket);
//
// if (!m_pSocket->Check()) // network failed
// {
// m_uStatus =_STATUS_EXIT;
//
// this->Destroy();
// return NULL;
// }
//
// char buffer[1024];
// int nSize =sizeof(buffer);
// char* pbufMsg =buffer;
//
// if (!m_pSocket->ReceiveMsg(pbufMsg, nSize))
// return NULL;
//
// DWORD dwCurMsgSize =CNetMsg::GetSize(pbufMsg, nSize);
// if((int)dwCurMsgSize > nSize || dwCurMsgSize <=0) // broken msg
// return NULL;
//
// CNetMsg* pMsg =CNetMsg::CreateMsg(pbufMsg, dwCurMsgSize);
//
// if (pMsg) {
// m_dwBytesReceived += dwCurMsgSize;
// }
//
// // flush net msg
// m_pSocket->Flush();
//
// return pMsg;
//}
CNetwork::NET_RET CNetwork::ProcessNetMsg(char* pBuf, IN OUT DWORD& dwBufSize)
{
if (!m_pSocket) {
return NET_BROKEN;
}
//MYASSERT (m_pSocket);
// bool bRet = false;
if (!m_pSocket->Check()) // network failed
{
m_uStatus =_STATUS_EXIT;
this->Destroy();
return NET_BROKEN;
}
// flush net msg
m_pSocket->Flush();
int nSize = dwBufSize;
char* pbufMsg = pBuf;
if (!m_pSocket->ReceiveMsg(pbufMsg, nSize))
return NET_NO_MSG;
dwBufSize = CNetMsg::GetSize(pbufMsg, nSize);
if((int)dwBufSize > nSize || dwBufSize <= 0) // broken msg
return NET_NO_MSG;
CNetMsg* pMsg =CNetMsg::CreateMsg(pbufMsg, dwBufSize);
if (pMsg) {
m_dwBytesReceived += dwBufSize;
delete pMsg;
}
return NET_HAVE_MSG;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNetwork::CNetwork()
{
m_bReady = false;
m_pSocket = NULL;
m_pChkData = NULL;
}
BOOL CNetwork::Init(const char* pszServerIP, int nServerPort)
{
if(!pszServerIP || nServerPort<0)
return false;
this->Destroy();
// init now
m_dwBytesReceived = 0;
m_dwBytesSend = 0;
//if(::ClientSocketInit(pszServerIP, nServerPort))
m_pSocket = new CLoginSocket;
if (!m_pSocket)
return false;
if(m_pSocket->Create((char*)pszServerIP, nServerPort))
{
this->m_bReady =true;
return true;
}
else
{
this->m_bReady =false;
return false;
}
m_dwMsgCount = 0;
}
//////////////////////////////////////////////////////////////////////
void CNetwork::CreateChkData (DWORD dwData)
{
if (m_pChkData)
{
delete [] m_pChkData;
m_pChkData = NULL;
}
// chk data ini
CONST SIZE_DATA = 256;
m_pChkData = new int[SIZE_DATA];
if (m_pChkData)
{
//::srand(g_objHero.GetID());
::srand(m_idHero);
for (int i=0; i<SIZE_DATA; i++)
m_pChkData[i] = ::rand()%0xffff;
}
}
//////////////////////////////////////////////////////////////////////
void CNetwork::Destroy()
{
if(m_bReady)
{
// ::ClientSocketDestroy();
m_pSocket->Flush();
m_pSocket->Destroy();
m_bReady =false;
}
SAFE_DELETE(m_pSocket);
SAFE_DELETE(m_pChkData);
}
//////////////////////////////////////////////////////////////////////
BOOL CNetwork::SendMsg(char* pbufMsg, DWORD dwSize)
{
if(!m_bReady)
return false;
BOOL bSucSend;
//bSucSend =::ClientSocketSendMsg(pbufMsg, dwSize);
bSucSend =m_pSocket->SendMsg(pbufMsg, dwSize);
m_dwBytesSend += dwSize;
this->AddMsgCount();
return bSucSend;
}
//////////////////////////////////////////////////////////////////////
DWORD CNetwork::GetMsgCount(void)
{
if (m_dwMsgCount == 0)
return 0;
return m_dwMsgCount^m_idHero;
}
//////////////////////////////////////////////////////////////////////
void CNetwork:: AddMsgCount (int nAddValue/* = 1*/)
{
DWORD dwCount = this->GetMsgCount()+nAddValue;
m_dwMsgCount = dwCount^m_idHero;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -