mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
close uncomplete when socket disconnect, see Issue #280
This commit is contained in:
@@ -4,6 +4,7 @@ local socket = require "socket"
|
|||||||
local sproto = require "sproto"
|
local sproto = require "sproto"
|
||||||
local sprotoloader = require "sprotoloader"
|
local sprotoloader = require "sprotoloader"
|
||||||
|
|
||||||
|
local WATCHDOG
|
||||||
local host
|
local host
|
||||||
local send_request
|
local send_request
|
||||||
|
|
||||||
@@ -26,6 +27,10 @@ function REQUEST:handshake()
|
|||||||
return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." }
|
return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function REQUEST:quit()
|
||||||
|
skynet.call(WATCHDOG, "lua", "close", client_fd)
|
||||||
|
end
|
||||||
|
|
||||||
local function request(name, args, response)
|
local function request(name, args, response)
|
||||||
local f = assert(REQUEST[name])
|
local f = assert(REQUEST[name])
|
||||||
local r = f(args)
|
local r = f(args)
|
||||||
@@ -62,7 +67,10 @@ skynet.register_protocol {
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
function CMD.start(gate, fd)
|
function CMD.start(conf)
|
||||||
|
local fd = conf.client
|
||||||
|
local gate = conf.gate
|
||||||
|
WATCHDOG = conf.watchdog
|
||||||
-- slot 1,2 set at main.lua
|
-- slot 1,2 set at main.lua
|
||||||
host = sprotoloader.load(1):host "package"
|
host = sprotoloader.load(1):host "package"
|
||||||
send_request = host:attach(sprotoloader.load(2))
|
send_request = host:attach(sprotoloader.load(2))
|
||||||
@@ -77,6 +85,11 @@ function CMD.start(gate, fd)
|
|||||||
skynet.call(gate, "lua", "forward", fd)
|
skynet.call(gate, "lua", "forward", fd)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function CMD.disconnect()
|
||||||
|
-- todo: do something before exit
|
||||||
|
skynet.exit()
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function(_,_, command, ...)
|
skynet.dispatch("lua", function(_,_, command, ...)
|
||||||
local f = CMD[command]
|
local f = CMD[command]
|
||||||
|
|||||||
@@ -104,7 +104,11 @@ while true do
|
|||||||
dispatch_package()
|
dispatch_package()
|
||||||
local cmd = socket.readstdin()
|
local cmd = socket.readstdin()
|
||||||
if cmd then
|
if cmd then
|
||||||
send_request("get", { what = cmd })
|
if cmd == "quit" then
|
||||||
|
send_request("quit")
|
||||||
|
else
|
||||||
|
send_request("get", { what = cmd })
|
||||||
|
end
|
||||||
else
|
else
|
||||||
socket.usleep(100)
|
socket.usleep(100)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ set 3 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
quit 4 {}
|
||||||
|
|
||||||
]]
|
]]
|
||||||
|
|
||||||
proto.s2c = sprotoparser.parse [[
|
proto.s2c = sprotoparser.parse [[
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local netpack = require "netpack"
|
local netpack = require "netpack"
|
||||||
require "skynet.manager"
|
|
||||||
|
|
||||||
local CMD = {}
|
local CMD = {}
|
||||||
local SOCKET = {}
|
local SOCKET = {}
|
||||||
@@ -10,15 +9,16 @@ local agent = {}
|
|||||||
function SOCKET.open(fd, addr)
|
function SOCKET.open(fd, addr)
|
||||||
skynet.error("New client from : " .. addr)
|
skynet.error("New client from : " .. addr)
|
||||||
agent[fd] = skynet.newservice("agent")
|
agent[fd] = skynet.newservice("agent")
|
||||||
skynet.call(agent[fd], "lua", "start", gate, fd)
|
skynet.call(agent[fd], "lua", "start", { gate = gate, client = fd, watchdog = skynet.self() })
|
||||||
end
|
end
|
||||||
|
|
||||||
local function close_agent(fd)
|
local function close_agent(fd)
|
||||||
local a = agent[fd]
|
local a = agent[fd]
|
||||||
|
agent[fd] = nil
|
||||||
if a then
|
if a then
|
||||||
-- The better way is send a message to agent, and let agent exit self.
|
skynet.call(gate, "lua", "kick", fd)
|
||||||
skynet.kill(a)
|
-- disconnect never return
|
||||||
agent[fd] = nil
|
skynet.send(a, "lua", "disconnect")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -39,6 +39,10 @@ function CMD.start(conf)
|
|||||||
skynet.call(gate, "lua", "open" , conf)
|
skynet.call(gate, "lua", "open" , conf)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function CMD.close(fd)
|
||||||
|
close_agent(fd)
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function(session, source, cmd, subcmd, ...)
|
skynet.dispatch("lua", function(session, source, cmd, subcmd, ...)
|
||||||
if cmd == "socket" then
|
if cmd == "socket" then
|
||||||
|
|||||||
@@ -210,6 +210,16 @@ push_more(lua_State *L, int fd, uint8_t *buffer, int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
close_uncomplete(lua_State *L, int fd) {
|
||||||
|
struct queue *q = lua_touserdata(L,1);
|
||||||
|
struct uncomplete * uc = find_uncomplete(q, fd);
|
||||||
|
if (uc) {
|
||||||
|
skynet_free(uc->pack.buffer);
|
||||||
|
skynet_free(uc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
|
filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
|
||||||
struct queue *q = lua_touserdata(L,1);
|
struct queue *q = lua_touserdata(L,1);
|
||||||
@@ -343,6 +353,8 @@ lfilter(lua_State *L) {
|
|||||||
// ignore listen fd connect
|
// ignore listen fd connect
|
||||||
return 1;
|
return 1;
|
||||||
case SKYNET_SOCKET_TYPE_CLOSE:
|
case SKYNET_SOCKET_TYPE_CLOSE:
|
||||||
|
// no more data in fd (message->id)
|
||||||
|
close_uncomplete(L, message->id);
|
||||||
lua_pushvalue(L, lua_upvalueindex(TYPE_CLOSE));
|
lua_pushvalue(L, lua_upvalueindex(TYPE_CLOSE));
|
||||||
lua_pushinteger(L, message->id);
|
lua_pushinteger(L, message->id);
|
||||||
return 3;
|
return 3;
|
||||||
@@ -353,6 +365,8 @@ lfilter(lua_State *L) {
|
|||||||
pushstring(L, buffer, size);
|
pushstring(L, buffer, size);
|
||||||
return 4;
|
return 4;
|
||||||
case SKYNET_SOCKET_TYPE_ERROR:
|
case SKYNET_SOCKET_TYPE_ERROR:
|
||||||
|
// no more data in fd (message->id)
|
||||||
|
close_uncomplete(L, message->id);
|
||||||
lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR));
|
lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR));
|
||||||
lua_pushinteger(L, message->id);
|
lua_pushinteger(L, message->id);
|
||||||
pushstring(L, buffer, size);
|
pushstring(L, buffer, size);
|
||||||
|
|||||||
Reference in New Issue
Block a user