dll_game.cpp 6.05 KB
#include <common.h>
#include "gateway.h"
#include "client_proto_head.h"
#include "user.h"
#include "dbproxy.h"
#include "loginserv.h"
#include "loginserv.h"
#include <gateway_msg.pb.h>
#include <lib_util.h>
#include <db.pb.h>
#include <statisitcs_srv.h>

#include "sys.h"
#include "level.h"
#include "lobby_service.h"
#ifdef EL_ASYNC_USE_THREAD
#error "EL_ASYNC_USE_THREAD"
#endif // EL_ASYNC_USE_THREAD

extern "C"{
	int on_init(){
		if(el_async::is_parent()){
			DEBUG_LOG("======daemon start======");
			DEBUG_LOG("Compiled at [%s-%s]", __DATE__, __TIME__);
		}else{
			DEBUG_LOG("======server start======");
			
			::srand(el_async::get_now_sec());
			g_gateway = new gateway_t;
 			if(SUCC != g_gateway->init()){
				ALERT_LOG("game init failed...");
 				return ERR;
			}
			//vip_level_cfg_t* vlc = g_vip_level_cfg_mgr->find(29);

			//////////////////////////////////////////////////////////////////////////
			/*
			std::map<uint32_t, uint32_t> m;
			for (int i = 0; i < 10000; i++){
				::srand(el_async::get_now_usec()+el::lib_random_t::random_u32(1, 10000));
				uint32_t probability = el::lib_random_t::random_u32(1, 10000);
				//::Sleep(el::lib_random_t::random_u32(1, 2)*1236);
				uint32_t sum_probability = 0;
				game_birds_element_t* element = NULL;
				FOREACH(g_game_birds_cfg_mgr->element_map, it){
					game_birds_element_t* p = &it->second;
					sum_probability += p->probability;
					if (probability <= sum_probability){
						element = p;
						break;
					}
				}

				m[element->birds]++;
			}
			FOREACH(m, it_m){
				DEBUG_LOG("%u,%u", it_m->first, it_m->second);

			}
			*/
			//////////////////////////////////////////////////////////////////////////
			
			{
				db_msg::game_server_boot_msg out;
				g_dbproxy->send_msg(&out, 0, db_game_server_boot_msg_cmd, NULL);
			}

			{
				db_msg::load_rank_msg out;
				g_dbproxy->send_msg(&out, 0, db_load_rank_msg_cmd);
			}
			{
				db_msg::load_btl_rank_msg out;
				g_dbproxy->send_msg(&out, 0, db_load_btl_rank_msg_cmd);
			}

			{
				db_msg::load_yesterday_btl_rank_msg out;
				g_dbproxy->send_msg(&out, 0, db_load_yesterday_btl_rank_msg_cmd);
			}

			{
				db_msg::load_exprie_rank_msg out;
				g_dbproxy->send_msg(&out, 0, db_load_exprie_rank_msg_cmd);
			}
		}
		return 0;
	}

	int on_fini(){
		if (el_async::is_parent()) {
			DEBUG_LOG("======daemon done======");
		}else{
			DEBUG_LOG("======server done======");
			SAFE_DELETE(g_gateway);
		}
		return 0;
	}

	void on_events(){
		if (el_async::is_parent()){
		}else{
			g_timer->handle_timer();
		}
	}

	int on_get_pkg_len(el::lib_tcp_peer_info_t* peer_fd_info,
		const void* data, uint32_t len){
		if (len < proto_head_t::PROTO_HEAD_LEN){
			return 0;
		}
		g_proto_head.unpack(data);
		
		PROTO_LEN pkg_len = g_proto_head.length;
		if (pkg_len < proto_head_t::PROTO_HEAD_LEN || pkg_len >= g_bench_conf->page_size_max){
			ERROR_LOG("head len err [len=%u]", len);
			return -1;
		}
		if (len < pkg_len){
			return 0;
		}
		return pkg_len;
	}

	int on_cli_pkg(const void* data, uint32_t len, el::lib_tcp_peer_info_t* peer_fd_info){
		g_proto_head.unpack(data);	
		return g_gateway->on_recv(peer_fd_info, ((char*)data)+proto_head_t::PROTO_HEAD_LEN , len-proto_head_t::PROTO_HEAD_LEN);
	}

	void on_srv_pkg(const void* data, uint32_t len, el::lib_tcp_peer_info_t* peer_fd_info){
		g_proto_head.unpack(data);
		char* body_data = ((char*)data)+proto_head_t::PROTO_HEAD_LEN;
		int body_len = len-proto_head_t::PROTO_HEAD_LEN;
		if (g_dbproxy->fd_info == peer_fd_info){
			int ret = g_dbproxy->on_recv(peer_fd_info, g_proto_head.cmd, 
				body_data, body_len, g_proto_head.id, g_proto_head.seq, g_proto_head.result);
			if (SUCC != ret){
				WARN_LOG("[ret:%d]", ret);
			}
		} else if (g_loginserv->fd_info == peer_fd_info){
			int ret = g_loginserv->on_recv(peer_fd_info, g_proto_head.cmd,
				body_data, body_len, g_proto_head.id, g_proto_head.seq, g_proto_head.result);
			if (SUCC != ret){
				WARN_LOG("[ret:%d]", ret);
			}
		} else if (g_lobby_service->peer == peer_fd_info){
			int ret = g_lobby_service->on_recv(peer_fd_info, g_proto_head.cmd,
				body_data, body_len, g_proto_head.id, g_proto_head.seq, g_proto_head.result);
			if (SUCC != ret){
				WARN_LOG("[ret:%d]", ret);
			}
		}
	}
	 
	void on_cli_conn(el::lib_tcp_peer_info_t* peer_fd_info){
		std::string str_ip = peer_fd_info->get_ip_str();
		TRACE_LOG("[ip:%s]", str_ip.c_str());
		g_user_mgr->add_fd_user(peer_fd_info);
	}

	void on_cli_conn_closed(int fd){
		g_user_mgr->offline(fd, false);
	}

	void on_svr_conn_closed(int fd){
		CRIT_LOG("[fd:%d]", fd);
		if (NULL != g_dbproxy->fd_info && fd == g_dbproxy->fd_info->fd){
			g_dbproxy->offline();
		} else if (NULL != g_loginserv->fd_info && fd == g_loginserv->fd_info->fd){
			g_loginserv->fd_info = NULL;
		} else if (NULL != g_statisitcs->fd_info && fd == g_statisitcs->fd_info->fd){
			g_statisitcs->offline();
		} else if (NULL != g_lobby_service->peer && fd == g_lobby_service->peer->fd){
			g_lobby_service->offline();
		}
	}

	void on_svr_conn(int fd){
		TRACE_LOG("[fd:%d]", fd);
	}

	void on_mcast_pkg(const void* data, int len){
		std::string str;
		str.append((char*)data, len);
		TRACE_LOG("[len:%d, data:%s]", len, str.c_str());

		std::string::size_type index = str.find_first_of(',', 0);
		if (std::string::npos == index){
			ALERT_LOG("");
			return ;
		}
		std::string str_id(str, 0, index);
		std::string str_val(str, index+1);
		uint32_t cmd_id = 0;
		el::convert_from_string(cmd_id, str_id);
		switch (cmd_id)
		{
		case 1:
			{
				gateway_msg::sys_notice_msg_res out;
				share_msg::notice_t* notice = out.mutable_notice();
				notice->set_id(share_msg::E_NOTICE_SYS);
				notice->add_param(str_val);

				FOREACH(g_user_mgr->user_login_complete_map, it){
					user_t* u = it->second;
					u->send_msg(cli_sys_notice_msg_cmd, &out);
				}
			}
			break;
		default:
			break;
		}
	}

	void on_addr_mcast_pkg(uint32_t id, const char* name, const char* ip, uint16_t port, const char* data, int flag){
//		TRACE_LOG("id:%u, name:%s, ip:%s, port:%u, data:%s, flag:%u", id, name, ip, port, data, flag);
	}

	void on_udp_pkg(int fd, const void* data, int len ,struct sockaddr_in* from, socklen_t fromlen){

	}
};