Files
open-xiaoai/packages/client-v2/src/net/protocol.rs
T

104 lines
2.7 KiB
Rust
Raw Normal View History

2026-01-04 11:24:37 +08:00
//! # Protocol - 通信协议定义
//!
//! 定义 Client 和 Server 之间的所有通信协议。
2026-01-02 13:04:19 +08:00
use crate::audio::config::AudioConfig;
2026-01-04 11:24:37 +08:00
use crate::net::command::{Command, CommandResult};
use crate::net::event::{ClientEvent, ServerEvent};
2026-01-02 09:59:29 +08:00
use serde::{Deserialize, Serialize};
2026-01-04 11:24:37 +08:00
// ==================== 基础类型 ====================
/// 客户端信息
2026-01-03 19:17:13 +08:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientInfo {
2026-01-02 13:04:19 +08:00
pub model: String,
2026-01-03 19:17:13 +08:00
pub serial_number: String,
2026-01-02 17:23:24 +08:00
}
2026-01-04 11:24:37 +08:00
// ==================== 控制包 ====================
/// 控制包 - TCP 通道传输的所有消息类型
2026-01-02 09:59:29 +08:00
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ControlPacket {
2026-01-04 11:24:37 +08:00
// ========== 服务发现 ==========
/// 服务发现广播
Discovery { protocol: String, port: u16 },
2026-01-02 13:04:19 +08:00
2026-01-04 11:24:37 +08:00
// ========== 握手 ==========
/// 服务端握手
2026-01-03 19:17:13 +08:00
ServerHello {
auth: String,
version: String,
2026-01-04 11:24:37 +08:00
udp_port: u16,
2026-01-02 09:59:29 +08:00
},
2026-01-04 11:24:37 +08:00
/// 客户端握手
2026-01-03 19:17:13 +08:00
ClientHello {
auth: String,
version: String,
2026-01-04 11:24:37 +08:00
udp_port: u16,
2026-01-03 19:17:13 +08:00
info: ClientInfo,
2026-01-02 09:59:29 +08:00
},
2026-01-03 19:17:13 +08:00
2026-01-04 11:24:37 +08:00
// ========== 心跳 ==========
2026-01-03 19:17:13 +08:00
Ping,
Pong,
2026-01-02 13:04:19 +08:00
2026-01-04 11:24:37 +08:00
// ========== RPC ==========
/// RPC 请求(新版,使用 Command 类型)
RpcRequest { id: u32, command: Command },
/// RPC 响应(新版,使用 CommandResult 类型)
RpcResponse { id: u32, result: CommandResult },
2026-01-02 13:04:19 +08:00
2026-01-04 11:24:37 +08:00
// ========== 事件 ==========
/// 服务端事件推送
ServerEvent(ServerEvent),
/// 客户端事件推送
ClientEvent(ClientEvent),
// ========== 音频控制 ==========
/// 开始录音
StartRecording { config: AudioConfig },
/// 停止录音
2026-01-03 19:17:13 +08:00
StopRecording,
2026-01-04 11:24:37 +08:00
/// 开始播放
StartPlayback { config: AudioConfig },
/// 停止播放
2026-01-03 19:17:13 +08:00
StopPlayback,
2026-01-04 11:24:37 +08:00
// ========== 订阅管理 ==========
/// 订阅事件类型
Subscribe { event_types: Vec<String> },
/// 取消订阅
Unsubscribe { event_types: Vec<String> },
2026-01-02 13:04:19 +08:00
}
2026-01-04 11:24:37 +08:00
// ==================== 音频包 ====================
/// 音频数据包 - UDP 通道传输
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AudioPacket {
/// Opus 编码的音频数据
pub data: Vec<u8>,
}
// ==================== 兼容性:旧版 RPC 结果 ====================
/// 旧版 RPC 结果(保持向后兼容)
2026-01-02 17:23:24 +08:00
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
2026-01-02 13:04:19 +08:00
pub struct RpcResult {
pub stdout: String,
pub stderr: String,
pub code: i32,
2026-01-02 09:59:29 +08:00
}
2026-01-04 11:24:37 +08:00
impl From<crate::net::command::ShellResponse> for RpcResult {
fn from(resp: crate::net::command::ShellResponse) -> Self {
Self {
stdout: resp.stdout,
stderr: resp.stderr,
code: resp.exit_code,
}
}
2026-01-02 09:59:29 +08:00
}