mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-03 14:49:29 +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);
|
return disable_tunnel_reverse(server->serial);
|
||||||
}
|
}
|
||||||
|
|
||||||
static sc_socket
|
static bool
|
||||||
listen_on_port(uint16_t port) {
|
listen_on_port(sc_socket socket, uint16_t port) {
|
||||||
#define IPV4_LOCALHOST 0x7F000001
|
#define IPV4_LOCALHOST 0x7F000001
|
||||||
return net_listen(IPV4_LOCALHOST, port, 1);
|
return net_listen(socket, IPV4_LOCALHOST, port, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
|
@ -131,13 +131,19 @@ enable_tunnel_reverse_any_port(struct server *server,
|
||||||
// client can listen before starting the server app, so there is no
|
// 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
|
// need to try to connect until the server socket is listening on the
|
||||||
// device.
|
// device.
|
||||||
server->server_socket = listen_on_port(port);
|
sc_socket server_socket = net_socket();
|
||||||
if (server->server_socket != SC_INVALID_SOCKET) {
|
if (server_socket != SC_INVALID_SOCKET) {
|
||||||
|
bool ok = listen_on_port(server_socket, port);
|
||||||
|
if (ok) {
|
||||||
// success
|
// success
|
||||||
|
server->server_socket = server_socket;
|
||||||
server->local_port = port;
|
server->local_port = port;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
net_close(server_socket);
|
||||||
|
}
|
||||||
|
|
||||||
// failure, disable tunnel and try another port
|
// failure, disable tunnel and try another port
|
||||||
if (!disable_tunnel_reverse(server->serial)) {
|
if (!disable_tunnel_reverse(server->serial)) {
|
||||||
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
||||||
|
@ -291,11 +297,17 @@ execute_server(struct server *server, const struct server_params *params) {
|
||||||
|
|
||||||
static sc_socket
|
static sc_socket
|
||||||
connect_and_read_byte(uint16_t port) {
|
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) {
|
if (socket == SC_INVALID_SOCKET) {
|
||||||
return 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;
|
char byte;
|
||||||
// the connection may succeed even if the server behind the "adb tunnel"
|
// the connection may succeed even if the server behind the "adb tunnel"
|
||||||
// is not listening, so read one byte to detect a working connection
|
// 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
|
// we know that the device is listening, we don't need several attempts
|
||||||
server->control_socket =
|
server->control_socket = net_socket();
|
||||||
net_connect(IPV4_LOCALHOST, server->local_port);
|
|
||||||
if (server->control_socket == SC_INVALID_SOCKET) {
|
if (server->control_socket == SC_INVALID_SOCKET) {
|
||||||
return false;
|
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
|
// we don't need the adb tunnel anymore
|
||||||
|
|
|
@ -94,13 +94,18 @@ net_perror(const char *s) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_socket
|
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_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
sc_socket sock = wrap(raw_sock);
|
sc_socket sock = wrap(raw_sock);
|
||||||
if (sock == SC_INVALID_SOCKET) {
|
if (sock == SC_INVALID_SOCKET) {
|
||||||
net_perror("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;
|
SOCKADDR_IN sin;
|
||||||
sin.sin_family = AF_INET;
|
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) {
|
if (connect(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||||
net_perror("connect");
|
net_perror("connect");
|
||||||
net_close(sock);
|
return false;
|
||||||
return SC_INVALID_SOCKET;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sock;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_socket
|
bool
|
||||||
net_listen(uint32_t addr, uint16_t port, int backlog) {
|
net_listen(sc_socket socket, uint32_t addr, uint16_t port, int backlog) {
|
||||||
sc_raw_socket raw_sock = socket(AF_INET, SOCK_STREAM, 0);
|
sc_raw_socket raw_sock = unwrap(socket);
|
||||||
sc_socket sock = wrap(raw_sock);
|
|
||||||
if (sock == SC_INVALID_SOCKET) {
|
|
||||||
net_perror("socket");
|
|
||||||
return SC_INVALID_SOCKET;
|
|
||||||
}
|
|
||||||
|
|
||||||
int reuse = 1;
|
int reuse = 1;
|
||||||
if (setsockopt(raw_sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
|
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) {
|
if (bind(raw_sock, (SOCKADDR *) &sin, sizeof(sin)) == SOCKET_ERROR) {
|
||||||
net_perror("bind");
|
net_perror("bind");
|
||||||
net_close(sock);
|
return false;
|
||||||
return SC_INVALID_SOCKET;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
if (listen(raw_sock, backlog) == SOCKET_ERROR) {
|
||||||
net_perror("listen");
|
net_perror("listen");
|
||||||
net_close(sock);
|
return false;
|
||||||
return SC_INVALID_SOCKET;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return sock;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
|
|
|
@ -31,10 +31,13 @@ void
|
||||||
net_cleanup(void);
|
net_cleanup(void);
|
||||||
|
|
||||||
sc_socket
|
sc_socket
|
||||||
net_connect(uint32_t addr, uint16_t port);
|
net_socket(void);
|
||||||
|
|
||||||
sc_socket
|
bool
|
||||||
net_listen(uint32_t addr, uint16_t port, int backlog);
|
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
|
sc_socket
|
||||||
net_accept(sc_socket server_socket);
|
net_accept(sc_socket server_socket);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue