diff --git a/common/cross_sockets/xsocket.cpp b/common/cross_sockets/xsocket.cpp index 0fa79e6864..96a575e8f0 100644 --- a/common/cross_sockets/xsocket.cpp +++ b/common/cross_sockets/xsocket.cpp @@ -51,6 +51,17 @@ int set_socket_option(int socket, int level, int optname, const char* optval, in #endif } +int set_socket_timeout(int socket, int microSeconds) { +#ifdef __linux + timeval timeout = {}; + timeout.tv_sec = 0; + timeout.tv_usec = microSeconds; +#elif _WIN32 + unsigned long timeout = microSeconds / 1000; // milliseconds +#endif + return set_socket_option(socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)); +} + int write_to_socket(int socket, const char* buf, int len) { #ifdef __linux return write(socket, buf, len); diff --git a/common/cross_sockets/xsocket.h b/common/cross_sockets/xsocket.h index 8f7bb359ae..06f1711ae6 100644 --- a/common/cross_sockets/xsocket.h +++ b/common/cross_sockets/xsocket.h @@ -15,5 +15,6 @@ const int TCP_SOCKET_LEVEL = IPPROTO_TCP; int open_socket(int af, int type, int protocol); void close_socket(int sock); int set_socket_option(int socket, int level, int optname, const char* optval, int optlen); +int set_socket_timeout(int socket, int microSeconds); int write_to_socket(int socket, const char* buf, int len); int read_from_socket(int socket, char* buf, int len); diff --git a/game/system/Deci2Server.cpp b/game/system/Deci2Server.cpp index 7579b60f65..cbcd924d37 100644 --- a/game/system/Deci2Server.cpp +++ b/game/system/Deci2Server.cpp @@ -74,24 +74,10 @@ bool Deci2Server::init() { return false; } -// TODO - put in library -#ifdef __linux - timeval timeout = {}; - timeout.tv_sec = 0; - timeout.tv_usec = 100000; - if (set_socket_option(server_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < - 0) { + if (set_socket_timeout(server_socket, 100000) < 0) { close_server_socket(); return false; } -#elif _WIN32 - unsigned long timeout = 100; // ms - if (set_socket_option(server_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < - 0) { - close_server_socket(); - return false; - } -#endif addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; diff --git a/goalc/listener/Listener.cpp b/goalc/listener/Listener.cpp index 11f62f0260..bdbcf47820 100644 --- a/goalc/listener/Listener.cpp +++ b/goalc/listener/Listener.cpp @@ -71,25 +71,12 @@ bool Listener::connect_to_target(const std::string& ip, int port) { return false; } - // TODO - put in library -#ifdef __linux - timeval timeout = {}; - timeout.tv_sec = 0; - timeout.tv_usec = 100000; - if (set_socket_option(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0) { - close_socket(socket_fd); - socket_fd = -1; - return false; - } -#elif _WIN32 - unsigned long timeout = 100; // ms - if (set_socket_option(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0) { + if (set_socket_timeout(socket_fd, 100000) < 0) { printf("[Listener] setsockopt failed\n"); close_socket(socket_fd); socket_fd = -1; return false; } -#endif // set nodelay, which makes small rapid messages faster, but large messages slower char one = 1;