-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver.h
192 lines (161 loc) · 3.9 KB
/
server.h
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
#ifndef SERVER_H
#define SERVER_H
#include <threadpool.hpp>
#include <boost/bind.hpp>
#include <boost/pool/pool.hpp>
#include <boost/lockfree/queue.hpp>
#include "Game/cmdtypes.h"
class Monster;
class JobPool;
class Task;
class MemDBManager;
class DiskDBManager;
class PlayerLogin;
class GameTask;
class TeamFriend;
class GameAttack;
class GameInterchange;
class OperationGoods;
class OperationPostgres;
class CmdParse;
class GameChat;
using namespace boost::threadpool;
class Server
{
public:
virtual ~Server();
void* malloc()
{
m_mtx.lock();
void *res = m_memory_factory.malloc();
m_mtx.unlock();
return res;
}
void free(void *ptr)
{
m_mtx.lock();
m_memory_factory.free( ptr );
m_mtx.unlock();
}
static void memDelete(void *ptr)
{
Server::GetInstance()->free(ptr);
}
/**
* @brief InitDB
*
* 初始化数据库连接
*
*/
void InitDB();
/**
* @brief InitListen
* 初始化监听套接字
*/
void InitListen();
/**
* @brief RunTask
*/
template <typename T>
void RunTask( T task)
{
m_task_pool.schedule(task);
}
template <typename T>
void RunPackTask( T task)
{
m_pack_pool.schedule(task);
}
static Server* GetInstance();
DiskDBManager* getDiskDB()
{
return m_DiskDB;
}
MemDBManager* getMemDB()
{
return m_MemDB;
}
Monster *GetMonster() { return m_monster;}
PlayerLogin* GetPlayerLogin()
{
return m_playerLogin;
}
GameTask* GetGameTask()
{
return m_gameTask;
}
TeamFriend* GetTeamFriend()
{
return m_teamFriend;
}
GameAttack* GetGameAttack()
{
return m_gameAttack;
}
GameInterchange* GetGameInterchange()
{
return m_gameInterchange;
}
OperationGoods* GetOperationGoods()
{
return m_operationGoods;
}
OperationPostgres* GetOperationPostgres()
{
return m_operationPostgres;
}
// CmdParse* GetCmdParse()
// {
// return m_cmdParse;
// }
GameChat* GetGameChat()
{
return m_gameChat;
}
void PushPackage(STR_Package package)
{
_push_times += 1;
uint16_t t = 0;
// Logger::GetLogger()->Debug("Queue length:%d",getQueueLength());
while ( getQueueLength() > 50 )
{
usleep(1000);
t++;
Logger::GetLogger()->Debug("Wait for server to process package");
if ( t > 1000 )
{
break;
}
}
m_package->push(package);
}
void PopPackage();
private:
Server();
static Server *m_instance;
MemDBManager *m_MemDB;
DiskDBManager *m_DiskDB;
////////////////////////////////////////////////////
boost::threadpool::pool m_task_pool;
boost::threadpool::pool m_pack_pool;
boost::pool<> m_memory_factory;
boost::mutex m_mtx;
Monster *m_monster;
PlayerLogin *m_playerLogin;
GameTask *m_gameTask;
TeamFriend *m_teamFriend;
GameAttack *m_gameAttack;
GameInterchange *m_gameInterchange;
OperationGoods *m_operationGoods;
OperationPostgres *m_operationPostgres;
// CmdParse *m_cmdParse;
GameChat *m_gameChat;
boost::lockfree::queue<STR_Package> *m_package;
int getQueueLength() const
{
return _push_times - _pop_times;
}
boost::atomic_uint32_t _push_times;
boost::atomic_uint32_t _pop_times;
};
#endif // SERVER_H