mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-24 11:21:03 +00:00
Extract ControlChannel class
This prevents many components from depending on the whole DesktopConnection.
This commit is contained in:
parent
a7cf4daf3b
commit
25f1e703b7
5 changed files with 51 additions and 37 deletions
|
@ -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 Thread thread;
|
||||||
|
|
||||||
private final Device device;
|
private final Device device;
|
||||||
private final DesktopConnection connection;
|
private final ControlChannel controlChannel;
|
||||||
private final CleanUp cleanUp;
|
private final CleanUp cleanUp;
|
||||||
private final DeviceMessageSender sender;
|
private final DeviceMessageSender sender;
|
||||||
private final boolean clipboardAutosync;
|
private final boolean clipboardAutosync;
|
||||||
|
@ -42,14 +42,14 @@ public class Controller implements AsyncProcessor {
|
||||||
|
|
||||||
private boolean keepPowerModeOff;
|
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.device = device;
|
||||||
this.connection = connection;
|
this.controlChannel = controlChannel;
|
||||||
this.cleanUp = cleanUp;
|
this.cleanUp = cleanUp;
|
||||||
this.clipboardAutosync = clipboardAutosync;
|
this.clipboardAutosync = clipboardAutosync;
|
||||||
this.powerOn = powerOn;
|
this.powerOn = powerOn;
|
||||||
initPointers();
|
initPointers();
|
||||||
sender = new DeviceMessageSender(connection);
|
sender = new DeviceMessageSender(controlChannel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initPointers() {
|
private void initPointers() {
|
||||||
|
@ -123,7 +123,7 @@ public class Controller implements AsyncProcessor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleEvent() throws IOException {
|
private void handleEvent() throws IOException {
|
||||||
ControlMessage msg = connection.receiveControlMessage();
|
ControlMessage msg = controlChannel.recv();
|
||||||
switch (msg.getType()) {
|
switch (msg.getType()) {
|
||||||
case ControlMessage.TYPE_INJECT_KEYCODE:
|
case ControlMessage.TYPE_INJECT_KEYCODE:
|
||||||
if (device.supportsInputEvents()) {
|
if (device.supportsInputEvents()) {
|
||||||
|
|
|
@ -7,8 +7,6 @@ import android.net.LocalSocketAddress;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public final class DesktopConnection implements Closeable {
|
public final class DesktopConnection implements Closeable {
|
||||||
|
@ -24,25 +22,16 @@ public final class DesktopConnection implements Closeable {
|
||||||
private final FileDescriptor audioFd;
|
private final FileDescriptor audioFd;
|
||||||
|
|
||||||
private final LocalSocket controlSocket;
|
private final LocalSocket controlSocket;
|
||||||
private final InputStream controlInputStream;
|
private final ControlChannel controlChannel;
|
||||||
private final OutputStream controlOutputStream;
|
|
||||||
|
|
||||||
private final ControlMessageReader reader = new ControlMessageReader();
|
|
||||||
private final DeviceMessageWriter writer = new DeviceMessageWriter();
|
|
||||||
|
|
||||||
private DesktopConnection(LocalSocket videoSocket, LocalSocket audioSocket, LocalSocket controlSocket) throws IOException {
|
private DesktopConnection(LocalSocket videoSocket, LocalSocket audioSocket, LocalSocket controlSocket) throws IOException {
|
||||||
this.videoSocket = videoSocket;
|
this.videoSocket = videoSocket;
|
||||||
this.controlSocket = controlSocket;
|
|
||||||
this.audioSocket = audioSocket;
|
this.audioSocket = audioSocket;
|
||||||
if (controlSocket != null) {
|
this.controlSocket = controlSocket;
|
||||||
controlInputStream = controlSocket.getInputStream();
|
|
||||||
controlOutputStream = controlSocket.getOutputStream();
|
|
||||||
} else {
|
|
||||||
controlInputStream = null;
|
|
||||||
controlOutputStream = null;
|
|
||||||
}
|
|
||||||
videoFd = videoSocket != null ? videoSocket.getFileDescriptor() : null;
|
videoFd = videoSocket != null ? videoSocket.getFileDescriptor() : null;
|
||||||
audioFd = audioSocket != null ? audioSocket.getFileDescriptor() : null;
|
audioFd = audioSocket != null ? audioSocket.getFileDescriptor() : null;
|
||||||
|
controlChannel = controlSocket != null ? new ControlChannel(controlSocket) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LocalSocket connect(String abstractName) throws IOException {
|
private static LocalSocket connect(String abstractName) throws IOException {
|
||||||
|
@ -179,16 +168,7 @@ public final class DesktopConnection implements Closeable {
|
||||||
return audioFd;
|
return audioFd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ControlMessage receiveControlMessage() throws IOException {
|
public ControlChannel getControlChannel() {
|
||||||
ControlMessage msg = reader.next();
|
return controlChannel;
|
||||||
while (msg == null) {
|
|
||||||
reader.readFrom(controlInputStream);
|
|
||||||
msg = reader.next();
|
|
||||||
}
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendDeviceMessage(DeviceMessage msg) throws IOException {
|
|
||||||
writer.writeTo(msg, controlOutputStream);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import java.io.IOException;
|
||||||
|
|
||||||
public final class DeviceMessageSender {
|
public final class DeviceMessageSender {
|
||||||
|
|
||||||
private final DesktopConnection connection;
|
private final ControlChannel controlChannel;
|
||||||
|
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
|
|
||||||
|
@ -12,8 +12,8 @@ public final class DeviceMessageSender {
|
||||||
|
|
||||||
private long ack;
|
private long ack;
|
||||||
|
|
||||||
public DeviceMessageSender(DesktopConnection connection) {
|
public DeviceMessageSender(ControlChannel controlChannel) {
|
||||||
this.connection = connection;
|
this.controlChannel = controlChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void pushClipboardText(String text) {
|
public synchronized void pushClipboardText(String text) {
|
||||||
|
@ -43,11 +43,11 @@ public final class DeviceMessageSender {
|
||||||
|
|
||||||
if (sequence != DeviceMessage.SEQUENCE_INVALID) {
|
if (sequence != DeviceMessage.SEQUENCE_INVALID) {
|
||||||
DeviceMessage event = DeviceMessage.createAckClipboard(sequence);
|
DeviceMessage event = DeviceMessage.createAckClipboard(sequence);
|
||||||
connection.sendDeviceMessage(event);
|
controlChannel.send(event);
|
||||||
}
|
}
|
||||||
if (text != null) {
|
if (text != null) {
|
||||||
DeviceMessage event = DeviceMessage.createClipboard(text);
|
DeviceMessage event = DeviceMessage.createClipboard(text);
|
||||||
connection.sendDeviceMessage(event);
|
controlChannel.send(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,7 +131,8 @@ public final class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (control) {
|
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));
|
device.setClipboardListener(text -> controller.getSender().pushClipboardText(text));
|
||||||
asyncProcessors.add(controller);
|
asyncProcessors.add(controller);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue