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)
return
end
if s.connected or s.connecting then
if s.connected then
skynet.error("socket: error on", id, err)
elseif s.connecting then
s.connecting = err
end
s.connected = false
driver.close(id)
@@ -179,11 +181,13 @@ local function connect(id, func)
}
socket_pool[id] = s
suspend(s)
local err = s.connecting
s.connecting = nil
if s.connected then
return id
else
socket_pool[id] = nil
return nil, err
end
end

View File

@@ -209,11 +209,11 @@ local function connect_once(self)
return false
end
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
fd = connect_backup(self)
if not fd then
return false
return false, err
end
end
if self.__nodelay then
@@ -247,13 +247,16 @@ end
local function try_connect(self , once)
local t = 0
while not self.__closed do
if connect_once(self) then
local ok, err = connect_once(self)
if ok then
if not once then
skynet.error("socket: connect to", self.__host, self.__port)
end
return true
return
elseif once then
return false
return err
else
skynet.error("socket: connect", err)
end
if t > 1000 then
skynet.error("socket: try to reconnect", self.__host, self.__port)
@@ -287,6 +290,7 @@ local function block_connect(self, once)
if r ~= nil then
return r
end
local err
if #self.__connecting > 0 then
-- connecting in other coroutine
@@ -295,7 +299,7 @@ local function block_connect(self, once)
skynet.wait(co)
else
self.__connecting[1] = true
try_connect(self, once)
err = try_connect(self, once)
self.__connecting[1] = nil
for i=2, #self.__connecting do
local co = self.__connecting[i]
@@ -306,7 +310,7 @@ local function block_connect(self, once)
r = check_connection(self)
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
return r
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);
if (code < 0 || error) {
force_close(ss,s, result);
result->data = strerror(errno);
if (code >= 0)
result->data = strerror(error);
else
result->data = strerror(errno);
return SOCKET_ERROR;
} else {
s->type = SOCKET_TYPE_CONNECTED;