statisitcs_srv.cpp 2.89 KB
#include "statisitcs_srv.h"
#include <statisitcs.pb.h>
#include <proto_header.h>

statisitcs_t* g_statisitcs;

int statisitcs_t::send_msg( google::protobuf::Message* msg, USER_ID uid, CMD_ID cmd )
{
	proto_head_t proto_head;
	proto_head.cmd = cmd;
	proto_head.id = uid;

	try{
		static void* sb = this->send_msg_buf.send_data + proto_head_t::PROTO_HEAD_LEN;
		static const int sb_len = sizeof(this->send_msg_buf.send_data) - proto_head_t::PROTO_HEAD_LEN;
		msg->SerializeToArray(sb, sb_len);

		uint32_t body_len = msg->ByteSize();

//		TRACE_LOG("======>s2center msg [uid:%" PRIu64 ", cmd:0x%x, body_len:%u]", uid, cmd, body_len);

		this->send_msg_buf.add_body_len(body_len);
		this->send_msg_buf.set_head(&proto_head);

		int ret = this->send(this->send_msg_buf.send_data, body_len + proto_head_t::PROTO_HEAD_LEN);

// 		if (!msg->Utf8DebugString().empty()){
// 			TRACE_LOG("%s", msg->Utf8DebugString().c_str());
// 		}
		return ret;
	} catch (...){
		ERROR_LOG("[uid:%" PRIu64 ", cmd:%u, seq:%u]", uid, (uint32_t)cmd, proto_head.seq);
	}
	return 0;
}

int statisitcs_t::send( const void* data, uint32_t len )
{
	if (NULL == this->fd_info){
		auto ip = g_bench_conf->get_strval("statisitcs", "ip");
		uint16_t port = ::atoi(g_bench_conf->get_strval("statisitcs", "port").c_str());	
		fd_info = el_async::connect(ip, port);
		if (fd_info == NULL){
			WARN_LOG("connet to center fail |%s|%u", ip.c_str(), port);
			return ERR;
		}
	}

	//    TRACE_MSG_HEX_LOG(data, len);

	return el_async::s2peer(fd_info, data, len);
}

statisitcs_t::statisitcs_t()
{
	this->fd_info = NULL;
}

statisitcs_t::~statisitcs_t()
{
}

void statisitcs_t::offline()
{
	this->fd_info = NULL;
}

void statisitcs_t::log( USER_ID uid, common_msg::E_STATISTICS_TYPE type )
{
	statisitcs_msg::save_msg out;
	out.set_platform(g_gen_platform(uid));
	out.set_event_id(type);

	this->send_msg(&out, uid, statisitcs_save_msg_cmd);
}

void statisitcs_t::log( USER_ID uid, common_msg::E_STATISTICS_TYPE type, uint32_t expand_param1 )
{
	statisitcs_msg::save_msg out;
	out.set_platform(g_gen_platform(uid));
	out.set_event_id(type);
	out.add_expand_param(expand_param1);

	this->send_msg(&out, uid, statisitcs_save_msg_cmd);
}

void statisitcs_t::log( USER_ID uid, common_msg::E_STATISTICS_TYPE type, uint32_t expand_param1, uint32_t expand_param2 )
{
	statisitcs_msg::save_msg out;
	out.set_platform(g_gen_platform(uid));
	out.set_event_id(type);
	out.add_expand_param(expand_param1);
	out.add_expand_param(expand_param2);

	this->send_msg(&out, uid, statisitcs_save_msg_cmd);
}

void statisitcs_t::log( USER_ID uid, common_msg::E_STATISTICS_TYPE type, uint32_t expand_param1, uint32_t expand_param2, uint32_t expand_param3 )
{
	statisitcs_msg::save_msg out;
	out.set_platform(g_gen_platform(uid));
	out.set_event_id(type);
	out.add_expand_param(expand_param1);
	out.add_expand_param(expand_param2);
	out.add_expand_param(expand_param3);

	this->send_msg(&out, uid, statisitcs_save_msg_cmd);
}