Files
pangolin/client/lib/services/latency_probe.dart
T
wangjia f20eddad55
ci-pangolin / Lint — shellcheck (push) Has been cancelled
ci-pangolin / OpenAPI Sync Check (push) Has been cancelled
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Has been cancelled
ci-pangolin / Flutter — analyze + test (push) Has been cancelled
feat(client): P4 节点实测延迟/设备/套餐价/兑换/统计接真(#6 6D)
- latency_probe.dart:并行 TCP 握手实测各节点 per-client RTT(服务端无法代知)。
- nodes_provider:解析 /v1/nodes 的 host/port,后台测延迟回填 Node.ping;智能选择
  按实测最小;去掉 8 个伪造演示节点(空列表用中性占位,不再伪造服务器)。
- Node:+host/port/copyWith/pingLabel(未测显示 —);各端 ping 显示改 pingLabel。
- account_screens:PlansScreen 接 /v1/plans(价格 priceLabel + me 标当前档);
  DevicesScreen 接 /v1/me/devices + 移除 DELETE + last_seen;RedeemScreen 接
  /v1/redeem(成功刷新 me,失败显示后端文案)。
- stats_page:周柱接 me.weekly_gb,本月流量/时长接 usage(30),延迟用生效节点实测。
- 顺带补登记早前 log_time.dart 的删除(log.dart 重构遗漏 stage)。

flutter analyze 0 error;115 tests passed;受影响 4 golden 重生成。
本机设备高亮 + 真实 device id 随 P6(device_info_plus)落地。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 00:04:25 +08:00

43 lines
1.4 KiB
Dart

// latency_probe.dart — 客户端实测节点延迟。
//
// per-client 到节点的 RTT 服务端无法代知(agent 到控制面的 RTT ≠ 用户 ping),
// 故由客户端对各节点入口做并行 TCP 握手计时,得到真实 per-client 延迟。
// 取多次取最小(规避抖动),失败返回 0(UI 显示 — )。
import 'dart:async';
import 'dart:io';
/// 对 [host]:[port] 做 [samples] 次 TCP 握手,返回最小耗时(ms);全失败返回 0。
Future<int> probeLatency(
String host,
int port, {
int samples = 2,
Duration timeout = const Duration(seconds: 3),
}) async {
if (host.isEmpty || port <= 0) return 0;
int best = 0;
for (var i = 0; i < samples; i++) {
final sw = Stopwatch()..start();
try {
final socket = await Socket.connect(host, port, timeout: timeout);
sw.stop();
socket.destroy();
final ms = sw.elapsedMilliseconds;
if (best == 0 || ms < best) best = ms;
} catch (_) {
// 单次失败忽略,继续下一次采样。
}
}
return best;
}
/// 并行测一组 (uuid, host, port),返回 uuid→延迟(ms)。
Future<Map<String, int>> probeAll(
Iterable<({String uuid, String host, int port})> targets,
) async {
final entries = await Future.wait(targets.map((tg) async {
final ms = await probeLatency(tg.host, tg.port);
return MapEntry(tg.uuid, ms);
}));
return Map.fromEntries(entries);
}