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>
This commit is contained in:
parent
840680f546
commit
021c5d371a
@ -413,7 +413,8 @@ public class Controller implements AsyncProcessor {
|
||||
if (!clipboardAutosync) {
|
||||
String clipboardText = Device.getClipboardText();
|
||||
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) {
|
||||
// Acknowledgement requested
|
||||
sender.pushAckClipboard(sequence);
|
||||
DeviceMessage msg = DeviceMessage.createAckClipboard(sequence);
|
||||
sender.send(msg);
|
||||
}
|
||||
|
||||
return ok;
|
||||
|
@ -5,8 +5,6 @@ public final class DeviceMessage {
|
||||
public static final int TYPE_CLIPBOARD = 0;
|
||||
public static final int TYPE_ACK_CLIPBOARD = 1;
|
||||
|
||||
public static final long SEQUENCE_INVALID = ControlMessage.SEQUENCE_INVALID;
|
||||
|
||||
private int type;
|
||||
private String text;
|
||||
private long sequence;
|
||||
|
@ -1,54 +1,30 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
|
||||
public final class DeviceMessageSender {
|
||||
|
||||
private final ControlChannel controlChannel;
|
||||
|
||||
private Thread thread;
|
||||
|
||||
private String clipboardText;
|
||||
|
||||
private long ack;
|
||||
private final BlockingQueue<DeviceMessage> queue = new ArrayBlockingQueue<>(16);
|
||||
|
||||
public DeviceMessageSender(ControlChannel controlChannel) {
|
||||
this.controlChannel = controlChannel;
|
||||
}
|
||||
|
||||
public synchronized void pushClipboardText(String text) {
|
||||
clipboardText = text;
|
||||
notify();
|
||||
}
|
||||
|
||||
public synchronized void pushAckClipboard(long sequence) {
|
||||
ack = sequence;
|
||||
notify();
|
||||
public void send(DeviceMessage msg) {
|
||||
if (!queue.offer(msg)) {
|
||||
Ln.w("Device message dropped: " + msg.getType());
|
||||
}
|
||||
}
|
||||
|
||||
private void loop() throws IOException, InterruptedException {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
String text;
|
||||
long sequence;
|
||||
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);
|
||||
}
|
||||
DeviceMessage msg = queue.take();
|
||||
controlChannel.send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,10 @@ public final class Server {
|
||||
if (control) {
|
||||
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 -> {
|
||||
DeviceMessage msg = DeviceMessage.createClipboard(text);
|
||||
controller.getSender().send(msg);
|
||||
});
|
||||
asyncProcessors.add(controller);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user