NetMQ 协议,支持摄像头增、删、改

This commit is contained in:
2026-01-12 18:27:58 +08:00
parent 031d4f3416
commit 3f8e42e560
20 changed files with 604 additions and 332 deletions

View File

@@ -1,10 +1,11 @@
using MessagePack;
using System.Text;
using MessagePack;
using Microsoft.Extensions.Hosting;
using NetMQ;
using NetMQ.Monitoring; // ★ 1. 必须引用 Monitoring 命名空间
using NetMQ.Sockets;
using SHH.CameraSdk;
using SHH.Contracts;
using System.Text;
namespace SHH.CameraService;
@@ -16,6 +17,10 @@ public class CommandClientWorker : BackgroundService
// 管理多个 Socket
private readonly List<DealerSocket> _sockets = new();
// ★ 2. 新增:保存 Monitor 列表,防止被 GC 回收
private readonly List<NetMQMonitor> _monitors = new();
private NetMQPoller? _poller;
public CommandClientWorker(
@@ -34,71 +39,53 @@ public class CommandClientWorker : BackgroundService
if (!_config.ShouldConnect || _config.CommandEndpoints.Count == 0) return;
// 1. 建立连接 (但不立即启动 Poller)
_poller = new NetMQPoller();
// -------------------------------------------------------------
// 核心修改区:建立连接并挂载监控器
// -------------------------------------------------------------
foreach (var ep in _config.CommandEndpoints)
{
try
{
var socket = new DealerSocket();
// 建议加上 Socket 索引或 UUID 以防服务端认为 Identity 冲突
// 或者保持原样,取决于服务端逻辑。通常同一个 AppId 连不同 Server 是没问题的。
socket.Options.Identity = Encoding.UTF8.GetBytes(_config.AppId);
socket.Connect(ep.Uri);
var monitorUrl = $"inproc://monitor_{Guid.NewGuid():N}";
var monitor = new NetMQMonitor(socket, monitorUrl, SocketEvents.Connected);
monitor.Connected += async (s, args) =>
{
Console.WriteLine($"[指令] 网络连接建立: {ep.Uri} -> 正在补发注册包...");
await SendRegisterAsync(socket);
};
// ★★★ 修正点:使用 AttachToPoller 代替 Add ★★★
// 错误写法: _poller.Add(monitor);
monitor.AttachToPoller(_poller);
// 依然需要保存引用,防止被 GC 回收
_monitors.Add(monitor);
socket.Connect(ep.Uri);
socket.ReceiveReady += OnSocketReceiveReady;
_sockets.Add(socket);
_poller.Add(socket);
Console.WriteLine($"[指令] 建立通道: {ep.Uri}");
Console.WriteLine($"[指令] 通道初始化完成: {ep.Uri} (带自动重连监控)");
}
catch (Exception ex)
{
Console.WriteLine($"[指令] 连接初始化异常: {ex.Message}");
}
catch (Exception ex) { Console.WriteLine($"[指令] 连接异常: {ex.Message}"); }
}
if (_sockets.Count == 0) return;
// =================================================================
// 2. 发送注册包 (在 Poller 启动前发送,绝对线程安全)
// 6. 绑定 ACK 逻辑 (保持不变)
// =================================================================
var registerPayload = new RegisterPayload
{
Protocol = ProtocolHeaders.ServerRegister,
InstanceId = _config.AppId,
ProcessId = Environment.ProcessId,
Version = "1.0.0",
ServerIp = "127.0.0.1",
WebApiPort = _config.BasePort,
StartTime = DateTime.Now
};
try
{
byte[] regData = MessagePackSerializer.Serialize(registerPayload);
var ctx = await _pipeline.ExecuteSendAsync(ProtocolHeaders.ServerRegister, regData);
if (ctx != null)
{
foreach (var socket in _sockets)
{
// 此时 Poller 还没跑,主线程发送是安全的
socket.SendMoreFrame(ctx.Protocol).SendFrame(ctx.Data);
}
Console.WriteLine($"[指令] 注册包已广播至 {_sockets.Count} 个目标");
}
}
catch (Exception ex)
{
Console.WriteLine($"[指令] 注册失败: {ex.Message}");
}
// =================================================================
// 3. 绑定 ACK 逻辑
// =================================================================
// 关键修正:直接使用 async void不要包裹在 Task.Run 中!
// 因为 OnResponseReady 是由 Dispatcher 触发的,而 Dispatcher 是由 Poller 线程触发的。
// 所以这里就在 Poller 线程内,可以直接操作 Socket。
_dispatcher.OnResponseReady += async (result) =>
{
try
@@ -122,8 +109,11 @@ public class CommandClientWorker : BackgroundService
};
// =================================================================
// 4. 启动 Poller (开始监听接收)
// 7. 启动 Poller
// =================================================================
// 注意:我们不需要手动发第一次注册包了,
// 因为 Poller 启动后,底层 TCP 会建立连接,从而触发 monitor.Connected 事件,
// 事件里会自动发送注册包。这就是“自动档”的好处。
_poller.RunAsync();
// 阻塞直到取消
@@ -135,12 +125,49 @@ public class CommandClientWorker : BackgroundService
// 清理
_poller.Stop();
_poller.Dispose();
foreach (var m in _monitors) m.Dispose(); // 释放监控器
foreach (var s in _sockets) s.Dispose();
}
// =================================================================
// ★ 8. 抽离出的注册包发送逻辑 (供 Monitor 调用)
// =================================================================
private async Task SendRegisterAsync(DealerSocket targetSocket)
{
try
{
var registerPayload = new RegisterPayload
{
Protocol = ProtocolHeaders.ServerRegister,
InstanceId = _config.AppId,
ProcessId = Environment.ProcessId,
Version = "1.0.0",
ServerIp = "127.0.0.1", // 建议优化获取本机真实IP
WebApiPort = _config.BasePort,
StartTime = DateTime.Now
};
byte[] regData = MessagePackSerializer.Serialize(registerPayload);
// 执行拦截器
var ctx = await _pipeline.ExecuteSendAsync(ProtocolHeaders.ServerRegister, regData);
if (ctx != null)
{
// 直接向触发事件的那个 Socket 发送
// DealerSocket 允许在连接未完全就绪时 Send它会缓存直到网络通畅
targetSocket.SendMoreFrame(ctx.Protocol).SendFrame(ctx.Data);
// Console.WriteLine($"[指令] 身份注册包已推入队列: {targetSocket.Options.Identity}");
}
}
catch (Exception ex)
{
Console.WriteLine($"[指令] 注册包发送失败: {ex.Message}");
}
}
private async void OnSocketReceiveReady(object? sender, NetMQSocketEventArgs e)
{
// 这里的代码运行在 Poller 线程
NetMQMessage incomingMsg = new NetMQMessage();
if (e.Socket.TryReceiveMultipartMessage(ref incomingMsg))
{
@@ -154,8 +181,6 @@ public class CommandClientWorker : BackgroundService
var ctx = await _pipeline.ExecuteReceiveAsync(rawProtocol, rawData);
if (ctx != null)
{
// DispatchAsync 会同步触发 OnResponseReady
// 从而在同一个线程内完成 ACK 发送,线程安全且高效。
await _dispatcher.DispatchAsync(ctx.Protocol, ctx.Data);
}
}