-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameProtocol.cpp
More file actions
128 lines (103 loc) · 3.08 KB
/
GameProtocol.cpp
File metadata and controls
128 lines (103 loc) · 3.08 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
117
118
119
120
121
122
123
124
125
126
127
128
#include "GameProtocol.h"
#include <iostream>
#include "GameMsg.h"
#include "GameChannel.h"
#include "msg.pb.h"
using namespace std;
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 |= (int)(unsigned char)szLast[0] << 0;
iLength |= (int)(unsigned char)szLast[1] << 8;
iLength |= (int)(unsigned char)szLast[2] << 16;
iLength |= (int)(unsigned char)szLast[3] << 24;
/*中四个字节读类型id*/
int id = 0;
id |= (int)(unsigned char)szLast[4] << 0;
id |= (int)(unsigned char)szLast[5] << 8;
id |= (int)(unsigned char)szLast[6] << 16;
id |= (int)(unsigned char)szLast[7] << 24;
//测试信息输出
std::cout << "DEBUG: Raw Input Size: " << _szInput.size() << std::endl;
std::cout << "DEBUG: Last Buffer Size: " << szLast.size() << std::endl;
std::cout << "DEBUG: Parsed iLength: " << iLength << ", Parsed ID: " << id << std::endl;
/*通过读到的长度判断后续报文是否合法*/
if (szLast.size() - 8 < iLength)
{
/*本条报文还没够,啥都不干*/
std::cout << "DEBUG: Breaking, Need " << iLength + 8 << ", Have " << szLast.size() << std::endl;
break;
}
/*构造一条用户请求*/
GameMsg* pMsg = new GameMsg((GameMsg::MSG_TYPE)id, szLast.substr(8, iLength));
pRet->m_Msgs.push_back(pMsg);
/*弹出已经处理成功的报文*/
szLast.erase(0, 8 + iLength);
}
for (auto single : pRet->m_Msgs)
{
cout << "Debugging-information:" << endl;
cout << single->pMsg->Utf8DebugString() << endl;
}
//启用这段注释会导致不能正常处理客户端连接的bug 只在是测试阶段为了调试使用
//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;
}