Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to catch server shutdown? #483

Closed
Lurker00 opened this issue Mar 31, 2019 · 9 comments
Closed

How to catch server shutdown? #483

Lurker00 opened this issue Mar 31, 2019 · 9 comments
Labels

Comments

@Lurker00
Copy link

I'm trying to configure the device (a phone) especially for scrcpy use, and restore device state back when the session ends. I think it is better to do at the server (Java) side, to restore the state even when the connection is broken.

But I've failed to find a point in the server source which is reached or called at the end. The control is not returned to main(), DesktopConnection.close() is never called.

I've even tried to create an obect of a class with finalize() method, to no avail. Runtime.getRuntime().addShutdownHook() has no effect either!

Does somebody know how to achive the goal: to run some code when the server stops?

@rom1v
Copy link
Collaborator

rom1v commented Mar 31, 2019

The server app_process is killed with SIGTERM:

server_stop(&server);

if (!cmd_terminate(server->process)) {

return kill(pid, SIGTERM) != -1;

Anyway, this is what happens if you close adb (e.g. unplug the cable), so currently there is no command you can guarantee to be executed on close.

@rom1v rom1v added the question label Mar 31, 2019
@Lurker00
Copy link
Author

Lurker00 commented Mar 31, 2019

The code you referred to is for the client (Linux/Windows) side, in C. The server (Android) is written in Java. My question is about Android/Java side.

@rom1v
Copy link
Collaborator

rom1v commented Mar 31, 2019

The java is executed by app_process, started by the client via adb shell. This process is killed by the client on close, so the server is killed without a chance to execute cleanup code.

@Lurker00
Copy link
Author

@rom1v, I can't understand you! The Android side server is started on Android device. Well, probably adb_execute creates a client-side platform process, which is killed in cmd_terminate() by platform methods (e.g. TerminateProcess & CloseHandle under Windows), but the question who and how kills the Android server process remains unanswered.

But it does not matter for me anymore :)

Meanwhile, I was able to resolve my problem by "self-forking" of com.genymobile.scrcpy.Server when it starts via adb. Now it works as it should, and is closed gracefully on cable disconnect. Now I just need to implement sending a "shutdown" command on client side window close event to the Android part. But it's rather trivial :)

@rom1v
Copy link
Collaborator

rom1v commented Mar 31, 2019

but the question who and how kills the Android server process remains unanswered.

The adb server (on the device side), when the adb shell is closed.

adb shell 'for i in `seq 1 10`; do echo $i | tee -a /data/local/tmp/test; sleep 1; done'

If you unplug the device, or kill the client process, you will see that the server process is also killed. Replug, then:

$ adb shell cat /data/local/tmp/test
1
2
3
4

This can be avoided by catching SIGHUP though.

Meanwhile, I was able to resolve my problem by "self-forking" of com.genymobile.scrcpy.Server when it starts via adb

What do you do exactly?

@Lurker00
Copy link
Author

I'm making an Android tablet from my, very small, phone (Unihertz Atom, 2.45", 240x432) and 7" FullHD Windows 10 convertable. For that purpose, I plan to expand scrcpy command line to accept device "screen size" and density. When device side server starts, it saves current screen parameters, and then
wm size WxH
wm density D
and restores the values back at shutdown. My currently working model already displays FullHD 240dpi screen, and Android switches into tablet mode (using tablet, not phone, app layouts).

The main problem was graceful server shutdown at the cable disconnect, to restore phone state. It has been solved. Then I plan to auto-dim phone's backlight, implement on-screen buttons to scrcpy UI window, etc.

But I have one more problem to solve: wireless connection! scrcpy can't reverse the protocol with adb over TCP, and the result is awful. I hope it can be done by extracting IP address/port from "device id" in this mode, and connecting via TCP to the server directly...

@rom1v
Copy link
Collaborator

rom1v commented Mar 31, 2019

Meanwhile, I was able to resolve my problem by "self-forking" of com.genymobile.scrcpy.Server when it starts via adb.

com.genymobile.scrcpy.Server is a java main class. What do you mean by "self-forking"?

The main problem was graceful server shutdown at the cable disconnect, to restore phone state. It has been solved.

Could you explain how, please?

@Lurker00
Copy link
Author

I pass a parameter to the Java server. It handles this parameter in main() by starting another process from its class, passing the rest of parameters to the child, and exits. That's all!

I can try to commit these changes, just have to cleanup the code, and learn how to push to not my project...

@rom1v
Copy link
Collaborator

rom1v commented Apr 11, 2020

@Lurker00 Oh I never answered to this message.

Thank you for the tip, I keep that in mind (one day I will probably start a new process just to do the cleanup, and use that for the existing --show-touches option). 👍

@rom1v rom1v closed this as completed Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants