Do not create control socket if no control

If --no-control is enabled, then it is not necessary to create a second
communication socket between the client and the server.

This also facilitates the use of the server alone (without the client)
to receive only the raw video stream.
This commit is contained in:
Romain Vimont 2021-12-06 21:50:24 +01:00
parent 80fe12a95f
commit cabcbc2b15
3 changed files with 51 additions and 32 deletions

View file

@ -388,6 +388,7 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
assert(tunnel->enabled); assert(tunnel->enabled);
const char *serial = server->params.serial; const char *serial = server->params.serial;
bool control = server->params.control;
sc_socket video_socket = SC_SOCKET_NONE; sc_socket video_socket = SC_SOCKET_NONE;
sc_socket control_socket = SC_SOCKET_NONE; sc_socket control_socket = SC_SOCKET_NONE;
@ -397,10 +398,13 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
goto fail; goto fail;
} }
control_socket = net_accept_intr(&server->intr, tunnel->server_socket); if (control) {
control_socket =
net_accept_intr(&server->intr, tunnel->server_socket);
if (control_socket == SC_SOCKET_NONE) { if (control_socket == SC_SOCKET_NONE) {
goto fail; goto fail;
} }
}
} else { } else {
uint32_t tunnel_host = server->params.tunnel_host; uint32_t tunnel_host = server->params.tunnel_host;
if (!tunnel_host) { if (!tunnel_host) {
@ -420,17 +424,20 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
goto fail; goto fail;
} }
// we know that the device is listening, we don't need several attempts if (control) {
// we know that the device is listening, we don't need several
// attempts
control_socket = net_socket(); control_socket = net_socket();
if (control_socket == SC_SOCKET_NONE) { if (control_socket == SC_SOCKET_NONE) {
goto fail; goto fail;
} }
bool ok = net_connect_intr(&server->intr, control_socket, tunnel_host, bool ok = net_connect_intr(&server->intr, control_socket,
tunnel_port); tunnel_host, tunnel_port);
if (!ok) { if (!ok) {
goto fail; goto fail;
} }
} }
}
// we don't need the adb tunnel anymore // we don't need the adb tunnel anymore
sc_adb_tunnel_close(tunnel, &server->intr, serial); sc_adb_tunnel_close(tunnel, &server->intr, serial);
@ -442,7 +449,7 @@ sc_server_connect_to(struct sc_server *server, struct sc_server_info *info) {
} }
assert(video_socket != SC_SOCKET_NONE); assert(video_socket != SC_SOCKET_NONE);
assert(control_socket != SC_SOCKET_NONE); assert(!control || control_socket != SC_SOCKET_NONE);
server->video_socket = video_socket; server->video_socket = video_socket;
server->control_socket = control_socket; server->control_socket = control_socket;

View file

@ -30,8 +30,13 @@ public final class DesktopConnection implements Closeable {
private DesktopConnection(LocalSocket videoSocket, LocalSocket controlSocket) throws IOException { private DesktopConnection(LocalSocket videoSocket, LocalSocket controlSocket) throws IOException {
this.videoSocket = videoSocket; this.videoSocket = videoSocket;
this.controlSocket = controlSocket; this.controlSocket = controlSocket;
if (controlSocket != null) {
controlInputStream = controlSocket.getInputStream(); controlInputStream = controlSocket.getInputStream();
controlOutputStream = controlSocket.getOutputStream(); controlOutputStream = controlSocket.getOutputStream();
} else {
controlInputStream = null;
controlOutputStream = null;
}
videoFd = videoSocket.getFileDescriptor(); videoFd = videoSocket.getFileDescriptor();
} }
@ -41,26 +46,29 @@ public final class DesktopConnection implements Closeable {
return localSocket; return localSocket;
} }
public static DesktopConnection open(Device device, boolean tunnelForward) throws IOException { public static DesktopConnection open(Device device, boolean tunnelForward, boolean control) throws IOException {
LocalSocket videoSocket; LocalSocket videoSocket;
LocalSocket controlSocket; LocalSocket controlSocket = null;
if (tunnelForward) { if (tunnelForward) {
LocalServerSocket localServerSocket = new LocalServerSocket(SOCKET_NAME); LocalServerSocket localServerSocket = new LocalServerSocket(SOCKET_NAME);
try { try {
videoSocket = localServerSocket.accept(); videoSocket = localServerSocket.accept();
// send one byte so the client may read() to detect a connection error // send one byte so the client may read() to detect a connection error
videoSocket.getOutputStream().write(0); videoSocket.getOutputStream().write(0);
if (control) {
try { try {
controlSocket = localServerSocket.accept(); controlSocket = localServerSocket.accept();
} catch (IOException | RuntimeException e) { } catch (IOException | RuntimeException e) {
videoSocket.close(); videoSocket.close();
throw e; throw e;
} }
}
} finally { } finally {
localServerSocket.close(); localServerSocket.close();
} }
} else { } else {
videoSocket = connect(SOCKET_NAME); videoSocket = connect(SOCKET_NAME);
if (control) {
try { try {
controlSocket = connect(SOCKET_NAME); controlSocket = connect(SOCKET_NAME);
} catch (IOException | RuntimeException e) { } catch (IOException | RuntimeException e) {
@ -68,6 +76,7 @@ public final class DesktopConnection implements Closeable {
throw e; throw e;
} }
} }
}
DesktopConnection connection = new DesktopConnection(videoSocket, controlSocket); DesktopConnection connection = new DesktopConnection(videoSocket, controlSocket);
Size videoSize = device.getScreenInfo().getVideoSize(); Size videoSize = device.getScreenInfo().getVideoSize();
@ -79,10 +88,12 @@ public final class DesktopConnection implements Closeable {
videoSocket.shutdownInput(); videoSocket.shutdownInput();
videoSocket.shutdownOutput(); videoSocket.shutdownOutput();
videoSocket.close(); videoSocket.close();
if (controlSocket != null) {
controlSocket.shutdownInput(); controlSocket.shutdownInput();
controlSocket.shutdownOutput(); controlSocket.shutdownOutput();
controlSocket.close(); controlSocket.close();
} }
}
private void send(String deviceName, int width, int height) throws IOException { private void send(String deviceName, int width, int height) throws IOException {
byte[] buffer = new byte[DEVICE_NAME_FIELD_LENGTH + 4]; byte[] buffer = new byte[DEVICE_NAME_FIELD_LENGTH + 4];

View file

@ -66,14 +66,15 @@ public final class Server {
Thread initThread = startInitThread(options); Thread initThread = startInitThread(options);
boolean tunnelForward = options.isTunnelForward(); boolean tunnelForward = options.isTunnelForward();
boolean control = options.getControl();
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) { try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward, control)) {
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), codecOptions, ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(), codecOptions,
options.getEncoderName()); options.getEncoderName());
Thread controllerThread = null; Thread controllerThread = null;
Thread deviceMessageSenderThread = null; Thread deviceMessageSenderThread = null;
if (options.getControl()) { if (control) {
final Controller controller = new Controller(device, connection, options.getClipboardAutosync()); final Controller controller = new Controller(device, connection, options.getClipboardAutosync());
// asynchronous // asynchronous