Skip to content

Commit

Permalink
network: Split FD select code to platform-specific C files
Browse files Browse the repository at this point in the history
Roll out two do_select() functions, one for Windows and one for UNIX, to
make the code simpler.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed May 20, 2021
1 parent 098582f commit 763b803
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 47 deletions.
22 changes: 22 additions & 0 deletions network-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,25 @@ int set_socket_timeout(int fd, unsigned int timeout)
else
return 0;
}

int do_select(int fd, unsigned int timeout)
{
struct pollfd pfd;
int ret;

pfd.fd = fd;
pfd.events = POLLOUT | POLLERR;
pfd.revents = 0;

do {
ret = poll(&pfd, 1, timeout);
} while (ret == -1 && errno == EINTR);

if (ret < 0)
return -errno;

if (ret == 0)
return -ETIMEDOUT;

return 0;
}
38 changes: 38 additions & 0 deletions network-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,41 @@ int set_socket_timeout(int fd, unsigned int timeout)
else
return 0;
}

int do_select(int fd, unsigned int timeout)
{
struct timeval tv;
struct timeval *ptv;
fd_set set;
int ret;

#ifdef _MSC_BUILD
/*
* This is so stupid, but studio emits a signed/unsigned mismatch
* on their own FD_ZERO macro, so turn the warning off/on
*/
#pragma warning(disable : 4389)
#endif
FD_ZERO(&set);
FD_SET(fd, &set);
#ifdef _MSC_BUILD
#pragma warning(default: 4389)
#endif

if (timeout != 0) {
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
ptv = &tv;
} else {
ptv = NULL;
}

ret = select(fd + 1, NULL, &set, &set, ptv);
if (ret < 0)
return -WSAGetLastError();

if (ret == 0)
return -WSAETIMEDOUT;

return 0;
}
49 changes: 2 additions & 47 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <netdb.h>
#include <net/if.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
Expand All @@ -48,9 +47,6 @@

#ifdef _WIN32
#define close(s) closesocket(s)
#define NETWORK_ERR_TIMEOUT WSAETIMEDOUT
#else
#define NETWORK_ERR_TIMEOUT ETIMEDOUT
#endif

#define DEFAULT_TIMEOUT_MS 5000
Expand Down Expand Up @@ -179,13 +175,6 @@ static int do_connect(int fd, const struct addrinfo *addrinfo,
{
int ret, error;
socklen_t len;
#ifdef _WIN32
struct timeval tv;
struct timeval *ptv;
fd_set set;
#else
struct pollfd pfd;
#endif

ret = set_blocking_mode(fd, false);
if (ret < 0)
Expand All @@ -198,43 +187,9 @@ static int do_connect(int fd, const struct addrinfo *addrinfo,
return ret;
}

#ifdef _WIN32
#ifdef _MSC_BUILD
/* This is so stupid, but studio emits a signed/unsigned mismatch
* on their own FD_ZERO macro, so turn the warning off/on
*/
#pragma warning(disable : 4389)
#endif
FD_ZERO(&set);
FD_SET(fd, &set);
#ifdef _MSC_BUILD
#pragma warning(default: 4389)
#endif

if (timeout != 0) {
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
ptv = &tv;
} else {
ptv = NULL;
}

ret = select(fd + 1, NULL, &set, &set, ptv);
#else
pfd.fd = fd;
pfd.events = POLLOUT | POLLERR;
pfd.revents = 0;

do {
ret = poll(&pfd, 1, timeout);
} while (ret == -1 && errno == EINTR);
#endif

ret = do_select(fd, timeout);
if (ret < 0)
return network_get_error();

if (ret == 0)
return -NETWORK_ERR_TIMEOUT;
return ret;

/* Verify that we don't have an error */
len = sizeof(error);
Expand Down
1 change: 1 addition & 0 deletions network.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void do_cancel(struct iiod_client_pdata *io_ctx);
int wait_cancellable(struct iiod_client_pdata *io_ctx, bool read);

int do_create_socket(const struct addrinfo *addrinfo);
int do_select(int fd, unsigned int timeout);

int set_blocking_mode(int s, bool blocking);
int set_socket_timeout(int fd, unsigned int timeout);
Expand Down

0 comments on commit 763b803

Please sign in to comment.