use string for msgserver request

This commit is contained in:
Cloud Wu
2015-08-12 14:53:36 +08:00
parent 7547126570
commit a4f827a48d
2 changed files with 10 additions and 21 deletions

View File

@@ -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;
}

View File

@@ -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