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:
parent
90a46b4c45
commit
eb09fefd43
5 changed files with 12 additions and 12 deletions
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
// contrary to SDLNet_TCP_Send and SDLNet_TCP_Recv, SDLNet_TCP_Accept is non-blocking
|
// contrary to SDLNet_TCP_Send and SDLNet_TCP_Recv, SDLNet_TCP_Accept is non-blocking
|
||||||
// so we need to block before calling it
|
// 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);
|
SDLNet_SocketSet set = SDLNet_AllocSocketSet(1);
|
||||||
if (!set) {
|
if (!set) {
|
||||||
SDL_LogCritical(SDL_LOG_CATEGORY_SYSTEM, "Could not allocate socket 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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// timeout is (2^32-1) milliseconds, this should be sufficient
|
if (SDLNet_CheckSockets(set, timeout_ms) != 1) {
|
||||||
if (SDLNet_CheckSockets(set, -1) != 1) {
|
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "No connection to accept");
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Could not check socket");
|
|
||||||
SDLNet_FreeSocketSet(set);
|
SDLNet_FreeSocketSet(set);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@
|
||||||
#include <SDL2/SDL_net.h>
|
#include <SDL2/SDL_net.h>
|
||||||
|
|
||||||
// blocking accept on the server socket
|
// blocking accept on the server socket
|
||||||
TCPsocket server_socket_accept(TCPsocket server_socket);
|
TCPsocket server_socket_accept(TCPsocket server_socket, Uint32 timeout_ms);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -111,10 +111,11 @@ SDL_bool scrcpy(const char *serial, Uint16 local_port, Uint16 max_size, Uint32 b
|
||||||
goto finally_destroy_server;
|
goto finally_destroy_server;
|
||||||
}
|
}
|
||||||
|
|
||||||
// to reduce startup time, we could be tempted to init other stuff before blocking here
|
// SDL initialization replace the signal handler for SIGTERM, so Ctrl+C is
|
||||||
// but we should not block after SDL_Init since it handles the signals (Ctrl+C) in its
|
// managed by the event loop. This blocking call blocks the event loop, so
|
||||||
// event loop: blocking could lead to deadlock
|
// timeout the connection not to block indefinitely in case of SIGTERM.
|
||||||
TCPsocket device_socket = server_connect_to(&server, serial);
|
#define SERVER_CONNECT_TIMEOUT_MS 2000
|
||||||
|
TCPsocket device_socket = server_connect_to(&server, serial, SERVER_CONNECT_TIMEOUT_MS);
|
||||||
if (!device_socket) {
|
if (!device_socket) {
|
||||||
server_stop(&server, serial);
|
server_stop(&server, serial);
|
||||||
ret = SDL_FALSE;
|
ret = SDL_FALSE;
|
||||||
|
|
|
@ -98,9 +98,9 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po
|
||||||
return SDL_TRUE;
|
return SDL_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPsocket server_connect_to(struct server *server, const char *serial) {
|
TCPsocket server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms) {
|
||||||
SDL_assert(server->server_socket);
|
SDL_assert(server->server_socket);
|
||||||
server->device_socket = server_socket_accept(server->server_socket);
|
server->device_socket = server_socket_accept(server->server_socket, timeout_ms);
|
||||||
|
|
||||||
// we don't need the server socket anymore
|
// we don't need the server socket anymore
|
||||||
SDLNet_TCP_Close(server->server_socket);
|
SDLNet_TCP_Close(server->server_socket);
|
||||||
|
|
|
@ -26,7 +26,7 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po
|
||||||
Uint16 max_size, Uint32 bit_rate);
|
Uint16 max_size, Uint32 bit_rate);
|
||||||
|
|
||||||
// block until the communication with the server is established
|
// block until the communication with the server is established
|
||||||
TCPsocket server_connect_to(struct server *server, const char *serial);
|
TCPsocket server_connect_to(struct server *server, const char *serial, Uint32 timeout_ms);
|
||||||
|
|
||||||
// disconnect and kill the server process
|
// disconnect and kill the server process
|
||||||
void server_stop(struct server *server, const char *serial);
|
void server_stop(struct server *server, const char *serial);
|
||||||
|
|
Loading…
Reference in a new issue