Extract ControlChannel class

This prevents many components from depending on the whole
DesktopConnection.
issue4703
Romain Vimont 2 months ago
parent a7cf4daf3b
commit 25f1e703b7

@ -0,0 +1,33 @@
package com.genymobile.scrcpy;
import android.net.LocalSocket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public final class ControlChannel {
private final InputStream inputStream;
private final OutputStream outputStream;
private final ControlMessageReader reader = new ControlMessageReader();
private final DeviceMessageWriter writer = new DeviceMessageWriter();
public ControlChannel(LocalSocket controlSocket) throws IOException {
this.inputStream = controlSocket.getInputStream();
this.outputStream = controlSocket.getOutputStream();
}
public ControlMessage recv() throws IOException {
ControlMessage msg = reader.next();
while (msg == null) {
reader.readFrom(inputStream);
msg = reader.next();
}
return msg;
}
public void send(DeviceMessage msg) throws IOException {
writer.writeTo(msg, outputStream);
}
}

@ -27,7 +27,7 @@ public class Controller implements AsyncProcessor {
private Thread thread;
private final Device device;
private final DesktopConnection connection;
private final ControlChannel controlChannel;
private final CleanUp cleanUp;
private final DeviceMessageSender sender;
private final boolean clipboardAutosync;
@ -42,14 +42,14 @@ public class Controller implements AsyncProcessor {
private boolean keepPowerModeOff;
public Controller(Device device, DesktopConnection connection, CleanUp cleanUp, boolean clipboardAutosync, boolean powerOn) {
public Controller(Device device, ControlChannel controlChannel, CleanUp cleanUp, boolean clipboardAutosync, boolean powerOn) {
this.device = device;
this.connection = connection;
this.controlChannel = controlChannel;
this.cleanUp = cleanUp;
this.clipboardAutosync = clipboardAutosync;
this.powerOn = powerOn;
initPointers();
sender = new DeviceMessageSender(connection);
sender = new DeviceMessageSender(controlChannel);
}
private void initPointers() {
@ -123,7 +123,7 @@ public class Controller implements AsyncProcessor {
}
private void handleEvent() throws IOException {
ControlMessage msg = connection.receiveControlMessage();
ControlMessage msg = controlChannel.recv();
switch (msg.getType()) {
case ControlMessage.TYPE_INJECT_KEYCODE:
if (device.supportsInputEvents()) {

@ -7,8 +7,6 @@ import android.net.LocalSocketAddress;
import java.io.Closeable;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public final class DesktopConnection implements Closeable {
@ -24,25 +22,16 @@ public final class DesktopConnection implements Closeable {
private final FileDescriptor audioFd;
private final LocalSocket controlSocket;
private final InputStream controlInputStream;
private final OutputStream controlOutputStream;
private final ControlMessageReader reader = new ControlMessageReader();
private final DeviceMessageWriter writer = new DeviceMessageWriter();
private final ControlChannel controlChannel;
private DesktopConnection(LocalSocket videoSocket, LocalSocket audioSocket, LocalSocket controlSocket) throws IOException {
this.videoSocket = videoSocket;
this.controlSocket = controlSocket;
this.audioSocket = audioSocket;
if (controlSocket != null) {
controlInputStream = controlSocket.getInputStream();
controlOutputStream = controlSocket.getOutputStream();
} else {
controlInputStream = null;
controlOutputStream = null;
}
this.controlSocket = controlSocket;
videoFd = videoSocket != null ? videoSocket.getFileDescriptor() : null;
audioFd = audioSocket != null ? audioSocket.getFileDescriptor() : null;
controlChannel = controlSocket != null ? new ControlChannel(controlSocket) : null;
}
private static LocalSocket connect(String abstractName) throws IOException {
@ -179,16 +168,7 @@ public final class DesktopConnection implements Closeable {
return audioFd;
}
public ControlMessage receiveControlMessage() throws IOException {
ControlMessage msg = reader.next();
while (msg == null) {
reader.readFrom(controlInputStream);
msg = reader.next();
}
return msg;
}
public void sendDeviceMessage(DeviceMessage msg) throws IOException {
writer.writeTo(msg, controlOutputStream);
public ControlChannel getControlChannel() {
return controlChannel;
}
}

@ -4,7 +4,7 @@ import java.io.IOException;
public final class DeviceMessageSender {
private final DesktopConnection connection;
private final ControlChannel controlChannel;
private Thread thread;
@ -12,8 +12,8 @@ public final class DeviceMessageSender {
private long ack;
public DeviceMessageSender(DesktopConnection connection) {
this.connection = connection;
public DeviceMessageSender(ControlChannel controlChannel) {
this.controlChannel = controlChannel;
}
public synchronized void pushClipboardText(String text) {
@ -43,11 +43,11 @@ public final class DeviceMessageSender {
if (sequence != DeviceMessage.SEQUENCE_INVALID) {
DeviceMessage event = DeviceMessage.createAckClipboard(sequence);
connection.sendDeviceMessage(event);
controlChannel.send(event);
}
if (text != null) {
DeviceMessage event = DeviceMessage.createClipboard(text);
connection.sendDeviceMessage(event);
controlChannel.send(event);
}
}
}

@ -131,7 +131,8 @@ public final class Server {
}
if (control) {
Controller controller = new Controller(device, connection, cleanUp, options.getClipboardAutosync(), options.getPowerOn());
ControlChannel controlChannel = connection.getControlChannel();
Controller controller = new Controller(device, controlChannel, cleanUp, options.getClipboardAutosync(), options.getPowerOn());
device.setClipboardListener(text -> controller.getSender().pushClipboardText(text));
asyncProcessors.add(controller);
}

Loading…
Cancel
Save