report connect failed reason

This commit is contained in:
Cloud Wu
2015-10-28 16:12:01 +08:00
parent 13d920e185
commit 935863f434
3 changed files with 20 additions and 9 deletions

View File

@@ -112,8 +112,10 @@ socket_message[5] = function(id, _, err)
skynet.error("socket: error on unknown", id, err) skynet.error("socket: error on unknown", id, err)
return return
end end
if s.connected or s.connecting then if s.connected then
skynet.error("socket: error on", id, err) skynet.error("socket: error on", id, err)
elseif s.connecting then
s.connecting = err
end end
s.connected = false s.connected = false
driver.close(id) driver.close(id)
@@ -179,11 +181,13 @@ local function connect(id, func)
} }
socket_pool[id] = s socket_pool[id] = s
suspend(s) suspend(s)
local err = s.connecting
s.connecting = nil s.connecting = nil
if s.connected then if s.connected then
return id return id
else else
socket_pool[id] = nil socket_pool[id] = nil
return nil, err
end end
end end

View File

@@ -209,11 +209,11 @@ local function connect_once(self)
return false return false
end end
assert(not self.__sock and not self.__authcoroutine) assert(not self.__sock and not self.__authcoroutine)
local fd = socket.open(self.__host, self.__port) local fd,err = socket.open(self.__host, self.__port)
if not fd then if not fd then
fd = connect_backup(self) fd = connect_backup(self)
if not fd then if not fd then
return false return false, err
end end
end end
if self.__nodelay then if self.__nodelay then
@@ -247,13 +247,16 @@ end
local function try_connect(self , once) local function try_connect(self , once)
local t = 0 local t = 0
while not self.__closed do while not self.__closed do
if connect_once(self) then local ok, err = connect_once(self)
if ok then
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 true return
elseif once then elseif once then
return false return err
else
skynet.error("socket: connect", err)
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)
@@ -287,6 +290,7 @@ local function block_connect(self, once)
if r ~= nil then if r ~= nil then
return r return r
end end
local err
if #self.__connecting > 0 then if #self.__connecting > 0 then
-- connecting in other coroutine -- connecting in other coroutine
@@ -295,7 +299,7 @@ local function block_connect(self, once)
skynet.wait(co) skynet.wait(co)
else else
self.__connecting[1] = true self.__connecting[1] = true
try_connect(self, once) err = try_connect(self, once)
self.__connecting[1] = nil self.__connecting[1] = nil
for i=2, #self.__connecting do for i=2, #self.__connecting do
local co = self.__connecting[i] local co = self.__connecting[i]
@@ -306,7 +310,7 @@ local function block_connect(self, once)
r = check_connection(self) r = check_connection(self)
if r == nil then if r == nil then
error(string.format("Connect to %s:%d failed", self.__host, self.__port)) error(string.format("Connect to %s:%d failed (%s)", self.__host, self.__port, err))
else else
return r return r
end end

View File

@@ -1095,7 +1095,10 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message
int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len); int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len);
if (code < 0 || error) { if (code < 0 || error) {
force_close(ss,s, result); force_close(ss,s, result);
result->data = strerror(errno); if (code >= 0)
result->data = strerror(error);
else
result->data = strerror(errno);
return SOCKET_ERROR; return SOCKET_ERROR;
} else { } else {
s->type = SOCKET_TYPE_CONNECTED; s->type = SOCKET_TYPE_CONNECTED;