-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameProtocol.cpp
More file actions
116 lines (95 loc) · 2.37 KB
/
GameProtocol.cpp
File metadata and controls
116 lines (95 loc) · 2.37 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
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "GameProtocol.h"
#include "GameMsg.h"
#include "GameChannel.h"
#include "msg.pb.h"
#include "GameRole.h"
#include <iostream>
GameProtocol::GameProtocol() {
}
GameProtocol::~GameProtocol() {
if (NULL != m_Role)
{
ZinxKernel::Zinx_Del_Role(*m_Role);
delete m_Role;
}
}
/*输入参数是通道传来的原始报文
返回值是转换后的消息对象MultiMsg
转换方式,TCP粘包处理*/
UserData* GameProtocol::raw2request(std::string _szInput)
{
MultiMsg* pRet = new MultiMsg();
szLast.append(_szInput);
while (1)
{
if (szLast.size() < 8)
{
break;
}
/*在前四个字节中读取消息内容长度*/
int iLength = 0;
iLength |= szLast[0] << 0;
iLength |= szLast[1] << 8;
iLength |= szLast[2] << 16;
iLength |= szLast[3] << 24;
/*中四个字节读类型id*/
int id = 0;
id |= szLast[4] << 0;
id |= szLast[5] << 8;
id |= szLast[6] << 16;
id |= szLast[7] << 24;
/*通过读到的长度判断后续报文是否合法*/
if (szLast.size() - 8 < iLength)
{
/*本条报文还没够,啥都不干*/
break;
}
/*构造一条用户请求*/
GameMsg* pMsg = new GameMsg((GameMsg::MSG_TYPE)id, szLast.substr(8, iLength));
pRet->m_Msgs.push_back(pMsg);
/*弹出已经处理成功的报文*/
szLast.erase(0, 8 + iLength);
}
////Debug打印每条请求
//for (auto single : pRet->m_Msgs)
//{
// std::cout << single->pMsg->Utf8DebugString() << std::endl;
//}
//pb::Talk* pmsg = new pb::Talk();
//pmsg->set_content("hello");
//GameMsg* pGameMsg = new GameMsg(GameMsg::MSG_TYPE_CHAT_CONTENT, pmsg);
//ZinxKernel::Zinx_SendOut(*(pGameMsg), *this);
return pRet;
}
/*参数来自业务层,待发送的消息
返回值转换后的字节流*/
std::string* GameProtocol::response2raw(UserData& _oUserData)
{
int iLength = 0;
int id = 0;
std::string MsgContent;
GET_REF2DATA(GameMsg, oOutput, _oUserData);
id = oOutput.enMsgType;
MsgContent = oOutput.serialize();
iLength = MsgContent.size();
auto pret = new std::string();
pret->push_back((iLength >> 0) & 0xff);
pret->push_back((iLength >> 8) & 0xff);
pret->push_back((iLength >> 16) & 0xff);
pret->push_back((iLength >> 24) & 0xff);
pret->push_back((id >> 0) & 0xff);
pret->push_back((id >> 8) & 0xff);
pret->push_back((id >> 16) & 0xff);
pret->push_back((id >> 24) & 0xff);
pret->append(MsgContent);
return pret;
}
Irole* GameProtocol::GetMsgProcessor(UserDataMsg& _oUserDataMsg)
{
return m_Role;
}
//返回数据发送的通道
Ichannel* GameProtocol::GetMsgSender(BytesMsg& _oBytes)
{
return m_channel;
}