// NetMsg.h: interface for the CNetMsg class.
//
//
#if !defined(AFX_NETMSG_H__B5470113_4819_4165_80AF_F23918BE2D76__INCLUDED_)
#define AFX_NETMSG_H__B5470113_4819_4165_80AF_F23918BE2D76__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CNetMsg
{
public:
CNetMsg();
virtual ~CNetMsg();
void Init(void);
void Reset();
const unsigned short GetType(void){return m_unMsgType;}
const unsigned short GetSize(void){return m_unMsgSize;}
char *GetBuf(void){return m_bufMsg;}
public:
virtual bool Create(const char* pbugMsg, DWORD dwMsgSize);
virtual bool IsValid(void);
virtual void Send(void);
virtual void Process(void *pInfo);
public:
static int GetMaxSize(void){return 1048;}
static unsigned short GetType(const char* pbugMsg,DWORD dwMsgSize);
static unsigned short GetSize(const char* pbugMsg,DWORD dwMsgSize);
static CNetMsg* CreateMsg(const char *pbugMsg, DWORD dwMsgSize);
protected:
union{
char m_bugMsg[1048];
struct {
unsigned short m_unMsgSize;
unsigned short m_unMsgType;
};
};
};
//extern CNetWork g_objNetwork;
#endif // !defined(AFX_NETMSG_H__B5470113_4819_4165_80AF_F23918BE2D76__INCLUDED_)\
// NetMsg.cpp: implementation of the CNetMsg class.
//
//
#include "stdafx.h"
#include "Test.h"
#include "NetMsg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
CNetMsg::CNetMsg()
{
Init();
}
CNetMsg::~CNetMsg()
{
}
void CNetMsg::Init(void)
{
memset(m_bugMsg, 0, sizeof(m_bugMsg));
m_unMsgSize = 0;
m_unMsgType = 0;
}
void CNetMsg::Reset()
{
Init();
}
bool CNetMsg::IsValid(void)
{
if ( 0 == this->GetType())
{
return false;
}
return true;
}
bool CNetMsg::Create(const char* pbugMsg, DWORD dwMsgSize)
{
if (!pbugMsg)
return false;
if ((unsigned short)dwMsgSize != GetSize(pbugMsg, dwMsgSize) - 8)
{
return false;
}
if ( 0 == GetType(pbugMsg, dwMsgSize))
{
return false;
}
memecpy(this->m_bugMsg, pbugMsg, dwMsgSize);
return true;
}
void CNetMsg::Process(void *pInfo)
{
}
void CNetMsg::Send(void)
{
char szMsg[2048];
memcpy(szMsg, m_bugMsg, m_unMsgSize);
memcpy(szMsg + m_unMsgSize, "TQClient", 8);
//g_objNetwork.SendData(szMsg, m_unMsgSize + 8);
}
unsigned short CNetMsg::GetType(const char* pbugMsg,DWORD dwMsgSize)
{
if (NULL == pbugMsg)
return 0;
if ((int)dwMsgSize > CNetMsg::GetMaxSize())
{
printf("error!\n");
return -1;
}
unsigned short* pUnShort = (unsigned short*)pbugMsg;
return pUnShort[1];
}
unsigned short CNetMsg::GetSize(const char* pbugMsg,DWORD dwMsgSize)
{
if (NULL == pbugMsg)
return 0;
unsigned short *pUnShort = (unsigned short*)pbugMsg;
if (pUnShort[0] + 8 > CNetMsg::GetMaxSize())
{
return 0;
}
return pUnShort[0] + 8;
}
CNetMsg* CNetMsg::CreateMsg(const char *pbugMsg, DWORD dwMsgSize)
{
if (!pbugMsg || (int)dwMsgSize > GetMaxSize())
{
return NULL;
}
int nSize = CNetMsg::GetSize(pbugMsg, dwMsgSize);
if ((int)dwMsgSize != nSize - 8)
{
return NULL;
}
CNetMsg *pMsg = NULL;
int type = CNetMsg::GetType(pbugMsg,dwMsgSize);
switch(type)
{
case 0:
// pMsg = new CMsgLogin;break;
break;
default:
break;
}
if (!pMsg)
return NULL;
if (!pMsg->Create(pbugMsg, dwMsgSize))
{
delete pMsg;
return NULL;
}
return pMsg;
}