之前的判定是「本机连不上 NVR 才走 B」,导致同 LAN 多台机器各连各的, 录像机并发登录数照样被打满,设备管理服务的会话池闲置。 选路(StreamGateway) - 新增 StreamGateway:广播询问(带 NVR 地址)→ 有应答就用它, 没有才本机直连;结果缓存 15 秒(含"没找到"的负结果) - 转发失败时 Invalidate,下次重新探测 - 广播询问带 nvrHosts:对端实测连不上该 NVR 就不应答 校验走设备管理服务 - B 侧新增 POST /check,复用 HikPlaybackService.CheckAsync (真拉一小段,语义与桌面端本地校验一致) - 桌面端校验改为优先交给设备管理服务;服务无响应则降级本机并重新探测 - 并发上限 4,避免多台查询机同时校验把 NVR 打满 下载走设备管理服务 - PlaybackDownloadService 去掉 CanReachNvrAsync 判定,改用 StreamGateway - 服务端(hik-worker)自身不装网关,永远本地执行 其他 - 边端端口支持 YUNCANG_EDGE_UDP_PORT / YUNCANG_EDGE_HTTP_PORT 覆盖 (同机并行跑两个实例测试时用,与 YUNCANG_LOCAL_WS_PORT 同思路) - SDK 校验路径不数帧(返回 -1),界面只在有帧数时显示"试拉 N 帧" - 顺带修复:下载临时文件名未对 CameraName 做 Sanitize, 摄像头名含 ? : * 等字符时会整段下载失败 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
102 lines
3.4 KiB
C#
102 lines
3.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YuncangTrace.LocalService.Services.Edge;
|
||
|
||
/// <summary>
|
||
/// 决定「这次取流请求由谁执行」:局域网里的**设备管理服务**,还是本机。
|
||
///
|
||
/// 规则只有一条 —— **有设备管理服务就全部交给它**,没有才由本机自己连 NVR。
|
||
/// 不看"本机能不能连 NVR":多台查询机各自连 NVR 会把录像机的并发登录数打满
|
||
/// (海康常见 6~16),而设备管理服务本来就有会话池,能复用、能限流。
|
||
///
|
||
/// 探测结果缓存 <see cref="CacheTtl"/> 秒(含"没找到"的负结果),
|
||
/// 避免每次请求都白等一次广播;转发失败时调用 <see cref="Invalidate"/> 重新探测。
|
||
/// </summary>
|
||
public sealed class StreamGateway
|
||
{
|
||
private static readonly TimeSpan CacheTtl = TimeSpan.FromSeconds(15);
|
||
|
||
private readonly Func<int> _subjectId;
|
||
private readonly SemaphoreSlim _gate = new(1, 1);
|
||
|
||
private string _cacheKey = "";
|
||
private EdgePeer? _cached;
|
||
private DateTime _cachedAt = DateTime.MinValue;
|
||
|
||
/// <param name="subjectIdProvider">当前主体(探测只认同主体应答)</param>
|
||
public StreamGateway(Func<int> subjectIdProvider)
|
||
{
|
||
_subjectId = subjectIdProvider;
|
||
}
|
||
|
||
/// <summary>找一台能服务这些 NVR 的设备管理服务;没有返回 null(调用方走本机)</summary>
|
||
public async Task<EdgePeer?> FindAsync(IReadOnlyList<(string Host, int Port)> nvrs,
|
||
CancellationToken ct = default)
|
||
{
|
||
var subjectId = _subjectId();
|
||
if (subjectId <= 0) return null;
|
||
|
||
var key = BuildKey(subjectId, nvrs);
|
||
if (TryGetCached(key, out var cached)) return cached;
|
||
|
||
await _gate.WaitAsync(ct).ConfigureAwait(false);
|
||
try
|
||
{
|
||
if (TryGetCached(key, out cached)) return cached;
|
||
|
||
var peer = await EdgeDiscoveryClient.FindAsync(subjectId, nvrs, ct).ConfigureAwait(false);
|
||
if (peer is not null)
|
||
{
|
||
AppLog.Write($"[edge] 走设备管理服务 {peer.Display}"
|
||
+ $"(可服务 {string.Join("、", peer.Reachable)})");
|
||
}
|
||
|
||
_cacheKey = key;
|
||
_cached = peer;
|
||
_cachedAt = DateTime.UtcNow;
|
||
return peer;
|
||
}
|
||
finally
|
||
{
|
||
_gate.Release();
|
||
}
|
||
}
|
||
|
||
/// <summary>转发失败时调用:下次重新探测,避免一直指着一台已经不可用的服务</summary>
|
||
public void Invalidate()
|
||
{
|
||
_cached = null;
|
||
_cachedAt = DateTime.MinValue;
|
||
_cacheKey = "";
|
||
}
|
||
|
||
private bool TryGetCached(string key, out EdgePeer? peer)
|
||
{
|
||
if (_cachedAt != DateTime.MinValue
|
||
&& string.Equals(_cacheKey, key, StringComparison.Ordinal)
|
||
&& DateTime.UtcNow - _cachedAt < CacheTtl)
|
||
{
|
||
peer = _cached;
|
||
return true;
|
||
}
|
||
|
||
peer = null;
|
||
return false;
|
||
}
|
||
|
||
private static string BuildKey(int subjectId, IReadOnlyList<(string Host, int Port)> nvrs)
|
||
{
|
||
var hosts = nvrs
|
||
.Where(item => !string.IsNullOrWhiteSpace(item.Host))
|
||
.Select(item => $"{item.Host}:{item.Port}")
|
||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||
.OrderBy(item => item, StringComparer.OrdinalIgnoreCase);
|
||
|
||
return subjectId + "|" + string.Join(",", hosts);
|
||
}
|
||
}
|