PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/TGame/TCommon/Message/MsgBuilderSimple.cpp

http://awoe.googlecode.com/
C++ | 63 lines | 47 code | 10 blank | 6 comment | 4 complexity | 1ba8596061fd65aa7730429b89838676 MD5 | raw file
 1#include "stdafx.h"
 2#include "MsgBuilderSimple.h"
 3#include "MsgCategory.h"
 4#include "MsgFactoryNetwork.h"
 5//#include "MsgLogin.h"
 6//#include "MsgPlayer.h"
 7
 8const char c_msg_header_flag = 47;
 9const short s_msg_header_len = 100;
10
11MsgBuilderWithFactory::MsgBuilderWithFactory(IMsgFactory* pFact /* = NULL */)
12:m_pMsgFact(pFact)
13{
14	if (m_pMsgFact==NULL)
15	{
16		m_pMsgFact = new MsgFactoryNetwork;
17	}
18}
19
20bool
21MsgBuilderWithFactory::onEncodeMsg(IMsg* pMsg, MsgOStream& stream)
22{
23	if (pMsg!=NULL)
24	{
25		stream << c_msg_header_flag;
26		stream << pMsg->getMsgCate() << pMsg->getMsgID() << s_msg_header_len;
27		return pMsg->encode(stream);
28	}
29	else
30	{
31		return false;
32	}
33}
34
35
36bool
37MsgBuilderWithFactory::onDecodeMsg(IMsg*& pMsg, MsgIStream& stream)
38{
39	char cFlag = 0;
40	stream >> cFlag;
41
42	//if (cType!=c_msg_header_type || cTag!=c_msg_header_tag)
43	//{
44	//	LOG_ERROR("Failed to decode msg, invalid message type or tag!");
45	//}
46
47	char ucMC = 0;
48	short nMID = 0, nLen = 0;
49	stream >> ucMC >> nMID >>nLen;
50
51
52	pMsg = m_pMsgFact->createMsg(ucMC, nMID);
53	if (pMsg != NULL)
54	{
55		return pMsg->decode(stream);
56	}
57	else
58	{
59		LOG_ERROR_V("Failed to decode msg, message cate or id %d, %d!", ucMC, nMID);
60		return false;
61	}
62}
63