-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMsg.cpp
More file actions
55 lines (47 loc) · 1.01 KB
/
GameMsg.cpp
File metadata and controls
55 lines (47 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "GameMsg.h"
#include "msg.pb.h"
GameMsg::GameMsg(MSG_TYPE _type, google::protobuf::Message* _pMsg) :enMsgType(_type), pMsg(_pMsg)
{
}
GameMsg::GameMsg(MSG_TYPE _type, std::string _stream) :enMsgType(_type)
{
/*通过简单工厂构造具体的消息对象*/
switch (_type)
{
case GameMsg::MSG_TYPE_LOGIN_ID_NAME:
pMsg = new pb::SyncPid();
break;
case GameMsg::MSG_TYPE_CHAT_CONTENT:
pMsg = new pb::Talk();
break;
case GameMsg::MSG_TYPE_NEW_POSTION:
pMsg = new pb::Position();
break;
case GameMsg::MSG_TYPE_BROADCAST:
pMsg = new pb::BroadCast();
break;
case GameMsg::MSG_TYPE_LOGOFF_ID_NAME:
pMsg = new pb::SyncPid();
break;
case GameMsg::MSG_TYPE_SRD_POSTION:
pMsg = new pb::SyncPlayers();
break;
default:
break;
}
/*将参数解析成消息对象内容*/
pMsg->ParseFromString(_stream);
}
std::string GameMsg::serialize()
{
std::string ret;
pMsg->SerializeToString(&ret);
return ret;
}
GameMsg::~GameMsg()
{
if (pMsg != NULL)
{
delete pMsg;
}
}