Rename SC_INVALID_SOCKET to SC_SOCKET_NONE

For consistency with SC_PROCESS_NONE.
This commit is contained in:
Romain Vimont 2021-11-13 09:58:52 +01:00
parent c4d008b96a
commit 9a0bd545d5
7 changed files with 39 additions and 39 deletions

View file

@ -59,7 +59,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
// need to try to connect until the server socket is listening on the
// device.
sc_socket server_socket = net_socket();
if (server_socket != SC_INVALID_SOCKET) {
if (server_socket != SC_SOCKET_NONE) {
bool ok = listen_on_port(intr, server_socket, port);
if (ok) {
// success
@ -141,7 +141,7 @@ void
sc_adb_tunnel_init(struct sc_adb_tunnel *tunnel) {
tunnel->enabled = false;
tunnel->forward = false;
tunnel->server_socket = SC_INVALID_SOCKET;
tunnel->server_socket = SC_SOCKET_NONE;
tunnel->local_port = 0;
}
@ -177,7 +177,7 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,
} else {
ret = disable_tunnel_reverse(intr, serial);
assert(tunnel->server_socket != SC_INVALID_SOCKET);
assert(tunnel->server_socket != SC_SOCKET_NONE);
if (!net_close(tunnel->server_socket)) {
LOGW("Could not close server socket");
}

View file

@ -223,7 +223,7 @@ connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) {
do {
LOGD("Remaining connection attempts: %d", (int) attempts);
sc_socket socket = net_socket();
if (socket != SC_INVALID_SOCKET) {
if (socket != SC_SOCKET_NONE) {
bool ok = connect_and_read_byte(&server->intr, socket, port);
if (ok) {
// it worked!
@ -249,7 +249,7 @@ connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) {
}
}
} while (--attempts > 0);
return SC_INVALID_SOCKET;
return SC_SOCKET_NONE;
}
bool
@ -287,8 +287,8 @@ server_init(struct server *server, const struct server_params *params,
server->stopped = false;
server->video_socket = SC_INVALID_SOCKET;
server->control_socket = SC_INVALID_SOCKET;
server->video_socket = SC_SOCKET_NONE;
server->control_socket = SC_SOCKET_NONE;
sc_adb_tunnel_init(&server->tunnel);
@ -331,29 +331,29 @@ server_connect_to(struct server *server, struct server_info *info) {
const char *serial = server->params.serial;
sc_socket video_socket = SC_INVALID_SOCKET;
sc_socket control_socket = SC_INVALID_SOCKET;
sc_socket video_socket = SC_SOCKET_NONE;
sc_socket control_socket = SC_SOCKET_NONE;
if (!tunnel->forward) {
video_socket = net_accept_intr(&server->intr, tunnel->server_socket);
if (video_socket == SC_INVALID_SOCKET) {
if (video_socket == SC_SOCKET_NONE) {
goto fail;
}
control_socket = net_accept_intr(&server->intr, tunnel->server_socket);
if (control_socket == SC_INVALID_SOCKET) {
if (control_socket == SC_SOCKET_NONE) {
goto fail;
}
} else {
uint32_t attempts = 100;
sc_tick delay = SC_TICK_FROM_MS(100);
video_socket = connect_to_server(server, attempts, delay);
if (video_socket == SC_INVALID_SOCKET) {
if (video_socket == SC_SOCKET_NONE) {
goto fail;
}
// we know that the device is listening, we don't need several attempts
control_socket = net_socket();
if (control_socket == SC_INVALID_SOCKET) {
if (control_socket == SC_SOCKET_NONE) {
goto fail;
}
bool ok = net_connect_intr(&server->intr, control_socket,
@ -372,8 +372,8 @@ server_connect_to(struct server *server, struct server_info *info) {
goto fail;
}
assert(video_socket != SC_INVALID_SOCKET);
assert(control_socket != SC_INVALID_SOCKET);
assert(video_socket != SC_SOCKET_NONE);
assert(control_socket != SC_SOCKET_NONE);
server->video_socket = video_socket;
server->control_socket = control_socket;
@ -381,13 +381,13 @@ server_connect_to(struct server *server, struct server_info *info) {
return true;
fail:
if (video_socket != SC_INVALID_SOCKET) {
if (video_socket != SC_SOCKET_NONE) {
if (!net_close(video_socket)) {
LOGW("Could not close video socket");
}
}
if (control_socket != SC_INVALID_SOCKET) {
if (control_socket != SC_SOCKET_NONE) {
if (!net_close(control_socket)) {
LOGW("Could not close control socket");
}

View file

@ -12,7 +12,7 @@ sc_intr_init(struct sc_intr *intr) {
return false;
}
intr->socket = SC_INVALID_SOCKET;
intr->socket = SC_SOCKET_NONE;
intr->process = SC_PROCESS_NONE;
atomic_store_explicit(&intr->interrupted, false, memory_order_relaxed);
@ -37,7 +37,7 @@ sc_intr_set_socket(struct sc_intr *intr, sc_socket socket) {
bool
sc_intr_set_process(struct sc_intr *intr, sc_pid pid) {
assert(intr->socket == SC_INVALID_SOCKET);
assert(intr->socket == SC_SOCKET_NONE);
sc_mutex_lock(&intr->mutex);
bool interrupted =
@ -57,13 +57,13 @@ sc_intr_interrupt(struct sc_intr *intr) {
atomic_store_explicit(&intr->interrupted, true, memory_order_relaxed);
// No more than one component to interrupt
assert(intr->socket == SC_INVALID_SOCKET ||
assert(intr->socket == SC_SOCKET_NONE ||
intr->process == SC_PROCESS_NONE);
if (intr->socket != SC_INVALID_SOCKET) {
if (intr->socket != SC_SOCKET_NONE) {
LOGD("Interrupting socket");
net_interrupt(intr->socket);
intr->socket = SC_INVALID_SOCKET;
intr->socket = SC_SOCKET_NONE;
}
if (intr->process != SC_PROCESS_NONE) {
LOGD("Interrupting process");
@ -76,7 +76,7 @@ sc_intr_interrupt(struct sc_intr *intr) {
void
sc_intr_destroy(struct sc_intr *intr) {
assert(intr->socket == SC_INVALID_SOCKET);
assert(intr->socket == SC_SOCKET_NONE);
assert(intr->process == SC_PROCESS_NONE);
sc_mutex_destroy(&intr->mutex);

View file

@ -37,7 +37,7 @@ sc_intr_init(struct sc_intr *intr);
/**
* Set a socket as the interruptible component
*
* Call with SC_INVALID_SOCKET to unset.
* Call with SC_SOCKET_NONE to unset.
*/
bool
sc_intr_set_socket(struct sc_intr *intr, sc_socket socket);

View file

@ -46,13 +46,13 @@ static inline sc_socket
wrap(sc_raw_socket sock) {
#ifdef __WINDOWS__
if (sock == INVALID_SOCKET) {
return SC_INVALID_SOCKET;
return SC_SOCKET_NONE;
}
struct sc_socket_windows *socket = malloc(sizeof(*socket));
if (!socket) {
closesocket(sock);
return SC_INVALID_SOCKET;
return SC_SOCKET_NONE;
}
socket->socket = sock;
@ -67,7 +67,7 @@ wrap(sc_raw_socket sock) {
static inline sc_raw_socket
unwrap(sc_socket socket) {
#ifdef __WINDOWS__
if (socket == SC_INVALID_SOCKET) {
if (socket == SC_SOCKET_NONE) {
return INVALID_SOCKET;
}
@ -97,7 +97,7 @@ sc_socket
net_socket(void) {
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
sc_socket sock = wrap(raw_sock);
if (sock == SC_INVALID_SOCKET) {
if (sock == SC_SOCKET_NONE) {
net_perror("socket");
}
return sock;
@ -195,7 +195,7 @@ net_send_all(sc_socket socket, const void *buf, size_t len) {
bool
net_interrupt(sc_socket socket) {
assert(socket != SC_INVALID_SOCKET);
assert(socket != SC_SOCKET_NONE);
sc_raw_socket raw_sock = unwrap(socket);

View file

@ -11,7 +11,7 @@
# include <winsock2.h>
# include <stdatomic.h>
# define SC_INVALID_SOCKET NULL
# define SC_SOCKET_NONE NULL
typedef struct sc_socket_windows {
SOCKET socket;
atomic_flag closed;
@ -20,7 +20,7 @@
#else // not __WINDOWS__
# include <sys/socket.h>
# define SC_INVALID_SOCKET -1
# define SC_SOCKET_NONE -1
typedef int sc_socket;
#endif

View file

@ -10,7 +10,7 @@ net_connect_intr(struct sc_intr *intr, sc_socket socket, uint32_t addr,
bool ret = net_connect(socket, addr, port);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return ret;
}
@ -24,7 +24,7 @@ net_listen_intr(struct sc_intr *intr, sc_socket socket, uint32_t addr,
bool ret = net_listen(socket, addr, port, backlog);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return ret;
}
@ -32,12 +32,12 @@ sc_socket
net_accept_intr(struct sc_intr *intr, sc_socket server_socket) {
if (!sc_intr_set_socket(intr, server_socket)) {
// Already interrupted
return SC_INVALID_SOCKET;
return SC_SOCKET_NONE;
}
sc_socket socket = net_accept(server_socket);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return socket;
}
@ -50,7 +50,7 @@ net_recv_intr(struct sc_intr *intr, sc_socket socket, void *buf, size_t len) {
ssize_t r = net_recv(socket, buf, len);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return r;
}
@ -64,7 +64,7 @@ net_recv_all_intr(struct sc_intr *intr, sc_socket socket, void *buf,
ssize_t r = net_recv_all(socket, buf, len);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return r;
}
@ -78,7 +78,7 @@ net_send_intr(struct sc_intr *intr, sc_socket socket, const void *buf,
ssize_t w = net_send(socket, buf, len);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return w;
}
@ -92,6 +92,6 @@ net_send_all_intr(struct sc_intr *intr, sc_socket socket, const void *buf,
ssize_t w = net_send_all(socket, buf, len);
sc_intr_set_socket(intr, SC_INVALID_SOCKET);
sc_intr_set_socket(intr, SC_SOCKET_NONE);
return w;
}