Skip to content

Commit

Permalink
Use a different server path for each session
Browse files Browse the repository at this point in the history
When scrcpy is run, a server is pushed to
/data/local/tmp/scrcpy-server.jar.

Running simultaneous scrcpy instances on the same device was not a
problem, because the file was unlinked (removed) almost immediately once
it started, avoiding any conflicts.

In order to support executing new process using the scrcpy-server.jar at
any time (to change the display power mode from a separate process), the
server file must not be unlinked, so using different names are necessary
to avoid conflicts.

Reuse the scid mechanism already used for generating device socket
names.

Refs 4315be1
Refs 439a1fd
  • Loading branch information
rom1v committed Nov 21, 2023
1 parent e76ff25 commit 798727a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#define SC_SERVER_FILENAME "scrcpy-server"

#define SC_SERVER_PATH_DEFAULT PREFIX "/share/scrcpy/" SC_SERVER_FILENAME
#define SC_DEVICE_SERVER_PATH "/data/local/tmp/scrcpy-server.jar"

#define SC_ADB_PORT_DEFAULT 5555
#define SC_SOCKET_NAME_PREFIX "scrcpy_"
Expand Down Expand Up @@ -117,7 +116,7 @@ sc_server_params_copy(struct sc_server_params *dst,
}

static bool
push_server(struct sc_intr *intr, const char *serial) {
push_server(struct sc_intr *intr, uint32_t scid, const char *serial) {
char *server_path = get_server_path();
if (!server_path) {
return false;
Expand All @@ -127,7 +126,16 @@ push_server(struct sc_intr *intr, const char *serial) {
free(server_path);
return false;
}
bool ok = sc_adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH, 0);

char *device_server_path;
if (asprintf(&device_server_path, "/data/local/tmp/scrcpy-server-%08x.jar",
scid) == -1) {
LOG_OOM();
free(server_path);
return false;
}
bool ok = sc_adb_push(intr, serial, server_path, device_server_path, 0);
free(device_server_path);
free(server_path);
return ok;
}
Expand Down Expand Up @@ -209,13 +217,20 @@ execute_server(struct sc_server *server,
const char *serial = server->serial;
assert(serial);

char *classpath;
if (asprintf(&classpath, "CLASSPATH=/data/local/tmp/scrcpy-server-%08x.jar",
params->scid) == -1) {
LOG_OOM();
return SC_PROCESS_NONE;
}

const char *cmd[128];
unsigned count = 0;
cmd[count++] = sc_adb_get_executable();
cmd[count++] = "-s";
cmd[count++] = serial;
cmd[count++] = "shell";
cmd[count++] = "CLASSPATH=" SC_DEVICE_SERVER_PATH;
cmd[count++] = classpath;
cmd[count++] = "app_process";

#ifdef SERVER_DEBUGGER
Expand Down Expand Up @@ -388,6 +403,7 @@ execute_server(struct sc_server *server,
for (unsigned i = dyn_idx; i < count; ++i) {
free((char *) cmd[i]);
}
free(classpath);

return pid;
}
Expand Down Expand Up @@ -937,7 +953,7 @@ run_server(void *data) {
assert(serial);
LOGD("Device serial: %s", serial);

ok = push_server(&server->intr, serial);
ok = push_server(&server->intr, params->scid, serial);
if (!ok) {
goto error_connection_failed;
}
Expand Down

0 comments on commit 798727a

Please sign in to comment.