From a4f827a48d1d83fd29acc7135267b7d7e6688dc6 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 12 Aug 2015 14:53:36 +0800 Subject: [PATCH] use string for msgserver request --- lualib-src/lua-netpack.c | 15 ++------------- lualib/snax/msgserver.lua | 16 ++++++++-------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/lualib-src/lua-netpack.c b/lualib-src/lua-netpack.c index 88a54d3d..0ec6f7e9 100644 --- a/lualib-src/lua-netpack.c +++ b/lualib-src/lua-netpack.c @@ -506,19 +506,8 @@ ltostring(lua_State *L) { if (ptr == NULL) { lua_pushliteral(L, ""); } else { - if (lua_isnumber(L, 3)) { - int offset = lua_tointeger(L, 3); - if (offset < 0) { - return luaL_error(L, "Invalid offset %d", offset); - } - if (offset > size) { - offset = size; - } - lua_pushlstring(L, (const char *)ptr + offset, size-offset); - } else { - lua_pushlstring(L, (const char *)ptr, size); - skynet_free(ptr); - } + lua_pushlstring(L, (const char *)ptr, size); + skynet_free(ptr); } return 1; } diff --git a/lualib/snax/msgserver.lua b/lualib/snax/msgserver.lua index a70ea415..c480a8a6 100644 --- a/lualib/snax/msgserver.lua +++ b/lualib/snax/msgserver.lua @@ -70,7 +70,7 @@ Config for server.start: conf.login_handler(uid, secret) -> subid : the function when a new user login, alloc a subid for it. (may call by login server) conf.logout_handler(uid, subid) : the functon when a user logout. (may call by agent) conf.kick_handler(uid, subid) : the functon when a user logout. (may call by login server) - conf.request_handler(username, session, msg, sz) : the function when recv a new request. + conf.request_handler(username, session, msg) : the function when recv a new request. conf.register_handler(servername) : call when gate open conf.disconnect_handler(username) : call when a connection disconnect (afk) ]] @@ -234,10 +234,10 @@ function server.start(conf) end end - local function do_request(fd, msg, sz) + local function do_request(fd, message) local u = assert(connection[fd], "invalid fd") - local msg_sz = sz - 4 - local session = netpack.tostring(msg, sz, msg_sz) + local session = string.unpack("I4", message, -4) + message = message:sub(1,-5) local p = u.response[session] if p then -- session can be reuse in the same connection @@ -256,7 +256,7 @@ function server.start(conf) if p == nil then p = { fd } u.response[session] = p - local ok, result = pcall(conf.request_handler, u.username, msg, msg_sz) + local ok, result = pcall(conf.request_handler, u.username, message) result = result or "" -- NOTICE: YIELD here, socket may close. if not ok then @@ -270,7 +270,6 @@ function server.start(conf) p[3] = u.version p[4] = u.index else - netpack.tostring(msg, sz) -- request before, so free msg -- update version/index, change return fd. -- resend response. p[1] = fd @@ -292,10 +291,11 @@ function server.start(conf) end local function request(fd, msg, sz) - local ok, err = pcall(do_request, fd, msg, sz) + local message = netpack.tostring(msg, sz) + local ok, err = pcall(do_request, fd, message) -- not atomic, may yield if not ok then - skynet.error(string.format("Invalid package %s : %s", err, netpack.tostring(msg, sz))) + skynet.error(string.format("Invalid package %s : %s", err, message)) if connection[fd] then gateserver.closeclient(fd) end