-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushServer.cpp
More file actions
372 lines (326 loc) · 10.2 KB
/
Copy pathPushServer.cpp
File metadata and controls
372 lines (326 loc) · 10.2 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
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
#include "PushServer.h"
#include "PushServantImp.h"
#include "UserStateProcessor.h"
//
using namespace std;
/////
PushServer g_app;
////
extern std::atomic<bool> g_PushUpdateNotify;
extern long g_RemainingTime;
/////////////////////////////////////////////////////////////////
void
PushServer::initialize()
{
//注册服务接口
addServant<PushServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".PushServantObj");
//初始化外部接口对象
initOuterFactory();
//初始化所有线程
initialThread();
//注册动态加载命令
TARS_ADD_ADMIN_CMD_NORMAL("reload", PushServer::reloadSvrConfig);
//清除在线玩家状态
TARS_ADD_ADMIN_CMD_NORMAL("CleanOnlineUser", PushServer::cleanOnlineUser);
//服务器维护
TARS_ADD_ADMIN_CMD_NORMAL("ServerUpdate", PushServer::serverUpdate);
//零点在线重置
TARS_ADD_ADMIN_CMD_NORMAL("DailyReset", PushServer::dailyReset);
//打印在线玩家
TARS_ADD_ADMIN_CMD_NORMAL("PrintOnline", PushServer::printOnlineUsers);
//打印在线机器人
TARS_ADD_ADMIN_CMD_NORMAL("PrintRobots", PushServer::printOnlineRobots);
}
/////////////////////////////////////////////////////////////////
void PushServer::destroyApp()
{
//结束所有线程
//
//
destroyThread();
}
/*
* 配置变更,重新加载配置
*/
bool PushServer::reloadSvrConfig(const string &command, const string ¶ms, string &result)
{
try
{
getOuterFactoryPtr()->load();
result = "reload server config success.";
LOG_ERROR << "reloadSvrConfig: " << result << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = "catch unknown exception.";
}
result += "\n fail, please check it.";
LOG_ERROR << "reloadSvrConfig: " << result << endl;
return true;
}
/*
* 清除在线玩家状态
* params : 指定在线玩家uid,如果为-1,则表示清除所有在线玩家
* example -> CleanOnlineUser -1 or CleanOnlineUser 123456
*/
bool PushServer::cleanOnlineUser(const string &command, const string ¶ms, string &result)
{
try
{
LOG_ERROR << "Enter clean online user, command : " << command << ", params : " << params << endl;
int iRet = 0;
if (params == "-1")
{
//所有在线玩家
iRet = getOuterFactoryPtr()->cleanOnlineStatus();
}
else
{
long lUserId = S2L(params);
if (lUserId < 0)
{
result = string("clean online user failed, error params : ") + params;
LOG_ERROR << "clean online user falied, result : " << result << endl;
return false;
}
//指定在线玩家
iRet = getOuterFactoryPtr()->cleanOnlineStatus(lUserId);
}
if (iRet != 0)
{
result = string("clean online user cleanOnlineStatus falied, iRet : ") + I2S(iRet);
LOG_ERROR << "clean online user falied, result : " << result << endl;
return false;
}
LOG_ERROR << "clean online user cleanOnlineStatus finished." << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = string("catch unknown exception.");
}
result += string("\n fail, please check it.");
LOG_DEBUG << "clean online user finished, result : " << result << endl;
return true;
}
/*
* 服务器维护
* params : 分钟数
* example -> ServerUpdate 5
*/
bool PushServer::serverUpdate(const string &command, const string ¶ms, string &result)
{
try
{
LOG_ERROR << "Enter server update, command : " << command << ", params : " << params << endl;
int iMinutes = S2I(params);
if (iMinutes <= 0)
{
result = string("server update failed, error params : ") + params;
LOG_ERROR << "server update falied, result : " << result << endl;
return false;
}
//通知所有在线玩家
g_RemainingTime = iMinutes;
g_PushUpdateNotify = true;
LOG_ERROR << "server update pushServantImp.serverUpdateNotify finished." << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = string("catch unknown exception.");
}
result += string("\n fail, please check it.");
LOG_ERROR << "server update finished, result : " << result << endl;
return true;
}
/**
* 零点在线状态重置
*/
bool PushServer::dailyReset(const string &command, const string ¶ms, string &result)
{
try
{
auto robots = g_app.getOuterFactoryPtr()->getRobotList();
if (robots.empty())
{
g_app.getOuterFactoryPtr()->readRobotListResp();
robots = g_app.getOuterFactoryPtr()->getRobotList();
LOG_ERROR << "Load online robot list, robotNum: " << robots.size() << endl;
}
std::map<long, long> uids;
UserStateSingleton::getInstance()->getOnlineUsers(uids, true);
LOG_ERROR << "Update online player status, iOnlineNum: " << uids.size() << endl;
for (auto iter = uids.begin(); iter != uids.end(); iter++)
{
long uid = (*iter).first;
g_app.getOuterFactoryPtr()->asyncUserStateNotify(uid);
LOG_ERROR << "Update player status, uid: " << uid << endl;
}
result = string("success");
LOG_ERROR << "daily reset finished." << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = string("catch unknown exception.");
}
result += string("\n fail, please check it.");
LOG_ERROR << "daily reset finished, result : " << result << endl;
return true;
}
/**
* 打印在线用户信息
*/
bool PushServer::printOnlineUsers(const string &command, const string ¶ms, string &result)
{
try
{
std::map<long, long> uids;
UserStateSingleton::getInstance()->getOnlineUsers(uids, true);
LOG_ERROR << "----------@Print onlineinfo list, playerNum: " << uids.size() << endl;
for (auto iter = uids.begin(); iter != uids.end(); iter++)
{
long uid = (*iter).first;
long lastUpdateTime = (*iter).second;
LOG_ERROR << "@Print onlineinfo, uid: " << uid << ",lastUpdateTime: " << lastUpdateTime << endl;
}
result = string("success");
LOG_ERROR << "----------@Print onlineinfo finished." << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = string("catch unknown exception.");
}
result += string("\n fail, please check it.");
LOG_ERROR << "print online finished, result : " << result << endl;
return true;
}
/**
* 打印在线机器人信息
*/
bool PushServer::printOnlineRobots(const string &command, const string ¶ms, string &result)
{
try
{
LOG_ERROR << "----------@Print robot list begin" << endl;
g_app.getOuterFactoryPtr()->printRobotListResp();
result = string("success");
LOG_ERROR << "----------@Print robot finished." << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = string("catch unknown exception.");
}
result += string("\n fail, please check it.");
LOG_ERROR << "print robot finished, result : " << result << endl;
return true;
}
/**
* 初始化外部接口对象
**/
int PushServer::initOuterFactory()
{
_pOuter = new OuterFactoryImp();
return 0;
}
//初始化所有线程
void PushServer::initialThread()
{
//定时启动线程
_tpool.init(1);
_tpool.start();
std::function<void(int)> cmd(_functor);
//更新在线人数
// _tpool.exec(cmd, Functor::eUpdateOnlineNumber);
//更新玩家状态
_tpool.exec(cmd, Functor::eUpdateUserPlayState);
}
//结束所有线程
void PushServer::destroyThread()
{
_functor.stop();
LOG_DEBUG << "wait for all done ... " << endl;
// 先等待一秒
bool b = _tpool.waitForAllDone(1000);
LOG_DEBUG << "wait for all done ... " << b << ":" << _tpool.getJobNum() << endl;
if (!b)
{
// 没停止则再永久等待
LOG_DEBUG << "wait for all done again, but forever..." << endl;
b = _tpool.waitForAllDone(-1);
LOG_DEBUG << "wait for all done again..." << b << ":" << _tpool.getJobNum() << endl;
}
//停止线程
_tpool.stop();
}
/////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
try
{
g_app.main(argc, argv);
g_app.waitForShutdown();
}
catch (std::exception &e)
{
cerr << "std::exception:" << e.what() << std::endl;
}
catch (...)
{
cerr << "unknown exception." << std::endl;
}
return -1;
}
/////////////////////////////////////////////////////////////////