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:
tongtong committed 2026-11-27 12:06:57 +08:00
1 parent 77ce882397
commit 32e7ca26d8
7 files changed
+269 -33

No files matched your search

+12 -3
View File
@@ -118,7 +118,10 @@ class SenderBridge {
final native = _native;
if (native != null) {
// 先确保本机控制/媒体端口在监听,再让被控端立刻拨号(不必等它下次广播)。
native.startDemoServer();
// 监听失败必须显式报错:否则被控端拨号会没人接,界面只能笼统地等到超时。
if (!native.startDemoServer()) {
throw Exception('本机监听启动失败(TCP 41001 / UDP 41000、41002 可能被占用)');
}
if (device.ip.isNotEmpty) {
native.discoveryConnect(device.ip);
}
@@ -281,9 +284,15 @@ class SenderBridge {
}
/// 把串流画面弹出为独立原生窗口(Windows);返回是否成功。
Future<bool> openVideoWindow() async {
///
/// [width] / [height] 是被控端屏幕尺寸提示:传了就按该比例开局,
/// 不再先弹出一个 16:9 的大窗口等首帧再缩回去。
Future<bool> openVideoWindow({int? width, int? height}) async {
try {
final opened = await _video.invokeMethod<bool>('openWindow');
final arguments = (width != null && height != null && width > 0 && height > 0)
? <String, dynamic>{'width': width, 'height': height}
: null;
final opened = await _video.invokeMethod<bool>('openWindow', arguments);
return opened ?? false;
} on MissingPluginException {
return false;
+23 -1
View File
@@ -215,7 +215,11 @@ class _HomePageState extends State<HomePage> {
});
try {
if (!_videoWindowOpen) {
final opened = await _bridge.openVideoWindow();
final size = _deviceScreenSize();
final opened = await _bridge.openVideoWindow(
width: size?.$1,
height: size?.$2,
);
if (!mounted) return;
if (!opened) {
_snack('当前平台不支持独立串流窗口');
@@ -230,6 +234,17 @@ class _HomePageState extends State<HomePage> {
}
}
/// 被控端屏幕尺寸(发现信标里的 `res`,如 "1080x2400");拿不到时返回 null。
(int, int)? _deviceScreenSize() {
final text = (_selected?.resolution ?? '').trim();
final match = RegExp(r'^(\d+)\s*[xX*]\s*(\d+)$').firstMatch(text);
if (match == null) return null;
final width = int.tryParse(match.group(1)!);
final height = int.tryParse(match.group(2)!);
if (width == null || height == null || width <= 0 || height <= 0) return null;
return (width, height);
}
/// 用户关掉独立串流窗口(点关闭按钮 / 按 ESC)→ 直接停止投屏。
///
/// 两个入口都会走到这里:原生的 `windowClosed` 回调,以及轮询 `stats.windowOpen`
@@ -428,6 +443,13 @@ class _HomePageState extends State<HomePage> {
Future<void> _disconnect() async {
_diagnosticLog.write('disconnect requested');
_connectWatchdog?.cancel();
// 断开时连串流窗口一起关掉,否则重连后窗口还挂在那儿显示上一路的旧帧。
if (_videoWindowOpen) {
_stopSentForWindowClose = true;
await _bridge.closeVideoWindow();
_videoWindowOpen = false;
_lastFrameAt = null;
}
await _bridge.disconnect();
if (!mounted) return;
setState(() => _phase = ConnectionPhase.idle);