Log socket errors

Do not silently ignore close() and shutdown() errors, and use perror()
to get the errno.
This commit is contained in:
Romain Vimont 2018-02-16 14:55:33 +01:00
parent 37af0c8076
commit 6db22ef339
5 changed files with 26 additions and 14 deletions

View file

@ -1,5 +1,7 @@
#include "net.h"
#include <stdio.h>
#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;
}

View file

@ -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

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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);
}