diff --git a/app/src/net.c b/app/src/net.c index e2f3feac..c370dbe3 100644 --- a/app/src/net.c +++ b/app/src/net.c @@ -1,5 +1,7 @@ #include "net.h" +#include + #include "log.h" #ifdef __WINDOWS__ @@ -19,7 +21,7 @@ socket_t net_listen(Uint32 addr, Uint16 port, int backlog) { socket_t sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { - LOGE("Cannot create socket"); + perror("socket"); return INVALID_SOCKET; } @@ -29,12 +31,12 @@ socket_t net_listen(Uint32 addr, Uint16 port, int backlog) { sin.sin_port = htons(port); if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) { - LOGE("Cannot bind"); + perror("bind"); return INVALID_SOCKET; } if (listen(sock, backlog) == SOCKET_ERROR) { - LOGE("Cannot listen on port %" PRIu16, port); + perror("listen"); return INVALID_SOCKET; } diff --git a/app/src/net.h b/app/src/net.h index 08ea61c1..03ad8c39 100644 --- a/app/src/net.h +++ b/app/src/net.h @@ -31,6 +31,6 @@ ssize_t net_send(socket_t socket, void *buf, size_t len); ssize_t net_send_all(socket_t socket, void *buf, size_t len); // how is SHUT_RD (read), SHUT_WR (write) or SHUT_RDWR (both) SDL_bool net_shutdown(socket_t socket, int how); -void net_close(socket_t socket); +SDL_bool net_close(socket_t socket); #endif diff --git a/app/src/server.c b/app/src/server.c index dfb522cc..a4cba260 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -62,6 +62,15 @@ static socket_t listen_on_port(Uint16 port) { return net_listen(IPV4_LOCALHOST, port, 1); } +static void close_socket(socket_t *socket) { + SDL_assert(*socket != INVALID_SOCKET); + if (!net_close(*socket)) { + LOGW("Cannot close socket"); + return; + } + *socket = INVALID_SOCKET; +} + void server_init(struct server *server) { *server = (struct server) SERVER_INITIALIZER; } @@ -92,7 +101,7 @@ SDL_bool server_start(struct server *server, const char *serial, Uint16 local_po // server will connect to our server socket server->process = execute_server(serial, max_size, bit_rate); if (server->process == PROCESS_NONE) { - net_close(server->server_socket); + close_socket(&server->server_socket); disable_tunnel(serial); return SDL_FALSE; } @@ -109,8 +118,7 @@ socket_t server_connect_to(struct server *server, const char *serial, Uint32 tim } // we don't need the server socket anymore - net_close(server->server_socket); - server->server_socket = INVALID_SOCKET; + close_socket(&server->server_socket); // we don't need the adb tunnel anymore disable_tunnel(serial); // ignore failure @@ -124,7 +132,9 @@ void server_stop(struct server *server, const char *serial) { if (server->device_socket != INVALID_SOCKET) { // shutdown the socket to finish the device process gracefully - net_shutdown(server->device_socket, SHUT_RDWR); + if (!net_shutdown(server->device_socket, SHUT_RDWR)) { + LOGW("Cannot shutdown socket"); + } } LOGD("Waiting the server to complete execution on the device..."); @@ -139,9 +149,9 @@ void server_stop(struct server *server, const char *serial) { void server_destroy(struct server *server) { if (server->server_socket != INVALID_SOCKET) { - net_close(server->server_socket); + close_socket(&server->server_socket); } if (server->device_socket != INVALID_SOCKET) { - net_close(server->device_socket); + close_socket(&server->device_socket); } } diff --git a/app/src/sys/unix/net.c b/app/src/sys/unix/net.c index 4219ddb8..f50fbe2e 100644 --- a/app/src/sys/unix/net.c +++ b/app/src/sys/unix/net.c @@ -11,6 +11,6 @@ void net_cleanup(void) { // do nothing } -void net_close(socket_t socket) { - close(socket); +SDL_bool net_close(socket_t socket) { + return !close(socket); } diff --git a/app/src/sys/win/net.c b/app/src/sys/win/net.c index b328361e..1837e0d1 100644 --- a/app/src/sys/win/net.c +++ b/app/src/sys/win/net.c @@ -16,6 +16,6 @@ void net_cleanup(void) { WSACleanup(); } -void net_close(socket_t socket) { - closesocket(socket); +SDL_bool net_close(socket_t socket) { + return !closesocket(socket); }