static void agent_connected(msgdisp_t disp,sock_ident sock,const char *ip,int32_t port) { agentservice_t service = get_thd_agentservice(); agentplayer_t ply = new_agentplayer(service); if(!ply) { //发送一个消息,通知系统繁忙然后关闭连接 wpacket_t wpk = wpk_create(64,0); wpk_write_uint16(wpk,CMD_GAME_BUSY); asyn_send(sock,wpk); asynsock_close(sock); }else { asynsock_set_ud(sock,(void*)ply->session.data); } }
void uthread_worker(void *arg){ ut_socket_t sock = ut_connect(&remote,4096,new_rpk_decoder(4096)); if(!is_empty_ident(sock)){ wpacket_t wpk = wpk_create(64); wpk_write_uint64(wpk,100); ut_send(sock,(packet_t)wpk); packet_t packet = NULL; int err = 0; while(ut_recv(sock,&packet,&err) == 0){ packet_t wpk = make_writepacket(packet); destroy_packet(packet); ut_send(sock,wpk); } }else{ printf("connect error\n"); } }
//向gate发送短通告消息 void shortmsg2gate(uint16_t cmd,uint32_t gateident){ wpacket_t wpk = wpk_create(64,0); wpk_write_uint16(wpk,cmd); wpk_write_uint32(wpk,gateident); send2gate(wpk); }
static int lua_new_packet(lua_State *L,int packettype){ int argtype = lua_type(L,1); if(packettype == WPACKET){ if(argtype == LUA_TNUMBER || argtype == LUA_TNIL || argtype == LUA_TNONE){ //参数为数字,构造一个初始大小为len的wpacket size_t len = 0; if(argtype == LUA_TNUMBER) len = size_of_pow2(lua_tointeger(L,1)); if(len < 64) len = 64; lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)wpk_create(len); return 1; }else if(argtype == LUA_TSTRING){ size_t len; char *data = (char*)lua_tolstring(L,1,&len); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)wpk_create_by_bin((int8_t*)data,len); return 1; }else if(argtype == LUA_TUSERDATA){ lua_packet_t other = lua_getluapacket(L,1); if(!other) return luaL_error(L,"invaild opration for arg1"); if(other->_packet->type == RAWPACKET) return luaL_error(L,"invaild opration for arg1"); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = make_writepacket(other->_packet);//(packet_t)wpk_copy_create(other->_packet); return 1; }else if(argtype == LUA_TTABLE){ wpacket_t wpk = wpk_create(512); if(0 != lua_pack_table(wpk,L,-1)){ destroy_packet((packet_t)wpk); return luaL_error(L,"table should not hava metatable"); //lua_pushnil(L); }else{ lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)wpk; } return 1; } else return luaL_error(L,"invaild opration for arg1"); }else if(packettype == RAWPACKET){ if(argtype == LUA_TSTRING){ //参数为string,构造一个函数数据data的rawpacket size_t len; char *data = (char*)lua_tolstring(L,1,&len); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = (packet_t)rawpacket_create2(data,len); return 1; }else if(argtype == LUA_TUSERDATA){ lua_packet_t other = lua_getluapacket(L,1); if(!other) return luaL_error(L,"invaild opration for arg1"); if(other->_packet->type != RAWPACKET) return luaL_error(L,"invaild opration for arg1"); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = clone_packet(other->_packet); return 1; }else return luaL_error(L,"invaild opration for arg1"); }else if(packettype == RPACKET){ if(argtype == LUA_TUSERDATA){ lua_packet_t other = lua_getluapacket(L,1); if(!other) return luaL_error(L,"invaild opration for arg1"); if(other->_packet->type == RAWPACKET) return luaL_error(L,"invaild opration for arg1"); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUAPACKET_METATABLE); lua_setmetatable(L, -2); p->_packet = make_readpacket(other->_packet); return 1; }else return luaL_error(L,"invaild opration for arg1"); }else return luaL_error(L,"invaild packet type"); }