static void dispatch_msg(msgdisp_t disp,msg_t msg) { //printf("dispatch_msg\n"); if(msg->type == MSG_RPACKET) { //printf("RPACKET\n"); rpacket_t rpk = (rpacket_t)msg; if(!disp->process_packet || disp->process_packet(disp,make_by_ident(MSG_IDENT(rpk)),rpk)) rpk_destroy(&rpk); }else if(msg->type == MSG_DB_RESULT){ db_result_t result = (db_result_t)msg; if(result->callback) result->callback(result); free_dbresult(result); }else if(msg->type == MSG_DO_FUNCTION){ msg_do_function_t _msg = (msg_do_function_t)msg; if(_msg->fn_function) _msg->fn_function(MSG_USRPTR(_msg)); free(msg); } else{ struct msg_connection *tmsg = (struct msg_connection*)msg; //printf("dispatch_msg %d\n",msg->type); sock_ident sock = CAST_2_SOCK(tmsg->base._ident); if(msg->type == MSG_ONCONNECT){ //printf("MSG_ONCONNECT\n"); if(disp->on_connect) disp->on_connect(disp,sock,tmsg->ip,tmsg->port); else asynsock_close(sock); } else if(msg->type == MSG_ONCONNECTED){ //printf("MSG_ONCONNECTED\n"); if(disp->on_connected) disp->on_connected(disp,sock,tmsg->ip,tmsg->port); else asynsock_close(sock); } else if(msg->type == MSG_DISCONNECTED && disp->on_disconnect){ disp->on_disconnect(disp,sock,tmsg->ip,tmsg->port,tmsg->reason); } else if(msg->type == MSG_CONNECT_FAIL && disp->connect_failed) disp->connect_failed(disp,tmsg->ip,tmsg->port,tmsg->reason); free(msg); } }
void asynconnect(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) { printf("asynconnect,ip:%s,port:%d\n",ip,port); disp->bind(disp,0,sock,16000,1,0,0);//由系统选择poller }
void to_server_connect(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) { //用第二个poller处理到服务器的连接 disp->bind(disp,2,sock,1,3*1000,0);//由系统选择poller }
void to_client_connect(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) { //用第3个poller处理到客户端的连接 disp->bind(disp,3,sock,1,3*1000,0);//由系统选择poller }
static void agent_connect(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) { disp->bind(g_agents[rand()%agentcount]->msgdisp,0,sock,4096,0,30*1000,0);//由系统选择poller }
void super_connect(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) { disp->bind(disp,0,sock,65536,1,3*1000,0);//由系统选择poller }