statisitcs.cpp
3.02 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
#include "statisitcs.h"
#include "sys.h"
#include <statisitcs.pb.h>
statisitcs_t* g_statisitcs;
statisitcs_t::statisitcs_t(){
this->init_msg_map();
g_sys = new sys_t;
}
int statisitcs_t::init(){
return SUCC;
}
void statisitcs_t::release(){
this->release_msg_map();
SAFE_DELETE(g_sys);
}
void statisitcs_t::release_msg_map(){
FOREACH(this->msg_map, it){
SAFE_DELETE(it->second.prototype);
}
this->msg_map.clear();
}
int statisitcs_t::on_recv(el::lib_tcp_peer_info_t* peer_fd_info, char* pdata, int len){
auto cmd_id = g_proto_head.cmd;
auto it = this->msg_map.find(cmd_id);
if(this->msg_map.end() == it){
CRIT_LOG("no msg define for [cmd_id:0x%x]", cmd_id);
return el::ERR_SYS::DISCONNECT_PEER;
}
auto prototype = it->second.prototype;
try{
if(!prototype->ParseFromArray(pdata, len)){
ERROR_LOG("decode message [cmd_id:0x%x] with %d bytes failed", cmd_id, len);
return el::ERR_SYS::DISCONNECT_PEER;
}
TRACE_LOG("<======recv gateway [cmd_id:0x%x, body_len:%d]",
cmd_id, len);
if (!prototype->Utf8DebugString().empty()){
TRACE_LOG("%s", prototype->Utf8DebugString().c_str());
}
int ret = (this->*it->second.func)(peer_fd_info, prototype);
if (SUCC != ret){
TRACE_LOG("======>gateway msg handle error [cmd_id:0x%x, ret:%d]",
cmd_id, ret);
}
return ret;
}catch(...){
ERROR_LOG("parse message failed cmd_id:0x%x]. kick client", cmd_id);
return el::ERR_SYS::DISCONNECT_PEER;
}
return SUCC;
}
int statisitcs_t::init_msg_map(){
#undef BIND_PROTO_CMD
#undef BIND_PROTO_CMD_NO_CB
#define BIND_PROTO_CMD_NO_CB(__cmd_id__, fun_name, proto_name)\
{\
if (this->msg_map.end() != this->msg_map.find(__cmd_id__)){\
ALERT_LOG("cmd inster err![0x%x]", __cmd_id__);\
exit(0);\
} else {\
this->msg_map[__cmd_id__] = MSG_HANDLE_STATISITCS_T(new statisitcs_msg::proto_name(), &statisitcs_t::fun_name);\
}\
}
#define BIND_PROTO_CMD(__cmd_id__, fun_name, proto_name)\
{\
if (this->msg_map.end() != this->msg_map.find(__cmd_id__)){\
ALERT_LOG("cmd inster err![0x%x]", __cmd_id__);\
exit(0);\
} else {\
this->msg_map[__cmd_id__] = MSG_HANDLE_STATISITCS_T(new statisitcs_msg::proto_name(), &statisitcs_t::fun_name);\
}\
}
#include <statisitcs_cmd.h>
#undef BIND_PROTO_CMD
return SUCC;
}
statisitcs_t::~statisitcs_t()
{
this->release();
}
int statisitcs_t::on_save_msg(el::lib_tcp_peer_info_t* fd_info,
google::protobuf::Message* msg){
statisitcs_msg::save_msg* in = (statisitcs_msg::save_msg*)msg;
std::string str;
FOREACH_PB(in->expand_param, idx){
str += el::convert_to_string(in->expand_param(idx));
str += ",";
}
if (!str.empty()){
str.pop_back();
INFO_MSG_LOG("statistics,platform:%u,uid:%" PRIu64 ",event_id:%u,time:%u,expand_param:%s",
in->platform(),g_proto_head.id,in->event_id(),el_async::get_now_sec(),str.c_str());
} else {
INFO_MSG_LOG("statistics,platform:%u,uid:%" PRIu64 ",event_id:%u,time:%u",
in->platform(),g_proto_head.id,in->event_id(),el_async::get_now_sec());
}
return SUCC;
}