diff options
Diffstat (limited to 'src/UDPServer/UDPServer.cpp')
-rw-r--r-- | src/UDPServer/UDPServer.cpp | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/src/UDPServer/UDPServer.cpp b/src/UDPServer/UDPServer.cpp index 3a50d0c..8c9decf 100644 --- a/src/UDPServer/UDPServer.cpp +++ b/src/UDPServer/UDPServer.cpp @@ -168,31 +168,19 @@ int UDPServer::recv(char *msg, size_t max_size) * * \param[in] msg The buffer where the message will be saved. * \param[in] max_size The size of the \p msg buffer in bytes. - * \param[in] max_wait_ms The maximum number of milliseconds to wait for a message. + * \param[in] max_wait_s The maximum number of seconds to wait for a message. * * \return -1 if an error occurs or the function timed out, the number of bytes received otherwise. */ -int UDPServer::timed_recv(char *msg, size_t max_size, int max_wait_ms) +int UDPServer::timed_recv(char *msg, size_t max_size, int max_wait_s) { fd_set s; FD_ZERO(&s); FD_SET(f_socket, &s); struct timeval timeout; - timeout.tv_sec = max_wait_ms / 1000; - timeout.tv_usec = (max_wait_ms % 1000) * 1000; - int retval = select(f_socket + 1, &s, &s, &s, &timeout); - if(retval == -1) - { - // select() set errno accordingly - return -1; - } - if(retval > 0) - { - // our socket has data - return ::recv(f_socket, msg, max_size, 0); - } - - // our socket has no data - errno = EAGAIN; - return -1; + timeout.tv_sec = max_wait_s; + timeout.tv_usec = 0; + setsockopt(f_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(struct timeval)); + // our socket has data + return ::recv(f_socket, msg, max_size, 0); } |