feat: 真实连接链路、移除虚拟数据、全量打包脚本
This commit is contained in:
1 parent
31b9a52eb4
commit
34340f4126
16 files changed
+601
-204
No files matched your search
@@ -7,12 +7,18 @@
|
||||
//! 4. 视频/音频通过 UDP 41000/41002 发送,使用 `MediaPackets` 的 13 字节头 + 4 字节 HMAC。
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::net::{TcpListener, UdpSocket};
|
||||
use std::net::{TcpListener, TcpStream, UdpSocket};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::thread::{self, JoinHandle};
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::control::events::{
|
||||
ClipboardEvent, KeyAction, KeyboardEvent, MouseAction, MouseEvent, TouchAction, TouchEvent,
|
||||
WheelEvent,
|
||||
};
|
||||
use crate::control::{ControlEvent, ControlEventKind};
|
||||
|
||||
use crate::control::SecureControlChannel;
|
||||
use crate::crypto::{derive_subkey, EphemeralKeyPair};
|
||||
use crate::media::decode_media_packet;
|
||||
@@ -61,6 +67,11 @@ impl DemoStatus {
|
||||
}
|
||||
}
|
||||
|
||||
struct ControlSender {
|
||||
stream: Arc<Mutex<TcpStream>>,
|
||||
channel: SecureControlChannel,
|
||||
}
|
||||
|
||||
struct Shared {
|
||||
running: AtomicBool,
|
||||
peer_connected: AtomicBool,
|
||||
@@ -69,6 +80,7 @@ struct Shared {
|
||||
control_key: Mutex<Option<[u8; 32]>>,
|
||||
video_key: Mutex<Option<[u8; 32]>>,
|
||||
audio_key: Mutex<Option<[u8; 32]>>,
|
||||
sender: Mutex<Option<ControlSender>>,
|
||||
events: Mutex<Vec<String>>,
|
||||
}
|
||||
|
||||
@@ -82,6 +94,7 @@ impl Shared {
|
||||
control_key: Mutex::new(None),
|
||||
video_key: Mutex::new(None),
|
||||
audio_key: Mutex::new(None),
|
||||
sender: Mutex::new(None),
|
||||
events: Mutex::new(Vec::new()),
|
||||
}
|
||||
}
|
||||
@@ -187,6 +200,81 @@ pub fn status() -> DemoStatus {
|
||||
server_handle().shared.status()
|
||||
}
|
||||
|
||||
/// 把控制事件编码为 AES-GCM 帧并下发给当前被控端。
|
||||
/// 返回 Ok(()) 表示已写入 TCP,Err 表示未连接或写入失败。
|
||||
pub fn send_control(event: ControlEvent) -> Result<(), String> {
|
||||
let handle = server_handle();
|
||||
let sender = handle
|
||||
.shared
|
||||
.sender
|
||||
.lock()
|
||||
.map_err(|_| "控制通道锁不可用".to_string())?;
|
||||
let sender = sender.as_ref().ok_or_else(|| "没有已连接的被控端".to_string())?;
|
||||
let frame = sender
|
||||
.channel
|
||||
.encode(&event)
|
||||
.map_err(|error| format!("控制帧加密失败:{}", error))?;
|
||||
let mut stream = sender
|
||||
.stream
|
||||
.lock()
|
||||
.map_err(|_| "控制写端锁不可用".to_string())?;
|
||||
stream
|
||||
.write_all(&frame)
|
||||
.and_then(|_| stream.flush())
|
||||
.map_err(|error| format!("控制帧下发失败:{}", error))?;
|
||||
handle.shared.push_event(format!(
|
||||
"sent:seq={}:type={}",
|
||||
event.sequence(),
|
||||
event.msg_type()
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 构造触摸控制事件。
|
||||
pub fn touch_event(sequence: u16, action: u8, pointer_id: u8, x: i16, y: i16, pressure: u16) -> Option<ControlEvent> {
|
||||
Some(ControlEvent::new(sequence, ControlEventKind::Touch(TouchEvent {
|
||||
action: TouchAction::from_u8(action)?,
|
||||
pointer_id,
|
||||
x,
|
||||
y,
|
||||
pressure,
|
||||
})))
|
||||
}
|
||||
|
||||
/// 构造按键控制事件。
|
||||
pub fn key_event(sequence: u16, action: u8, keycode: u16, modifiers: u16) -> Option<ControlEvent> {
|
||||
Some(ControlEvent::new(sequence, ControlEventKind::Key(KeyboardEvent {
|
||||
action: KeyAction::from_u8(action)?,
|
||||
keycode,
|
||||
modifiers,
|
||||
})))
|
||||
}
|
||||
|
||||
/// 构造鼠标控制事件。
|
||||
pub fn mouse_event(sequence: u16, action: u8, x: i16, y: i16, buttons: u8) -> Option<ControlEvent> {
|
||||
Some(ControlEvent::new(sequence, ControlEventKind::Mouse(MouseEvent {
|
||||
action: MouseAction::from_u8(action)?,
|
||||
x,
|
||||
y,
|
||||
buttons,
|
||||
})))
|
||||
}
|
||||
|
||||
/// 构造滚轮控制事件。
|
||||
pub fn wheel_event(sequence: u16, delta_x: i16, delta_y: i16, buttons: u8) -> ControlEvent {
|
||||
ControlEvent::new(sequence, ControlEventKind::Wheel(WheelEvent { delta_x, delta_y, buttons }))
|
||||
}
|
||||
|
||||
/// 构造剪贴板控制事件。
|
||||
pub fn clipboard_event(sequence: u16, text: String) -> ControlEvent {
|
||||
ControlEvent::new(sequence, ControlEventKind::Clipboard(ClipboardEvent { text }))
|
||||
}
|
||||
|
||||
/// 构造 Ping 控制事件。
|
||||
pub fn ping_event(sequence: u16, timestamp: u64) -> ControlEvent {
|
||||
ControlEvent::new(sequence, ControlEventKind::Ping(timestamp))
|
||||
}
|
||||
|
||||
fn run_tcp(listener: TcpListener, shared: Arc<Shared>) {
|
||||
while shared.running.load(Ordering::Relaxed) {
|
||||
match listener.accept() {
|
||||
@@ -202,6 +290,9 @@ fn run_tcp(listener: TcpListener, shared: Arc<Shared>) {
|
||||
handle_peer(&mut stream, &shared);
|
||||
shared.peer_connected.store(false, Ordering::Relaxed);
|
||||
shared.key_established.store(false, Ordering::Relaxed);
|
||||
if let Ok(mut guard) = shared.sender.lock() {
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
thread::sleep(Duration::from_millis(50));
|
||||
@@ -258,6 +349,14 @@ fn handle_peer(stream: &mut std::net::TcpStream, shared: &Arc<Shared>) {
|
||||
let _ = stream.flush();
|
||||
|
||||
let channel = SecureControlChannel::new(control);
|
||||
if let Ok(write_stream) = stream.try_clone() {
|
||||
if let Ok(mut guard) = shared.sender.lock() {
|
||||
*guard = Some(ControlSender {
|
||||
stream: Arc::new(Mutex::new(write_stream)),
|
||||
channel: SecureControlChannel::new(control),
|
||||
});
|
||||
}
|
||||
}
|
||||
let mut buffer = Vec::new();
|
||||
let mut chunk = [0u8; 4096];
|
||||
while shared.running.load(Ordering::Relaxed) {
|
||||
|
||||
Reference in new issue
Block a user