bugfix: socketchannel connect once

This commit is contained in:
Cloud Wu
2014-08-14 17:03:09 +08:00
parent d2cea9b70f
commit bd52c5fece
3 changed files with 39 additions and 17 deletions

View File

@@ -12,6 +12,7 @@ Dev version
* add cluster.proxy * add cluster.proxy
* add DEBUG command exit (send a message to lua service by DEBUG) * add DEBUG command exit (send a message to lua service by DEBUG)
* add DEBUG command run (debug_console command inject) * add DEBUG command run (debug_console command inject)
* bugfix : socketchannel connect once
v0.5.2 (2014-8-11) v0.5.2 (2014-8-11)
----------- -----------

View File

@@ -218,9 +218,9 @@ local function try_connect(self , once)
if not once then if not once then
skynet.error("socket: connect to", self.__host, self.__port) skynet.error("socket: connect to", self.__host, self.__port)
end end
return return true
elseif once then elseif once then
error(string.format("Connect to %s:%d failed", self.__host, self.__port)) return false
end end
if t > 1000 then if t > 1000 then
skynet.error("socket: try to reconnect", self.__host, self.__port) skynet.error("socket: try to reconnect", self.__host, self.__port)
@@ -233,7 +233,7 @@ local function try_connect(self , once)
end end
end end
local function block_connect(self, once) local function check_connection(self)
if self.__sock then if self.__sock then
local authco = self.__authcoroutine local authco = self.__authcoroutine
if not authco then if not authco then
@@ -247,15 +247,20 @@ local function block_connect(self, once)
if self.__closed then if self.__closed then
return false return false
end end
end
local function block_connect(self, once)
local r = check_connection(self)
if r ~= nil then
return r
end
if #self.__connecting > 0 then if #self.__connecting > 0 then
-- connecting in other coroutine -- connecting in other coroutine
local co = coroutine.running() local co = coroutine.running()
table.insert(self.__connecting, co) table.insert(self.__connecting, co)
skynet.wait() skynet.wait()
-- check connection again else
return block_connect(self, once)
end
self.__connecting[1] = true self.__connecting[1] = true
try_connect(self, once) try_connect(self, once)
self.__connecting[1] = nil self.__connecting[1] = nil
@@ -264,9 +269,14 @@ local function block_connect(self, once)
self.__connecting[i] = nil self.__connecting[i] = nil
skynet.wakeup(co) skynet.wakeup(co)
end end
end
-- check again r = check_connection(self)
return block_connect(self, once) if r == nil then
error(string.format("Connect to %s:%d failed", self.__host, self.__port))
else
return r
end
end end
function channel:connect(once) function channel:connect(once)

View File

@@ -51,13 +51,24 @@ function command.listen(source, addr, port)
skynet.ret(skynet.pack(nil)) skynet.ret(skynet.pack(nil))
end end
function command.req(source, node, addr, msg, sz) local function send_request(source, node, addr, msg, sz)
local request local request
local c = node_channel[node] local c = node_channel[node]
local session = node_session[node] local session = node_session[node]
-- msg is a local pointer, cluster.packrequest will free it -- msg is a local pointer, cluster.packrequest will free it
request, node_session[node] = cluster.packrequest(addr, session , msg, sz) request, node_session[node] = cluster.packrequest(addr, session , msg, sz)
skynet.ret(c:request(request, session))
return c:request(request, session)
end
function command.req(...)
local ok, msg, sz = pcall(send_request, ...)
if ok then
skynet.ret(msg, sz)
else
skynet.error(msg)
skynet.response()(false)
end
end end
local proxy = {} local proxy = {}