Skip to content

Commit

Permalink
Make the server unlink itself
Browse files Browse the repository at this point in the history
To clean up the device, the client executed "adb shell rm" once the
server was guaranteed to be started (after the connection succeeded).

This implied to track whether the installation state, and failed if an
additional tunnel was used in "forward" mode:
<#386 (comment)>

Instead, make the server unlink itself on start.
  • Loading branch information
rom1v committed Jan 14, 2019
1 parent fefb981 commit 39c5e71
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
5 changes: 0 additions & 5 deletions app/src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,6 @@ process_t adb_install(const char *serial, const char *local) {
return proc;
}

process_t adb_remove_path(const char *serial, const char *path) {
const char *const adb_cmd[] = {"shell", "rm", path};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
}

SDL_bool process_check_success(process_t proc, const char *name) {
if (proc == PROCESS_NONE) {
LOGE("Could not execute \"%s\"", name);
Expand Down
1 change: 0 additions & 1 deletion app/src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ process_t adb_reverse(const char *serial, const char *device_socket_name, uint16
process_t adb_reverse_remove(const char *serial, const char *device_socket_name);
process_t adb_push(const char *serial, const char *local, const char *remote);
process_t adb_install(const char *serial, const char *local);
process_t adb_remove_path(const char *serial, const char *path);

// convenience function to wait for a successful process execution
// automatically log process errors with the provided process name
Expand Down
15 changes: 0 additions & 15 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ static SDL_bool push_server(const char *serial) {
return process_check_success(process, "adb push");
}

static SDL_bool remove_server(const char *serial) {
process_t process = adb_remove_path(serial, DEVICE_SERVER_PATH);
return process_check_success(process, "adb shell rm");
}

static SDL_bool enable_tunnel_reverse(const char *serial, Uint16 local_port) {
process_t process = adb_reverse(serial, SOCKET_NAME, local_port);
return process_check_success(process, "adb reverse");
Expand Down Expand Up @@ -167,8 +162,6 @@ SDL_bool server_start(struct server *server, const char *serial,
return SDL_FALSE;
}

server->server_copied_to_device = SDL_TRUE;

if (!enable_tunnel(server)) {
SDL_free((void *) server->serial);
return SDL_FALSE;
Expand Down Expand Up @@ -229,10 +222,6 @@ socket_t server_connect_to(struct server *server) {
close_socket(&server->server_socket);
}

// the server is started, we can clean up the jar from the temporary folder
remove_server(server->serial); // ignore failure
server->server_copied_to_device = SDL_FALSE;

// we don't need the adb tunnel anymore
disable_tunnel(server); // ignore failure
server->tunnel_enabled = SDL_FALSE;
Expand All @@ -254,10 +243,6 @@ void server_stop(struct server *server) {
// ignore failure
disable_tunnel(server);
}

if (server->server_copied_to_device) {
remove_server(server->serial); // ignore failure
}
}

void server_destroy(struct server *server) {
Expand Down
2 changes: 0 additions & 2 deletions app/src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct server {
SDL_bool tunnel_enabled;
SDL_bool tunnel_forward; // use "adb forward" instead of "adb reverse"
SDL_bool send_frame_meta; // request frame PTS to be able to record properly
SDL_bool server_copied_to_device;
};

#define SERVER_INITIALIZER { \
Expand All @@ -25,7 +24,6 @@ struct server {
.tunnel_enabled = SDL_FALSE, \
.tunnel_forward = SDL_FALSE, \
.send_frame_meta = SDL_FALSE, \
.server_copied_to_device = SDL_FALSE, \
}

// init default values
Expand Down
12 changes: 12 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import android.graphics.Rect;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public final class Server {

private static final String SERVER_PATH = "/data/local/tmp/scrcpy-server.jar";

private Server() {
// not instantiable
}
Expand Down Expand Up @@ -86,6 +89,14 @@ private static Rect parseCrop(String crop) {
return new Rect(x, y, x + width, y + height);
}

private static void unlinkSelf() {
try {
new File(SERVER_PATH).delete();
} catch (Exception e) {
Ln.e("Cannot unlink server", e);
}
}

public static void main(String... args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
Expand All @@ -94,6 +105,7 @@ public void uncaughtException(Thread t, Throwable e) {
}
});

unlinkSelf();
Options options = createOptions(args);
scrcpy(options);
}
Expand Down

0 comments on commit 39c5e71

Please sign in to comment.