Files
Ayay/SHH.CameraDashboard/Invokes/Interceptors/InterceptorPipeline.cs
wilson 3351ae739e 在 AiVideo 中能看到图像
增加了在线状态同步逻辑
2026-01-09 12:30:36 +08:00

47 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace SHH.CameraDashboard
{
// 简单的上下文定义
public class ProtocolContext
{
public string Protocol { get; set; }
public byte[] Data { get; set; }
public bool IsBlocked { get; set; } = false;
public ProtocolContext(string p, byte[] d) { Protocol = p; Data = d; }
}
public interface IProtocolInterceptor
{
Task OnSendingAsync(ProtocolContext context);
Task OnReceivedAsync(ProtocolContext context);
}
public class InterceptorPipeline
{
// 因为 Dashboard 可能没有复杂的 DI这里支持手动添加列表
private readonly List<IProtocolInterceptor> _interceptors = new List<IProtocolInterceptor>();
public void Add(IProtocolInterceptor interceptor) => _interceptors.Add(interceptor);
public async Task<ProtocolContext?> ExecuteSendAsync(string protocol, byte[] data)
{
var ctx = new ProtocolContext(protocol, data);
foreach (var i in _interceptors)
{
await i.OnSendingAsync(ctx);
if (ctx.IsBlocked) return null;
}
return ctx;
}
public async Task<ProtocolContext?> ExecuteReceiveAsync(string protocol, byte[] data)
{
var ctx = new ProtocolContext(protocol, data);
foreach (var i in _interceptors)
{
await i.OnReceivedAsync(ctx);
if (ctx.IsBlocked) return null;
}
return ctx;
}
}
}