func_rount.cpp 4.7 KB
#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
}