-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgameserver.cpp
More file actions
253 lines (197 loc) · 7.97 KB
/
gameserver.cpp
File metadata and controls
253 lines (197 loc) · 7.97 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***************************************************************************************
# Copyright (c) 2018
# All rights reserved
#
# @author :
# @name :
# @file :
# @date :
# @describe:GameServer通信类 游戏与外部的唯一接口实现
#***************************************************************************************/
#include "utils/tarslog.h"
#include "common/macros.h"
//#include "protos/headproto.h"
//#include "protos/dzproto.h"
#include "head.pb.h"
#include "dz.pb.h"
#include "gameroot.h"
#include "gameserver.h"
#include "message/onroommessage.h"
#include "message/onclientmessage.h"
namespace game
{
namespace gameserver
{
GameServer::GameServer(GameRoot *root)
: _root(root)
, _pTable(root->sParams.pTable)
, _strRoomTableId(root->sParams.strRoomTableId)
{
}
GameServer::~GameServer()
{
}
long GameServer::toSoData(const RoomSo::E_ROOM_TO_SO eMsgType, void *p)
{
long ret = 0;
__TRY__;
ret = message::onRoomMessage(eMsgType, p, _root);
__CATCH__;
return ret;
}
int GameServer::onGameSoMessage(const long lPlayerID, const vector<char> &vecMsgData)
{
int ret = 0;
__TRY__
XGameSoProto::TSoMsg somsg = pbToObj<XGameSoProto::TSoMsg>(vecMsgData);
//校验版本
if (somsg.nver() != XGameDZProto::iProtoVersion)
{
LOG_WARN("client proto version : \"" << somsg.nver() << "\" server proto version : \"" << XGameDZProto::iProtoVersion << "\";");
}
//校验命令字
if (somsg.ncmd() > XGameDZProto::MSG_ID_END || somsg.ncmd() < XGameDZProto::MSG_ID_BEGIN)
{
LOG_ERROR("invalid message command type value.");
return -1;
}
//data
vector<char> vecOutBuffer;
vecOutBuffer.insert(vecOutBuffer.begin(), somsg.vecmsgdata().begin(), somsg.vecmsgdata().end());
RLOG_DEBUG << "process game so reqeust, msg size = " << vecOutBuffer.size() << ", msg data len: " << somsg.vecmsgdata().length() << endl;
message::onClientMessage(lPlayerID, somsg.ncmd(), vecOutBuffer, _root);
__CATCH__
return ret;
}
int GameServer::toRoomData(const RoomSo::E_SO_TO_ROOM eMSG, void *p)
{
RLOG_DEBUG << " test send room data, eMSG: " << etos(eMSG) << endl;
if(_pTable)
{
return _pTable->toRoomData(eMSG, p);
}
else
{
RLOG_ERROR << "_pTable is nullptr." << endl;
return 0;
}
}
int GameServer::doSendGameData(const long &pid, int eMSG, const vector<char> &vecData)
{
int ret = 0;
__TRY__
XGameSoProto::TSoMsg sTSoMsg;
sTSoMsg.set_nver(XGameDZProto::iProtoVersion);
sTSoMsg.set_ncmd(eMSG);
string msgData;
msgData.assign(vecData.begin(), vecData.end());
sTSoMsg.set_vecmsgdata(msgData);
ret = _pTable->doSendGameData(pid, pbTobuffer(sTSoMsg));
RLOG_DEBUG << "send game data, pid : " << pid << ", eMSG: " << eMSG << ", vecData size : " << vecData.size() << endl;
__CATCH__
return ret;
}
int GameServer::doSendGameData(const long &pid, vector<int> &eMSG, const std::vector<vector<char> > &vecData)
{
int ret = 0;
__TRY__
if (eMSG.size() == 0 || vecData.size() == 0 || eMSG.size() != vecData.size())
{
return -1;
}
std::vector<vector<char> > vecSendData;
for (size_t i = 0; i < vecData.size(); ++i)
{
XGameSoProto::TSoMsg sTSoMsg;
sTSoMsg.set_nver(XGameDZProto::iProtoVersion);
sTSoMsg.set_ncmd(eMSG[i]);
string msgData;
msgData.assign(vecData[i].begin(), vecData[i].end());
sTSoMsg.set_vecmsgdata(msgData);
vecSendData.push_back(pbTobuffer(sTSoMsg));
}
ret = _pTable->doSendGameData(pid, vecSendData);
RLOG_DEBUG << "send game data, pid : " << pid << ", vecData size : " << vecData.size() << endl;
__CATCH__
return ret;
}
int GameServer::doSendAllGameData(int eMSG, const std::vector<char> &vecData)
{
int ret = 0;
__TRY__
XGameSoProto::TSoMsg sTSoMsg;
sTSoMsg.set_nver(XGameDZProto::iProtoVersion);
sTSoMsg.set_ncmd(eMSG);
string msgData;
msgData.assign(vecData.begin(), vecData.end());
sTSoMsg.set_vecmsgdata(msgData);
ret = _pTable->doSendAllGameData(pbTobuffer(sTSoMsg));
RLOG_DEBUG << "send all game data, eMSG: " << eMSG << ", vecData size : " << vecData.size() << endl;
__CATCH__
return ret;
}
int GameServer::doSendAllGameData(vector<int> &eMSG, const std::vector<vector<char> > &vecData)
{
int ret = 0;
__TRY__
if (eMSG.size() == 0 || vecData.size() == 0 || eMSG.size() != vecData.size())
{
return -1;
}
std::vector<vector<char> > vecSendData;
for (size_t i = 0; i < vecData.size(); ++i)
{
XGameSoProto::TSoMsg sTSoMsg;
sTSoMsg.set_nver(XGameDZProto::iProtoVersion);
sTSoMsg.set_ncmd(eMSG[i]);
string msgData;
msgData.assign(vecData[i].begin(), vecData[i].end());
sTSoMsg.set_vecmsgdata(msgData);
vecSendData.push_back(pbTobuffer(sTSoMsg));
}
ret = _pTable->doSendAllGameData(vecSendData);
RLOG_DEBUG << "send all game data, vecData size : " << vecData.size() << endl;
__CATCH__
return ret;
}
int GameServer::doSendWatchGameData(int eMSG, const std::vector<char> &vecData)
{
int ret = 0;
__TRY__
XGameSoProto::TSoMsg sTSoMsg;
sTSoMsg.set_nver(XGameDZProto::iProtoVersion);
sTSoMsg.set_ncmd(eMSG);
string msgData;
msgData.assign(vecData.begin(), vecData.end());
sTSoMsg.set_vecmsgdata(msgData);
ret = _pTable->doSendWatchGameData(pbTobuffer(sTSoMsg));
RLOG_DEBUG << "send watch game data, eMSG: " << eMSG << ", vecData size : " << vecData.size() << endl;
__CATCH__
return ret;
}
int GameServer::doSendWatchGameData(vector<int> &eMSG, const std::vector<vector<char> > &vecData)
{
int ret = 0;
__TRY__
if (eMSG.size() == 0 || vecData.size() == 0 || eMSG.size() != vecData.size())
{
return -1;
}
std::vector<vector<char> > vecSendData;
for (size_t i = 0; i < vecData.size(); ++i)
{
XGameSoProto::TSoMsg sTSoMsg;
sTSoMsg.set_nver(XGameDZProto::iProtoVersion);
sTSoMsg.set_ncmd(eMSG[i]);
string msgData;
msgData.assign(vecData[i].begin(), vecData[i].end());
sTSoMsg.set_vecmsgdata(msgData);
vecSendData.push_back(pbTobuffer(sTSoMsg));
}
ret = _pTable->doSendWatchGameData(vecSendData);
RLOG_DEBUG << "send watch game data, vecData size : " << vecData.size() << endl;
__CATCH__
return ret;
}
}
};