-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgate_main.cc
431 lines (392 loc) · 11.3 KB
/
gate_main.cc
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include "../system/server.h"
#include "../system/ios.h"
#include "../system/define.h"
#include "../system/send.h"
#include "../protocol/game_def.h"
#include "../protocol/internal.h"
#include "../protocol/data.h"
#include "../system/timer.h"
#include "../mq/config.h"
#include "../system/mq_helper.h"
#include "users.h"
#include "interact.h"
#include "world.h"
#include "account.h"
#include "data.h"
#include "../protocol/misc.h"
using namespace network;
void NetError(Serial serial)
{
//need to logout player
if (GetUsers().Has(serial))
{
UserID uid = GetUsers().Serial2UserID(serial);
NotifyUserExit2Interact(uid);
NotifyUserExit2World(uid);
GetUsers().Remove(serial);
}
}
bool IsSessionValid(Serial serial)
{
return GetUsers().Has(serial);
}
void PlayerMsgProcess( Serial serial, int16_t type, int16_t bytes, int16_t flag, const uint8_t* data )
{
assert(type>=0 && bytes>=0);
//printf("gate: got msg type: %d, serial: %08X\n", type, serial);
UserID uid;
MqHead head;
switch ( type )
{
case Ping::kType:
{
if(bytes == sizeof(Ping))
{
Ping* ping_data = (Ping*)(data);
switch(ping_data->server)
{
case kServerIDGate:
{
int null_data = 0;
GetServer().Send(serial,PingResult::kType, sizeof(null_data), 0, &null_data);
break;
}
}
}
break;
}
case p::Login::kType:
InternalLogin login;
memcpy( &login, data, sizeof(p::Login) );
login.serial = serial;
NotifyUserEnter2Account( flag, login );
break;
case p::Register::kType:
InternalRegister reg;
p::Register* p_reg;
p_reg = (p::Register*)data;
reg.serial = serial;
reg.uid = p_reg->uid;
reg.sex = p_reg->sex;
memcpy( ®.name, &(p_reg->name), sizeof(p_reg->name) );
//
const char * ip;
ip = GetServer().GetSessionAddress(serial);
if(ip)
{
reg.ipAddr[0] = 0;
strcat(reg.ipAddr, ip);
}
NotifyUserRegister2Account( flag, reg );
break;
case p::IsUidExist::kType:
InternalIsUidExist ue;
ue.serial = serial;
ue.uid = ((p::IsUidExist*)data)->uid;
NotifyUidExist2Account(flag, ue);
break;
case p::IsNicknameExist::kType:
InternalIsNicknameExist ne;
ne.serial = serial;
memcpy(&ne.name, &(((p::IsNicknameExist*)data)->name), sizeof(Nickname));
head.type = InternalIsNicknameExist::kType; head.flag = flag;
ProcessMsg2Account(head, (const uint8_t*)(&ne), sizeof(ne));
break;
case p::ProveAntiAddictionInfo::kType:
InternalProveAntiAddictionInfo paa;
paa.serial = serial;
memcpy(&paa.uid, data, sizeof(p::ProveAntiAddictionInfo));
NotifyProveAntiAddictionInfo(flag, paa);
break;
case p::GetAntiAddictionInfo::kType:
if ( !IsSessionValid(serial) )
{
return;
}
uid = GetUsers().Serial2UserID(serial);
head.aid = uid; head.type = type; head.flag = flag;
ProcessMsg2Account( head, data, bytes);
break;
default:
if ( !IsSessionValid(serial) )
{
return;
}
uid = GetUsers().Serial2UserID(serial);
head.aid = uid; head.type = type; head.flag = flag;
if (type < (int16_t)CommonType::kMax && type>0)
{
ProcessPlayerMsg2Interact( head, data, bytes);
}
else if (type>0)
{
if( type<=(int16_t)DataGetType::kDataGetTypeEnd && type>=kDataGetTypeBegin)
{
ProcessPlayerMsg2Data(head,data,bytes);
}
ProcessPlayerMsg2World(head, data, bytes);
}
break;
}
}
void ProcessMsgFromWord( MqHead head, uint8_t* data, size_t len )
{
//printf("got msg from world: %d\n", head.type);
auto& users = GetUsers();
Serial serial = users.UserID2Serial(head.aid);
//std::cout <<"Send 2 client type="<<head.type<<" len="<<len<<"\n";
switch ( head.type )
{
case kUserEnterSucceeded:
//printf("enter succeeded , uid: %d\n", head.aid);
p::LoginReturn login_return;
login_return.error = p::LoginReturn::Result::kSucceeded;
login_return.result = p::LoginReturn::Result::kSucceeded;
login_return.now = (int32_t)time(nullptr);
login_return.version = GetMyVersion();
Send(serial, head.flag, login_return);
//
InternalLoginSucceeded ls;
memset(&ls, 0, sizeof(ls));
ls.uid = head.aid;
const char * ip;
ip = GetServer().GetSessionAddress(serial);
if(ip)
{
strcat(ls.ipAddr, ip);
}
NotifyUserLoginSucceeded2Account( ls );
//-----------------------test-----------------------
//InternalWelcomeToGame welcome;
//welcome.uid = head.aid;
//MqHead new_head;
//new_head.aid = head.aid;
//new_head.type= welcome.kType;
//new_head.flag = -1;
//ProcessPlayerMsg2World(new_head, (uint8_t*)&welcome, sizeof(welcome));
//
return;
case kKickUser:
InternalLogout lo;
lo.uid = head.aid;
NotifyUserExit2Account(lo);
NotifyKick2Account( (KickUser&)*data );
//
GetServer().Close(serial);
NetError(serial);
return;
default:
break;
}
if (head.type<=kBroadcastTypeEnd&&head.type>=kBroadcastTypeBegin)
{
users.ForEachSerial([&](Serial serial){Send(serial, head.type, -1, data, len);});
}
else
{
Send( serial, head.type, head.flag, data, len);
}
}
void ProcessMsgFromInteract( MqHead head, uint8_t* data, size_t len )
{
//assert(head.type<(int16_t)CommonType::kMax);
auto& users = GetUsers();
if (head.type<=kBroadcastTypeEnd&&head.type>=kBroadcastTypeBegin)
{
if (head.type==29022) return;
//printf("recv a broadcast msg from interact: type=%d,len=%d\n");
users.ForEachSerial([&](Serial serial){Send(serial, head.type, -1, data, len);});
}
else
{
Serial serial = users.UserID2Serial(head.aid);
Send( serial, head.type, head.flag, data, len);
}
}
bool DoUserLogin(MqHead& head, const InternalLoginResult& l_res)
{
bool bret = false;
Serial serial = (Serial)l_res.serial;
//printf("user login: %d , %08X\n", l_res.uid, serial );
//
p::LoginReturn login_return = {p::LoginReturn::Result::kSucceeded, (p::LoginReturn::Result)l_res.result, (int32_t)time(nullptr)};
login_return.version = GetMyVersion();
//
if( l_res.result == (Result)p::LoginReturn::Result::kSucceeded )
{
auto old_serial = GetUsers().UserID2Serial(l_res.uid);
if (old_serial != network::kErrorSerial)
{
p::AnotherLoginNotify aln;
Send(old_serial, -1, aln);
GetServer().Close(old_serial);
NetError(old_serial);
}
//
//uid和serial关联
bret = GetUsers().Add(l_res.uid, serial);
if (!bret)
{
printf("Uid=%d login failed: user add failed\n", l_res.uid);
login_return.result = p::LoginReturn::Result::kFailed;
Send(serial, head.flag, login_return);
}else
{//通知后端程序 , 用户登录验证成功
NotifyUserEnter2Interact(l_res.uid, head.flag);
NotifyUserEnter2World(l_res.uid, head.flag);
}
}else
{
printf("Uid=%d login failed, ret_val=%d\n", l_res.uid, l_res.result);
Send(serial, head.flag, login_return);
}
return bret;
}
bool DoUserRegister(MqHead& head, InternalRegisterResult& r_res)
{
//printf("register: %d, %08X\n", r_res.result, (int32_t)r_res.serial);
p::RegisterResult r_result = {p::RegisterResult::Result::kSucceeded, (p::RegisterResult::Result)r_res.result };
Serial serial = (Serial)r_res.serial;
Send(serial, head.flag, r_result);
if( r_res.result==p::RegisterResult::Result::kSucceeded )
{
InternalWelcomeToGame welcome = { r_res.uid };
MqHead head = { r_res.uid, welcome.kType, -1 };
ProcessPlayerMsg2World(head, (uint8_t*)&welcome, sizeof(welcome));
}
return true;
}
bool DoIsUidExist(MqHead& head, InternalIsUidExistResult& iue_res)
{
p::IsUidExistResult ue_res = { (p::IsUidExistResult::Result)0, (p::IsUidExistResult::Result)iue_res.result };
Serial serial = (Serial)iue_res.serial;
Send(serial, head.flag, ue_res);
return true;
}
bool DoIsNicknameExist(MqHead&h, InternalIsNicknameExistResult& ine_res)
{
p::IsNicknameExistResult ne_res = { ine_res.b_exist };
Serial serial = (Serial)ine_res.serial;
Send(serial, h.flag, ne_res);
return true;
}
bool DoAntiAddictionShutdown(InternalAntiAddictionShutdown& sd)
{
auto& users = GetUsers();
Serial serial = users.UserID2Serial(sd.uid);
p::NotifyAntiAddictionShutdown notify_sd;
Send( serial, -1, notify_sd);
InternalLogout lo = { sd.uid };
NotifyUserExit2Account(lo);
//
GetServer().Close(serial);
NetError(serial);
return true;
}
bool DoNotifyAntiAddictionInfo(MqHead& head, p::NotifyAntiAddictionInfo& aa_info)
{
auto& users = GetUsers();
Serial serial = users.UserID2Serial(head.aid);
Send(serial, -1, aa_info);
return true;
}
bool DoProveAntiAddictionInfoResult(MqHead& head, InternalProveAntiAddictionInfoResult& paar)
{
p::ProveAntiAddictionInfoResult res = { (p::ProveAntiAddictionInfoResult::Result)paar.result };
Serial serial = (Serial)paar.serial;
Send(serial, head.flag, res);
return true;
}
void ProcessMsgFromAccount( MqHead head, uint8_t* data, size_t len)
{
//printf("user : %d ,, type : %d\n", head.aid, head.type);
switch ( head.type )
{
case InternalLoginResult::kType:
DoUserLogin(head, (const InternalLoginResult&)*data);
break;
case InternalRegisterResult::kType:
DoUserRegister(head, (InternalRegisterResult&)*data);
break;
case InternalIsUidExistResult::kType:
DoIsUidExist(head, (InternalIsUidExistResult&)*data);
break;
case InternalIsNicknameExistResult::kType:
DoIsNicknameExist(head, (InternalIsNicknameExistResult&)*data);
break;
case InternalAntiAddictionShutdown::kType:
DoAntiAddictionShutdown((InternalAntiAddictionShutdown&)*data);
break;
case p::NotifyAntiAddictionInfo::kType:
DoNotifyAntiAddictionInfo(head, (p::NotifyAntiAddictionInfo&)*data);
break;
case InternalProveAntiAddictionInfoResult::kType:
DoProveAntiAddictionInfoResult(head, (InternalProveAntiAddictionInfoResult&)*data);
break;
default:
auto& users = GetUsers();
Serial serial = users.UserID2Serial(head.aid);
Send( serial, head.type, head.flag, data, len);
break;
}
}
void ProcessMsgFromData( MqHead head, uint8_t* data, size_t len)
{
// printf("recv msg %d from data\n", head.type);
const uint16_t kAddPrestigeBegin = 8901;
const uint16_t kAddPrestigeEnd = 9000;
//推送给world() 8901 ~ 9000直接传给 world
if (head.type >= kAddPrestigeBegin && head.type <= kAddPrestigeEnd )//从data发过来的数据发到world里面
{
ProcessPlayerMsg2World(head,data,len);
}
else
{
auto& users = GetUsers();
Serial serial = users.UserID2Serial(head.aid);
Send( serial, head.type, head.flag, data, len);
}
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cerr << "Usage: gate <port>\n";
return 1;
}
//for world server
auto& mq_word = CreateMQ4World(kGateForWorld);
//for interact server
auto& mq_interact = CreateMQ4Interact(kGateForInteract);
//for account server
auto& mq_account = CreateMQ4Account(kGateForAccount);
InitAccount();
//for data
auto& mq_data = CreateMQ4Data(kGateForData);
network::CServer s("0.0.0.0", atoi(argv[1]), PlayerMsgProcess);
if(argc>=3) s.SetSessionDeadSeconds(atoi(argv[2]));
InitSend(&s);
for (;;)
{
DealwithMQ(mq_word, ProcessMsgFromWord);
DealwithMQ(mq_interact, ProcessMsgFromInteract);
DealwithMQ(mq_account, ProcessMsgFromAccount);
DealwithMQ(mq_data, ProcessMsgFromData);
network::Serial dead_serials[2000];
size_t dead_serials_len = 0;
s.GetDeadSessions(dead_serials, dead_serials_len);
for (size_t i=0; i<dead_serials_len; ++i)
{
if (GetUsers().Has(dead_serials[i]))
{
UserID uid = GetUsers().Serial2UserID(dead_serials[i]);
InternalLogout lo = { uid };
NotifyUserExit2Account(lo);
}
s.Close(dead_serials[i]);
NetError(dead_serials[i]);
}
IOSWork();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}