From 7d2d107518e753698fb2a1a2b461efd73543e5e9 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Jul 2014 17:06:47 +0800 Subject: [PATCH] bugfix: socketchannel auth --- lualib/socketchannel.lua | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index 4b2f28ad..c2886e31 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -177,6 +177,9 @@ local function connect_backup(self) end local function connect_once(self) + if self.__closed then + return false + end assert(not self.__sock and not self.__authcoroutine) local fd = socket.open(self.__host, self.__port) if not fd then @@ -185,29 +188,29 @@ local function connect_once(self) return false end end - while not self.__closed do + + self.__sock = setmetatable( {fd} , channel_socket_meta ) + skynet.fork(dispatch_function(self), self) + + if self.__auth then self.__authcoroutine = coroutine.running() - self.__sock = setmetatable( {fd} , channel_socket_meta ) - skynet.fork(dispatch_function(self), self) - - if self.__auth then - local ok , message = pcall(self.__auth, self) - if not ok then - close_channel_socket(self) - if message ~= socket_error then - skynet.error("socket: auth failed", message) - end + local ok , message = pcall(self.__auth, self) + if not ok then + close_channel_socket(self) + if message ~= socket_error then + skynet.error("socket: auth failed", message) end - self.__authcoroutine = false - return ok end - self.__authcoroutine = false if self.__sock then - return true + return ok + else + -- auth may change host, so connect again + return connect_once(self) end - -- auth may change host, so connect again end + + return true end local function try_connect(self , once)