static int _write_wpk(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != WPACKET) return luaL_error(L,"invaild opration"); lua_packet_t v = lua_getluapacket(L,2); if(!v->_packet || v->_packet->type != WPACKET) return luaL_error(L,"invaild arg2"); wpk_write_wpk((wpacket_t)p->_packet,(wpacket_t)v->_packet); return 0; }
static int destroy_luapacket(lua_State *L) { lua_packet_t p = lua_getluapacket(L,1); if(p->packet){ delete p->packet; } return 0; }
static int NewRawPacket(lua_State *L){ int argtype = lua_type(L,1); if(argtype == LUA_TSTRING){ const char *str; size_t len; str = lua_tolstring(L,1,&len); lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUARAWPACKET_METATABLE); lua_setmetatable(L, -2); p->packet = new net::RawBinPacket(str,len); return 1; }else if(argtype == LUA_TUSERDATA){ lua_packet_t o = lua_getluapacket(L,1); if(!o || !o->packet || o->packet->Type() != RAWBINARY) { return luaL_error(L,"invaild opration for arg1"); } lua_packet_t p = (lua_packet_t)lua_newuserdata(L, sizeof(*p)); luaL_getmetatable(L, LUARAWPACKET_METATABLE); lua_setmetatable(L, -2); p->packet = o->packet->Clone(); return 1; }else{ return luaL_error(L,"invaild opration for arg1"); } }
static int to_lua_table(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != RPACKET) return luaL_error(L,"invaild opration"); rpacket_t rpk = (rpacket_t)p->_packet; //backup uint32_t rpos = rpk->rpos; uint32_t data_remain = packet_dataremain(rpk); buffer_t readbuf = rpk->readbuf; do{ int top = lua_gettop(L); int ret = lua_unpack_table(rpk,L); if(0 != ret){ lua_settop(L,top); lua_pushnil(L); } }while(0); //recover rpk->rpos = rpos; packet_dataremain(rpk) = data_remain; rpk->readbuf = readbuf; return 1; }
static int GetHeaders(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::HttpPacket *rpk = dynamic_cast<net::HttpPacket*>(p->packet); rpk->PushHeaders(L); return 1; }
static int ReadCmd(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::CmdRPacket *rpk = dynamic_cast<net::CmdRPacket*>(p->packet); if(!rpk) return luaL_error(L,"invaild opration"); lua_pushinteger(L,rpk->ReadCmd()); return 1; }
static int GetWritePos(lua_State *L) { lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::StreamWPacket *wpk = dynamic_cast<net::StreamWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); lua_pushinteger(L, (lua_Integer)wpk->GetWritePos()); return 1; }
static int destroy_luapacket(lua_State *L) { lua_packet_t p = lua_getluapacket(L,1); if(p->_packet){ destroy_packet(p->_packet); p->_packet = NULL; } return 0; }
static int _read_double(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(p->_packet->type != RPACKET) return luaL_error(L,"invaild opration"); rpacket_t rpk = (rpacket_t)p->_packet; lua_pushnumber(L,rpk_read_double(rpk)); return 1; }
static int _peek_uint8(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(p->_packet->type != RPACKET) return luaL_error(L,"invaild opration"); rpacket_t rpk = (rpacket_t)p->_packet; lua_pushinteger(L,rpk_peek_uint8(rpk)); return 1; }
static int ReadDouble(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::StreamRPacket *rpk = dynamic_cast<net::StreamRPacket*>(p->packet); if(!rpk) return luaL_error(L,"invaild opration"); lua_pushnumber(L,(lua_Number)rpk->ReadDouble()); return 1; }
static int WriteCmd(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::CmdWPacket *wpk = dynamic_cast<net::CmdWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TNUMBER) return luaL_error(L,"invaild arg2"); wpk->WriteCmd((unsigned short)lua_tointeger(L,2)); return 0; }
static int _peek_uint32(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(p->_packet->type != RPACKET) lua_pushnil(L); else{ rpacket_t rpk = (rpacket_t)p->_packet; lua_pushinteger(L,rpk_peek_uint32(rpk)); } return 1; }
int _write_table(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != WPACKET) return luaL_error(L,"invaild opration"); if(LUA_TTABLE != lua_type(L, 2)) return luaL_error(L,"argument should be lua table"); if(0 != lua_pack_table((wpacket_t)p->_packet,L,-1)) return luaL_error(L,"table should not hava metatable"); return 0; }
static int _write_double(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != WPACKET) return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TNUMBER) return luaL_error(L,"invaild arg2"); double v = (double)lua_tonumber(L,2); wpacket_t wpk = (wpacket_t)p->_packet; wpk_write_double(wpk,v); return 0; }
static int GetStatus(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::HttpPacket *rpk = dynamic_cast<net::HttpPacket*>(p->packet); const char *status = rpk->GetStatus(); if(status) lua_pushstring(L,status); else lua_pushnil(L); return 1; }
static int _read_rawbin(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != RAWPACKET) return luaL_error(L,"invaild opration"); rawpacket_t rawpk = (rawpacket_t)p->_packet; uint32_t len = 0; const char *data = rawpacket_data(rawpk,&len); lua_pushlstring(L,data,(size_t)len); return 1; }
static int _write_uint32(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != WPACKET) return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TNUMBER) return luaL_error(L,"invaild arg2"); uint32_t v = (uint32_t)lua_tointeger(L,2); wpacket_t wpk = (wpacket_t)p->_packet; wpk_write_uint32(wpk,v); return 0; }
static int WriteDouble(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::StreamWPacket *wpk = dynamic_cast<net::StreamWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TNUMBER) return luaL_error(L,"invaild arg2"); double value = (double)lua_tonumber(L, 2); wpk->WriteDouble(value); return 0; }
static int WriteUint32(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::StreamWPacket *wpk = dynamic_cast<net::StreamWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TNUMBER) return luaL_error(L,"invaild arg2"); unsigned int value = (unsigned int)lua_tointeger(L, 2); wpk->WriteUint32(value); return 0; }
static int WriteTable(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::StreamWPacket *wpk = dynamic_cast<net::StreamWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); if(LUA_TTABLE != lua_type(L, 2)) return luaL_error(L,"argument should be lua table"); if(0 != luabin_pack_table(wpk,L,-1)) return luaL_error(L,"table should not hava metatable"); return 0; }
static int _get_write_pos(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != WPACKET) return luaL_error(L,"invaild opration"); write_pos wpos = wpk_get_writepos((wpacket_t)p->_packet); lua_newtable(L); lua_pushlightuserdata(L,wpos.buf); lua_rawseti(L,-2,1); lua_pushinteger(L,wpos.wpos); lua_rawseti(L,-2,2); return 1; }
static int _write_string(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != WPACKET) return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TSTRING) return luaL_error(L,"invaild arg2"); size_t len; const char *data = lua_tolstring(L,2,&len); wpacket_t wpk = (wpacket_t)p->_packet; wpk_write_binary(wpk,data,len); return 0; }
static int GetMethod(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::HttpPacket *rpk = dynamic_cast<net::HttpPacket*>(p->packet); int method = rpk->GetMethod(); if(method < 0) lua_pushnil(L); else{ lua_pushstring(L,http_method_name[method]); } return 1; }
static int GetBody(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::HttpPacket *rpk = dynamic_cast<net::HttpPacket*>(p->packet); size_t len; const char *body = rpk->GetBody(len); if(body && len > 0) lua_pushlstring(L,body,len); else lua_pushnil(L); return 1; }
static int WriteString(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::StreamWPacket *wpk = dynamic_cast<net::StreamWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TSTRING) return luaL_error(L,"invaild arg2"); size_t len; const char *val = lua_tolstring(L, 2,&len); wpk->WriteBin((void*)val,len); return 0; }
static int ReadBinary(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet) return luaL_error(L,"invaild opration"); net::RawBinPacket *rpk = dynamic_cast<net::RawBinPacket*>(p->packet); if(!rpk) return luaL_error(L,"invaild opration"); size_t len; const char* str = (const char*)rpk->ReadBin(len); if(str) lua_pushlstring(L,str,len); else lua_pushnil(L); return 1; }
static int _read_table(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(!p->_packet || p->_packet->type != RPACKET) return luaL_error(L,"invaild opration"); rpacket_t rpk = (rpacket_t)p->_packet; int old_top = lua_gettop(L); int ret = lua_unpack_table(rpk,L); if(0 != ret){ lua_settop(L,old_top); lua_pushnil(L); } return 1; }
static int _read_string(lua_State *L){ lua_packet_t p = lua_getluapacket(L,1); if(p->_packet->type != RPACKET) return luaL_error(L,"invaild opration"); rpacket_t rpk = (rpacket_t)p->_packet; uint32_t len; const char *data = rpk_read_binary(rpk,&len); if(data) lua_pushlstring(L,data,(size_t)len); else lua_pushnil(L); return 1; }
static int RewriteUint16(lua_State *L) { lua_packet_t p = lua_getluapacket(L,1); if (!p || !p->packet)return luaL_error(L,"invaild opration"); net::StreamWPacket *wpk = dynamic_cast<net::StreamWPacket*>(p->packet); if(!wpk)return luaL_error(L,"invaild opration"); if(lua_type(L,2) != LUA_TNUMBER) return luaL_error(L,"invaild arg2"); if(lua_type(L,3) != LUA_TNUMBER) return luaL_error(L,"invaild arg3"); net::write_pos wpos = (net::write_pos)lua_tointeger(L,2); unsigned short value = (unsigned short)lua_tointeger(L, 3); wpk->RewriteUint16(wpos, value); return 0; }