statisitcs.cpp 3.02 KB
#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;
}