X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=blobdiff_plain;f=lib%2Fsilcutil%2Fwin32%2Fsilcwin32sockconn.c;h=7f604c0c6c8b19cf73bfc08668ff8f77a2e4e3a7;hp=c78640308398c344bde074d1226a055db1d60693;hb=e5d8d3db6caa344b3d419b884556c21b15e7d123;hpb=306ec7a94a1097ccd432c9dd022efe234605fa38 diff --git a/lib/silcutil/win32/silcwin32sockconn.c b/lib/silcutil/win32/silcwin32sockconn.c index c7864030..7f604c0c 100644 --- a/lib/silcutil/win32/silcwin32sockconn.c +++ b/lib/silcutil/win32/silcwin32sockconn.c @@ -33,6 +33,9 @@ int silc_socket_write(SilcSocketConnection sock) SOCKET fd = sock->sock; SilcBuffer src = sock->outbuf; + if (SILC_IS_DISABLED(sock)) + return -1; + SILC_LOG_DEBUG(("Writing data to socket %d", fd)); if (src->len > 0) { @@ -41,11 +44,9 @@ int silc_socket_write(SilcSocketConnection sock) err = WSAGetLastError(); if (err == WSAEWOULDBLOCK) { SILC_LOG_DEBUG(("Could not write immediately, will do it later")); - silc_net_set_socket_nonblock(fd); return -2; } SILC_LOG_ERROR(("Cannot write to socket: %d", (int)fd)); - silc_net_set_socket_nonblock(fd); return -1; } @@ -54,7 +55,6 @@ int silc_socket_write(SilcSocketConnection sock) SILC_LOG_DEBUG(("Wrote data %d bytes", ret)); - silc_net_set_socket_nonblock(fd); return ret; } @@ -68,25 +68,35 @@ int silc_socket_read(SilcSocketConnection sock) int len = 0, err; unsigned char buf[SILC_SOCKET_READ_SIZE]; SOCKET fd = sock->sock; + int argp; + + if (SILC_IS_DISABLED(sock)) + return -1; SILC_LOG_DEBUG(("Reading data from socket %d", fd)); + /* Check whether there is data available, without calling recv(). */ + ioctlsocket(fd, FIONREAD, (unsigned long *)&argp); + if (argp == 0) { + /* Is this kludge or what? Without this thing this contraption + does not work at all!?. */ + SleepEx(1, TRUE); + SILC_LOG_DEBUG(("Could not read immediately, will do it later")); + return -2; + } + /* Read the data from the socket. */ len = recv(fd, buf, sizeof(buf), 0); if (len == SOCKET_ERROR) { err = WSAGetLastError(); if (err == WSAEWOULDBLOCK || err == WSAEINTR) { SILC_LOG_DEBUG(("Could not read immediately, will do it later")); - silc_net_set_socket_nonblock(fd); return -2; } SILC_LOG_ERROR(("Cannot read from socket: %d", (int)fd)); - silc_net_set_socket_nonblock(fd); return -1; } - silc_net_set_socket_nonblock(fd); - if (!len) return 0;