修复文本指令只发一个通道,多通道时没有每个通道独立发的问题

This commit is contained in:
2026-01-09 13:02:00 +08:00
parent 3351ae739e
commit 031d4f3416
2 changed files with 120 additions and 112 deletions

View File

@@ -12,14 +12,16 @@ public class CommandClientWorker : BackgroundService
{
private readonly ServiceConfig _config;
private readonly CommandDispatcher _dispatcher;
// ★ 1. 注入拦截器管道管理器
private readonly InterceptorPipeline _pipeline;
// 管理多个 Socket
private readonly List<DealerSocket> _sockets = new();
private NetMQPoller? _poller;
public CommandClientWorker(
ServiceConfig config,
CommandDispatcher dispatcher,
InterceptorPipeline pipeline) // <--- 注入
InterceptorPipeline pipeline)
{
_config = config;
_dispatcher = dispatcher;
@@ -30,24 +32,35 @@ public class CommandClientWorker : BackgroundService
{
await Task.Yield();
if (!_config.ShouldConnect) return;
if (_config.CommandEndpoints.Count == 0) return;
if (!_config.ShouldConnect || _config.CommandEndpoints.Count == 0) return;
using var dealer = new DealerSocket();
string myIdentity = _config.AppId;
dealer.Options.Identity = Encoding.UTF8.GetBytes(myIdentity);
// 1. 建立连接 (但不立即启动 Poller)
_poller = new NetMQPoller();
foreach (var ep in _config.CommandEndpoints)
{
try { dealer.Connect(ep.Uri); }
catch (Exception ex) { Console.WriteLine($"[指令] 连接失败 {ep.Uri}: {ex.Message}"); }
try
{
var socket = new DealerSocket();
// 建议加上 Socket 索引或 UUID 以防服务端认为 Identity 冲突
// 或者保持原样,取决于服务端逻辑。通常同一个 AppId 连不同 Server 是没问题的。
socket.Options.Identity = Encoding.UTF8.GetBytes(_config.AppId);
socket.Connect(ep.Uri);
socket.ReceiveReady += OnSocketReceiveReady;
_sockets.Add(socket);
_poller.Add(socket);
Console.WriteLine($"[指令] 建立通道: {ep.Uri}");
}
catch (Exception ex) { Console.WriteLine($"[指令] 连接异常: {ex.Message}"); }
}
string localIp = "127.0.0.1";
// ... (获取 IP 代码省略,保持不变) ...
if (_sockets.Count == 0) return;
// =================================================================
// 构建注册包
// 2. 发送注册包 (在 Poller 启动前发送,绝对线程安全)
// =================================================================
var registerPayload = new RegisterPayload
{
@@ -55,7 +68,7 @@ public class CommandClientWorker : BackgroundService
InstanceId = _config.AppId,
ProcessId = Environment.ProcessId,
Version = "1.0.0",
ServerIp = localIp,
ServerIp = "127.0.0.1",
WebApiPort = _config.BasePort,
StartTime = DateTime.Now
};
@@ -63,92 +76,94 @@ public class CommandClientWorker : BackgroundService
try
{
byte[] regData = MessagePackSerializer.Serialize(registerPayload);
// =============================================================
// ★ 2. 拦截点 A: 发送注册包 (Outbound)
// =============================================================
var ctx = await _pipeline.ExecuteSendAsync(ProtocolHeaders.ServerRegister, regData);
if (ctx != null) // 如果未被拦截
if (ctx != null)
{
// 注意:这里使用 ctx.Protocol 和 ctx.Data允许拦截器修改内容
dealer.SendMoreFrame(ctx.Protocol)
.SendFrame(ctx.Data);
Console.WriteLine($"[指令] 注册包已发送 ({ctx.Data.Length} bytes)");
foreach (var socket in _sockets)
{
// 此时 Poller 还没跑,主线程发送是安全的
socket.SendMoreFrame(ctx.Protocol).SendFrame(ctx.Data);
}
Console.WriteLine($"[指令] 注册包已广播至 {_sockets.Count} 个目标");
}
}
catch (Exception ex)
{
Console.WriteLine($"[致命错误] 注册流程异常: {ex.Message}");
return;
Console.WriteLine($"[指令] 注册失败: {ex.Message}");
}
// =================================================================
// 定义 ACK 发送逻辑 (包含拦截器)
// 3. 绑定 ACK 逻辑
// =================================================================
// 注意:这里需要 async因为拦截器是异步的
Action<CommandResult> sendAckHandler = async (result) =>
// 关键修正:直接使用 async void不要包裹在 Task.Run 中!
// 因为 OnResponseReady 是由 Dispatcher 触发的,而 Dispatcher 是由 Poller 线程触发的。
// 所以这里就在 Poller 线程内,可以直接操作 Socket。
_dispatcher.OnResponseReady += async (result) =>
{
try
{
byte[] resultBytes = MessagePackSerializer.Serialize(result);
// =========================================================
// ★ 3. 拦截点 B: 发送 ACK 回执 (Outbound)
// =========================================================
// 协议头是 COMMAND_RESULT
var ctx = await _pipeline.ExecuteSendAsync(ProtocolHeaders.CommandResult, resultBytes);
if (ctx != null)
{
dealer.SendMoreFrame(ctx.Protocol)
.SendFrame(ctx.Data);
Console.WriteLine($"[指令] 已回复 ACK -> Req: {result.RequestId}");
foreach (var socket in _sockets)
{
socket.SendMoreFrame(ctx.Protocol).SendFrame(ctx.Data);
}
Console.WriteLine($"[指令] ACK 已广播 (ID: {result.RequestId})");
}
}
catch (Exception ex)
{
Console.WriteLine($"[ACK Error] 回执发送失败: {ex.Message}");
Console.WriteLine($"[ACK] 发送失败: {ex.Message}");
}
};
// 订阅事件 (需要适配 async void注意异常捕获)
_dispatcher.OnResponseReady += async (res) => await Task.Run(() => sendAckHandler(res));
// =================================================================
// 4. 启动 Poller (开始监听接收)
// =================================================================
_poller.RunAsync();
// =================================================================
// 接收循环
// =================================================================
try
// 阻塞直到取消
while (!stoppingToken.IsCancellationRequested)
{
while (!stoppingToken.IsCancellationRequested)
await Task.Delay(1000, stoppingToken);
}
// 清理
_poller.Stop();
_poller.Dispose();
foreach (var s in _sockets) s.Dispose();
}
private async void OnSocketReceiveReady(object? sender, NetMQSocketEventArgs e)
{
// 这里的代码运行在 Poller 线程
NetMQMessage incomingMsg = new NetMQMessage();
if (e.Socket.TryReceiveMultipartMessage(ref incomingMsg))
{
if (incomingMsg.FrameCount >= 2)
{
NetMQMessage incomingMsg = new NetMQMessage();
if (dealer.TryReceiveMultipartMessage(TimeSpan.FromMilliseconds(500), ref incomingMsg))
try
{
if (incomingMsg.FrameCount >= 2)
string rawProtocol = incomingMsg[0].ConvertToString();
byte[] rawData = incomingMsg[1].ToByteArray();
var ctx = await _pipeline.ExecuteReceiveAsync(rawProtocol, rawData);
if (ctx != null)
{
string rawProtocol = incomingMsg[0].ConvertToString();
byte[] rawData = incomingMsg[1].ToByteArray();
// =================================================
// ★ 4. 拦截点 C: 接收指令 (Inbound)
// =================================================
var ctx = await _pipeline.ExecuteReceiveAsync(rawProtocol, rawData);
if (ctx != null) // 如果未被拦截
{
// 将处理后的数据交给 Dispatcher
await _dispatcher.DispatchAsync(ctx.Protocol, ctx.Data);
}
// DispatchAsync 会同步触发 OnResponseReady
// 从而在同一个线程内完成 ACK 发送,线程安全且高效。
await _dispatcher.DispatchAsync(ctx.Protocol, ctx.Data);
}
}
catch (Exception ex)
{
Console.WriteLine($"[指令] 处理异常: {ex.Message}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"[指令] 接收循环异常: {ex.Message}");
}
}
}