diff --git a/lualib/socket.lua b/lualib/socket.lua index 12c53f41..dd716a02 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -275,7 +275,7 @@ function socket.unlock(id) end -- 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) local s = socket_pool[id] if s and s.buffer then diff --git a/service/testsocket.lua b/service/testsocket.lua index 632b4cb5..26ce71e7 100644 --- a/service/testsocket.lua +++ b/service/testsocket.lua @@ -1,27 +1,44 @@ local skynet = require "skynet" local socket = require "socket" -local function accepter(id) - socket.start(id) - socket.write(id, "Hello Skynet\n") - while true do - local str = socket.readline(id,"\n") - if str then - socket.write(id, str .. "\n") - else - socket.close(id) - print("closed " .. id) - return +local mode , id = ... + +if mode == "agent" then + id = tonumber(id) + + skynet.start(function() + socket.start(id) + + while true do + local str = socket.readline(id,"\n") + if str then + socket.write(id, str .. "\n") + else + socket.close(id) + print("closed " .. id) + break + end end - end -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) + skynet.exit() 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 \ No newline at end of file diff --git a/skynet-src/socket_server.c b/skynet-src/socket_server.c index 2e705a54..23a56d68 100644 --- a/skynet-src/socket_server.c +++ b/skynet-src/socket_server.c @@ -513,6 +513,10 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc s->opaque = request->opaque; result->data = "start"; return SOCKET_OPEN; + } else if (s->type == SOCKET_TYPE_CONNECTED) { + s->opaque = request->opaque; + result->data = "transfer"; + return SOCKET_OPEN; } return -1; }