-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame_protocol.cpp
More file actions
103 lines (84 loc) · 2.57 KB
/
Copy pathgame_protocol.cpp
File metadata and controls
103 lines (84 loc) · 2.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "game_protocol.h"
#include <string>
#include "GameMsg.h"
using namespace std;
game_protocol::game_protocol()
{
}
game_protocol::~game_protocol()
{
/*连接断了--》析构协议对象--》摘出role对象--》释放之*/
ZinxKernel::Zinx_Del_Role(*pGameRole);
delete pGameRole;
}
UserData * game_protocol::raw2request(std::string _szInput)
{
/*原始数据(TLV格式数据)----》返回包含multi_msg(若干用户请求GameMsg对象的容器的类)*/
multi_msg *pret = new multi_msg();
/*组合上次的报文和这次收到的报文*/
last_dgram.append(_szInput);
/*有粘包现象---》处理粘包*/
while (true)
{
/*判断报文是否够长--》缓存*/
if (last_dgram.size() < 8)
{
//肯定短--》return 空
break;
}
/*取出长度和ID*/
unsigned int uilength = (unsigned int)(unsigned char)last_dgram[0];
uilength += ((unsigned int)(unsigned char)last_dgram[1]) * 256;
uilength += ((unsigned int)(unsigned char)last_dgram[2]) * 256 *256;
uilength += ((unsigned int)(unsigned char)last_dgram[3]) * 256 * 256 * 256;
unsigned int uiID = (unsigned int)(unsigned char)last_dgram[4];
uiID |= ((unsigned int)(unsigned char)last_dgram[5]) << 8;
uiID |= ((unsigned int)(unsigned char)last_dgram[6]) << 16;
uiID |= ((unsigned int)(unsigned char)last_dgram[7]) << 24;
/*判断后续内容是否够长*/
if (uilength > last_dgram.size() - 8)
{
/*不够长*/
break;
}
/*取出消息内容,和ID一起构造游戏消息*/
string msg_content = last_dgram.substr(8, uilength);
auto pgamemsg = new GameMsg((GameMsg::MSG_TYPE)uiID, msg_content);
pret->m_msg_list.push_back(pgamemsg);
/*判断后续是否还有报文--->还会产生很多gamemsg对象*/
/*弹出已处理的报文,继续循环*/
last_dgram.erase(0, 8 + uilength);
}
return pret;
}
std::string * game_protocol::response2raw(UserData & _oUserData)
{
/*游戏消息(gamemsg)---》字节*/
auto pret = new string();
GET_REF2DATA(GameMsg, output_msg, _oUserData);
auto msg_content = output_msg.serielize();
/*拼消息长度和消息类型*/
unsigned int uilength = msg_content.size();
unsigned int uiID = output_msg.m_msg_type;
pret->push_back(uilength & 0xff);
pret->push_back((uilength >> 8) & 0xff);
pret->push_back((uilength >> 16) & 0xff);
pret->push_back((uilength >> 24) & 0xff);
pret->push_back(uiID & 0xff);
pret->push_back((uiID >> 8) & 0xff);
pret->push_back((uiID >> 16) & 0xff);
pret->push_back((uiID >> 24) & 0xff);
/*追加消息内容*/
pret->append(msg_content);
return pret;
}
Irole * game_protocol::GetMsgProcessor(UserDataMsg & _oUserDataMsg)
{
/*返回绑定的处理role对象*/
return pGameRole;
}
Ichannel * game_protocol::GetMsgSender(BytesMsg & _oBytes)
{
/*返回绑定的输出通道*/
return pGameChannel;
}