Timeout the server socket connection

Wait no more than 2 seconds for accepting the connection from the
device, since it blocks the event loop, preventing to react to SIGTERM
(Ctrl+C).
This commit is contained in:
Romain Vimont
2018-02-09 15:18:09 +01:00
parent 90a46b4c45
commit eb09fefd43
5 changed files with 12 additions and 12 deletions

View File

@@ -4,7 +4,7 @@
// contrary to SDLNet_TCP_Send and SDLNet_TCP_Recv, SDLNet_TCP_Accept is non-blocking
// so we need to block before calling it
TCPsocket server_socket_accept(TCPsocket server_socket) {
TCPsocket server_socket_accept(TCPsocket server_socket, Uint32 timeout_ms) {
SDLNet_SocketSet set = SDLNet_AllocSocketSet(1);
if (!set) {
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Could not allocate socket set");
@@ -17,9 +17,8 @@ TCPsocket server_socket_accept(TCPsocket server_socket) {
return NULL;
}
// timeout is (2^32-1) milliseconds, this should be sufficient
if (SDLNet_CheckSockets(set, -1) != 1) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Could not check socket");
if (SDLNet_CheckSockets(set, timeout_ms) != 1) {
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "No connection to accept");
SDLNet_FreeSocketSet(set);
return NULL;
}