网络消息的封装

// 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;
}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值