dll_game.cpp 2.1 KB
#include <common.h>
#include "client_proto_head.h"
#include "statisitcs.h"
#include "sys.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_statisitcs = new statisitcs_t;

 			if(SUCC != g_statisitcs->init()){
				ALERT_LOG("game init failed...");
 				return ERR;
			}
		}
		return 0;
	}

	int on_fini(){
		if (el_async::is_parent()) {
			DEBUG_LOG("======daemon done======");
		}else{
			DEBUG_LOG("======server done======");
			SAFE_DELETE(g_statisitcs);
		}
		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("server 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_statisitcs->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){
	}
	 
	void on_cli_conn(el::lib_tcp_peer_info_t* peer_fd_info){
	}

	void on_cli_conn_closed(int fd){
	}

	void on_svr_conn_closed(int fd){
		CRIT_LOG("[fd:%d]", fd);
	}

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

	void on_mcast_pkg(const void* data, int len){
	}

	void on_addr_mcast_pkg(uint32_t id, const char* name, const char* ip, uint16_t port, const char* data, int flag){
	}

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