Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for slow tcp connect in non blocking mode #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/wsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,19 @@ int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
if (connect(*ps, addr, len) == 0) return IO_DONE;
/* make sure the system is trying to connect */
err = WSAGetLastError();

if (err == WSAEISCONN) return IO_DONE; // already connected?

// hotfix for nonblocking, slow TCP connections not returning instantly
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx
// "Note In order to preserve backward compatibility, this error is reported as WSAEINVAL to Windows Sockets 1.1 applications that link to either Winsock.dll or Wsock32.dll."
// The only other valid reason to get WSAINVAL is that: The parameter s is a listening socket. So we hope that doesn't happen here.
if (err == WSAEINVAL) {
return WSAEALREADY;
}

if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err;

/* zero timeout case optimization */
if (timeout_iszero(tm)) return IO_TIMEOUT;
/* we wait until something happens */
Expand Down