Use sc_ prefix for sockets

Rename:
 - socket_t to sc_socket
 - INVALID_SOCKET to SC_INVALID_SOCKET
This commit is contained in:
Romain Vimont 2021-10-26 22:49:45 +02:00
parent eb6afe7669
commit 3adff37c2d
10 changed files with 78 additions and 73 deletions

View file

@ -5,7 +5,7 @@
#include "util/log.h" #include "util/log.h"
bool bool
controller_init(struct controller *controller, socket_t control_socket) { controller_init(struct controller *controller, sc_socket control_socket) {
cbuf_init(&controller->queue); cbuf_init(&controller->queue);
bool ok = receiver_init(&controller->receiver, control_socket); bool ok = receiver_init(&controller->receiver, control_socket);

View file

@ -14,7 +14,7 @@
struct control_msg_queue CBUF(struct control_msg, 64); struct control_msg_queue CBUF(struct control_msg, 64);
struct controller { struct controller {
socket_t control_socket; sc_socket control_socket;
sc_thread thread; sc_thread thread;
sc_mutex mutex; sc_mutex mutex;
sc_cond msg_cond; sc_cond msg_cond;
@ -24,7 +24,7 @@ struct controller {
}; };
bool bool
controller_init(struct controller *controller, socket_t control_socket); controller_init(struct controller *controller, sc_socket control_socket);
void void
controller_destroy(struct controller *controller); controller_destroy(struct controller *controller);

View file

@ -7,7 +7,7 @@
#include "util/log.h" #include "util/log.h"
bool bool
receiver_init(struct receiver *receiver, socket_t control_socket) { receiver_init(struct receiver *receiver, sc_socket control_socket) {
bool ok = sc_mutex_init(&receiver->mutex); bool ok = sc_mutex_init(&receiver->mutex);
if (!ok) { if (!ok) {
return false; return false;

View file

@ -11,13 +11,13 @@
// receive events from the device // receive events from the device
// managed by the controller // managed by the controller
struct receiver { struct receiver {
socket_t control_socket; sc_socket control_socket;
sc_thread thread; sc_thread thread;
sc_mutex mutex; sc_mutex mutex;
}; };
bool bool
receiver_init(struct receiver *receiver, socket_t control_socket); receiver_init(struct receiver *receiver, sc_socket control_socket);
void void
receiver_destroy(struct receiver *receiver); receiver_destroy(struct receiver *receiver);

View file

@ -109,7 +109,7 @@ disable_tunnel(struct server *server) {
return disable_tunnel_reverse(server->serial); return disable_tunnel_reverse(server->serial);
} }
static socket_t static sc_socket
listen_on_port(uint16_t port) { listen_on_port(uint16_t port) {
#define IPV4_LOCALHOST 0x7F000001 #define IPV4_LOCALHOST 0x7F000001
return net_listen(IPV4_LOCALHOST, port, 1); return net_listen(IPV4_LOCALHOST, port, 1);
@ -132,7 +132,7 @@ enable_tunnel_reverse_any_port(struct server *server,
// need to try to connect until the server socket is listening on the // need to try to connect until the server socket is listening on the
// device. // device.
server->server_socket = listen_on_port(port); server->server_socket = listen_on_port(port);
if (server->server_socket != INVALID_SOCKET) { if (server->server_socket != SC_INVALID_SOCKET) {
// success // success
server->local_port = port; server->local_port = port;
return true; return true;
@ -289,11 +289,11 @@ execute_server(struct server *server, const struct server_params *params) {
return adb_execute(server->serial, cmd, ARRAY_LEN(cmd)); return adb_execute(server->serial, cmd, ARRAY_LEN(cmd));
} }
static socket_t static sc_socket
connect_and_read_byte(uint16_t port) { connect_and_read_byte(uint16_t port) {
socket_t socket = net_connect(IPV4_LOCALHOST, port); sc_socket socket = net_connect(IPV4_LOCALHOST, port);
if (socket == INVALID_SOCKET) { if (socket == SC_INVALID_SOCKET) {
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
char byte; char byte;
@ -302,17 +302,17 @@ connect_and_read_byte(uint16_t port) {
if (net_recv(socket, &byte, 1) != 1) { if (net_recv(socket, &byte, 1) != 1) {
// the server is not listening yet behind the adb tunnel // the server is not listening yet behind the adb tunnel
net_close(socket); net_close(socket);
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
return socket; return socket;
} }
static socket_t static sc_socket
connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) { connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
do { do {
LOGD("Remaining connection attempts: %d", (int) attempts); LOGD("Remaining connection attempts: %d", (int) attempts);
socket_t socket = connect_and_read_byte(port); sc_socket socket = connect_and_read_byte(port);
if (socket != INVALID_SOCKET) { if (socket != SC_INVALID_SOCKET) {
// it worked! // it worked!
return socket; return socket;
} }
@ -320,12 +320,12 @@ connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
SDL_Delay(delay); SDL_Delay(delay);
} }
} while (--attempts > 0); } while (--attempts > 0);
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
static void static void
close_socket(socket_t socket) { close_socket(sc_socket socket) {
assert(socket != INVALID_SOCKET); assert(socket != SC_INVALID_SOCKET);
net_shutdown(socket, SHUT_RDWR); net_shutdown(socket, SHUT_RDWR);
if (!net_close(socket)) { if (!net_close(socket)) {
LOGW("Could not close socket"); LOGW("Could not close socket");
@ -352,9 +352,9 @@ server_init(struct server *server) {
server->process_terminated = false; server->process_terminated = false;
server->server_socket = INVALID_SOCKET; server->server_socket = SC_INVALID_SOCKET;
server->video_socket = INVALID_SOCKET; server->video_socket = SC_INVALID_SOCKET;
server->control_socket = INVALID_SOCKET; server->control_socket = SC_INVALID_SOCKET;
server->local_port = 0; server->local_port = 0;
@ -376,7 +376,7 @@ run_wait_server(void *data) {
// no need for synchronization, server_socket is initialized before this // no need for synchronization, server_socket is initialized before this
// thread was created // thread was created
if (server->server_socket != INVALID_SOCKET if (server->server_socket != SC_INVALID_SOCKET
&& !atomic_flag_test_and_set(&server->server_socket_closed)) { && !atomic_flag_test_and_set(&server->server_socket_closed)) {
// On Linux, accept() is unblocked by shutdown(), but on Windows, it is // On Linux, accept() is unblocked by shutdown(), but on Windows, it is
// unblocked by closesocket(). Therefore, call both (close_socket()). // unblocked by closesocket(). Therefore, call both (close_socket()).
@ -444,7 +444,8 @@ error:
} }
static bool static bool
device_read_info(socket_t device_socket, char *device_name, struct size *size) { device_read_info(sc_socket device_socket, char *device_name,
struct size *size) {
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4]; unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4];
ssize_t r = net_recv_all(device_socket, buf, sizeof(buf)); ssize_t r = net_recv_all(device_socket, buf, sizeof(buf));
if (r < DEVICE_NAME_FIELD_LENGTH + 4) { if (r < DEVICE_NAME_FIELD_LENGTH + 4) {
@ -467,12 +468,12 @@ bool
server_connect_to(struct server *server, char *device_name, struct size *size) { server_connect_to(struct server *server, char *device_name, struct size *size) {
if (!server->tunnel_forward) { if (!server->tunnel_forward) {
server->video_socket = net_accept(server->server_socket); server->video_socket = net_accept(server->server_socket);
if (server->video_socket == INVALID_SOCKET) { if (server->video_socket == SC_INVALID_SOCKET) {
return false; return false;
} }
server->control_socket = net_accept(server->server_socket); server->control_socket = net_accept(server->server_socket);
if (server->control_socket == INVALID_SOCKET) { if (server->control_socket == SC_INVALID_SOCKET) {
// the video_socket will be cleaned up on destroy // the video_socket will be cleaned up on destroy
return false; return false;
} }
@ -488,14 +489,14 @@ server_connect_to(struct server *server, char *device_name, struct size *size) {
uint32_t delay = 100; // ms uint32_t delay = 100; // ms
server->video_socket = server->video_socket =
connect_to_server(server->local_port, attempts, delay); connect_to_server(server->local_port, attempts, delay);
if (server->video_socket == INVALID_SOCKET) { if (server->video_socket == SC_INVALID_SOCKET) {
return false; return false;
} }
// we know that the device is listening, we don't need several attempts // we know that the device is listening, we don't need several attempts
server->control_socket = server->control_socket =
net_connect(IPV4_LOCALHOST, server->local_port); net_connect(IPV4_LOCALHOST, server->local_port);
if (server->control_socket == INVALID_SOCKET) { if (server->control_socket == SC_INVALID_SOCKET) {
return false; return false;
} }
} }
@ -510,14 +511,14 @@ server_connect_to(struct server *server, char *device_name, struct size *size) {
void void
server_stop(struct server *server) { server_stop(struct server *server) {
if (server->server_socket != INVALID_SOCKET if (server->server_socket != SC_INVALID_SOCKET
&& !atomic_flag_test_and_set(&server->server_socket_closed)) { && !atomic_flag_test_and_set(&server->server_socket_closed)) {
close_socket(server->server_socket); close_socket(server->server_socket);
} }
if (server->video_socket != INVALID_SOCKET) { if (server->video_socket != SC_INVALID_SOCKET) {
close_socket(server->video_socket); close_socket(server->video_socket);
} }
if (server->control_socket != INVALID_SOCKET) { if (server->control_socket != SC_INVALID_SOCKET) {
close_socket(server->control_socket); close_socket(server->control_socket);
} }

View file

@ -24,9 +24,9 @@ struct server {
sc_cond process_terminated_cond; sc_cond process_terminated_cond;
bool process_terminated; bool process_terminated;
socket_t server_socket; // only used if !tunnel_forward sc_socket server_socket; // only used if !tunnel_forward
socket_t video_socket; sc_socket video_socket;
socket_t control_socket; sc_socket control_socket;
uint16_t local_port; // selected from port_range uint16_t local_port; // selected from port_range
bool tunnel_enabled; bool tunnel_enabled;
bool tunnel_forward; // use "adb forward" instead of "adb reverse" bool tunnel_forward; // use "adb forward" instead of "adb reverse"

View file

@ -260,7 +260,7 @@ end:
} }
void void
stream_init(struct stream *stream, socket_t socket, stream_init(struct stream *stream, sc_socket socket,
const struct stream_callbacks *cbs, void *cbs_userdata) { const struct stream_callbacks *cbs, void *cbs_userdata) {
stream->socket = socket; stream->socket = socket;
stream->pending = NULL; stream->pending = NULL;

View file

@ -14,7 +14,7 @@
#define STREAM_MAX_SINKS 2 #define STREAM_MAX_SINKS 2
struct stream { struct stream {
socket_t socket; sc_socket socket;
sc_thread thread; sc_thread thread;
struct sc_packet_sink *sinks[STREAM_MAX_SINKS]; struct sc_packet_sink *sinks[STREAM_MAX_SINKS];
@ -35,7 +35,7 @@ struct stream_callbacks {
}; };
void void
stream_init(struct stream *stream, socket_t socket, stream_init(struct stream *stream, sc_socket socket,
const struct stream_callbacks *cbs, void *cbs_userdata); const struct stream_callbacks *cbs, void *cbs_userdata);
void void

View file

@ -55,12 +55,12 @@ net_perror(const char *s) {
#endif #endif
} }
socket_t sc_socket
net_connect(uint32_t addr, uint16_t port) { net_connect(uint32_t addr, uint16_t port) {
socket_t sock = socket(AF_INET, SOCK_STREAM, 0); sc_socket sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) { if (sock == SC_INVALID_SOCKET) {
net_perror("socket"); net_perror("socket");
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
SOCKADDR_IN sin; SOCKADDR_IN sin;
@ -71,18 +71,18 @@ net_connect(uint32_t addr, uint16_t port) {
if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) { if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
net_perror("connect"); net_perror("connect");
net_close(sock); net_close(sock);
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
return sock; return sock;
} }
socket_t sc_socket
net_listen(uint32_t addr, uint16_t port, int backlog) { net_listen(uint32_t addr, uint16_t port, int backlog) {
socket_t sock = socket(AF_INET, SOCK_STREAM, 0); sc_socket sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) { if (sock == SC_INVALID_SOCKET) {
net_perror("socket"); net_perror("socket");
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
int reuse = 1; int reuse = 1;
@ -99,42 +99,42 @@ net_listen(uint32_t addr, uint16_t port, int backlog) {
if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) { if (bind(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
net_perror("bind"); net_perror("bind");
net_close(sock); net_close(sock);
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
if (listen(sock, backlog) == SOCKET_ERROR) { if (listen(sock, backlog) == SOCKET_ERROR) {
net_perror("listen"); net_perror("listen");
net_close(sock); net_close(sock);
return INVALID_SOCKET; return SC_INVALID_SOCKET;
} }
return sock; return sock;
} }
socket_t sc_socket
net_accept(socket_t server_socket) { net_accept(sc_socket server_socket) {
SOCKADDR_IN csin; SOCKADDR_IN csin;
socklen_t sinsize = sizeof(csin); socklen_t sinsize = sizeof(csin);
return accept(server_socket, (SOCKADDR *) &csin, &sinsize); return accept(server_socket, (SOCKADDR *) &csin, &sinsize);
} }
ssize_t ssize_t
net_recv(socket_t socket, void *buf, size_t len) { net_recv(sc_socket socket, void *buf, size_t len) {
return recv(socket, buf, len, 0); return recv(socket, buf, len, 0);
} }
ssize_t ssize_t
net_recv_all(socket_t socket, void *buf, size_t len) { net_recv_all(sc_socket socket, void *buf, size_t len) {
return recv(socket, buf, len, MSG_WAITALL); return recv(socket, buf, len, MSG_WAITALL);
} }
ssize_t ssize_t
net_send(socket_t socket, const void *buf, size_t len) { net_send(sc_socket socket, const void *buf, size_t len) {
return send(socket, buf, len, 0); return send(socket, buf, len, 0);
} }
ssize_t ssize_t
net_send_all(socket_t socket, const void *buf, size_t len) { net_send_all(sc_socket socket, const void *buf, size_t len) {
size_t copied = 0; size_t copied = 0;
while (len > 0) { while (len > 0) {
ssize_t w = send(socket, buf, len, 0); ssize_t w = send(socket, buf, len, 0);
@ -149,12 +149,12 @@ net_send_all(socket_t socket, const void *buf, size_t len) {
} }
bool bool
net_shutdown(socket_t socket, int how) { net_shutdown(sc_socket socket, int how) {
return !shutdown(socket, how); return !shutdown(socket, how);
} }
bool bool
net_close(socket_t socket) { net_close(sc_socket socket) {
#ifdef __WINDOWS__ #ifdef __WINDOWS__
return !closesocket(socket); return !closesocket(socket);
#else #else

View file

@ -8,15 +8,19 @@
#include <SDL2/SDL_platform.h> #include <SDL2/SDL_platform.h>
#ifdef __WINDOWS__ #ifdef __WINDOWS__
# include <winsock2.h> # include <winsock2.h>
#define SHUT_RD SD_RECEIVE # define SHUT_RD SD_RECEIVE
#define SHUT_WR SD_SEND # define SHUT_WR SD_SEND
#define SHUT_RDWR SD_BOTH # define SHUT_RDWR SD_BOTH
typedef SOCKET socket_t; # define SC_INVALID_SOCKET INVALID_SOCKET
#else typedef SOCKET sc_socket;
#else // not __WINDOWS__
# include <sys/socket.h> # include <sys/socket.h>
# define INVALID_SOCKET -1 # define SC_INVALID_SOCKET -1
typedef int socket_t; typedef int sc_socket;
#endif #endif
bool bool
@ -25,33 +29,33 @@ net_init(void);
void void
net_cleanup(void); net_cleanup(void);
socket_t sc_socket
net_connect(uint32_t addr, uint16_t port); net_connect(uint32_t addr, uint16_t port);
socket_t sc_socket
net_listen(uint32_t addr, uint16_t port, int backlog); net_listen(uint32_t addr, uint16_t port, int backlog);
socket_t sc_socket
net_accept(socket_t server_socket); net_accept(sc_socket server_socket);
// the _all versions wait/retry until len bytes have been written/read // the _all versions wait/retry until len bytes have been written/read
ssize_t ssize_t
net_recv(socket_t socket, void *buf, size_t len); net_recv(sc_socket socket, void *buf, size_t len);
ssize_t ssize_t
net_recv_all(socket_t socket, void *buf, size_t len); net_recv_all(sc_socket socket, void *buf, size_t len);
ssize_t ssize_t
net_send(socket_t socket, const void *buf, size_t len); net_send(sc_socket socket, const void *buf, size_t len);
ssize_t ssize_t
net_send_all(socket_t socket, const void *buf, size_t len); net_send_all(sc_socket socket, const void *buf, size_t len);
// how is SHUT_RD (read), SHUT_WR (write) or SHUT_RDWR (both) // how is SHUT_RD (read), SHUT_WR (write) or SHUT_RDWR (both)
bool bool
net_shutdown(socket_t socket, int how); net_shutdown(sc_socket socket, int how);
bool bool
net_close(socket_t socket); net_close(sc_socket socket);
#endif #endif