dll_game.cpp
6.05 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
#include <common.h>
#include "gateway.h"
#include "client_proto_head.h"
#include "user.h"
#include "dbproxy.h"
#include "loginserv.h"
#include "loginserv.h"
#include <gateway_msg.pb.h>
#include <lib_util.h>
#include <db.pb.h>
#include <statisitcs_srv.h>
#include "sys.h"
#include "level.h"
#include "lobby_service.h"
#ifdef EL_ASYNC_USE_THREAD
#error "EL_ASYNC_USE_THREAD"
#endif // EL_ASYNC_USE_THREAD
extern "C"{
int on_init(){
if(el_async::is_parent()){
DEBUG_LOG("======daemon start======");
DEBUG_LOG("Compiled at [%s-%s]", __DATE__, __TIME__);
}else{
DEBUG_LOG("======server start======");
::srand(el_async::get_now_sec());
g_gateway = new gateway_t;
if(SUCC != g_gateway->init()){
ALERT_LOG("game init failed...");
return ERR;
}
//vip_level_cfg_t* vlc = g_vip_level_cfg_mgr->find(29);
//////////////////////////////////////////////////////////////////////////
/*
std::map<uint32_t, uint32_t> m;
for (int i = 0; i < 10000; i++){
::srand(el_async::get_now_usec()+el::lib_random_t::random_u32(1, 10000));
uint32_t probability = el::lib_random_t::random_u32(1, 10000);
//::Sleep(el::lib_random_t::random_u32(1, 2)*1236);
uint32_t sum_probability = 0;
game_birds_element_t* element = NULL;
FOREACH(g_game_birds_cfg_mgr->element_map, it){
game_birds_element_t* p = &it->second;
sum_probability += p->probability;
if (probability <= sum_probability){
element = p;
break;
}
}
m[element->birds]++;
}
FOREACH(m, it_m){
DEBUG_LOG("%u,%u", it_m->first, it_m->second);
}
*/
//////////////////////////////////////////////////////////////////////////
{
db_msg::game_server_boot_msg out;
g_dbproxy->send_msg(&out, 0, db_game_server_boot_msg_cmd, NULL);
}
{
db_msg::load_rank_msg out;
g_dbproxy->send_msg(&out, 0, db_load_rank_msg_cmd);
}
{
db_msg::load_btl_rank_msg out;
g_dbproxy->send_msg(&out, 0, db_load_btl_rank_msg_cmd);
}
{
db_msg::load_yesterday_btl_rank_msg out;
g_dbproxy->send_msg(&out, 0, db_load_yesterday_btl_rank_msg_cmd);
}
{
db_msg::load_exprie_rank_msg out;
g_dbproxy->send_msg(&out, 0, db_load_exprie_rank_msg_cmd);
}
}
return 0;
}
int on_fini(){
if (el_async::is_parent()) {
DEBUG_LOG("======daemon done======");
}else{
DEBUG_LOG("======server done======");
SAFE_DELETE(g_gateway);
}
return 0;
}
void on_events(){
if (el_async::is_parent()){
}else{
g_timer->handle_timer();
}
}
int on_get_pkg_len(el::lib_tcp_peer_info_t* peer_fd_info,
const void* data, uint32_t len){
if (len < proto_head_t::PROTO_HEAD_LEN){
return 0;
}
g_proto_head.unpack(data);
PROTO_LEN pkg_len = g_proto_head.length;
if (pkg_len < proto_head_t::PROTO_HEAD_LEN || pkg_len >= g_bench_conf->page_size_max){
ERROR_LOG("head len err [len=%u]", len);
return -1;
}
if (len < pkg_len){
return 0;
}
return pkg_len;
}
int on_cli_pkg(const void* data, uint32_t len, el::lib_tcp_peer_info_t* peer_fd_info){
g_proto_head.unpack(data);
return g_gateway->on_recv(peer_fd_info, ((char*)data)+proto_head_t::PROTO_HEAD_LEN , len-proto_head_t::PROTO_HEAD_LEN);
}
void on_srv_pkg(const void* data, uint32_t len, el::lib_tcp_peer_info_t* peer_fd_info){
g_proto_head.unpack(data);
char* body_data = ((char*)data)+proto_head_t::PROTO_HEAD_LEN;
int body_len = len-proto_head_t::PROTO_HEAD_LEN;
if (g_dbproxy->fd_info == peer_fd_info){
int ret = g_dbproxy->on_recv(peer_fd_info, g_proto_head.cmd,
body_data, body_len, g_proto_head.id, g_proto_head.seq, g_proto_head.result);
if (SUCC != ret){
WARN_LOG("[ret:%d]", ret);
}
} else if (g_loginserv->fd_info == peer_fd_info){
int ret = g_loginserv->on_recv(peer_fd_info, g_proto_head.cmd,
body_data, body_len, g_proto_head.id, g_proto_head.seq, g_proto_head.result);
if (SUCC != ret){
WARN_LOG("[ret:%d]", ret);
}
} else if (g_lobby_service->peer == peer_fd_info){
int ret = g_lobby_service->on_recv(peer_fd_info, g_proto_head.cmd,
body_data, body_len, g_proto_head.id, g_proto_head.seq, g_proto_head.result);
if (SUCC != ret){
WARN_LOG("[ret:%d]", ret);
}
}
}
void on_cli_conn(el::lib_tcp_peer_info_t* peer_fd_info){
std::string str_ip = peer_fd_info->get_ip_str();
TRACE_LOG("[ip:%s]", str_ip.c_str());
g_user_mgr->add_fd_user(peer_fd_info);
}
void on_cli_conn_closed(int fd){
g_user_mgr->offline(fd, false);
}
void on_svr_conn_closed(int fd){
CRIT_LOG("[fd:%d]", fd);
if (NULL != g_dbproxy->fd_info && fd == g_dbproxy->fd_info->fd){
g_dbproxy->offline();
} else if (NULL != g_loginserv->fd_info && fd == g_loginserv->fd_info->fd){
g_loginserv->fd_info = NULL;
} else if (NULL != g_statisitcs->fd_info && fd == g_statisitcs->fd_info->fd){
g_statisitcs->offline();
} else if (NULL != g_lobby_service->peer && fd == g_lobby_service->peer->fd){
g_lobby_service->offline();
}
}
void on_svr_conn(int fd){
TRACE_LOG("[fd:%d]", fd);
}
void on_mcast_pkg(const void* data, int len){
std::string str;
str.append((char*)data, len);
TRACE_LOG("[len:%d, data:%s]", len, str.c_str());
std::string::size_type index = str.find_first_of(',', 0);
if (std::string::npos == index){
ALERT_LOG("");
return ;
}
std::string str_id(str, 0, index);
std::string str_val(str, index+1);
uint32_t cmd_id = 0;
el::convert_from_string(cmd_id, str_id);
switch (cmd_id)
{
case 1:
{
gateway_msg::sys_notice_msg_res out;
share_msg::notice_t* notice = out.mutable_notice();
notice->set_id(share_msg::E_NOTICE_SYS);
notice->add_param(str_val);
FOREACH(g_user_mgr->user_login_complete_map, it){
user_t* u = it->second;
u->send_msg(cli_sys_notice_msg_cmd, &out);
}
}
break;
default:
break;
}
}
void on_addr_mcast_pkg(uint32_t id, const char* name, const char* ip, uint16_t port, const char* data, int flag){
// TRACE_LOG("id:%u, name:%s, ip:%s, port:%u, data:%s, flag:%u", id, name, ip, port, data, flag);
}
void on_udp_pkg(int fd, const void* data, int len ,struct sockaddr_in* from, socklen_t fromlen){
}
};