feat: 真实连接链路、移除虚拟数据、全量打包脚本
This commit is contained in:
1 parent
31b9a52eb4
commit
34340f4126
16 files changed
+601
-204
No files matched your search
@@ -0,0 +1,24 @@
|
||||
//! 运行本机 demo 服务,供 Android 被控端反连测试。
|
||||
//! cargo run --example demo_server
|
||||
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use ttstdconnect_core::demo_server;
|
||||
|
||||
fn main() {
|
||||
demo_server::start(41001, 41000, 41002).expect("启动 demo 服务失败");
|
||||
println!("demo server listening: TCP 41001, UDP 41000/41002");
|
||||
loop {
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
let status = demo_server::status();
|
||||
if !status.events.is_empty() {
|
||||
for event in &status.events {
|
||||
println!("[event] {}", event);
|
||||
}
|
||||
// 事件会累积,这里只打印最新一批后清空显示(服务内部保留全部)。
|
||||
std::io::Write::flush(&mut std::io::stdout()).ok();
|
||||
}
|
||||
thread::sleep(Duration::from_secs(2));
|
||||
}
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -310,6 +310,65 @@ pub unsafe extern "C" fn ttstd_control_decode(
|
||||
copy_out(json.as_bytes(), out_json, capacity)
|
||||
}
|
||||
|
||||
/// 向当前被控端下发控制帧。payload 为各事件类型的二进制负载。
|
||||
/// msg_type: 0=touch 1=mouse 2=key 3=wheel 4=clipboard(UTF-8) 5=ping(u64 LE)
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ttstd_demo_send_control(
|
||||
sequence: u16,
|
||||
msg_type: u8,
|
||||
payload: *const u8,
|
||||
payload_len: usize,
|
||||
) -> i32 {
|
||||
use crate::control::events::MSG_CLIPBOARD;
|
||||
let bytes = match read_slice(payload, payload_len) {
|
||||
Ok(value) => value,
|
||||
Err(code) => return code,
|
||||
};
|
||||
let event = match msg_type {
|
||||
0 if bytes.len() == 8 => crate::demo_server::touch_event(
|
||||
sequence, bytes[0], bytes[1],
|
||||
i16::from_le_bytes([bytes[2], bytes[3]]),
|
||||
i16::from_le_bytes([bytes[4], bytes[5]]),
|
||||
u16::from_le_bytes([bytes[6], bytes[7]]),
|
||||
),
|
||||
1 if bytes.len() == 6 => crate::demo_server::mouse_event(
|
||||
sequence, bytes[0],
|
||||
i16::from_le_bytes([bytes[1], bytes[2]]),
|
||||
i16::from_le_bytes([bytes[3], bytes[4]]),
|
||||
bytes[5],
|
||||
),
|
||||
2 if bytes.len() == 5 => crate::demo_server::key_event(
|
||||
sequence, bytes[0],
|
||||
u16::from_le_bytes([bytes[1], bytes[2]]),
|
||||
u16::from_le_bytes([bytes[3], bytes[4]]),
|
||||
),
|
||||
3 if bytes.len() == 5 => Some(crate::demo_server::wheel_event(
|
||||
sequence,
|
||||
i16::from_le_bytes([bytes[0], bytes[1]]),
|
||||
i16::from_le_bytes([bytes[2], bytes[3]]),
|
||||
bytes[4],
|
||||
)),
|
||||
MSG_CLIPBOARD => Some(crate::demo_server::clipboard_event(
|
||||
sequence,
|
||||
String::from_utf8_lossy(bytes).to_string(),
|
||||
)),
|
||||
5 if bytes.len() == 8 => Some(crate::demo_server::ping_event(
|
||||
sequence,
|
||||
u64::from_le_bytes([
|
||||
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
|
||||
]),
|
||||
)),
|
||||
_ => None,
|
||||
};
|
||||
match event {
|
||||
Some(event) => match crate::demo_server::send_control(event) {
|
||||
Ok(()) => OK,
|
||||
Err(_) => ERR_INVALID_ARGUMENT,
|
||||
},
|
||||
None => ERR_INVALID_ARGUMENT,
|
||||
}
|
||||
}
|
||||
|
||||
/// 启动本机 demo 服务:TCP 控制 + UDP 视频/音频。
|
||||
#[no_mangle]
|
||||
pub extern "C" fn ttstd_demo_start(control_port: u16, video_port: u16, audio_port: u16) -> i32 {
|
||||
|
||||
@@ -58,6 +58,23 @@ fn demo_server_handshake_control_and_media_roundtrip() {
|
||||
assert_eq!(pong.sequence(), 1);
|
||||
assert!(matches!(pong.kind, ControlEventKind::Pong(1234)));
|
||||
|
||||
// 主控端主动下发一个 Ping,被控端应能解密并识别。
|
||||
demo_server::send_control(ControlEvent::new(
|
||||
99,
|
||||
ControlEventKind::Ping(4321),
|
||||
))
|
||||
.expect("下发控制帧");
|
||||
let mut downlink = [0u8; 512];
|
||||
let downlink_size = control
|
||||
.read(&mut downlink)
|
||||
.expect("读取主控端下发的控制帧");
|
||||
let (downlink_event, _) = channel
|
||||
.decode(&downlink[..downlink_size])
|
||||
.expect("解密下发控制帧")
|
||||
.expect("完整的下发控制帧");
|
||||
assert_eq!(downlink_event.sequence(), 99);
|
||||
assert!(matches!(downlink_event.kind, ControlEventKind::Ping(4321)));
|
||||
|
||||
let video = UdpSocket::bind("127.0.0.1:0").expect("创建视频 UDP");
|
||||
let video_packet = encode_media_packet(
|
||||
&MediaPacket {
|
||||
|
||||
Reference in new issue
Block a user