Skip to content

Commit

Permalink
Follow up to b0bcd06
Browse files Browse the repository at this point in the history
Added a special error message for timeout, since it does not set an error code when a timeout occurs.
Also added a message when the socket is not writeable after successful connection.
  • Loading branch information
Lemongrass3110 committed Jan 3, 2017
1 parent 396b5c3 commit 8b6b9c1
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/common/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,34 @@ int make_connection(uint32 ip, uint16 port, bool silent,int timeout) {
tv.tv_sec = timeout;
tv.tv_usec = 0;

// Try to find out if the socket is writeable yet(within the timeout) and check if it is really writeable afterwards
if( sSelect(0, NULL, &writeSet, NULL, &tv) != 0 && sFD_ISSET(fd, &writeSet) != 0 ){
// Our socket is writeable now => we have connected successfully
break; // leave the pseudo scope
result = sSelect(0, NULL, &writeSet, NULL, &tv);

// Connection attempt timed out
if( result == 0 ){
if( !silent ){
// Needs special handling, because it does not set an error code and therefore does not provide an error message from the API
ShowError("make_connection: connection failed (socket #%d, timeout after %ds)!\n", fd, timeout);
}

do_close(fd);
return -1;
// If the select operation did not return an error
}else if( result != SOCKET_ERROR ){
// Check if it is really writeable
if( sFD_ISSET(fd, &writeSet) != 0 ){
// Our socket is writeable now => we have connected successfully
break; // leave the pseudo scope
}

if( !silent ){
// Needs special handling, because it does not set an error code and therefore does not provide an error message from the API
ShowError("make_connection: connection failed (socket #%d, not writeable)!\n", fd);
}

do_close(fd);
return -1;
}
// Our connection attempt timed out or the socket was not writeable
// The select operation failed
}

if( !silent )
Expand Down

0 comments on commit 8b6b9c1

Please sign in to comment.