2018-02-08 22:16:27 +08:00
|
|
|
#include "server.h"
|
2018-01-22 18:22:31 +08:00
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2018-01-22 18:22:31 +08:00
|
|
|
#include <errno.h>
|
Replace SDL_net by custom implementation
SDL_net is not very suitable for scrcpy.
For example, SDLNet_TCP_Accept() is non-blocking, so we have to wrap it
by calling many SDL_Net-specific functions to make it blocking.
But above all, SDLNet_TCP_Open() is a server socket only when no IP is
provided; otherwise, it's a client socket. Therefore, it is not possible
to create a server socket bound to localhost, so it accepts connections
from anywhere.
This is a problem for scrcpy, because on start, the application listens
for nearly 1 second until it accepts the first connection, supposedly
from the device. If someone on the local network manages to connect to
the server socket first, then they can stream arbitrary H.264 video.
This may be troublesome, for example during a public presentation ;-)
Provide our own simplified API (net.h) instead, implemented for the
different platforms.
2018-02-16 05:59:21 +08:00
|
|
|
#include <inttypes.h>
|
2019-06-10 21:14:10 +08:00
|
|
|
#include <libgen.h>
|
2018-05-13 21:33:13 +08:00
|
|
|
#include <stdio.h>
|
2018-03-12 15:35:51 +08:00
|
|
|
#include <SDL2/SDL_timer.h>
|
2019-12-14 14:34:49 +08:00
|
|
|
#include <SDL2/SDL_platform.h>
|
2018-02-13 17:10:18 +08:00
|
|
|
|
2018-02-13 18:55:12 +08:00
|
|
|
#include "config.h"
|
2019-06-10 21:14:10 +08:00
|
|
|
#include "command.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
|
|
|
#include "util/net.h"
|
2019-12-14 14:34:49 +08:00
|
|
|
#include "util/str_util.h"
|
2018-01-22 18:22:31 +08:00
|
|
|
|
2018-01-23 22:46:34 +08:00
|
|
|
#define SOCKET_NAME "scrcpy"
|
2019-10-31 06:40:10 +08:00
|
|
|
#define SERVER_FILENAME "scrcpy-server"
|
2018-01-23 22:46:34 +08:00
|
|
|
|
2019-06-12 17:14:08 +08:00
|
|
|
#define DEFAULT_SERVER_PATH PREFIX "/share/scrcpy/" SERVER_FILENAME
|
2019-11-22 22:23:57 +08:00
|
|
|
#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
|
2018-02-28 22:13:56 +08:00
|
|
|
|
2019-12-15 01:13:56 +08:00
|
|
|
static char *
|
2019-03-03 03:09:56 +08:00
|
|
|
get_server_path(void) {
|
2019-12-14 14:34:49 +08:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
const wchar_t *server_path_env = _wgetenv(L"SCRCPY_SERVER_PATH");
|
|
|
|
#else
|
2019-06-10 21:14:10 +08:00
|
|
|
const char *server_path_env = getenv("SCRCPY_SERVER_PATH");
|
2019-12-14 14:34:49 +08:00
|
|
|
#endif
|
2019-06-10 21:14:10 +08:00
|
|
|
if (server_path_env) {
|
|
|
|
// if the envvar is set, use it
|
2019-12-14 14:34:49 +08:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
char *server_path = utf8_from_wide_char(server_path_env);
|
|
|
|
#else
|
2019-12-15 01:13:56 +08:00
|
|
|
char *server_path = SDL_strdup(server_path_env);
|
2019-12-14 14:34:49 +08:00
|
|
|
#endif
|
2019-12-15 01:13:56 +08:00
|
|
|
if (!server_path) {
|
|
|
|
LOGE("Could not allocate memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
|
|
|
|
return server_path;
|
2019-06-10 21:14:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef PORTABLE
|
|
|
|
LOGD("Using server: " DEFAULT_SERVER_PATH);
|
2019-12-15 01:13:56 +08:00
|
|
|
char *server_path = SDL_strdup(DEFAULT_SERVER_PATH);
|
|
|
|
if (!server_path) {
|
|
|
|
LOGE("Could not allocate memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-06-10 21:14:10 +08:00
|
|
|
// the absolute path is hardcoded
|
2019-12-15 01:13:56 +08:00
|
|
|
return server_path;
|
2019-06-10 21:14:10 +08:00
|
|
|
#else
|
2019-12-15 01:13:56 +08:00
|
|
|
|
2019-10-31 06:40:10 +08:00
|
|
|
// use scrcpy-server in the same directory as the executable
|
2019-06-10 21:14:10 +08:00
|
|
|
char *executable_path = get_executable_path();
|
|
|
|
if (!executable_path) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGE("Could not get executable path, "
|
2019-06-10 21:14:10 +08:00
|
|
|
"using " SERVER_FILENAME " from current directory");
|
|
|
|
// not found, use current directory
|
|
|
|
return SERVER_FILENAME;
|
|
|
|
}
|
|
|
|
char *dir = dirname(executable_path);
|
|
|
|
size_t dirlen = strlen(dir);
|
|
|
|
|
|
|
|
// sizeof(SERVER_FILENAME) gives statically the size including the null byte
|
|
|
|
size_t len = dirlen + 1 + sizeof(SERVER_FILENAME);
|
|
|
|
char *server_path = SDL_malloc(len);
|
2018-02-02 16:31:44 +08:00
|
|
|
if (!server_path) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGE("Could not alloc server path string, "
|
2019-06-10 21:14:10 +08:00
|
|
|
"using " SERVER_FILENAME " from current directory");
|
|
|
|
SDL_free(executable_path);
|
|
|
|
return SERVER_FILENAME;
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
2019-06-10 21:14:10 +08:00
|
|
|
|
|
|
|
memcpy(server_path, dir, dirlen);
|
|
|
|
server_path[dirlen] = PATH_SEPARATOR;
|
|
|
|
memcpy(&server_path[dirlen + 1], SERVER_FILENAME, sizeof(SERVER_FILENAME));
|
|
|
|
// the final null byte has been copied with SERVER_FILENAME
|
|
|
|
|
|
|
|
SDL_free(executable_path);
|
|
|
|
|
|
|
|
LOGD("Using server (portable): %s", server_path);
|
2018-02-13 18:55:12 +08:00
|
|
|
return server_path;
|
2019-06-10 21:14:10 +08:00
|
|
|
#endif
|
2018-02-13 18:55:12 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
push_server(const char *serial) {
|
2019-12-15 01:13:56 +08:00
|
|
|
char *server_path = get_server_path();
|
|
|
|
if (!server_path) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-06 04:07:11 +08:00
|
|
|
if (!is_regular_file(server_path)) {
|
|
|
|
LOGE("'%s' does not exist or is not a regular file\n", server_path);
|
2019-12-15 01:13:56 +08:00
|
|
|
SDL_free(server_path);
|
2019-12-06 04:07:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
process_t process = adb_push(serial, server_path, DEVICE_SERVER_PATH);
|
2019-12-15 01:13:56 +08:00
|
|
|
SDL_free(server_path);
|
2018-02-08 22:16:27 +08:00
|
|
|
return process_check_success(process, "adb push");
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
|
|
|
enable_tunnel_reverse(const char *serial, uint16_t local_port) {
|
2018-02-08 22:16:27 +08:00
|
|
|
process_t process = adb_reverse(serial, SOCKET_NAME, local_port);
|
|
|
|
return process_check_success(process, "adb reverse");
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
disable_tunnel_reverse(const char *serial) {
|
2018-02-08 22:16:27 +08:00
|
|
|
process_t process = adb_reverse_remove(serial, SOCKET_NAME);
|
|
|
|
return process_check_success(process, "adb reverse --remove");
|
2018-01-23 22:46:34 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
|
|
|
enable_tunnel_forward(const char *serial, uint16_t local_port) {
|
2018-03-12 15:35:51 +08:00
|
|
|
process_t process = adb_forward(serial, local_port, SOCKET_NAME);
|
|
|
|
return process_check_success(process, "adb forward");
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
|
|
|
disable_tunnel_forward(const char *serial, uint16_t local_port) {
|
2018-03-12 15:35:51 +08:00
|
|
|
process_t process = adb_forward_remove(serial, local_port);
|
|
|
|
return process_check_success(process, "adb forward --remove");
|
|
|
|
}
|
|
|
|
|
2019-12-10 05:14:43 +08:00
|
|
|
static bool
|
|
|
|
disable_tunnel(struct server *server) {
|
|
|
|
if (server->tunnel_forward) {
|
|
|
|
return disable_tunnel_forward(server->serial, server->local_port);
|
|
|
|
}
|
|
|
|
return disable_tunnel_reverse(server->serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
static socket_t
|
|
|
|
listen_on_port(uint16_t port) {
|
|
|
|
#define IPV4_LOCALHOST 0x7F000001
|
|
|
|
return net_listen(IPV4_LOCALHOST, port, 1);
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-12-10 04:16:09 +08:00
|
|
|
enable_tunnel_reverse_any_port(struct server *server,
|
|
|
|
struct port_range port_range) {
|
|
|
|
uint16_t port = port_range.first;
|
|
|
|
for (;;) {
|
|
|
|
if (!enable_tunnel_reverse(server->serial, port)) {
|
|
|
|
// the command itself failed, it will fail on any port
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-10 05:10:42 +08:00
|
|
|
// 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.
|
2019-12-10 04:16:09 +08:00
|
|
|
server->server_socket = listen_on_port(port);
|
|
|
|
if (server->server_socket != INVALID_SOCKET) {
|
|
|
|
// success
|
|
|
|
server->local_port = port;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// failure, disable tunnel and try another port
|
|
|
|
if (!disable_tunnel_reverse(server->serial)) {
|
|
|
|
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check before incrementing to avoid overflow on port 65535
|
|
|
|
if (port < port_range.last) {
|
|
|
|
LOGW("Could not listen on port %" PRIu16", retrying on %" PRIu16,
|
|
|
|
port, port + 1);
|
|
|
|
port++;
|
|
|
|
continue;
|
2019-12-10 05:10:42 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 04:16:09 +08:00
|
|
|
if (port_range.first == port_range.last) {
|
|
|
|
LOGE("Could not listen on port %" PRIu16, port_range.first);
|
|
|
|
} else {
|
|
|
|
LOGE("Could not listen on any port in range %" PRIu16 ":%" PRIu16,
|
|
|
|
port_range.first, port_range.last);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
enable_tunnel_forward_any_port(struct server *server,
|
|
|
|
struct port_range port_range) {
|
|
|
|
server->tunnel_forward = true;
|
|
|
|
uint16_t port = port_range.first;
|
|
|
|
for (;;) {
|
|
|
|
if (enable_tunnel_forward(server->serial, port)) {
|
|
|
|
// success
|
|
|
|
server->local_port = port;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port < port_range.last) {
|
|
|
|
LOGW("Could not forward port %" PRIu16", retrying on %" PRIu16,
|
|
|
|
port, port + 1);
|
|
|
|
port++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port_range.first == port_range.last) {
|
|
|
|
LOGE("Could not forward port %" PRIu16, port_range.first);
|
|
|
|
} else {
|
|
|
|
LOGE("Could not forward any port in range %" PRIu16 ":%" PRIu16,
|
|
|
|
port_range.first, port_range.last);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
enable_tunnel_any_port(struct server *server, struct port_range port_range) {
|
|
|
|
if (enable_tunnel_reverse_any_port(server, port_range)) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-03-12 15:35:51 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 05:10:42 +08:00
|
|
|
// if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to
|
|
|
|
// "adb forward", so the app socket is the client
|
2019-12-10 04:16:09 +08:00
|
|
|
|
2018-03-12 15:35:51 +08:00
|
|
|
LOGW("'adb reverse' failed, fallback to 'adb forward'");
|
2019-12-10 04:16:09 +08:00
|
|
|
return enable_tunnel_forward_any_port(server, port_range);
|
2018-03-12 15:35:51 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static process_t
|
2019-06-05 05:59:55 +08:00
|
|
|
execute_server(struct server *server, const struct server_params *params) {
|
2018-02-01 19:18:06 +08:00
|
|
|
char max_size_string[6];
|
2018-02-01 23:36:50 +08:00
|
|
|
char bit_rate_string[11];
|
2019-11-18 05:07:19 +08:00
|
|
|
char max_fps_string[6];
|
2020-02-16 19:30:36 +08:00
|
|
|
char lock_video_orientation_string[3];
|
2019-06-05 05:59:55 +08:00
|
|
|
sprintf(max_size_string, "%"PRIu16, params->max_size);
|
|
|
|
sprintf(bit_rate_string, "%"PRIu32, params->bit_rate);
|
2019-11-18 05:07:19 +08:00
|
|
|
sprintf(max_fps_string, "%"PRIu16, params->max_fps);
|
2020-02-16 19:30:36 +08:00
|
|
|
sprintf(lock_video_orientation_string, "%"PRIi8, params->lock_video_orientation);
|
2018-01-22 18:22:31 +08:00
|
|
|
const char *const cmd[] = {
|
|
|
|
"shell",
|
2019-11-22 22:16:00 +08:00
|
|
|
"CLASSPATH=" DEVICE_SERVER_PATH,
|
2018-01-22 18:22:31 +08:00
|
|
|
"app_process",
|
2019-11-04 02:31:56 +08:00
|
|
|
#ifdef SERVER_DEBUGGER
|
|
|
|
# define SERVER_DEBUGGER_PORT "5005"
|
2020-03-20 02:15:43 +08:00
|
|
|
# ifdef SERVER_DEBUGGER_METHOD_NEW
|
|
|
|
/* Android 9 and above */
|
|
|
|
"-XjdwpProvider:internal -XjdwpOptions:transport=dt_socket,suspend=y,server=y,address="
|
|
|
|
# else
|
|
|
|
/* Android 8 and below */
|
2019-11-04 02:31:56 +08:00
|
|
|
"-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address="
|
2020-03-20 02:15:43 +08:00
|
|
|
# endif
|
2019-11-04 02:31:56 +08:00
|
|
|
SERVER_DEBUGGER_PORT,
|
|
|
|
#endif
|
2018-01-30 00:06:44 +08:00
|
|
|
"/", // unused
|
2018-02-28 21:57:18 +08:00
|
|
|
"com.genymobile.scrcpy.Server",
|
2019-11-10 20:23:58 +08:00
|
|
|
SCRCPY_VERSION,
|
2018-02-01 19:18:06 +08:00
|
|
|
max_size_string,
|
2018-02-01 23:36:50 +08:00
|
|
|
bit_rate_string,
|
2019-11-18 05:07:19 +08:00
|
|
|
max_fps_string,
|
2020-02-16 19:30:36 +08:00
|
|
|
lock_video_orientation_string,
|
2019-06-05 05:59:55 +08:00
|
|
|
server->tunnel_forward ? "true" : "false",
|
|
|
|
params->crop ? params->crop : "-",
|
2019-07-31 07:55:32 +08:00
|
|
|
"true", // always send frame meta (packet boundaries + timestamp)
|
2019-06-05 03:31:46 +08:00
|
|
|
params->control ? "true" : "false",
|
2018-01-22 18:22:31 +08:00
|
|
|
};
|
2019-11-04 02:31:56 +08:00
|
|
|
#ifdef SERVER_DEBUGGER
|
|
|
|
LOGI("Server debugger waiting for a client on device port "
|
|
|
|
SERVER_DEBUGGER_PORT "...");
|
|
|
|
// From the computer, run
|
|
|
|
// adb forward tcp:5005 tcp:5005
|
|
|
|
// Then, from Android Studio: Run > Debug > Edit configurations...
|
|
|
|
// On the left, click on '+', "Remote", with:
|
|
|
|
// Host: localhost
|
|
|
|
// Port: 5005
|
|
|
|
// Then click on "Debug"
|
|
|
|
#endif
|
2019-06-05 05:59:55 +08:00
|
|
|
return adb_execute(server->serial, cmd, sizeof(cmd) / sizeof(cmd[0]));
|
2018-01-22 18:22:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static socket_t
|
2019-03-03 06:52:22 +08:00
|
|
|
connect_and_read_byte(uint16_t port) {
|
2018-03-12 15:35:51 +08:00
|
|
|
socket_t socket = net_connect(IPV4_LOCALHOST, port);
|
|
|
|
if (socket == INVALID_SOCKET) {
|
|
|
|
return 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
|
2019-05-29 03:03:44 +08:00
|
|
|
if (net_recv(socket, &byte, 1) != 1) {
|
2018-03-12 15:35:51 +08:00
|
|
|
// the server is not listening yet behind the adb tunnel
|
2019-06-20 18:15:45 +08:00
|
|
|
net_close(socket);
|
2018-03-12 15:35:51 +08:00
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static socket_t
|
2019-03-03 06:52:22 +08:00
|
|
|
connect_to_server(uint16_t port, uint32_t attempts, uint32_t delay) {
|
2018-03-12 15:35:51 +08:00
|
|
|
do {
|
|
|
|
LOGD("Remaining connection attempts: %d", (int) attempts);
|
|
|
|
socket_t socket = connect_and_read_byte(port);
|
|
|
|
if (socket != INVALID_SOCKET) {
|
|
|
|
// it worked!
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
if (attempts) {
|
|
|
|
SDL_Delay(delay);
|
|
|
|
}
|
|
|
|
} while (--attempts > 0);
|
|
|
|
return INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
close_socket(socket_t *socket) {
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(*socket != INVALID_SOCKET);
|
2018-03-05 22:09:26 +08:00
|
|
|
net_shutdown(*socket, SHUT_RDWR);
|
2018-02-16 21:55:33 +08:00
|
|
|
if (!net_close(*socket)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not close socket");
|
2018-02-16 21:55:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
*socket = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
server_init(struct server *server) {
|
2018-02-08 22:16:27 +08:00
|
|
|
*server = (struct server) SERVER_INITIALIZER;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
server_start(struct server *server, const char *serial,
|
2019-06-05 05:59:55 +08:00
|
|
|
const struct server_params *params) {
|
2019-12-10 04:16:09 +08:00
|
|
|
server->port_range = params->port_range;
|
2018-03-12 15:35:51 +08:00
|
|
|
|
2018-03-12 17:19:12 +08:00
|
|
|
if (serial) {
|
|
|
|
server->serial = SDL_strdup(serial);
|
2018-05-26 20:20:05 +08:00
|
|
|
if (!server->serial) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-05-26 20:20:05 +08:00
|
|
|
}
|
2018-03-12 17:19:12 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
if (!push_server(serial)) {
|
2019-03-03 07:01:16 +08:00
|
|
|
SDL_free(server->serial);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
|
2019-12-10 04:16:09 +08:00
|
|
|
if (!enable_tunnel_any_port(server, params->port_range)) {
|
2019-03-03 07:01:16 +08:00
|
|
|
SDL_free(server->serial);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// server will connect to our server socket
|
2019-06-05 05:59:55 +08:00
|
|
|
server->process = execute_server(server, params);
|
2018-11-11 21:41:54 +08:00
|
|
|
|
2018-02-08 22:16:27 +08:00
|
|
|
if (server->process == PROCESS_NONE) {
|
2018-03-12 15:35:51 +08:00
|
|
|
if (!server->tunnel_forward) {
|
|
|
|
close_socket(&server->server_socket);
|
|
|
|
}
|
|
|
|
disable_tunnel(server);
|
2019-05-24 23:15:32 +08:00
|
|
|
SDL_free(server->serial);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
server->tunnel_enabled = true;
|
2018-02-08 22:16:27 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
|
2019-05-28 19:41:19 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
server_connect_to(struct server *server) {
|
2018-03-12 15:35:51 +08:00
|
|
|
if (!server->tunnel_forward) {
|
2019-05-29 03:03:54 +08:00
|
|
|
server->video_socket = net_accept(server->server_socket);
|
|
|
|
if (server->video_socket == INVALID_SOCKET) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server->control_socket = net_accept(server->server_socket);
|
|
|
|
if (server->control_socket == INVALID_SOCKET) {
|
2019-11-15 16:44:24 +08:00
|
|
|
// the video_socket will be cleaned up on destroy
|
2019-05-29 03:02:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we don't need the server socket anymore
|
|
|
|
close_socket(&server->server_socket);
|
2018-03-12 15:35:51 +08:00
|
|
|
} else {
|
2019-03-03 06:52:22 +08:00
|
|
|
uint32_t attempts = 100;
|
|
|
|
uint32_t delay = 100; // ms
|
2019-05-29 03:03:54 +08:00
|
|
|
server->video_socket =
|
|
|
|
connect_to_server(server->local_port, attempts, delay);
|
|
|
|
if (server->video_socket == 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) {
|
2019-05-29 03:02:57 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-12 15:35:51 +08:00
|
|
|
}
|
2018-02-09 00:38:38 +08:00
|
|
|
|
|
|
|
// we don't need the adb tunnel anymore
|
2018-03-12 15:35:51 +08:00
|
|
|
disable_tunnel(server); // ignore failure
|
2019-03-03 06:52:22 +08:00
|
|
|
server->tunnel_enabled = false;
|
2018-02-09 00:38:38 +08:00
|
|
|
|
2019-05-28 19:41:19 +08:00
|
|
|
return true;
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
server_stop(struct server *server) {
|
2019-05-30 17:40:12 +08:00
|
|
|
if (server->server_socket != INVALID_SOCKET) {
|
|
|
|
close_socket(&server->server_socket);
|
|
|
|
}
|
2019-05-29 03:03:54 +08:00
|
|
|
if (server->video_socket != INVALID_SOCKET) {
|
|
|
|
close_socket(&server->video_socket);
|
|
|
|
}
|
|
|
|
if (server->control_socket != INVALID_SOCKET) {
|
|
|
|
close_socket(&server->control_socket);
|
2019-05-30 17:40:12 +08:00
|
|
|
}
|
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(server->process != PROCESS_NONE);
|
2018-02-16 18:11:07 +08:00
|
|
|
|
2018-02-28 23:46:03 +08:00
|
|
|
if (!cmd_terminate(server->process)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not terminate server");
|
2018-02-16 18:11:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd_simple_wait(server->process, NULL); // ignore exit code
|
|
|
|
LOGD("Server terminated");
|
2018-02-08 22:16:27 +08:00
|
|
|
|
2018-03-12 15:35:51 +08:00
|
|
|
if (server->tunnel_enabled) {
|
2018-02-08 22:16:27 +08:00
|
|
|
// ignore failure
|
2018-03-12 15:35:51 +08:00
|
|
|
disable_tunnel(server);
|
2018-02-08 22:16:27 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-09 19:59:36 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
server_destroy(struct server *server) {
|
2019-03-03 07:01:16 +08:00
|
|
|
SDL_free(server->serial);
|
2018-02-09 19:59:36 +08:00
|
|
|
}
|