fix(sender): 修复断开后重连一直超时,串流窗口按被控端比例开局
- rust_core: stop 先关闭控制连接再 join 监听线程,保证端口真正释放 - rust_core: start 前回收残留线程,按 AddrInUse 重试绑定,失败不再静默 - rust_core: 监听套接字异常时把 running 置回 false;半开握手读超时缩到 1.2s - rust_core: 新增 demo_reconnect 回归测试(stop 后立刻 start 仍可接入) - flutter_sender: connect 检查本机监听结果,失败直接提示端口被占用 - flutter_sender: 断开时一并关闭独立串流窗口,重连不再残留旧帧 - flutter_sender: openWindow 传入被控端分辨率,窗口开局即按竖屏比例居中
This commit is contained in:
1 parent
77ce882397
commit
32e7ca26d8
7 files changed
+269
-33
No files matched your search
@@ -148,20 +148,90 @@ fn server_handle() -> &'static ServerHandle {
|
||||
})
|
||||
}
|
||||
|
||||
/// 启动 demo 服务端。重复调用会返回 ERR_ALREADY_RUNNING。
|
||||
/// 回收上一轮留下的监听线程。
|
||||
///
|
||||
/// `stop()` 只是置标志,线程退出有几十毫秒延迟;必须 join 掉才能保证监听端口
|
||||
/// 真正释放,否则紧接着重新 `start` 会撞上 `Address already in use`,
|
||||
/// 导致「断开后重连一直连不上」。
|
||||
fn join_threads(handle: &ServerHandle) {
|
||||
let threads = handle
|
||||
.threads
|
||||
.lock()
|
||||
.map(|mut guard| std::mem::take(&mut *guard))
|
||||
.unwrap_or_default();
|
||||
for thread in threads {
|
||||
let _ = thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
/// 关掉当前控制连接,让阻塞在 `read` 上的 `handle_peer` 立刻返回,
|
||||
/// 从而尽快释放监听端口。
|
||||
fn shutdown_control_channel(shared: &Shared) {
|
||||
let stream = shared
|
||||
.sender
|
||||
.lock()
|
||||
.ok()
|
||||
.and_then(|mut guard| guard.take())
|
||||
.map(|sender| sender.stream);
|
||||
if let Some(stream) = stream {
|
||||
if let Ok(stream) = stream.lock() {
|
||||
let _ = stream.shutdown(std::net::Shutdown::Both);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 清掉上一次会话的密钥与控制写端,避免重连后误用旧密钥。
|
||||
fn reset_session_state(shared: &Shared) {
|
||||
if let Ok(mut guard) = shared.sender.lock() {
|
||||
*guard = None;
|
||||
}
|
||||
for slot in [&shared.control_key, &shared.video_key, &shared.audio_key] {
|
||||
if let Ok(mut guard) = slot.lock() {
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
shared.peer_connected.store(false, Ordering::SeqCst);
|
||||
shared.key_established.store(false, Ordering::SeqCst);
|
||||
if let Ok(mut ip) = shared.peer_ip.lock() {
|
||||
ip.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// 绑定端口时重试若干次:上一次的监听套接字可能刚关闭、内核尚未回收。
|
||||
/// 只在「端口被占用」时重试,其它错误(权限、地址不可用)立刻返回。
|
||||
fn retry_bind<T>(mut attempt_bind: impl FnMut() -> std::io::Result<T>) -> std::io::Result<T> {
|
||||
let mut last_error = None;
|
||||
for _ in 0..20 {
|
||||
match attempt_bind() {
|
||||
Ok(value) => return Ok(value),
|
||||
Err(error) => {
|
||||
if error.kind() != std::io::ErrorKind::AddrInUse {
|
||||
return Err(error);
|
||||
}
|
||||
last_error = Some(error);
|
||||
thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(last_error.unwrap_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "绑定失败")))
|
||||
}
|
||||
|
||||
/// 启动 demo 服务端。已运行或端口已就绪时返回 Ok(幂等)。
|
||||
pub fn start(control_port: u16, video_port: u16, audio_port: u16) -> Result<(), String> {
|
||||
let handle = server_handle();
|
||||
if handle.shared.running.load(Ordering::SeqCst) {
|
||||
return Ok(());
|
||||
}
|
||||
handle.shared.running.store(true, Ordering::SeqCst);
|
||||
// 上一轮的线程可能还没退干净,先 join 再重新绑定端口。
|
||||
join_threads(handle);
|
||||
reset_session_state(&handle.shared);
|
||||
|
||||
let result = (|| -> Result<(), String> {
|
||||
let listener = TcpListener::bind(("0.0.0.0", control_port))
|
||||
let listener = retry_bind(|| TcpListener::bind(("0.0.0.0", control_port)))
|
||||
.map_err(|error| format!("监听 TCP {} 失败:{}", control_port, error))?;
|
||||
let video = UdpSocket::bind(("0.0.0.0", video_port))
|
||||
let video = retry_bind(|| UdpSocket::bind(("0.0.0.0", video_port)))
|
||||
.map_err(|error| format!("监听 UDP {} 失败:{}", video_port, error))?;
|
||||
let audio = UdpSocket::bind(("0.0.0.0", audio_port))
|
||||
let audio = retry_bind(|| UdpSocket::bind(("0.0.0.0", audio_port)))
|
||||
.map_err(|error| format!("监听 UDP {} 失败:{}", audio_port, error))?;
|
||||
listener
|
||||
.set_nonblocking(true)
|
||||
@@ -173,6 +243,7 @@ pub fn start(control_port: u16, video_port: u16, audio_port: u16) -> Result<(),
|
||||
.set_read_timeout(Some(Duration::from_millis(200)))
|
||||
.map_err(|error| format!("设置 UDP 超时失败:{}", error))?;
|
||||
|
||||
handle.shared.running.store(true, Ordering::SeqCst);
|
||||
let shared = Arc::clone(&handle.shared);
|
||||
let tcp = thread::spawn(move || run_tcp(listener, shared));
|
||||
let video_thread = {
|
||||
@@ -191,16 +262,22 @@ pub fn start(control_port: u16, video_port: u16, audio_port: u16) -> Result<(),
|
||||
|
||||
if result.is_err() {
|
||||
handle.shared.running.store(false, Ordering::SeqCst);
|
||||
join_threads(handle);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// 停止 demo 服务端。
|
||||
/// 停止 demo 服务端。链路与监听都会断开,可再次 [start]。
|
||||
pub fn stop() {
|
||||
let handle = server_handle();
|
||||
handle.shared.running.store(false, Ordering::SeqCst);
|
||||
handle.shared.peer_connected.store(false, Ordering::SeqCst);
|
||||
handle.shared.key_established.store(false, Ordering::SeqCst);
|
||||
// 先断连接再 join,否则 handle_peer 要等到读超时才退出,端口释放被拖慢。
|
||||
shutdown_control_channel(&handle.shared);
|
||||
join_threads(handle);
|
||||
reset_session_state(&handle.shared);
|
||||
if let Ok(mut events) = handle.shared.events.lock() {
|
||||
events.clear();
|
||||
}
|
||||
crate::media::stream::clear();
|
||||
}
|
||||
|
||||
@@ -300,7 +377,8 @@ fn run_tcp(listener: TcpListener, shared: Arc<Shared>) {
|
||||
}
|
||||
shared.peer_connected.store(true, Ordering::Relaxed);
|
||||
shared.push_event(format!("peer:{}", address));
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(2)));
|
||||
// 握手前给短一点的超时:半开连接不能长时间拖住监听线程(会推迟端口释放)。
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_millis(1200)));
|
||||
handle_peer(&mut stream, &shared);
|
||||
shared.peer_connected.store(false, Ordering::Relaxed);
|
||||
shared.key_established.store(false, Ordering::Relaxed);
|
||||
@@ -311,7 +389,12 @@ fn run_tcp(listener: TcpListener, shared: Arc<Shared>) {
|
||||
Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
|
||||
thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
Err(_) => break,
|
||||
Err(error) => {
|
||||
// 监听套接字已坏:置回 not running,避免界面显示「已监听」却收不到连接。
|
||||
shared.push_event(format!("error:listener:{}", error));
|
||||
shared.running.store(false, Ordering::SeqCst);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -361,6 +444,8 @@ fn handle_peer(stream: &mut std::net::TcpStream, shared: &Arc<Shared>) {
|
||||
return;
|
||||
}
|
||||
let _ = stream.flush();
|
||||
// 握手完成,恢复正常读超时(兼作心跳周期)。
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(2)));
|
||||
|
||||
let channel = SecureControlChannel::new(control);
|
||||
if let Ok(write_stream) = stream.try_clone() {
|
||||
|
||||
Reference in new issue
Block a user