From 8f314c74b073a7f844de16539657a264f05662d6 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 4 Jun 2020 21:09:42 +0200 Subject: [PATCH] Reorganize message size constants Make the max clipboard text length depend on the max message size. --- app/src/control_msg.h | 8 +++++--- app/src/device_msg.h | 5 +++-- .../java/com/genymobile/scrcpy/ControlMessageReader.java | 8 ++++---- .../java/com/genymobile/scrcpy/DeviceMessageWriter.java | 6 +++--- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/src/control_msg.h b/app/src/control_msg.h index 0e85c97e..1a5449a0 100644 --- a/app/src/control_msg.h +++ b/app/src/control_msg.h @@ -10,10 +10,12 @@ #include "android/keycodes.h" #include "common.h" +#define CONTROL_MSG_SERIALIZED_MAX_SIZE 4096 + #define CONTROL_MSG_INJECT_TEXT_MAX_LENGTH 300 -#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH 4092 -#define CONTROL_MSG_SERIALIZED_MAX_SIZE \ - (4 + CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH) +// type: 1 byte; paste flag: 1 byte; length: 2 bytes +#define CONTROL_MSG_CLIPBOARD_TEXT_MAX_LENGTH \ + (CONTROL_MSG_SERIALIZED_MAX_SIZE - 4) #define POINTER_ID_MOUSE UINT64_C(-1); diff --git a/app/src/device_msg.h b/app/src/device_msg.h index 04723597..f19630bf 100644 --- a/app/src/device_msg.h +++ b/app/src/device_msg.h @@ -7,8 +7,9 @@ #include "config.h" -#define DEVICE_MSG_TEXT_MAX_LENGTH 4093 -#define DEVICE_MSG_SERIALIZED_MAX_SIZE (3 + DEVICE_MSG_TEXT_MAX_LENGTH) +#define DEVICE_MSG_SERIALIZED_MAX_SIZE 4096 +// type: 1 byte; length: 2 bytes +#define DEVICE_MSG_TEXT_MAX_LENGTH (DEVICE_MSG_SERIALIZED_MAX_SIZE - 3) enum device_msg_type { DEVICE_MSG_TYPE_CLIPBOARD, diff --git a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java index fbf49a61..f80ffebb 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java +++ b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java @@ -14,12 +14,12 @@ public class ControlMessageReader { static final int SET_SCREEN_POWER_MODE_PAYLOAD_LENGTH = 1; static final int SET_CLIPBOARD_FIXED_PAYLOAD_LENGTH = 1; - public static final int CLIPBOARD_TEXT_MAX_LENGTH = 4092; // 4096 - 1 (type) - 1 (parse flag) - 2 (length) + private static final int MESSAGE_MAX_SIZE = 4096; + + public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 4; // type: 1 byte; paste flag: 1 byte; length: 2 bytes public static final int INJECT_TEXT_MAX_LENGTH = 300; - private static final int RAW_BUFFER_SIZE = 4096; - - private final byte[] rawBuffer = new byte[RAW_BUFFER_SIZE]; + private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE]; private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer); private final byte[] textBuffer = new byte[CLIPBOARD_TEXT_MAX_LENGTH]; diff --git a/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java b/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java index 6c7f3634..ea54d533 100644 --- a/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java +++ b/server/src/main/java/com/genymobile/scrcpy/DeviceMessageWriter.java @@ -7,10 +7,10 @@ import java.nio.charset.StandardCharsets; public class DeviceMessageWriter { - public static final int CLIPBOARD_TEXT_MAX_LENGTH = 4093; - private static final int MAX_EVENT_SIZE = CLIPBOARD_TEXT_MAX_LENGTH + 3; + private static final int MESSAGE_MAX_SIZE = 4096; + public static final int CLIPBOARD_TEXT_MAX_LENGTH = MESSAGE_MAX_SIZE - 3; // type: 1 byte; length: 2 bytes - private final byte[] rawBuffer = new byte[MAX_EVENT_SIZE]; + private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE]; private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer); public void writeTo(DeviceMessage msg, OutputStream output) throws IOException {