把海康取流从扫码服务主进程拆到 --hik-worker 子进程,原生 SDK 崩溃
不再连带扫码收码;新增局域网询问-应答与跨网下载通道。
隔离与守护
- 新增 HikWorker(子进程):独占 HCNetSDK 与 NVR 会话池
- 新增 HikWorkerSupervisor(主进程):懒启动、管道通信、
5 分钟内失败 3 次熔断 10 分钟
- 管道协议为 [4 字节长度][JSON],进程退出即知,不占端口
- ServiceInstaller 复制 native(HCNetSDK/ffmpeg),先删后拷,
避免 SDK 升级残留旧 DLL;安装时放行 TCP 17311 / UDP 17310
局域网询问-应答(不是主动公告)
- UDP 17310 收询问,只回同 subjectId;带了 nvrHosts 时先实测可达,
连不上就不应答,让查询机继续走本机
- 应答带 TCP 17311,查询机一次连接即可判定是否同 LAN
跨网下载
- TCP 17311 提供 /health、/probe、/download、/download/{id}
- 除 /health 外均要求 X-Edge-Subject 且与配置主体一致
- 成片在传输完成后立即删除,另有 done 10 分钟 / failed 15 分钟
TTL 兜底,避免现场磁盘被临时文件吃满
桌面端选路
- DownloadAsync 先探 NVR 8000(600ms):通则沿用原有本机直连,
不通才广播找取流服务,取回后落到本机录像目录
- DownloadByPlanAsync 拆分:不查库、不需要用户令牌,
供只有设备令牌的服务端复用
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
131 lines
4.9 KiB
C#
131 lines
4.9 KiB
C#
using System;
|
||
using System.Buffers.Binary;
|
||
using System.IO;
|
||
using System.Text.Json;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>
|
||
/// 服务主进程 ↔ <c>--hik-worker</c> 子进程的管道协议。
|
||
///
|
||
/// 为什么单独开子进程跑海康:HCNetSDK 是原生库,**访问越界会直接杀掉进程**
|
||
/// (0xC0000005),放主进程里等于把扫码收码/上报一起带走。
|
||
/// 隔离后 worker 崩了只需重启它,扫码链路无感。
|
||
///
|
||
/// 传输格式:<c>[4 字节小端长度][UTF-8 JSON]</c>。
|
||
/// 选命名管道而不是本机端口:不占端口、管道断开即知对端已死、ACL 天然限制访问者。
|
||
/// </summary>
|
||
public sealed record HikWorkerRequest(string Id, string Op, JsonElement? Payload)
|
||
{
|
||
public static HikWorkerRequest Create(string op, object? payload = null)
|
||
{
|
||
var element = payload is null
|
||
? (JsonElement?)null
|
||
: JsonSerializer.SerializeToElement(payload, HikWorkerJson.Options);
|
||
return new HikWorkerRequest(Guid.NewGuid().ToString("N"), op, element);
|
||
}
|
||
}
|
||
|
||
/// <summary>worker 的响应;<see cref="Data"/> 由调用方按 <see cref="Op"/> 自行反序列化</summary>
|
||
public sealed record HikWorkerResponse(string Id, bool Ok, string? Error, JsonElement? Data);
|
||
|
||
/// <summary>管道消息的读写(长度前缀 + JSON),两端共用</summary>
|
||
public static class HikWorkerPipe
|
||
{
|
||
/// <summary>管道名(服务单实例,固定名即可)</summary>
|
||
public const string PipeName = "YuncangTrace.HikWorker";
|
||
|
||
/// <summary>单条消息上限(流计划带十几个通道也远小于这个值)</summary>
|
||
private const int MaxMessageBytes = 8 * 1024 * 1024;
|
||
|
||
public static async Task WriteAsync<T>(Stream stream, T payload, CancellationToken ct)
|
||
{
|
||
var json = JsonSerializer.SerializeToUtf8Bytes(payload, HikWorkerJson.Options);
|
||
var header = new byte[4];
|
||
BinaryPrimitives.WriteInt32LittleEndian(header, json.Length);
|
||
|
||
await stream.WriteAsync(header, ct).ConfigureAwait(false);
|
||
await stream.WriteAsync(json, ct).ConfigureAwait(false);
|
||
await stream.FlushAsync(ct).ConfigureAwait(false);
|
||
}
|
||
|
||
/// <summary>读一条消息;对端正常关闭返回 null</summary>
|
||
public static async Task<T?> ReadAsync<T>(Stream stream, CancellationToken ct) where T : class
|
||
{
|
||
var header = new byte[4];
|
||
if (!await ReadExactAsync(stream, header, ct).ConfigureAwait(false)) return null;
|
||
|
||
var length = BinaryPrimitives.ReadInt32LittleEndian(header);
|
||
if (length is <= 0 or > MaxMessageBytes)
|
||
{
|
||
throw new InvalidDataException($"管道消息长度非法:{length}");
|
||
}
|
||
|
||
var buffer = new byte[length];
|
||
if (!await ReadExactAsync(stream, buffer, ct).ConfigureAwait(false))
|
||
{
|
||
throw new EndOfStreamException("管道消息被截断");
|
||
}
|
||
|
||
return JsonSerializer.Deserialize<T>(buffer, HikWorkerJson.Options);
|
||
}
|
||
|
||
private static async Task<bool> ReadExactAsync(Stream stream, byte[] buffer, CancellationToken ct)
|
||
{
|
||
var offset = 0;
|
||
while (offset < buffer.Length)
|
||
{
|
||
var read = await stream.ReadAsync(buffer.AsMemory(offset), ct).ConfigureAwait(false);
|
||
if (read == 0) return offset > 0 ? throw new EndOfStreamException("管道消息被截断") : false;
|
||
offset += read;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
|
||
/// <summary>统一的序列化选项(两端必须一致,否则时间/枚举会解析出偏差)</summary>
|
||
public static class HikWorkerJson
|
||
{
|
||
public static readonly JsonSerializerOptions Options = new()
|
||
{
|
||
PropertyNameCaseInsensitive = true,
|
||
// 中文警告/错误直接可读,不转 \uXXXX
|
||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||
};
|
||
}
|
||
|
||
/// <summary>worker 支持的操作名(两端共用,避免拼错)</summary>
|
||
public static class HikWorkerOps
|
||
{
|
||
/// <summary>自检:返回 SDK / ffmpeg 是否就绪、录像根目录</summary>
|
||
public const string Probe = "probe";
|
||
|
||
/// <summary>按流计划下载并合并</summary>
|
||
public const string Download = "download";
|
||
|
||
/// <summary>优雅退出</summary>
|
||
public const string Shutdown = "shutdown";
|
||
}
|
||
|
||
/// <summary>
|
||
/// 局域网发现用的端口(与 17300/17301 同族,但独立进程独立端口)。
|
||
///
|
||
/// - <see cref="UdpQuery"/>:收桌面端的广播询问(**询问-应答**,不是主动公告);
|
||
/// - <see cref="Http"/>:取流健康端点,桌面端用一次 TCP 连接就能判定"是否同 LAN"。
|
||
/// </summary>
|
||
public static class HikWorkerPorts
|
||
{
|
||
public const int UdpQuery = 17310;
|
||
public const int Http = 17311;
|
||
}
|
||
|
||
/// <summary>局域网询问/应答的报文(两端共用)</summary>
|
||
public static class HikWorkerDiscovery
|
||
{
|
||
public const string QueryType = "edge.query";
|
||
public const string ReplyType = "edge.reply";
|
||
}
|
||
|