Use sc_ prefix for server

This commit is contained in:
Romain Vimont 2021-11-12 23:24:12 +01:00
parent 057c7a4df4
commit c1a34881d7
3 changed files with 73 additions and 71 deletions

View file

@ -34,7 +34,7 @@
#endif #endif
struct scrcpy { struct scrcpy {
struct server server; struct sc_server server;
struct screen screen; struct screen screen;
struct stream stream; struct stream stream;
struct decoder decoder; struct decoder decoder;
@ -286,7 +286,7 @@ stream_on_eos(struct stream *stream, void *userdata) {
} }
static void static void
server_on_connection_failed(struct server *server, void *userdata) { sc_server_on_connection_failed(struct sc_server *server, void *userdata) {
(void) server; (void) server;
(void) userdata; (void) userdata;
@ -294,7 +294,7 @@ server_on_connection_failed(struct server *server, void *userdata) {
} }
static void static void
server_on_connected(struct server *server, void *userdata) { sc_server_on_connected(struct sc_server *server, void *userdata) {
(void) server; (void) server;
(void) userdata; (void) userdata;
@ -302,7 +302,7 @@ server_on_connected(struct server *server, void *userdata) {
} }
static void static void
server_on_disconnected(struct server *server, void *userdata) { sc_server_on_disconnected(struct sc_server *server, void *userdata) {
(void) server; (void) server;
(void) userdata; (void) userdata;
@ -340,7 +340,7 @@ scrcpy(struct scrcpy_options *options) {
bool controller_started = false; bool controller_started = false;
bool screen_initialized = false; bool screen_initialized = false;
struct server_params params = { struct sc_server_params params = {
.serial = options->serial, .serial = options->serial,
.log_level = options->log_level, .log_level = options->log_level,
.crop = options->crop, .crop = options->crop,
@ -359,16 +359,16 @@ scrcpy(struct scrcpy_options *options) {
.power_off_on_close = options->power_off_on_close, .power_off_on_close = options->power_off_on_close,
}; };
static const struct server_callbacks cbs = { static const struct sc_server_callbacks cbs = {
.on_connection_failed = server_on_connection_failed, .on_connection_failed = sc_server_on_connection_failed,
.on_connected = server_on_connected, .on_connected = sc_server_on_connected,
.on_disconnected = server_on_disconnected, .on_disconnected = sc_server_on_disconnected,
}; };
if (!server_init(&s->server, &params, &cbs, NULL)) { if (!sc_server_init(&s->server, &params, &cbs, NULL)) {
return false; return false;
} }
if (!server_start(&s->server)) { if (!sc_server_start(&s->server)) {
goto end; goto end;
} }
@ -392,7 +392,7 @@ scrcpy(struct scrcpy_options *options) {
} }
// It is necessarily initialized here, since the device is connected // It is necessarily initialized here, since the device is connected
struct server_info *info = &s->server.info; struct sc_server_info *info = &s->server.info;
if (options->display && options->control) { if (options->display && options->control) {
if (!file_handler_init(&s->file_handler, options->serial, if (!file_handler_init(&s->file_handler, options->serial,
@ -608,7 +608,7 @@ end:
if (server_started) { if (server_started) {
// shutdown the sockets and kill the server // shutdown the sockets and kill the server
server_stop(&s->server); sc_server_stop(&s->server);
} }
// now that the sockets are shutdown, the stream and controller are // now that the sockets are shutdown, the stream and controller are
@ -653,7 +653,7 @@ end:
file_handler_destroy(&s->file_handler); file_handler_destroy(&s->file_handler);
} }
server_destroy(&s->server); sc_server_destroy(&s->server);
return ret; return ret;
} }

View file

@ -14,10 +14,10 @@
#include "util/process_intr.h" #include "util/process_intr.h"
#include "util/str.h" #include "util/str.h"
#define SERVER_FILENAME "scrcpy-server" #define SC_SERVER_FILENAME "scrcpy-server"
#define DEFAULT_SERVER_PATH PREFIX "/share/scrcpy/" SERVER_FILENAME #define SC_SERVER_PATH_DEFAULT PREFIX "/share/scrcpy/" SC_SERVER_FILENAME
#define DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar" #define SC_DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"
static char * static char *
get_server_path(void) { get_server_path(void) {
@ -42,18 +42,18 @@ get_server_path(void) {
} }
#ifndef PORTABLE #ifndef PORTABLE
LOGD("Using server: " DEFAULT_SERVER_PATH); LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
char *server_path = strdup(DEFAULT_SERVER_PATH); char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
if (!server_path) { if (!server_path) {
LOGE("Could not allocate memory"); LOGE("Could not allocate memory");
return NULL; return NULL;
} }
#else #else
char *server_path = sc_file_get_local_path(SERVER_FILENAME); char *server_path = sc_file_get_local_path(SC_SERVER_FILENAME);
if (!server_path) { if (!server_path) {
LOGE("Could not get local file path, " LOGE("Could not get local file path, "
"using " SERVER_FILENAME " from current directory"); "using " SC_SERVER_FILENAME " from current directory");
return strdup(SERVER_FILENAME); return strdup(SC_SERVER_FILENAME);
} }
LOGD("Using server (portable): %s", server_path); LOGD("Using server (portable): %s", server_path);
@ -63,7 +63,7 @@ get_server_path(void) {
} }
static void static void
server_params_destroy(struct server_params *params) { sc_server_params_destroy(struct sc_server_params *params) {
// The server stores a copy of the params provided by the user // The server stores a copy of the params provided by the user
free((char *) params->serial); free((char *) params->serial);
free((char *) params->crop); free((char *) params->crop);
@ -72,7 +72,8 @@ server_params_destroy(struct server_params *params) {
} }
static bool static bool
server_params_copy(struct server_params *dst, const struct server_params *src) { sc_server_params_copy(struct sc_server_params *dst,
const struct sc_server_params *src) {
*dst = *src; *dst = *src;
// The params reference user-allocated memory, so we must copy them to // The params reference user-allocated memory, so we must copy them to
@ -96,7 +97,7 @@ server_params_copy(struct server_params *dst, const struct server_params *src) {
return true; return true;
error: error:
server_params_destroy(dst); sc_server_params_destroy(dst);
return false; return false;
} }
@ -111,7 +112,7 @@ push_server(struct sc_intr *intr, const char *serial) {
free(server_path); free(server_path);
return false; return false;
} }
sc_pid pid = adb_push(serial, server_path, DEVICE_SERVER_PATH); sc_pid pid = adb_push(serial, server_path, SC_DEVICE_SERVER_PATH);
free(server_path); free(server_path);
return sc_process_check_success_intr(intr, pid, "adb push"); return sc_process_check_success_intr(intr, pid, "adb push");
} }
@ -136,7 +137,8 @@ log_level_to_server_string(enum sc_log_level level) {
} }
static sc_pid static sc_pid
execute_server(struct server *server, const struct server_params *params) { execute_server(struct sc_server *server,
const struct sc_server_params *params) {
const char *serial = server->params.serial; const char *serial = server->params.serial;
char max_size_string[6]; char max_size_string[6];
@ -152,7 +154,7 @@ execute_server(struct server *server, const struct server_params *params) {
sprintf(display_id_string, "%"PRIu32, params->display_id); sprintf(display_id_string, "%"PRIu32, params->display_id);
const char *const cmd[] = { const char *const cmd[] = {
"shell", "shell",
"CLASSPATH=" DEVICE_SERVER_PATH, "CLASSPATH=" SC_DEVICE_SERVER_PATH,
"app_process", "app_process",
#ifdef SERVER_DEBUGGER #ifdef SERVER_DEBUGGER
# define SERVER_DEBUGGER_PORT "5005" # define SERVER_DEBUGGER_PORT "5005"
@ -218,7 +220,7 @@ connect_and_read_byte(struct sc_intr *intr, sc_socket socket, uint16_t port) {
} }
static sc_socket static sc_socket
connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) { connect_to_server(struct sc_server *server, uint32_t attempts, sc_tick delay) {
uint16_t port = server->tunnel.local_port; uint16_t port = server->tunnel.local_port;
do { do {
LOGD("Remaining connection attempts: %d", (int) attempts); LOGD("Remaining connection attempts: %d", (int) attempts);
@ -253,9 +255,9 @@ connect_to_server(struct server *server, uint32_t attempts, sc_tick delay) {
} }
bool bool
server_init(struct server *server, const struct server_params *params, sc_server_init(struct sc_server *server, const struct sc_server_params *params,
const struct server_callbacks *cbs, void *cbs_userdata) { const struct sc_server_callbacks *cbs, void *cbs_userdata) {
bool ok = server_params_copy(&server->params, params); bool ok = sc_server_params_copy(&server->params, params);
if (!ok) { if (!ok) {
LOGE("Could not copy server params"); LOGE("Could not copy server params");
return false; return false;
@ -264,7 +266,7 @@ server_init(struct server *server, const struct server_params *params,
ok = sc_mutex_init(&server->mutex); ok = sc_mutex_init(&server->mutex);
if (!ok) { if (!ok) {
LOGE("Could not create server mutex"); LOGE("Could not create server mutex");
server_params_destroy(&server->params); sc_server_params_destroy(&server->params);
return false; return false;
} }
@ -272,7 +274,7 @@ server_init(struct server *server, const struct server_params *params,
if (!ok) { if (!ok) {
LOGE("Could not create server cond_stopped"); LOGE("Could not create server cond_stopped");
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);
server_params_destroy(&server->params); sc_server_params_destroy(&server->params);
return false; return false;
} }
@ -281,7 +283,7 @@ server_init(struct server *server, const struct server_params *params,
LOGE("Could not create intr"); LOGE("Could not create intr");
sc_cond_destroy(&server->cond_stopped); sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);
server_params_destroy(&server->params); sc_server_params_destroy(&server->params);
return false; return false;
} }
@ -305,26 +307,26 @@ server_init(struct server *server, const struct server_params *params,
static bool static bool
device_read_info(struct sc_intr *intr, sc_socket device_socket, device_read_info(struct sc_intr *intr, sc_socket device_socket,
struct server_info *info) { struct sc_server_info *info) {
unsigned char buf[DEVICE_NAME_FIELD_LENGTH + 4]; unsigned char buf[SC_DEVICE_NAME_FIELD_LENGTH + 4];
ssize_t r = net_recv_all_intr(intr, device_socket, buf, sizeof(buf)); ssize_t r = net_recv_all_intr(intr, device_socket, buf, sizeof(buf));
if (r < DEVICE_NAME_FIELD_LENGTH + 4) { if (r < SC_DEVICE_NAME_FIELD_LENGTH + 4) {
LOGE("Could not retrieve device information"); LOGE("Could not retrieve device information");
return false; return false;
} }
// in case the client sends garbage // in case the client sends garbage
buf[DEVICE_NAME_FIELD_LENGTH - 1] = '\0'; buf[SC_DEVICE_NAME_FIELD_LENGTH - 1] = '\0';
memcpy(info->device_name, (char *) buf, sizeof(info->device_name)); memcpy(info->device_name, (char *) buf, sizeof(info->device_name));
info->frame_size.width = (buf[DEVICE_NAME_FIELD_LENGTH] << 8) info->frame_size.width = (buf[SC_DEVICE_NAME_FIELD_LENGTH] << 8)
| buf[DEVICE_NAME_FIELD_LENGTH + 1]; | buf[SC_DEVICE_NAME_FIELD_LENGTH + 1];
info->frame_size.height = (buf[DEVICE_NAME_FIELD_LENGTH + 2] << 8) info->frame_size.height = (buf[SC_DEVICE_NAME_FIELD_LENGTH + 2] << 8)
| buf[DEVICE_NAME_FIELD_LENGTH + 3]; | buf[SC_DEVICE_NAME_FIELD_LENGTH + 3];
return true; return true;
} }
static bool static bool
server_connect_to(struct server *server, struct server_info *info) { sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
struct sc_adb_tunnel *tunnel = &server->tunnel; struct sc_adb_tunnel *tunnel = &server->tunnel;
assert(tunnel->enabled); assert(tunnel->enabled);
@ -400,8 +402,8 @@ fail:
} }
static void static void
server_on_terminated(void *userdata) { sc_server_on_terminated(void *userdata) {
struct server *server = userdata; struct sc_server *server = userdata;
// If the server process dies before connecting to the server socket, // If the server process dies before connecting to the server socket,
// then the client will be stuck forever on accept(). To avoid the problem, // then the client will be stuck forever on accept(). To avoid the problem,
@ -416,9 +418,9 @@ server_on_terminated(void *userdata) {
static int static int
run_server(void *data) { run_server(void *data) {
struct server *server = data; struct sc_server *server = data;
const struct server_params *params = &server->params; const struct sc_server_params *params = &server->params;
bool ok = push_server(&server->intr, params->serial); bool ok = push_server(&server->intr, params->serial);
if (!ok) { if (!ok) {
@ -439,7 +441,7 @@ run_server(void *data) {
} }
static const struct sc_process_listener listener = { static const struct sc_process_listener listener = {
.on_terminated = server_on_terminated, .on_terminated = sc_server_on_terminated,
}; };
struct sc_process_observer observer; struct sc_process_observer observer;
ok = sc_process_observer_init(&observer, pid, &listener, server); ok = sc_process_observer_init(&observer, pid, &listener, server);
@ -450,7 +452,7 @@ run_server(void *data) {
goto error_connection_failed; goto error_connection_failed;
} }
ok = server_connect_to(server, &server->info); ok = sc_server_connect_to(server, &server->info);
// The tunnel is always closed by server_connect_to() // The tunnel is always closed by server_connect_to()
if (!ok) { if (!ok) {
sc_process_terminate(pid); sc_process_terminate(pid);
@ -499,7 +501,7 @@ error_connection_failed:
} }
bool bool
server_start(struct server *server) { sc_server_start(struct sc_server *server) {
bool ok = sc_thread_create(&server->thread, run_server, "server", server); bool ok = sc_thread_create(&server->thread, run_server, "server", server);
if (!ok) { if (!ok) {
LOGE("Could not create server thread"); LOGE("Could not create server thread");
@ -510,7 +512,7 @@ server_start(struct server *server) {
} }
void void
server_stop(struct server *server) { sc_server_stop(struct sc_server *server) {
sc_mutex_lock(&server->mutex); sc_mutex_lock(&server->mutex);
server->stopped = true; server->stopped = true;
sc_cond_signal(&server->cond_stopped); sc_cond_signal(&server->cond_stopped);
@ -521,8 +523,8 @@ server_stop(struct server *server) {
} }
void void
server_destroy(struct server *server) { sc_server_destroy(struct sc_server *server) {
server_params_destroy(&server->params); sc_server_params_destroy(&server->params);
sc_intr_destroy(&server->intr); sc_intr_destroy(&server->intr);
sc_cond_destroy(&server->cond_stopped); sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);

View file

@ -16,13 +16,13 @@
#include "util/net.h" #include "util/net.h"
#include "util/thread.h" #include "util/thread.h"
#define DEVICE_NAME_FIELD_LENGTH 64 #define SC_DEVICE_NAME_FIELD_LENGTH 64
struct server_info { struct sc_server_info {
char device_name[DEVICE_NAME_FIELD_LENGTH]; char device_name[SC_DEVICE_NAME_FIELD_LENGTH];
struct sc_size frame_size; struct sc_size frame_size;
}; };
struct server_params { struct sc_server_params {
const char *serial; const char *serial;
enum sc_log_level log_level; enum sc_log_level log_level;
const char *crop; const char *crop;
@ -41,12 +41,12 @@ struct server_params {
bool power_off_on_close; bool power_off_on_close;
}; };
struct server { struct sc_server {
// The internal allocated strings are copies owned by the server // The internal allocated strings are copies owned by the server
struct server_params params; struct sc_server_params params;
sc_thread thread; sc_thread thread;
struct server_info info; // initialized once connected struct sc_server_info info; // initialized once connected
sc_mutex mutex; sc_mutex mutex;
sc_cond cond_stopped; sc_cond cond_stopped;
@ -58,45 +58,45 @@ struct server {
sc_socket video_socket; sc_socket video_socket;
sc_socket control_socket; sc_socket control_socket;
const struct server_callbacks *cbs; const struct sc_server_callbacks *cbs;
void *cbs_userdata; void *cbs_userdata;
}; };
struct server_callbacks { struct sc_server_callbacks {
/** /**
* Called when the server failed to connect * Called when the server failed to connect
* *
* If it is called, then on_connected() and on_disconnected() will never be * If it is called, then on_connected() and on_disconnected() will never be
* called. * called.
*/ */
void (*on_connection_failed)(struct server *server, void *userdata); void (*on_connection_failed)(struct sc_server *server, void *userdata);
/** /**
* Called on server connection * Called on server connection
*/ */
void (*on_connected)(struct server *server, void *userdata); void (*on_connected)(struct sc_server *server, void *userdata);
/** /**
* Called on server disconnection (after it has been connected) * Called on server disconnection (after it has been connected)
*/ */
void (*on_disconnected)(struct server *server, void *userdata); void (*on_disconnected)(struct sc_server *server, void *userdata);
}; };
// init the server with the given params // init the server with the given params
bool bool
server_init(struct server *server, const struct server_params *params, sc_server_init(struct sc_server *server, const struct sc_server_params *params,
const struct server_callbacks *cbs, void *cbs_userdata); const struct sc_server_callbacks *cbs, void *cbs_userdata);
// start the server asynchronously // start the server asynchronously
bool bool
server_start(struct server *server); sc_server_start(struct sc_server *server);
// disconnect and kill the server process // disconnect and kill the server process
void void
server_stop(struct server *server); sc_server_stop(struct sc_server *server);
// close and release sockets // close and release sockets
void void
server_destroy(struct server *server); sc_server_destroy(struct sc_server *server);
#endif #endif