feat(core): 新增上行事件队列与 msgType=9 会话配置帧
- demo_server 增加 inbound 队列,mirror 应答与上行剪贴板入队供界面轮询(上限 64,丢最旧) - 新增 peek_inbound_event / pop_inbound_event,FFI 暴露 ttstd_event_next(缓冲不足时不出队) - 协议新增 MSG_CONFIG=9:payload 为 UTF-8 JSON,binary 编解码与 ffi 下发均已支持 - 补充 Config 事件编解码单测
This commit is contained in:
1 parent
cbbc2a642d
commit
e43e51d9c3
4 files changed
+154
-5
No files matched your search
@@ -6,6 +6,7 @@
|
||||
//! 3. 后续控制帧使用 `SecureControlChannel`(8 字节明头 + 12 字节 nonce + 密文 + 16 字节 tag)。
|
||||
//! 4. 视频/音频通过 UDP 41000/41002 发送,使用 `MediaPackets` 的 13 字节头 + 4 字节 HMAC。
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, TcpStream, UdpSocket};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
@@ -14,8 +15,8 @@ use std::thread::{self, JoinHandle};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::control::events::{
|
||||
ClipboardEvent, KeyAction, KeyboardEvent, MirrorEvent, MouseAction, MouseEvent, TouchAction,
|
||||
TouchEvent, WheelEvent,
|
||||
ClipboardEvent, ConfigEvent, KeyAction, KeyboardEvent, MirrorEvent, MouseAction, MouseEvent,
|
||||
TouchAction, TouchEvent, WheelEvent, MSG_CLIPBOARD, MSG_MIRROR,
|
||||
};
|
||||
use crate::control::{ControlEvent, ControlEventKind};
|
||||
|
||||
@@ -72,6 +73,44 @@ struct ControlSender {
|
||||
channel: SecureControlChannel,
|
||||
}
|
||||
|
||||
/// 被控端上行的控制事件(mirror 应答 / 剪贴板),供 UI 侧轮询消费。
|
||||
///
|
||||
/// 与 `events`(诊断用的字符串环形缓冲)分开:这里只放界面真正要处理的事件,
|
||||
/// 取走即出队,不会因为诊断日志刷太快把有用事件挤掉。
|
||||
#[derive(Debug, Clone)]
|
||||
struct InboundEvent {
|
||||
msg_type: u8,
|
||||
action: i32,
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl InboundEvent {
|
||||
fn to_json(&self) -> String {
|
||||
format!(
|
||||
"{{\"type\":{},\"action\":{},\"text\":\"{}\"}}",
|
||||
self.msg_type,
|
||||
self.action,
|
||||
escape_json(&self.text)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_json(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
for ch in text.chars() {
|
||||
match ch {
|
||||
'"' => out.push_str("\\\""),
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'\n' => out.push_str("\\n"),
|
||||
'\r' => out.push_str("\\r"),
|
||||
'\t' => out.push_str("\\t"),
|
||||
control if (control as u32) < 0x20 => out.push(' '),
|
||||
other => out.push(other),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
struct Shared {
|
||||
running: AtomicBool,
|
||||
peer_connected: AtomicBool,
|
||||
@@ -82,6 +121,7 @@ struct Shared {
|
||||
audio_key: Mutex<Option<[u8; 32]>>,
|
||||
sender: Mutex<Option<ControlSender>>,
|
||||
events: Mutex<Vec<String>>,
|
||||
inbound: Mutex<VecDeque<InboundEvent>>,
|
||||
}
|
||||
|
||||
impl Shared {
|
||||
@@ -96,6 +136,7 @@ impl Shared {
|
||||
audio_key: Mutex::new(None),
|
||||
sender: Mutex::new(None),
|
||||
events: Mutex::new(Vec::new()),
|
||||
inbound: Mutex::new(VecDeque::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,6 +149,22 @@ impl Shared {
|
||||
}
|
||||
}
|
||||
|
||||
fn push_inbound(&self, event: InboundEvent) {
|
||||
if let Ok(mut queue) = self.inbound.lock() {
|
||||
// 界面没及时消费时丢最旧的,避免无限增长。
|
||||
if queue.len() >= 64 {
|
||||
queue.pop_front();
|
||||
}
|
||||
queue.push_back(event);
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_inbound(&self) {
|
||||
if let Ok(mut queue) = self.inbound.lock() {
|
||||
queue.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn status(&self) -> DemoStatus {
|
||||
let key_prefix = |key: &Mutex<Option<[u8; 32]>>| -> String {
|
||||
key.lock()
|
||||
@@ -190,6 +247,7 @@ fn reset_session_state(shared: &Shared) {
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
shared.clear_inbound();
|
||||
shared.peer_connected.store(false, Ordering::SeqCst);
|
||||
shared.key_established.store(false, Ordering::SeqCst);
|
||||
if let Ok(mut ip) = shared.peer_ip.lock() {
|
||||
@@ -286,6 +344,24 @@ pub fn status() -> DemoStatus {
|
||||
server_handle().shared.status()
|
||||
}
|
||||
|
||||
/// 查看队首的上行事件(mirror 应答 / 剪贴板)JSON,队列空时返回 None。
|
||||
///
|
||||
/// 与 [`pop_inbound_event`] 配对使用:调用方先把 JSON 拷进自己的缓冲,再出队,
|
||||
/// 避免缓冲不够时把事件丢掉。
|
||||
pub fn peek_inbound_event() -> Option<String> {
|
||||
let shared = &server_handle().shared;
|
||||
let queue = shared.inbound.lock().ok()?;
|
||||
queue.front().map(|event| event.to_json())
|
||||
}
|
||||
|
||||
/// 丢弃队首的上行事件(通常紧跟在 [`peek_inbound_event`] 之后)。
|
||||
pub fn pop_inbound_event() {
|
||||
let shared = &server_handle().shared;
|
||||
if let Ok(mut queue) = shared.inbound.lock() {
|
||||
queue.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
/// 把控制事件编码为 AES-GCM 帧并下发给当前被控端。
|
||||
/// 返回 Ok(()) 表示已写入 TCP,Err 表示未连接或写入失败。
|
||||
pub fn send_control(event: ControlEvent) -> Result<(), String> {
|
||||
@@ -366,6 +442,11 @@ pub fn mirror_event(sequence: u16, action: u8, message: String) -> ControlEvent
|
||||
ControlEvent::new(sequence, ControlEventKind::Mirror(MirrorEvent { action, message }))
|
||||
}
|
||||
|
||||
/// msgType=9 会话配置帧:payload 是主控端拼好的 UTF-8 JSON。
|
||||
pub fn config_event(sequence: u16, json: String) -> ControlEvent {
|
||||
ControlEvent::new(sequence, ControlEventKind::Config(ConfigEvent { json }))
|
||||
}
|
||||
|
||||
fn run_tcp(listener: TcpListener, shared: Arc<Shared>) {
|
||||
while shared.running.load(Ordering::Relaxed) {
|
||||
match listener.accept() {
|
||||
@@ -505,6 +586,24 @@ fn handle_peer(stream: &mut std::net::TcpStream, shared: &Arc<Shared>) {
|
||||
event.sequence(),
|
||||
event.msg_type()
|
||||
));
|
||||
// 界面要处理的事件单独入队:诊断日志刷得快,不能只靠 events 环。
|
||||
match &event.kind {
|
||||
ControlEventKind::Mirror(mirror) => {
|
||||
shared.push_inbound(InboundEvent {
|
||||
msg_type: MSG_MIRROR,
|
||||
action: i32::from(mirror.action),
|
||||
text: mirror.message.clone(),
|
||||
});
|
||||
}
|
||||
ControlEventKind::Clipboard(clipboard) => {
|
||||
shared.push_inbound(InboundEvent {
|
||||
msg_type: MSG_CLIPBOARD,
|
||||
action: 0,
|
||||
text: clipboard.text.clone(),
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
buffer.drain(..consumed);
|
||||
if let crate::control::ControlEventKind::Ping(timestamp) = event.kind {
|
||||
let pong = crate::control::ControlEvent::new(
|
||||
|
||||
Reference in new issue
Block a user