feat: 协议消息类型改名,为以后更多服务器命令做预留
This commit is contained in:
parent
c05c02c098
commit
735ead43b5
|
|
@ -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);
|
ssize_t written = write(*output_fd, payload, payload_len);
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
DEBUG_LOG("写入输出失败: %s", strerror(errno));
|
DEBUG_LOG("写入输出失败: %s", strerror(errno));
|
||||||
|
|
|
||||||
|
|
@ -11,14 +11,14 @@
|
||||||
|
|
||||||
// 消息类型枚举
|
// 消息类型枚举
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MSG_TYPE_INIT = 1, // 初始化连接,发送命令信息
|
MSG_TYPE_INIT = 1, // 初始化连接,发送命令信息
|
||||||
MSG_TYPE_WINDOW_SIZE_UPDATE = 2, // 终端窗口大小更新
|
MSG_TYPE_WINDOW_SIZE_UPDATE = 2, // 终端窗口大小更新
|
||||||
MSG_TYPE_SERVER_RESPONSE = 3, // 服务器响应消息
|
MSG_TYPE_SERVER_RENDER = 3, // 服务器渲染消息
|
||||||
MSG_TYPE_CLOSE = 4, // 关闭连接
|
MSG_TYPE_CLOSE = 4, // 关闭连接
|
||||||
MSG_TYPE_TERMINAL_INPUT = 5, // 终端输入数据(键盘、鼠标等)
|
MSG_TYPE_TERMINAL_INPUT = 5, // 终端输入数据(键盘、鼠标等)
|
||||||
MSG_TYPE_TERMINAL_OUTPUT = 6, // 终端输出数据
|
MSG_TYPE_TERMINAL_OUTPUT = 6, // 终端输出数据
|
||||||
MSG_TYPE_MOUSE_EVENT = 7, // 鼠标事件
|
MSG_TYPE_MOUSE_EVENT = 7, // 鼠标事件
|
||||||
MSG_TYPE_KEY_EVENT = 8 // 键盘事件
|
MSG_TYPE_KEY_EVENT = 8 // 键盘事件
|
||||||
} MessageType;
|
} MessageType;
|
||||||
|
|
||||||
// 消息头结构(固定大小)
|
// 消息头结构(固定大小)
|
||||||
|
|
@ -28,29 +28,30 @@ typedef enum {
|
||||||
// bit 2: MSG_FLAG_COMPRESS_HC - 使用高压缩比模式
|
// bit 2: MSG_FLAG_COMPRESS_HC - 使用高压缩比模式
|
||||||
// 高 16 位: 压缩前的原始大小(用于解压缓冲区分配)
|
// 高 16 位: 压缩前的原始大小(用于解压缓冲区分配)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t magic; // 魔数,用于验证 0x42534D54 ("BSMT")
|
uint32_t magic; // 魔数,用于验证 0x42534D54 ("BSMT")
|
||||||
uint32_t type; // 消息类型
|
uint32_t type; // 消息类型
|
||||||
uint32_t payload_len; // 载荷长度(压缩后)
|
uint32_t payload_len; // 载荷长度(压缩后)
|
||||||
uint32_t reserved; // 低 16 位: 压缩标志; 高 16 位: 原始大小/256
|
uint32_t reserved; // 低 16 位: 压缩标志; 高 16 位: 原始大小/256
|
||||||
} __attribute__((packed)) MessageHeader;
|
} __attribute__((packed)) MessageHeader;
|
||||||
|
|
||||||
// 从 reserved 字段提取压缩标志和原始大小
|
// 从 reserved 字段提取压缩标志和原始大小
|
||||||
#define GET_COMPRESS_FLAGS(reserved) ((reserved) & 0xFFFF)
|
#define GET_COMPRESS_FLAGS(reserved) ((reserved) & 0xFFFF)
|
||||||
#define GET_ORIGINAL_SIZE_HINT(reserved) (((reserved) >> 16) * 256)
|
#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 {
|
typedef struct {
|
||||||
uint32_t is_tty; // 是否为TTY
|
uint32_t is_tty; // 是否为TTY
|
||||||
uint16_t rows; // 行数
|
uint16_t rows; // 行数
|
||||||
uint16_t cols; // 列数
|
uint16_t cols; // 列数
|
||||||
uint16_t x_pixel; // X像素
|
uint16_t x_pixel; // X像素
|
||||||
uint16_t y_pixel; // Y像素
|
uint16_t y_pixel; // Y像素
|
||||||
uint32_t has_termios; // 是否有termios属性
|
uint32_t has_termios; // 是否有termios属性
|
||||||
uint32_t input_flags; // termios输入标志
|
uint32_t input_flags; // termios输入标志
|
||||||
uint32_t output_flags; // termios输出标志
|
uint32_t output_flags; // termios输出标志
|
||||||
uint32_t control_flags; // termios控制标志
|
uint32_t control_flags; // termios控制标志
|
||||||
uint32_t local_flags; // termios本地标志
|
uint32_t local_flags; // termios本地标志
|
||||||
} __attribute__((packed)) TerminalInfoFixed;
|
} __attribute__((packed)) TerminalInfoFixed;
|
||||||
|
|
||||||
// 鼠标事件类型
|
// 鼠标事件类型
|
||||||
|
|
@ -73,9 +74,9 @@ typedef struct {
|
||||||
|
|
||||||
// 键盘事件结构
|
// 键盘事件结构
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t key_code; // 键码
|
uint32_t key_code; // 键码
|
||||||
uint32_t modifiers; // 修饰键
|
uint32_t modifiers; // 修饰键
|
||||||
uint32_t is_press; // 1=按下,0=释放
|
uint32_t is_press; // 1=按下,0=释放
|
||||||
} __attribute__((packed)) KeyEvent;
|
} __attribute__((packed)) KeyEvent;
|
||||||
|
|
||||||
// 魔数定义
|
// 魔数定义
|
||||||
|
|
@ -84,23 +85,26 @@ typedef struct {
|
||||||
#ifdef HAVE_LZ4
|
#ifdef HAVE_LZ4
|
||||||
// 协议上下文(包含压缩状态)
|
// 协议上下文(包含压缩状态)
|
||||||
typedef struct {
|
typedef struct {
|
||||||
CompressionContext compress_ctx; // 压缩上下文
|
CompressionContext compress_ctx; // 压缩上下文
|
||||||
int compression_enabled; // 是否启用压缩
|
int compression_enabled; // 是否启用压缩
|
||||||
} ProtocolContext;
|
} 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);
|
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);
|
uint32_t* payload_len, uint32_t* original_len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 函数声明
|
// 函数声明
|
||||||
int write_message(int sock, MessageType type, const void* payload, uint32_t payload_len);
|
int write_message(int sock, MessageType type, const void* payload,
|
||||||
int read_message(int sock, MessageType* type, void** payload, uint32_t* payload_len);
|
uint32_t payload_len);
|
||||||
|
int read_message(int sock, MessageType* type, void** payload,
|
||||||
|
uint32_t* payload_len);
|
||||||
|
|
||||||
void free_message_payload(void* payload);
|
void free_message_payload(void* payload);
|
||||||
|
|
||||||
|
|
@ -110,4 +114,4 @@ int restore_terminal_mode(int fd);
|
||||||
int enable_mouse_tracking(int fd);
|
int enable_mouse_tracking(int fd);
|
||||||
int disable_mouse_tracking(int fd);
|
int disable_mouse_tracking(int fd);
|
||||||
|
|
||||||
#endif // SOCKET_PROTOCOL_H
|
#endif // SOCKET_PROTOCOL_H
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue