Use sc_ prefix for sockets
Rename: - socket_t to sc_socket - INVALID_SOCKET to SC_INVALID_SOCKET
This commit is contained in:
parent
eb6afe7669
commit
3adff37c2d
10 changed files with 78 additions and 73 deletions
|
@ -5,7 +5,7 @@
|
|||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
controller_init(struct controller *controller, socket_t control_socket) {
|
||||
controller_init(struct controller *controller, sc_socket control_socket) {
|
||||
cbuf_init(&controller->queue);
|
||||
|
||||
bool ok = receiver_init(&controller->receiver, control_socket);
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
struct control_msg_queue CBUF(struct control_msg, 64);
|
||||
|
||||
struct controller {
|
||||
socket_t control_socket;
|
||||
sc_socket control_socket;
|
||||
sc_thread thread;
|
||||
sc_mutex mutex;
|
||||
sc_cond msg_cond;
|
||||
|
@ -24,7 +24,7 @@ struct controller {
|
|||
};
|
||||
|
||||
bool
|
||||
controller_init(struct controller *controller, socket_t control_socket);
|
||||
controller_init(struct controller *controller, sc_socket control_socket);
|
||||
|
||||
void
|
||||
controller_destroy(struct controller *controller);
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include "util/log.h"
|
||||
|
||||
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);
|
||||
if (!ok) {
|
||||
return false;
|
||||
|
|
|
@ -11,13 +11,13 @@
|
|||
// receive events from the device
|
||||
// managed by the controller
|
||||
struct receiver {
|
||||
socket_t control_socket;
|
||||
sc_socket control_socket;
|
||||
sc_thread thread;
|
||||
sc_mutex mutex;
|
||||
};
|
||||
|
||||
bool
|
||||
receiver_init(struct receiver *receiver, socket_t control_socket);
|
||||
receiver_init(struct receiver *receiver, sc_socket control_socket);
|
||||
|
||||
void
|
||||
receiver_destroy(struct receiver *receiver);
|
||||
|
|
|
@ -109,7 +109,7 @@ disable_tunnel(struct server *server) {
|
|||
return disable_tunnel_reverse(server->serial);
|
||||
}
|
||||
|
||||
static socket_t
|
||||
static sc_socket
|
||||
listen_on_port(uint16_t port) {
|
||||
#define IPV4_LOCALHOST 0x7F000001
|
||||
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
|
||||
// device.
|
||||
server->server_socket = listen_on_port(port);
|
||||
if (server->server_socket != INVALID_SOCKET) {
|
||||
if (server->server_socket != SC_INVALID_SOCKET) {
|
||||
// success
|
||||
server->local_port = port;
|
||||
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));
|
||||
}
|
||||
|
||||
static socket_t
|
||||
static sc_socket
|
||||
connect_and_read_byte(uint16_t port) {
|
||||
socket_t socket = net_connect(IPV4_LOCALHOST, port);
|
||||
if (socket == INVALID_SOCKET) {
|
||||
return INVALID_SOCKET;
|
||||
sc_socket socket = net_connect(IPV4_LOCALHOST, port);
|
||||
if (socket == SC_INVALID_SOCKET) {
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
char byte;
|
||||
|
@ -302,17 +302,17 @@ connect_and_read_byte(uint16_t port) {
|
|||
if (net_recv(socket, &byte, 1) != 1) {
|
||||
// the server is not listening yet behind the adb tunnel
|
||||
net_close(socket);
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
|
||||
static socket_t
|
||||
static sc_socket
|
||||
connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
|
||||
do {
|
||||
LOGD("Remaining connection attempts: %d", (int) attempts);
|
||||
socket_t socket = connect_and_read_byte(port);
|
||||
if (socket != INVALID_SOCKET) {
|
||||
sc_socket socket = connect_and_read_byte(port);
|
||||
if (socket != SC_INVALID_SOCKET) {
|
||||
// it worked!
|
||||
return socket;
|
||||
}
|
||||
|
@ -320,12 +320,12 @@ connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
|
|||
SDL_Delay(delay);
|
||||
}
|
||||
} while (--attempts > 0);
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
static void
|
||||
close_socket(socket_t socket) {
|
||||
assert(socket != INVALID_SOCKET);
|
||||
close_socket(sc_socket socket) {
|
||||
assert(socket != SC_INVALID_SOCKET);
|
||||
net_shutdown(socket, SHUT_RDWR);
|
||||
if (!net_close(socket)) {
|
||||
LOGW("Could not close socket");
|
||||
|
@ -352,9 +352,9 @@ server_init(struct server *server) {
|
|||
|
||||
server->process_terminated = false;
|
||||
|
||||
server->server_socket = INVALID_SOCKET;
|
||||
server->video_socket = INVALID_SOCKET;
|
||||
server->control_socket = INVALID_SOCKET;
|
||||
server->server_socket = SC_INVALID_SOCKET;
|
||||
server->video_socket = SC_INVALID_SOCKET;
|
||||
server->control_socket = SC_INVALID_SOCKET;
|
||||
|
||||
server->local_port = 0;
|
||||
|
||||
|
@ -376,7 +376,7 @@ run_wait_server(void *data) {
|
|||
|
||||
// no need for synchronization, server_socket is initialized before this
|
||||
// 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)) {
|
||||
// On Linux, accept() is unblocked by shutdown(), but on Windows, it is
|
||||
// unblocked by closesocket(). Therefore, call both (close_socket()).
|
||||
|
@ -444,7 +444,8 @@ error:
|
|||
}
|
||||
|
||||
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];
|
||||
ssize_t r = net_recv_all(device_socket, buf, sizeof(buf));
|
||||
if (r < DEVICE_NAME_FIELD_LENGTH + 4) {
|
||||
|
@ -467,12 +468,12 @@ bool
|
|||
server_connect_to(struct server *server, char *device_name, struct size *size) {
|
||||
if (!server->tunnel_forward) {
|
||||
server->video_socket = net_accept(server->server_socket);
|
||||
if (server->video_socket == INVALID_SOCKET) {
|
||||
if (server->video_socket == SC_INVALID_SOCKET) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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
|
||||
return false;
|
||||
}
|
||||
|
@ -488,14 +489,14 @@ server_connect_to(struct server *server, char *device_name, struct size *size) {
|
|||
uint32_t delay = 100; // ms
|
||||
server->video_socket =
|
||||
connect_to_server(server->local_port, attempts, delay);
|
||||
if (server->video_socket == INVALID_SOCKET) {
|
||||
if (server->video_socket == SC_INVALID_SOCKET) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// we know that the device is listening, we don't need several attempts
|
||||
server->control_socket =
|
||||
net_connect(IPV4_LOCALHOST, server->local_port);
|
||||
if (server->control_socket == INVALID_SOCKET) {
|
||||
if (server->control_socket == SC_INVALID_SOCKET) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -510,14 +511,14 @@ server_connect_to(struct server *server, char *device_name, struct size *size) {
|
|||
|
||||
void
|
||||
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)) {
|
||||
close_socket(server->server_socket);
|
||||
}
|
||||
if (server->video_socket != INVALID_SOCKET) {
|
||||
if (server->video_socket != SC_INVALID_SOCKET) {
|
||||
close_socket(server->video_socket);
|
||||
}
|
||||
if (server->control_socket != INVALID_SOCKET) {
|
||||
if (server->control_socket != SC_INVALID_SOCKET) {
|
||||
close_socket(server->control_socket);
|
||||
}
|
||||
|
||||
|
|
|
@ -24,9 +24,9 @@ struct server {
|
|||
sc_cond process_terminated_cond;
|
||||
bool process_terminated;
|
||||
|
||||
socket_t server_socket; // only used if !tunnel_forward
|
||||
socket_t video_socket;
|
||||
socket_t control_socket;
|
||||
sc_socket server_socket; // only used if !tunnel_forward
|
||||
sc_socket video_socket;
|
||||
sc_socket control_socket;
|
||||
uint16_t local_port; // selected from port_range
|
||||
bool tunnel_enabled;
|
||||
bool tunnel_forward; // use "adb forward" instead of "adb reverse"
|
||||
|
|
|
@ -260,7 +260,7 @@ end:
|
|||
}
|
||||
|
||||
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) {
|
||||
stream->socket = socket;
|
||||
stream->pending = NULL;
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#define STREAM_MAX_SINKS 2
|
||||
|
||||
struct stream {
|
||||
socket_t socket;
|
||||
sc_socket socket;
|
||||
sc_thread thread;
|
||||
|
||||
struct sc_packet_sink *sinks[STREAM_MAX_SINKS];
|
||||
|
@ -35,7 +35,7 @@ struct stream_callbacks {
|
|||
};
|
||||
|
||||
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);
|
||||
|
||||
void
|
||||
|
|
|
@ -55,12 +55,12 @@ net_perror(const char *s) {
|
|||
#endif
|
||||
}
|
||||
|
||||
socket_t
|
||||
sc_socket
|
||||
net_connect(uint32_t addr, uint16_t port) {
|
||||
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
sc_socket sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == SC_INVALID_SOCKET) {
|
||||
net_perror("socket");
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
|
@ -71,18 +71,18 @@ net_connect(uint32_t addr, uint16_t port) {
|
|||
if (connect(sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("connect");
|
||||
net_close(sock);
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
socket_t
|
||||
sc_socket
|
||||
net_listen(uint32_t addr, uint16_t port, int backlog) {
|
||||
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
sc_socket sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock == SC_INVALID_SOCKET) {
|
||||
net_perror("socket");
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
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) {
|
||||
net_perror("bind");
|
||||
net_close(sock);
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (listen(sock, backlog) == SOCKET_ERROR) {
|
||||
net_perror("listen");
|
||||
net_close(sock);
|
||||
return INVALID_SOCKET;
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
return sock;
|
||||
}
|
||||
|
||||
socket_t
|
||||
net_accept(socket_t server_socket) {
|
||||
sc_socket
|
||||
net_accept(sc_socket server_socket) {
|
||||
SOCKADDR_IN csin;
|
||||
socklen_t sinsize = sizeof(csin);
|
||||
return accept(server_socket, (SOCKADDR *) &csin, &sinsize);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
while (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
|
||||
net_shutdown(socket_t socket, int how) {
|
||||
net_shutdown(sc_socket socket, int how) {
|
||||
return !shutdown(socket, how);
|
||||
}
|
||||
|
||||
bool
|
||||
net_close(socket_t socket) {
|
||||
net_close(sc_socket socket) {
|
||||
#ifdef __WINDOWS__
|
||||
return !closesocket(socket);
|
||||
#else
|
||||
|
|
|
@ -8,15 +8,19 @@
|
|||
#include <SDL2/SDL_platform.h>
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
# include <winsock2.h>
|
||||
#define SHUT_RD SD_RECEIVE
|
||||
#define SHUT_WR SD_SEND
|
||||
#define SHUT_RDWR SD_BOTH
|
||||
typedef SOCKET socket_t;
|
||||
#else
|
||||
# define SHUT_RD SD_RECEIVE
|
||||
# define SHUT_WR SD_SEND
|
||||
# define SHUT_RDWR SD_BOTH
|
||||
# define SC_INVALID_SOCKET INVALID_SOCKET
|
||||
typedef SOCKET sc_socket;
|
||||
|
||||
#else // not __WINDOWS__
|
||||
|
||||
# include <sys/socket.h>
|
||||
# define INVALID_SOCKET -1
|
||||
typedef int socket_t;
|
||||
# define SC_INVALID_SOCKET -1
|
||||
typedef int sc_socket;
|
||||
#endif
|
||||
|
||||
bool
|
||||
|
@ -25,33 +29,33 @@ net_init(void);
|
|||
void
|
||||
net_cleanup(void);
|
||||
|
||||
socket_t
|
||||
sc_socket
|
||||
net_connect(uint32_t addr, uint16_t port);
|
||||
|
||||
socket_t
|
||||
sc_socket
|
||||
net_listen(uint32_t addr, uint16_t port, int backlog);
|
||||
|
||||
socket_t
|
||||
net_accept(socket_t server_socket);
|
||||
sc_socket
|
||||
net_accept(sc_socket server_socket);
|
||||
|
||||
// the _all versions wait/retry until len bytes have been written/read
|
||||
ssize_t
|
||||
net_recv(socket_t socket, void *buf, size_t len);
|
||||
net_recv(sc_socket socket, void *buf, size_t len);
|
||||
|
||||
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
|
||||
net_send(socket_t socket, const void *buf, size_t len);
|
||||
net_send(sc_socket socket, const void *buf, size_t len);
|
||||
|
||||
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)
|
||||
bool
|
||||
net_shutdown(socket_t socket, int how);
|
||||
net_shutdown(sc_socket socket, int how);
|
||||
|
||||
bool
|
||||
net_close(socket_t socket);
|
||||
net_close(sc_socket socket);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue