mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-04-21 12:05:00 +00:00
Split socket creation and connect/listen
This will allow to assign the socket to a variable before connecting or listening, so that it can be interrupted from another thread.
This commit is contained in:
parent
58ea238fb2
commit
59d1b0a269
3 changed files with 49 additions and 32 deletions
|
@ -109,10 +109,10 @@ disable_tunnel(struct server *server) {
|
|||
return disable_tunnel_reverse(server->serial);
|
||||
}
|
||||
|
||||
static sc_socket
|
||||
listen_on_port(uint16_t port) {
|
||||
static bool
|
||||
listen_on_port(sc_socket socket, uint16_t port) {
|
||||
#define IPV4_LOCALHOST 0x7F000001
|
||||
return net_listen(IPV4_LOCALHOST, port, 1);
|
||||
return net_listen(socket, IPV4_LOCALHOST, port, 1);
|
||||
}
|
||||
|
||||
static bool
|
||||
|
@ -131,11 +131,17 @@ enable_tunnel_reverse_any_port(struct server *server,
|
|||
// client can listen before starting the server app, so there is no
|
||||
// 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 != SC_INVALID_SOCKET) {
|
||||
// success
|
||||
server->local_port = port;
|
||||
return true;
|
||||
sc_socket server_socket = net_socket();
|
||||
if (server_socket != SC_INVALID_SOCKET) {
|
||||
bool ok = listen_on_port(server_socket, port);
|
||||
if (ok) {
|
||||
// success
|
||||
server->server_socket = server_socket;
|
||||
server->local_port = port;
|
||||
return true;
|
||||
}
|
||||
|
||||
net_close(server_socket);
|
||||
}
|
||||
|
||||
// failure, disable tunnel and try another port
|
||||
|
@ -291,11 +297,17 @@ execute_server(struct server *server, const struct server_params *params) {
|
|||
|
||||
static sc_socket
|
||||
connect_and_read_byte(uint16_t port) {
|
||||
sc_socket socket = net_connect(IPV4_LOCALHOST, port);
|
||||
sc_socket socket = net_socket();
|
||||
if (socket == SC_INVALID_SOCKET) {
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
bool ok = net_connect(socket, IPV4_LOCALHOST, port);
|
||||
if (!ok) {
|
||||
net_close(socket);
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
|
||||
char byte;
|
||||
// the connection may succeed even if the server behind the "adb tunnel"
|
||||
// is not listening, so read one byte to detect a working connection
|
||||
|
@ -477,11 +489,16 @@ server_connect_to(struct server *server, char *device_name,
|
|||
}
|
||||
|
||||
// we know that the device is listening, we don't need several attempts
|
||||
server->control_socket =
|
||||
net_connect(IPV4_LOCALHOST, server->local_port);
|
||||
server->control_socket = net_socket();
|
||||
if (server->control_socket == SC_INVALID_SOCKET) {
|
||||
return false;
|
||||
}
|
||||
bool ok = net_connect(server->control_socket, IPV4_LOCALHOST,
|
||||
server->local_port);
|
||||
if (!ok) {
|
||||
net_close(server->control_socket);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// we don't need the adb tunnel anymore
|
||||
|
|
|
@ -94,13 +94,18 @@ net_perror(const char *s) {
|
|||
}
|
||||
|
||||
sc_socket
|
||||
net_connect(uint32_t addr, uint16_t port) {
|
||||
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) {
|
||||
net_perror("socket");
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
bool
|
||||
net_connect(sc_socket socket, uint32_t addr, uint16_t port) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
|
||||
SOCKADDR_IN sin;
|
||||
sin.sin_family = AF_INET;
|
||||
|
@ -109,21 +114,15 @@ net_connect(uint32_t addr, uint16_t port) {
|
|||
|
||||
if (connect(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("connect");
|
||||
net_close(sock);
|
||||
return SC_INVALID_SOCKET;
|
||||
return false;
|
||||
}
|
||||
|
||||
return sock;
|
||||
return true;
|
||||
}
|
||||
|
||||
sc_socket
|
||||
net_listen(uint32_t addr, uint16_t port, int backlog) {
|
||||
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
sc_socket sock = wrap(raw_sock);
|
||||
if (sock == SC_INVALID_SOCKET) {
|
||||
net_perror("socket");
|
||||
return SC_INVALID_SOCKET;
|
||||
}
|
||||
bool
|
||||
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog) {
|
||||
sc_raw_socket raw_sock = unwrap(socket);
|
||||
|
||||
int reuse = 1;
|
||||
if (setsockopt(raw_sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
||||
|
@ -138,17 +137,15 @@ net_listen(uint32_t addr, uint16_t port, int backlog) {
|
|||
|
||||
if (bind(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||
net_perror("bind");
|
||||
net_close(sock);
|
||||
return SC_INVALID_SOCKET;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
||||
net_perror("listen");
|
||||
net_close(sock);
|
||||
return SC_INVALID_SOCKET;
|
||||
return false;
|
||||
}
|
||||
|
||||
return sock;
|
||||
return true;
|
||||
}
|
||||
|
||||
sc_socket
|
||||
|
|
|
@ -31,10 +31,13 @@ void
|
|||
net_cleanup(void);
|
||||
|
||||
sc_socket
|
||||
net_connect(uint32_t addr, uint16_t port);
|
||||
net_socket(void);
|
||||
|
||||
sc_socket
|
||||
net_listen(uint32_t addr, uint16_t port, int backlog);
|
||||
bool
|
||||
net_connect(sc_socket socket, uint32_t addr, uint16_t port);
|
||||
|
||||
bool
|
||||
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog);
|
||||
|
||||
sc_socket
|
||||
net_accept(sc_socket server_socket);
|
||||
|
|
Loading…
Add table
Reference in a new issue