Try both fd and socket ops for net_read() and net_write()

When using WinSock, a socket is not a file descriptor and does not
interoperate with read()/write().  File descriptors do not work with
send()/recv().  However, for net_read() and net_write(), we don't know
whether we are dealing with a socket or a file descriptor.  So try
one, and if it fails, try the other.

This is an ugly hack until we clean up the users of this API so it
doesn't use sockets and fds interchangably.
This commit is contained in:
Asanka Herath
2010-05-26 10:33:50 -04:00
parent 5c0f3f99c6
commit 3e2b840565
2 changed files with 37 additions and 0 deletions

View File

@@ -73,8 +73,27 @@ net_read(rk_socket_t sock, void *buf, size_t nbytes)
ssize_t count;
size_t rem = nbytes;
#ifdef SOCKET_IS_NOT_AN_FD
int use_read = 0;
#endif
while (rem > 0) {
#ifdef SOCKET_IS_NOT_AN_FD
if (use_read)
count = _read (sock, cbuf, rem);
else
count = recv (sock, cbuf, rem, 0);
if (use_read == 0 &&
rk_IS_SOCKET_ERROR(count) &&
rk_SOCK_ERRNO == WSAENOTSOCK) {
use_read = 1;
count = _read (sock, cbuf, rem);
}
#else
count = recv (sock, cbuf, rem, 0);
#endif
if (count < 0) {
/* With WinSock, the error EINTR (WSAEINTR), is used to