Refactor server tunnel initialization

Start the server socket in enable_tunnel() directly.

For the caller point of view, enabling the tunnel opens a port (either
the server socket locally or the "adb forward" process).
This commit is contained in:
Romain Vimont 2019-12-09 22:10:42 +01:00
parent d1a9a76cc6
commit ca0031cbde

View file

@ -143,9 +143,24 @@ listen_on_port(uint16_t port) {
static bool static bool
enable_tunnel(struct server *server) { enable_tunnel(struct server *server) {
if (enable_tunnel_reverse(server->serial, server->local_port)) { if (enable_tunnel_reverse(server->serial, server->local_port)) {
// At the application level, the device part is "the server" because it
// serves video stream and control. However, at the network level, the
// client listens and the server connects to the client. That way, the
// 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(server->local_port);
if (server->server_socket == INVALID_SOCKET) {
LOGE("Could not listen on port %" PRIu16, server->local_port);
disable_tunnel(server);
return false;
}
return true; return true;
} }
// if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to
// "adb forward", so the app socket is the client
LOGW("'adb reverse' failed, fallback to 'adb forward'"); LOGW("'adb reverse' failed, fallback to 'adb forward'");
server->tunnel_forward = true; server->tunnel_forward = true;
return enable_tunnel_forward(server->serial, server->local_port); return enable_tunnel_forward(server->serial, server->local_port);
@ -265,25 +280,6 @@ server_start(struct server *server, const char *serial,
return false; return false;
} }
// if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to
// "adb forward", so the app socket is the client
if (!server->tunnel_forward) {
// At the application level, the device part is "the server" because it
// serves video stream and control. However, at the network level, the
// client listens and the server connects to the client. That way, the
// 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(params->local_port);
if (server->server_socket == INVALID_SOCKET) {
LOGE("Could not listen on port %" PRIu16, params->local_port);
disable_tunnel(server);
SDL_free(server->serial);
return false;
}
}
// server will connect to our server socket // server will connect to our server socket
server->process = execute_server(server, params); server->process = execute_server(server, params);