func_rount.cpp
4.7 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
#include "func_rount.h"
std::string g_db_name_pre;
char tmp_db_name[1024];
const char* g_gen_db_name(const char* db_name)
{
::memset(tmp_db_name, 0, sizeof(tmp_db_name));
::sprintf(tmp_db_name, "%s_%s", g_db_name_pre.c_str(), db_name);
return tmp_db_name;
}
int Cfunc_route::do_handle_dispatcher(const void* data, uint32_t len, el::lib_tcp_peer_info_t* peer_fd_info){
this->head.unpack(data);
this->peer = peer_fd_info;
char* body = (char*)data + proto_head_t::PROTO_HEAD_LEN;
uint32_t body_len = len- proto_head_t::PROTO_HEAD_LEN;
TRACE_LOG("<====== recv[body_len:%u, cmd:%#x, seq:%u, ret:%#x, uid:%" PRIu64 ", fd:%d, pkglen:%u]",
this->head.length, this->head.cmd, this->head.seq,
this->head.result, this->head.id, this->peer->fd, len);
auto iter = m_msgMap.find(this->head.cmd);
if (iter == m_msgMap.end()){
ERROR_LOG("no msg define for 0x%x", head.cmd);
return 0;
}
try{
iter->second.prototype->Clear();
if(!iter->second.prototype->ParseFromArray(body, body_len)){
ERROR_LOG("decode message 0x%x with %u bytes failed", head.cmd, body_len);
return 0;
}
if (!iter->second.prototype->Utf8DebugString().empty()){
TRACE_LOG("%s", iter->second.prototype->Utf8DebugString().c_str());
}
}catch(...){
ERROR_LOG("parse message failed 0x%x ", head.cmd);
return 0;
}
this->ret = (this->*iter->second.func)(head.id, iter->second.prototype);
if (0 != this->ret){
this->db->db_rollback();
}
//提交数据
this->db->db_commit();
if (0 != this->ret){
this->do_s2err(this->peer, this->head, this->ret);
} else {
if (iter->second.is_callback){
this->send_to_client(this->send_buf.write_pos);
}
}
return 0;
}
Cfunc_route::Cfunc_route( el::lib_mysql_if* db )
: Cfunc_route_base(db),
account(db),
role(db),
user_event(db),
item(db),
mail(db),
furniture(db),
pet(db),
rank(db),
btl_rank(db),
yesterday_btl_rank(db),
exprie_rank(db)
{
this->init();
}
void Cfunc_route::init()
{
uint32_t platform_server_id = atoi(g_db_name_pre.substr(1).c_str());
this->server_id = platform_server_id%10000;
this->platform_id = platform_server_id/10000;
this->init_msg_map();
}
void Cfunc_route::encode_msg2sendbuf(google::protobuf::Message* msg)
{
static char* buf = this->send_buf.send_data + proto_head_t::PROTO_HEAD_LEN;
static uint32_t len = sizeof(send_buf.send_data) - proto_head_t::PROTO_HEAD_LEN;
msg->SerializeToArray(buf, len);
uint32_t body_len = msg->ByteSize();
TRACE_LOG("======> send msg len:%u, %s ", body_len, msg->Utf8DebugString().c_str());
this->send_buf.add_body_len(body_len);
this->send_buf.set_head(&this->head);
}
int Cfunc_route::gen_uid( USER_ID& uid )
{
this->ret = user_event.get_user_data(0, common_msg::FOREVER_EVENT_SYS_UID, 0, uid);
if (this->ret != SUCC){
if (this->ret == (int)el::ERR_DB::KEY_INEXIST){
uid = GEN_UID(this->platform_id, this->server_id, MIN_UID);
//check uid is valid
if (uid >= GEN_UID(this->platform_id, this->server_id, MAX_UID)){
CRIT_LOG("");
return common_msg::EEC_SERVER_ACCOUNT_FULL;
}
}else{
return this->ret;
}
}else{
uid++;
}
ret = user_event.update(0, common_msg::FOREVER_EVENT_SYS_UID, 0, uid, 0, "");
if (ret != SUCC){
return ret;
}
return SUCC;
}
void Cfunc_route::init_msg_map()
{
#undef BIND_PROTO_CMD
#undef BIND_PROTO_CMD_NO_CB
#define BIND_PROTO_CMD(cmd, fun_name, proto_name)\
{\
if (m_msgMap.end() != m_msgMap.find(cmd)){\
ALERT_LOG("cmd inster err![0x%x]", cmd);\
exit(0);\
} else {\
m_msgMap[cmd] = MSG_HANDLE_DB_T(new db_msg::proto_name(), &Cfunc_route::fun_name);\
}\
}
#define BIND_PROTO_CMD_NO_CB(cmd, fun_name, proto_name)\
{\
if (m_msgMap.end() != m_msgMap.find(cmd)){\
ALERT_LOG("cmd inster err![0x%x]", cmd);\
exit(0);\
} else {\
m_msgMap[cmd] = MSG_HANDLE_DB_T(new db_msg::proto_name(), &Cfunc_route::fun_name, false);\
}\
}
#include <db_cmd.h>
#undef BIND_PROTO_CMD
#undef BIND_PROTO_CMD_NO_CB
//////////////////////////////////////////////////////////////////////////
#undef BIND_PROTO_CMD
#undef BIND_PROTO_CMD_NO_CB
#define BIND_PROTO_CMD(cmd, fun_name, proto_name)\
{\
if (m_msgMap.end() != m_msgMap.find(cmd)){\
ALERT_LOG("cmd inster err![0x%x]", cmd);\
exit(0);\
} else {\
m_msgMap[cmd] = MSG_HANDLE_DB_T(new db_center_msg::proto_name(), &Cfunc_route::fun_name);\
}\
}
#define BIND_PROTO_CMD_NO_CB(cmd, fun_name, proto_name)\
{\
if (m_msgMap.end() != m_msgMap.find(cmd)){\
ALERT_LOG("cmd inster err![0x%x]", cmd);\
exit(0);\
} else {\
m_msgMap[cmd] = MSG_HANDLE_DB_T(new db_center_msg::proto_name(), &Cfunc_route::fun_name, false);\
}\
}
#undef BIND_PROTO_CMD
#undef BIND_PROTO_CMD_NO_CB
}