diff options
author | Marc Schink <jaylink-dev@marcschink.de> | 2017-08-20 18:14:23 +0200 |
---|---|---|
committer | Marc Schink <jaylink-dev@marcschink.de> | 2017-08-22 11:31:36 +0200 |
commit | 8c67221bb7f7a606b828d08c8c873299fcf2db1a (patch) | |
tree | 7028c8d658f52ade5d42d04dd1422b26b9acae63 | |
parent | 2c03ebf8b5b45a46db07dbc880a8a552c89291b6 (diff) | |
download | libjaylink-8c67221bb7f7a606b828d08c8c873299fcf2db1a.zip libjaylink-8c67221bb7f7a606b828d08c8c873299fcf2db1a.tar.gz libjaylink-8c67221bb7f7a606b828d08c8c873299fcf2db1a.tar.bz2 |
transport/tcp: Fix socket timeouts on Windows
Signed-off-by: Marc Schink <jaylink-dev@marcschink.de>
-rw-r--r-- | libjaylink/transport_tcp.c | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/libjaylink/transport_tcp.c b/libjaylink/transport_tcp.c index 3d5409c..7e10179 100644 --- a/libjaylink/transport_tcp.c +++ b/libjaylink/transport_tcp.c @@ -177,6 +177,53 @@ static int handle_server_hello(struct jaylink_device_handle *devh) return JAYLINK_OK; } +static int set_socket_timeouts(struct jaylink_device_handle *devh) +{ + struct jaylink_context *ctx; + + ctx = devh->dev->ctx; +#ifdef _WIN32 + DWORD timeout; + + timeout = RECV_TIMEOUT; + + if (!socket_set_option(devh->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(timeout))) { + log_err(ctx, "Failed to set socket receive timeout."); + return JAYLINK_ERR; + } + + timeout = SEND_TIMEOUT; + + if (!socket_set_option(devh->sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, + sizeof(timeout))) { + log_err(ctx, "Failed to set socket send timeout."); + return JAYLINK_ERR; + } +#else + struct timeval timeout; + + timeout.tv_sec = RECV_TIMEOUT / 1000; + timeout.tv_usec = (RECV_TIMEOUT % 1000) * 1000; + + if (!socket_set_option(devh->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, + sizeof(struct timeval))) { + log_err(ctx, "Failed to set socket receive timeout."); + return JAYLINK_ERR; + } + + timeout.tv_sec = SEND_TIMEOUT / 1000; + timeout.tv_usec = (SEND_TIMEOUT % 1000) * 1000; + + if (!socket_set_option(devh->sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, + sizeof(struct timeval))) { + log_err(ctx, "Failed to set socket send timeout."); + return JAYLINK_ERR; + } +#endif + return JAYLINK_OK; +} + JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh) { int ret; @@ -185,7 +232,6 @@ JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh) struct addrinfo hints; struct addrinfo *info; struct addrinfo *rp; - struct timeval timeout; int sock; dev = devh->dev; @@ -239,30 +285,15 @@ JAYLINK_PRIV int transport_tcp_open(struct jaylink_device_handle *devh) log_dbg(ctx, "Device opened successfully."); - timeout.tv_sec = RECV_TIMEOUT / 1000; - timeout.tv_usec = (RECV_TIMEOUT % 1000) * 1000; - - if (!socket_set_option(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, - sizeof(struct timeval))) { - log_err(ctx, "Failed to set socket receive timeout."); - socket_close(sock); - cleanup_handle(devh); - return JAYLINK_ERR; - } - - timeout.tv_sec = SEND_TIMEOUT / 1000; - timeout.tv_usec = (SEND_TIMEOUT % 1000) * 1000; + devh->sock = sock; + ret = set_socket_timeouts(devh); - if (!socket_set_option(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, - sizeof(struct timeval))) { - log_err(ctx, "Failed to set socket send timeout."); + if (ret != JAYLINK_OK) { socket_close(sock); cleanup_handle(devh); - return JAYLINK_ERR; + return ret; } - devh->sock = sock; - ret = handle_server_hello(devh); if (ret != JAYLINK_OK) { |