mirror of
https://github.com/Genymobile/scrcpy.git
synced 2026-01-26 17:14:36 +01:00
Simplify messages reader/writer
In Java, control messages were parsed using manual buffering, which was convoluted and error-prone. Instead, read the socket directly through a DataInputStream and a BufferedInputStream. Symmetrically, use a DataOutputStream and a BufferedOutputStream to write messages.
This commit is contained in:
@@ -3,31 +3,22 @@ package com.genymobile.scrcpy.control;
|
||||
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();
|
||||
private final ControlMessageReader reader;
|
||||
private final DeviceMessageWriter writer;
|
||||
|
||||
public ControlChannel(LocalSocket controlSocket) throws IOException {
|
||||
this.inputStream = controlSocket.getInputStream();
|
||||
this.outputStream = controlSocket.getOutputStream();
|
||||
reader = new ControlMessageReader(controlSocket.getInputStream());
|
||||
writer = new DeviceMessageWriter(controlSocket.getOutputStream());
|
||||
}
|
||||
|
||||
public ControlMessage recv() throws IOException {
|
||||
ControlMessage msg = reader.next();
|
||||
while (msg == null) {
|
||||
reader.readFrom(inputStream);
|
||||
msg = reader.next();
|
||||
}
|
||||
return msg;
|
||||
return reader.read();
|
||||
}
|
||||
|
||||
public void send(DeviceMessage msg) throws IOException {
|
||||
writer.writeTo(msg, outputStream);
|
||||
writer.write(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,259 +1,152 @@
|
||||
package com.genymobile.scrcpy.control;
|
||||
|
||||
import com.genymobile.scrcpy.util.Binary;
|
||||
import com.genymobile.scrcpy.util.Ln;
|
||||
import com.genymobile.scrcpy.device.Position;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class ControlMessageReader {
|
||||
|
||||
static final int INJECT_KEYCODE_PAYLOAD_LENGTH = 13;
|
||||
static final int INJECT_TOUCH_EVENT_PAYLOAD_LENGTH = 31;
|
||||
static final int INJECT_SCROLL_EVENT_PAYLOAD_LENGTH = 20;
|
||||
static final int BACK_OR_SCREEN_ON_LENGTH = 1;
|
||||
static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1;
|
||||
static final int GET_CLIPBOARD_LENGTH = 1;
|
||||
static final int SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH = 9;
|
||||
static final int UHID_CREATE_FIXED_PAYLOAD_LENGTH = 4;
|
||||
static final int UHID_INPUT_FIXED_PAYLOAD_LENGTH = 4;
|
||||
|
||||
private static final int MESSAGE_MAX_SIZE = 1 << 18; // 256k
|
||||
|
||||
public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 14; // type: 1 byte; sequence: 8 bytes; paste flag: 1 byte; length: 4 bytes
|
||||
public static final int INJECT_TEXT_MAX_LENGTH = 300;
|
||||
|
||||
private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE];
|
||||
private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
|
||||
private final DataInputStream dis;
|
||||
|
||||
public ControlMessageReader() {
|
||||
// invariant: the buffer is always in "get" mode
|
||||
buffer.limit(0);
|
||||
public ControlMessageReader(InputStream rawInputStream) {
|
||||
dis = new DataInputStream(new BufferedInputStream(rawInputStream));
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return buffer.remaining() == rawBuffer.length;
|
||||
}
|
||||
|
||||
public void readFrom(InputStream input) throws IOException {
|
||||
if (isFull()) {
|
||||
throw new IllegalStateException("Buffer full, call next() to consume");
|
||||
}
|
||||
buffer.compact();
|
||||
int head = buffer.position();
|
||||
int r = input.read(rawBuffer, head, rawBuffer.length - head);
|
||||
if (r == -1) {
|
||||
throw new EOFException("Controller socket closed");
|
||||
}
|
||||
buffer.position(head + r);
|
||||
buffer.flip();
|
||||
}
|
||||
|
||||
public ControlMessage next() {
|
||||
if (!buffer.hasRemaining()) {
|
||||
return null;
|
||||
}
|
||||
int savedPosition = buffer.position();
|
||||
|
||||
int type = buffer.get();
|
||||
ControlMessage msg;
|
||||
public ControlMessage read() throws IOException {
|
||||
int type = dis.readUnsignedByte();
|
||||
switch (type) {
|
||||
case ControlMessage.TYPE_INJECT_KEYCODE:
|
||||
msg = parseInjectKeycode();
|
||||
break;
|
||||
return parseInjectKeycode();
|
||||
case ControlMessage.TYPE_INJECT_TEXT:
|
||||
msg = parseInjectText();
|
||||
break;
|
||||
return parseInjectText();
|
||||
case ControlMessage.TYPE_INJECT_TOUCH_EVENT:
|
||||
msg = parseInjectTouchEvent();
|
||||
break;
|
||||
return parseInjectTouchEvent();
|
||||
case ControlMessage.TYPE_INJECT_SCROLL_EVENT:
|
||||
msg = parseInjectScrollEvent();
|
||||
break;
|
||||
return parseInjectScrollEvent();
|
||||
case ControlMessage.TYPE_BACK_OR_SCREEN_ON:
|
||||
msg = parseBackOrScreenOnEvent();
|
||||
break;
|
||||
return parseBackOrScreenOnEvent();
|
||||
case ControlMessage.TYPE_GET_CLIPBOARD:
|
||||
msg = parseGetClipboard();
|
||||
break;
|
||||
return parseGetClipboard();
|
||||
case ControlMessage.TYPE_SET_CLIPBOARD:
|
||||
msg = parseSetClipboard();
|
||||
break;
|
||||
return parseSetClipboard();
|
||||
case ControlMessage.TYPE_SET_SCREEN_POWER_MODE:
|
||||
msg = parseSetScreenPowerMode();
|
||||
break;
|
||||
return parseSetScreenPowerMode();
|
||||
case ControlMessage.TYPE_EXPAND_NOTIFICATION_PANEL:
|
||||
case ControlMessage.TYPE_EXPAND_SETTINGS_PANEL:
|
||||
case ControlMessage.TYPE_COLLAPSE_PANELS:
|
||||
case ControlMessage.TYPE_ROTATE_DEVICE:
|
||||
case ControlMessage.TYPE_OPEN_HARD_KEYBOARD_SETTINGS:
|
||||
msg = ControlMessage.createEmpty(type);
|
||||
break;
|
||||
return ControlMessage.createEmpty(type);
|
||||
case ControlMessage.TYPE_UHID_CREATE:
|
||||
msg = parseUhidCreate();
|
||||
break;
|
||||
return parseUhidCreate();
|
||||
case ControlMessage.TYPE_UHID_INPUT:
|
||||
msg = parseUhidInput();
|
||||
break;
|
||||
return parseUhidInput();
|
||||
default:
|
||||
Ln.w("Unknown event type: " + type);
|
||||
msg = null;
|
||||
break;
|
||||
throw new ControlProtocolException("Unknown event type: " + type);
|
||||
}
|
||||
|
||||
if (msg == null) {
|
||||
// failure, reset savedPosition
|
||||
buffer.position(savedPosition);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
private ControlMessage parseInjectKeycode() {
|
||||
if (buffer.remaining() < INJECT_KEYCODE_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int action = Binary.toUnsigned(buffer.get());
|
||||
int keycode = buffer.getInt();
|
||||
int repeat = buffer.getInt();
|
||||
int metaState = buffer.getInt();
|
||||
private ControlMessage parseInjectKeycode() throws IOException {
|
||||
int action = dis.readUnsignedByte();
|
||||
int keycode = dis.readInt();
|
||||
int repeat = dis.readInt();
|
||||
int metaState = dis.readInt();
|
||||
return ControlMessage.createInjectKeycode(action, keycode, repeat, metaState);
|
||||
}
|
||||
|
||||
private int parseBufferLength(int sizeBytes) {
|
||||
private int parseBufferLength(int sizeBytes) throws IOException {
|
||||
assert sizeBytes > 0 && sizeBytes <= 4;
|
||||
if (buffer.remaining() < sizeBytes) {
|
||||
return -1;
|
||||
}
|
||||
int value = 0;
|
||||
for (int i = 0; i < sizeBytes; ++i) {
|
||||
value = (value << 8) | (buffer.get() & 0xFF);
|
||||
value = (value << 8) | dis.readUnsignedByte();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String parseString() {
|
||||
int len = parseBufferLength(4);
|
||||
if (len == -1 || buffer.remaining() < len) {
|
||||
return null;
|
||||
}
|
||||
int position = buffer.position();
|
||||
// Move the buffer position to consume the text
|
||||
buffer.position(position + len);
|
||||
return new String(rawBuffer, position, len, StandardCharsets.UTF_8);
|
||||
private String parseString() throws IOException {
|
||||
byte[] data = parseByteArray(4);
|
||||
return new String(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private byte[] parseByteArray(int sizeBytes) {
|
||||
private byte[] parseByteArray(int sizeBytes) throws IOException {
|
||||
int len = parseBufferLength(sizeBytes);
|
||||
if (len == -1 || buffer.remaining() < len) {
|
||||
return null;
|
||||
}
|
||||
byte[] data = new byte[len];
|
||||
buffer.get(data);
|
||||
dis.readFully(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
private ControlMessage parseInjectText() {
|
||||
private ControlMessage parseInjectText() throws IOException {
|
||||
String text = parseString();
|
||||
if (text == null) {
|
||||
return null;
|
||||
}
|
||||
return ControlMessage.createInjectText(text);
|
||||
}
|
||||
|
||||
private ControlMessage parseInjectTouchEvent() {
|
||||
if (buffer.remaining() < INJECT_TOUCH_EVENT_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int action = Binary.toUnsigned(buffer.get());
|
||||
long pointerId = buffer.getLong();
|
||||
Position position = readPosition(buffer);
|
||||
float pressure = Binary.u16FixedPointToFloat(buffer.getShort());
|
||||
int actionButton = buffer.getInt();
|
||||
int buttons = buffer.getInt();
|
||||
private ControlMessage parseInjectTouchEvent() throws IOException {
|
||||
int action = dis.readUnsignedByte();
|
||||
long pointerId = dis.readLong();
|
||||
Position position = parsePosition();
|
||||
float pressure = Binary.u16FixedPointToFloat(dis.readShort());
|
||||
int actionButton = dis.readInt();
|
||||
int buttons = dis.readInt();
|
||||
return ControlMessage.createInjectTouchEvent(action, pointerId, position, pressure, actionButton, buttons);
|
||||
}
|
||||
|
||||
private ControlMessage parseInjectScrollEvent() {
|
||||
if (buffer.remaining() < INJECT_SCROLL_EVENT_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
Position position = readPosition(buffer);
|
||||
float hScroll = Binary.i16FixedPointToFloat(buffer.getShort());
|
||||
float vScroll = Binary.i16FixedPointToFloat(buffer.getShort());
|
||||
int buttons = buffer.getInt();
|
||||
private ControlMessage parseInjectScrollEvent() throws IOException {
|
||||
Position position = parsePosition();
|
||||
float hScroll = Binary.i16FixedPointToFloat(dis.readShort());
|
||||
float vScroll = Binary.i16FixedPointToFloat(dis.readShort());
|
||||
int buttons = dis.readInt();
|
||||
return ControlMessage.createInjectScrollEvent(position, hScroll, vScroll, buttons);
|
||||
}
|
||||
|
||||
private ControlMessage parseBackOrScreenOnEvent() {
|
||||
if (buffer.remaining() < BACK_OR_SCREEN_ON_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int action = Binary.toUnsigned(buffer.get());
|
||||
private ControlMessage parseBackOrScreenOnEvent() throws IOException {
|
||||
int action = dis.readUnsignedByte();
|
||||
return ControlMessage.createBackOrScreenOn(action);
|
||||
}
|
||||
|
||||
private ControlMessage parseGetClipboard() {
|
||||
if (buffer.remaining() < GET_CLIPBOARD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int copyKey = Binary.toUnsigned(buffer.get());
|
||||
private ControlMessage parseGetClipboard() throws IOException {
|
||||
int copyKey = dis.readUnsignedByte();
|
||||
return ControlMessage.createGetClipboard(copyKey);
|
||||
}
|
||||
|
||||
private ControlMessage parseSetClipboard() {
|
||||
if (buffer.remaining() < SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
long sequence = buffer.getLong();
|
||||
boolean paste = buffer.get() != 0;
|
||||
private ControlMessage parseSetClipboard() throws IOException {
|
||||
long sequence = dis.readLong();
|
||||
boolean paste = dis.readByte() != 0;
|
||||
String text = parseString();
|
||||
if (text == null) {
|
||||
return null;
|
||||
}
|
||||
return ControlMessage.createSetClipboard(sequence, text, paste);
|
||||
}
|
||||
|
||||
private ControlMessage parseSetScreenPowerMode() {
|
||||
if (buffer.remaining() < SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int mode = buffer.get();
|
||||
private ControlMessage parseSetScreenPowerMode() throws IOException {
|
||||
int mode = dis.readUnsignedByte();
|
||||
return ControlMessage.createSetScreenPowerMode(mode);
|
||||
}
|
||||
|
||||
private ControlMessage parseUhidCreate() {
|
||||
if (buffer.remaining() < UHID_CREATE_FIXED_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int id = buffer.getShort();
|
||||
private ControlMessage parseUhidCreate() throws IOException {
|
||||
int id = dis.readUnsignedShort();
|
||||
byte[] data = parseByteArray(2);
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
return ControlMessage.createUhidCreate(id, data);
|
||||
}
|
||||
|
||||
private ControlMessage parseUhidInput() {
|
||||
if (buffer.remaining() < UHID_INPUT_FIXED_PAYLOAD_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
int id = buffer.getShort();
|
||||
private ControlMessage parseUhidInput() throws IOException {
|
||||
int id = dis.readUnsignedShort();
|
||||
byte[] data = parseByteArray(2);
|
||||
if (data == null) {
|
||||
return null;
|
||||
}
|
||||
return ControlMessage.createUhidInput(id, data);
|
||||
}
|
||||
|
||||
private static Position readPosition(ByteBuffer buffer) {
|
||||
int x = buffer.getInt();
|
||||
int y = buffer.getInt();
|
||||
int screenWidth = Binary.toUnsigned(buffer.getShort());
|
||||
int screenHeight = Binary.toUnsigned(buffer.getShort());
|
||||
private Position parsePosition() throws IOException {
|
||||
int x = dis.readInt();
|
||||
int y = dis.readInt();
|
||||
int screenWidth = dis.readUnsignedShort();
|
||||
int screenHeight = dis.readUnsignedShort();
|
||||
return new Position(x, y, screenWidth, screenHeight);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.genymobile.scrcpy.control;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ControlProtocolException extends IOException {
|
||||
public ControlProtocolException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.genymobile.scrcpy.control;
|
||||
|
||||
import com.genymobile.scrcpy.util.Ln;
|
||||
import com.genymobile.scrcpy.util.StringUtils;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class DeviceMessageWriter {
|
||||
@@ -13,35 +13,35 @@ public class DeviceMessageWriter {
|
||||
private static final int MESSAGE_MAX_SIZE = 1 << 18; // 256k
|
||||
public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 5; // type: 1 byte; length: 4 bytes
|
||||
|
||||
private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE];
|
||||
private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer);
|
||||
private final DataOutputStream dos;
|
||||
|
||||
public void writeTo(DeviceMessage msg, OutputStream output) throws IOException {
|
||||
buffer.clear();
|
||||
buffer.put((byte) msg.getType());
|
||||
switch (msg.getType()) {
|
||||
public DeviceMessageWriter(OutputStream rawOutputStream) {
|
||||
dos = new DataOutputStream(new BufferedOutputStream(rawOutputStream));
|
||||
}
|
||||
|
||||
public void write(DeviceMessage msg) throws IOException {
|
||||
int type = msg.getType();
|
||||
dos.writeByte(type);
|
||||
switch (type) {
|
||||
case DeviceMessage.TYPE_CLIPBOARD:
|
||||
String text = msg.getText();
|
||||
byte[] raw = text.getBytes(StandardCharsets.UTF_8);
|
||||
int len = StringUtils.getUtf8TruncationIndex(raw, CLIPBOARD_TEXT_MAX_LENGTH);
|
||||
buffer.putInt(len);
|
||||
buffer.put(raw, 0, len);
|
||||
output.write(rawBuffer, 0, buffer.position());
|
||||
dos.writeInt(len);
|
||||
dos.write(raw, 0, len);
|
||||
break;
|
||||
case DeviceMessage.TYPE_ACK_CLIPBOARD:
|
||||
buffer.putLong(msg.getSequence());
|
||||
output.write(rawBuffer, 0, buffer.position());
|
||||
dos.writeLong(msg.getSequence());
|
||||
break;
|
||||
case DeviceMessage.TYPE_UHID_OUTPUT:
|
||||
buffer.putShort((short) msg.getId());
|
||||
dos.writeShort(msg.getId());
|
||||
byte[] data = msg.getData();
|
||||
buffer.putShort((short) data.length);
|
||||
buffer.put(data);
|
||||
output.write(rawBuffer, 0, buffer.position());
|
||||
dos.writeShort(data.length);
|
||||
dos.write(data);
|
||||
break;
|
||||
default:
|
||||
Ln.w("Unknown device message: " + msg.getType());
|
||||
break;
|
||||
throw new ControlProtocolException("Unknown event type: " + type);
|
||||
}
|
||||
dos.flush();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user