From 735ead43b584915b7fdbceb981a9abdd0776ee72 Mon Sep 17 00:00:00 2001 From: "QCQCQC@Opi5" Date: Sat, 13 Dec 2025 18:42:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=8F=E8=AE=AE=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=94=B9=E5=90=8D=EF=BC=8C=E4=B8=BA=E4=BB=A5?= =?UTF-8?q?=E5=90=8E=E6=9B=B4=E5=A4=9A=E6=9C=8D=E5=8A=A1=E5=99=A8=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=81=9A=E9=A2=84=E7=95=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client.c | 2 +- src/socket_protocol.h | 72 +++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/client.c b/src/client.c index dbae468..5200b5a 100644 --- a/src/client.c +++ b/src/client.c @@ -532,7 +532,7 @@ static void* response_listener_thread(void* arg) { } /* 处理服务端响应 */ - if (msg_type == MSG_TYPE_SERVER_RESPONSE && payload != NULL) { + if (msg_type == MSG_TYPE_SERVER_RENDER && payload != NULL) { ssize_t written = write(*output_fd, payload, payload_len); if (written < 0) { DEBUG_LOG("写入输出失败: %s", strerror(errno)); diff --git a/src/socket_protocol.h b/src/socket_protocol.h index ab00f4e..efe4816 100644 --- a/src/socket_protocol.h +++ b/src/socket_protocol.h @@ -11,14 +11,14 @@ // 消息类型枚举 typedef enum { - MSG_TYPE_INIT = 1, // 初始化连接,发送命令信息 - MSG_TYPE_WINDOW_SIZE_UPDATE = 2, // 终端窗口大小更新 - MSG_TYPE_SERVER_RESPONSE = 3, // 服务器响应消息 - MSG_TYPE_CLOSE = 4, // 关闭连接 - MSG_TYPE_TERMINAL_INPUT = 5, // 终端输入数据(键盘、鼠标等) - MSG_TYPE_TERMINAL_OUTPUT = 6, // 终端输出数据 - MSG_TYPE_MOUSE_EVENT = 7, // 鼠标事件 - MSG_TYPE_KEY_EVENT = 8 // 键盘事件 + MSG_TYPE_INIT = 1, // 初始化连接,发送命令信息 + MSG_TYPE_WINDOW_SIZE_UPDATE = 2, // 终端窗口大小更新 + MSG_TYPE_SERVER_RENDER = 3, // 服务器渲染消息 + MSG_TYPE_CLOSE = 4, // 关闭连接 + MSG_TYPE_TERMINAL_INPUT = 5, // 终端输入数据(键盘、鼠标等) + MSG_TYPE_TERMINAL_OUTPUT = 6, // 终端输出数据 + MSG_TYPE_MOUSE_EVENT = 7, // 鼠标事件 + MSG_TYPE_KEY_EVENT = 8 // 键盘事件 } MessageType; // 消息头结构(固定大小) @@ -28,29 +28,30 @@ typedef enum { // bit 2: MSG_FLAG_COMPRESS_HC - 使用高压缩比模式 // 高 16 位: 压缩前的原始大小(用于解压缓冲区分配) typedef struct { - uint32_t magic; // 魔数,用于验证 0x42534D54 ("BSMT") - uint32_t type; // 消息类型 - uint32_t payload_len; // 载荷长度(压缩后) - uint32_t reserved; // 低 16 位: 压缩标志; 高 16 位: 原始大小/256 + uint32_t magic; // 魔数,用于验证 0x42534D54 ("BSMT") + uint32_t type; // 消息类型 + uint32_t payload_len; // 载荷长度(压缩后) + uint32_t reserved; // 低 16 位: 压缩标志; 高 16 位: 原始大小/256 } __attribute__((packed)) MessageHeader; // 从 reserved 字段提取压缩标志和原始大小 #define GET_COMPRESS_FLAGS(reserved) ((reserved) & 0xFFFF) #define GET_ORIGINAL_SIZE_HINT(reserved) (((reserved) >> 16) * 256) -#define MAKE_RESERVED(flags, orig_size) (((flags) & 0xFFFF) | (((orig_size) / 256) << 16)) +#define MAKE_RESERVED(flags, orig_size) \ + (((flags) & 0xFFFF) | (((orig_size) / 256) << 16)) // 终端信息结构 typedef struct { - uint32_t is_tty; // 是否为TTY - uint16_t rows; // 行数 - uint16_t cols; // 列数 - uint16_t x_pixel; // X像素 - uint16_t y_pixel; // Y像素 - uint32_t has_termios; // 是否有termios属性 - uint32_t input_flags; // termios输入标志 - uint32_t output_flags; // termios输出标志 - uint32_t control_flags; // termios控制标志 - uint32_t local_flags; // termios本地标志 + uint32_t is_tty; // 是否为TTY + uint16_t rows; // 行数 + uint16_t cols; // 列数 + uint16_t x_pixel; // X像素 + uint16_t y_pixel; // Y像素 + uint32_t has_termios; // 是否有termios属性 + uint32_t input_flags; // termios输入标志 + uint32_t output_flags; // termios输出标志 + uint32_t control_flags; // termios控制标志 + uint32_t local_flags; // termios本地标志 } __attribute__((packed)) TerminalInfoFixed; // 鼠标事件类型 @@ -73,9 +74,9 @@ typedef struct { // 键盘事件结构 typedef struct { - uint32_t key_code; // 键码 - uint32_t modifiers; // 修饰键 - uint32_t is_press; // 1=按下,0=释放 + uint32_t key_code; // 键码 + uint32_t modifiers; // 修饰键 + uint32_t is_press; // 1=按下,0=释放 } __attribute__((packed)) KeyEvent; // 魔数定义 @@ -84,23 +85,26 @@ typedef struct { #ifdef HAVE_LZ4 // 协议上下文(包含压缩状态) typedef struct { - CompressionContext compress_ctx; // 压缩上下文 - int compression_enabled; // 是否启用压缩 + CompressionContext compress_ctx; // 压缩上下文 + int compression_enabled; // 是否启用压缩 } ProtocolContext; // 初始化协议上下文 -void protocol_init(ProtocolContext* ctx, CompressionType compress_type, int compress_level); +void protocol_init(ProtocolContext* ctx, CompressionType compress_type, + int compress_level); // 带压缩支持的消息读写 -int write_message_compressed(int sock, ProtocolContext* ctx, MessageType type, +int write_message_compressed(int sock, ProtocolContext* ctx, MessageType type, const void* payload, uint32_t payload_len); -int read_message_decompressed(int sock, MessageType* type, void** payload, +int read_message_decompressed(int sock, MessageType* type, void** payload, uint32_t* payload_len, uint32_t* original_len); #endif // 函数声明 -int write_message(int sock, MessageType type, const void* payload, uint32_t payload_len); -int read_message(int sock, MessageType* type, void** payload, uint32_t* payload_len); +int write_message(int sock, MessageType type, const void* payload, + uint32_t payload_len); +int read_message(int sock, MessageType* type, void** payload, + uint32_t* payload_len); void free_message_payload(void* payload); @@ -110,4 +114,4 @@ int restore_terminal_mode(int fd); int enable_mouse_tracking(int fd); int disable_mouse_tracking(int fd); -#endif // SOCKET_PROTOCOL_H +#endif // SOCKET_PROTOCOL_H