Refactor DeviceMessageSender

Refactor DeviceMessage as a queue of message. This will allow to add
other message types.

PR #4473 <https://github.com/Genymobile/scrcpy/pull/4473>
uhid.38
Romain Vimont 3 months ago
parent 840680f546
commit 021c5d371a

@ -413,7 +413,8 @@ public class Controller implements AsyncProcessor {
if (!clipboardAutosync) { if (!clipboardAutosync) {
String clipboardText = Device.getClipboardText(); String clipboardText = Device.getClipboardText();
if (clipboardText != null) { if (clipboardText != null) {
sender.pushClipboardText(clipboardText); DeviceMessage msg = DeviceMessage.createClipboard(clipboardText);
sender.send(msg);
} }
} }
} }
@ -431,7 +432,8 @@ public class Controller implements AsyncProcessor {
if (sequence != ControlMessage.SEQUENCE_INVALID) { if (sequence != ControlMessage.SEQUENCE_INVALID) {
// Acknowledgement requested // Acknowledgement requested
sender.pushAckClipboard(sequence); DeviceMessage msg = DeviceMessage.createAckClipboard(sequence);
sender.send(msg);
} }
return ok; return ok;

@ -5,8 +5,6 @@ public final class DeviceMessage {
public static final int TYPE_CLIPBOARD = 0; public static final int TYPE_CLIPBOARD = 0;
public static final int TYPE_ACK_CLIPBOARD = 1; public static final int TYPE_ACK_CLIPBOARD = 1;
public static final long SEQUENCE_INVALID = ControlMessage.SEQUENCE_INVALID;
private int type; private int type;
private String text; private String text;
private long sequence; private long sequence;

@ -1,54 +1,30 @@
package com.genymobile.scrcpy; package com.genymobile.scrcpy;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public final class DeviceMessageSender { public final class DeviceMessageSender {
private final ControlChannel controlChannel; private final ControlChannel controlChannel;
private Thread thread; private Thread thread;
private final BlockingQueue<DeviceMessage> queue = new ArrayBlockingQueue<>(16);
private String clipboardText;
private long ack;
public DeviceMessageSender(ControlChannel controlChannel) { public DeviceMessageSender(ControlChannel controlChannel) {
this.controlChannel = controlChannel; this.controlChannel = controlChannel;
} }
public synchronized void pushClipboardText(String text) { public void send(DeviceMessage msg) {
clipboardText = text; if (!queue.offer(msg)) {
notify(); Ln.w("Device message dropped: " + msg.getType());
} }
public synchronized void pushAckClipboard(long sequence) {
ack = sequence;
notify();
} }
private void loop() throws IOException, InterruptedException { private void loop() throws IOException, InterruptedException {
while (!Thread.currentThread().isInterrupted()) { while (!Thread.currentThread().isInterrupted()) {
String text; DeviceMessage msg = queue.take();
long sequence; controlChannel.send(msg);
synchronized (this) {
while (ack == DeviceMessage.SEQUENCE_INVALID && clipboardText == null) {
wait();
}
text = clipboardText;
clipboardText = null;
sequence = ack;
ack = DeviceMessage.SEQUENCE_INVALID;
}
if (sequence != DeviceMessage.SEQUENCE_INVALID) {
DeviceMessage event = DeviceMessage.createAckClipboard(sequence);
controlChannel.send(event);
}
if (text != null) {
DeviceMessage event = DeviceMessage.createClipboard(text);
controlChannel.send(event);
}
} }
} }

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

Loading…
Cancel
Save