mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
support socket.abandon / socket.start , Issue #73
This commit is contained in:
@@ -275,7 +275,7 @@ function socket.unlock(id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- abandon use to forward socket id to other service
|
-- abandon use to forward socket id to other service
|
||||||
-- you must call socket.accept(id) later in other service
|
-- you must call socket.start(id) later in other service
|
||||||
function socket.abandon(id)
|
function socket.abandon(id)
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
if s and s.buffer then
|
if s and s.buffer then
|
||||||
|
|||||||
@@ -1,27 +1,44 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
|
|
||||||
local function accepter(id)
|
local mode , id = ...
|
||||||
socket.start(id)
|
|
||||||
socket.write(id, "Hello Skynet\n")
|
if mode == "agent" then
|
||||||
while true do
|
id = tonumber(id)
|
||||||
local str = socket.readline(id,"\n")
|
|
||||||
if str then
|
skynet.start(function()
|
||||||
socket.write(id, str .. "\n")
|
socket.start(id)
|
||||||
else
|
|
||||||
socket.close(id)
|
while true do
|
||||||
print("closed " .. id)
|
local str = socket.readline(id,"\n")
|
||||||
return
|
if str then
|
||||||
|
socket.write(id, str .. "\n")
|
||||||
|
else
|
||||||
|
socket.close(id)
|
||||||
|
print("closed " .. id)
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.exit()
|
||||||
local id = socket.listen("127.0.0.1", 8000)
|
|
||||||
|
|
||||||
socket.start(id , function(id, addr)
|
|
||||||
print("connect from " .. addr .. " " .. id)
|
|
||||||
-- you can also call skynet.newservice for this socket id
|
|
||||||
skynet.fork(accepter, id)
|
|
||||||
end)
|
end)
|
||||||
end)
|
else
|
||||||
|
local function accepter(id)
|
||||||
|
socket.start(id)
|
||||||
|
socket.write(id, "Hello Skynet\n")
|
||||||
|
skynet.newservice("testsocket", "agent", id)
|
||||||
|
-- notice: Some data on this connection(id) may lost before new service start.
|
||||||
|
-- So, be careful when you want to use start / abandon / start .
|
||||||
|
socket.abandon(id)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local id = socket.listen("127.0.0.1", 8000)
|
||||||
|
|
||||||
|
socket.start(id , function(id, addr)
|
||||||
|
print("connect from " .. addr .. " " .. id)
|
||||||
|
-- you can also call skynet.newservice for this socket id
|
||||||
|
skynet.fork(accepter, id)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
@@ -513,6 +513,10 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc
|
|||||||
s->opaque = request->opaque;
|
s->opaque = request->opaque;
|
||||||
result->data = "start";
|
result->data = "start";
|
||||||
return SOCKET_OPEN;
|
return SOCKET_OPEN;
|
||||||
|
} else if (s->type == SOCKET_TYPE_CONNECTED) {
|
||||||
|
s->opaque = request->opaque;
|
||||||
|
result->data = "transfer";
|
||||||
|
return SOCKET_OPEN;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user