在 UtilSearchNasIp 中添加探测开始时间记录和总耗时打印,增强了命中记录的调试信息;更新相关方法以传递开始时间参数。
This commit is contained in:
@@ -20,6 +20,7 @@ class UtilSearchNasIp {
|
|||||||
return nasIp;
|
return nasIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final startedAt = DateTime.now();
|
||||||
final preUrl = await _resolvePreUrl();
|
final preUrl = await _resolvePreUrl();
|
||||||
final dio = Dio(
|
final dio = Dio(
|
||||||
BaseOptions(
|
BaseOptions(
|
||||||
@@ -39,8 +40,13 @@ class UtilSearchNasIp {
|
|||||||
cancelToken: cancelToken,
|
cancelToken: cancelToken,
|
||||||
preUrl: preUrl,
|
preUrl: preUrl,
|
||||||
host: host,
|
host: host,
|
||||||
|
startedAt: startedAt,
|
||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
if (nasIp == null) {
|
||||||
|
final elapsedMs = DateTime.now().difference(startedAt).inMilliseconds;
|
||||||
|
debugPrint('NAS 探测未命中,总耗时: ${elapsedMs}ms');
|
||||||
|
}
|
||||||
return nasIp;
|
return nasIp;
|
||||||
} finally {
|
} finally {
|
||||||
dio.close(force: true);
|
dio.close(force: true);
|
||||||
@@ -66,30 +72,37 @@ class UtilSearchNasIp {
|
|||||||
required CancelToken cancelToken,
|
required CancelToken cancelToken,
|
||||||
required String preUrl,
|
required String preUrl,
|
||||||
required int host,
|
required int host,
|
||||||
|
required DateTime startedAt,
|
||||||
}) async {
|
}) async {
|
||||||
if (cancelToken.isCancelled || nasIp != null) return;
|
if (cancelToken.isCancelled || nasIp != null) return;
|
||||||
|
|
||||||
final target = '$preUrl$host$_probePath';
|
final target = '$preUrl$host$_probePath';
|
||||||
try {
|
try {
|
||||||
await dio.get<dynamic>(target, cancelToken: cancelToken);
|
await dio.get<dynamic>(target, cancelToken: cancelToken);
|
||||||
_onHit(preUrl, host, cancelToken);
|
_onHit(preUrl, host, cancelToken, startedAt);
|
||||||
} on DioException catch (error) {
|
} on DioException catch (error) {
|
||||||
if (error.type == DioExceptionType.cancel) return;
|
if (error.type == DioExceptionType.cancel) return;
|
||||||
// 带 response 的 DioException 也表示已连通
|
// 带 response 的 DioException 也表示已连通
|
||||||
if (error.response != null) {
|
if (error.response != null) {
|
||||||
_onHit(preUrl, host, cancelToken);
|
_onHit(preUrl, host, cancelToken, startedAt);
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// 超时 / 连接失败:未命中
|
// 超时 / 连接失败:未命中
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 记录命中:写 [nasIp]、打印并取消其余请求。
|
/// 记录命中:写 [nasIp]、打印 IP 与总耗时,并取消其余请求。
|
||||||
static void _onHit(String preUrl, int host, CancelToken cancelToken) {
|
static void _onHit(
|
||||||
|
String preUrl,
|
||||||
|
int host,
|
||||||
|
CancelToken cancelToken,
|
||||||
|
DateTime startedAt,
|
||||||
|
) {
|
||||||
if (nasIp != null) return;
|
if (nasIp != null) return;
|
||||||
final ip = '${_hostPrefix(preUrl)}$host';
|
final ip = '${_hostPrefix(preUrl)}$host';
|
||||||
nasIp = ip;
|
nasIp = ip;
|
||||||
debugPrint('NAS IP: $ip');
|
final elapsedMs = DateTime.now().difference(startedAt).inMilliseconds;
|
||||||
|
debugPrint('NAS IP: $ip,探测总耗时: ${elapsedMs}ms');
|
||||||
if (!cancelToken.isCancelled) {
|
if (!cancelToken.isCancelled) {
|
||||||
cancelToken.cancel('nas-found');
|
cancelToken.cancel('nas-found');
|
||||||
}
|
}
|
||||||
@@ -108,15 +121,15 @@ class UtilSearchNasIp {
|
|||||||
// 1) Auth 页静默探测(不阻塞登录):
|
// 1) Auth 页静默探测(不阻塞登录):
|
||||||
//
|
//
|
||||||
// import 'dart:async';
|
// import 'dart:async';
|
||||||
// import 'package:recording_tool/core/utils/util_getNasIp.dart';
|
// import 'package:recording_tool/core/utils/util_search_nasIp.dart';
|
||||||
//
|
//
|
||||||
// WidgetsBinding.instance.addPostFrameCallback((_) {
|
// WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
// unawaited(UtilGetNasIp.discover());
|
// unawaited(UtilSearchNasIp.discover());
|
||||||
// });
|
// });
|
||||||
//
|
//
|
||||||
// 2) 业务侧读取已命中 IP:
|
// 2) 业务侧读取已命中 IP:
|
||||||
//
|
//
|
||||||
// final ip = UtilGetNasIp.nasIp;
|
// final ip = UtilSearchNasIp.nasIp;
|
||||||
// if (ip != null) {
|
// if (ip != null) {
|
||||||
// final base = 'http://$ip:5666';
|
// final base = 'http://$ip:5666';
|
||||||
// // 使用 base 访问 NAS...
|
// // 使用 base 访问 NAS...
|
||||||
@@ -124,6 +137,6 @@ class UtilSearchNasIp {
|
|||||||
//
|
//
|
||||||
// 3) 主动等待探测结果(少用,会阻塞当前异步流程):
|
// 3) 主动等待探测结果(少用,会阻塞当前异步流程):
|
||||||
//
|
//
|
||||||
// final ip = await UtilGetNasIp.discover();
|
// final ip = await UtilSearchNasIp.discover();
|
||||||
// debugPrint('result: $ip');
|
// debugPrint('result: $ip');
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user