在 AiVideo 中能看到图像
增加了在线状态同步逻辑
This commit is contained in:
45
SHH.CameraService/Interceptors/InterceptorPipeline.cs
Normal file
45
SHH.CameraService/Interceptors/InterceptorPipeline.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace SHH.CameraService;
|
||||
|
||||
public class InterceptorPipeline
|
||||
{
|
||||
private readonly IEnumerable<IProtocolInterceptor> _interceptors;
|
||||
|
||||
// 通过依赖注入获取所有注册的拦截器
|
||||
public InterceptorPipeline(IEnumerable<IProtocolInterceptor> interceptors)
|
||||
{
|
||||
_interceptors = interceptors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行发送管道
|
||||
/// </summary>
|
||||
/// <returns>返回处理后的上下文,如果被拦截则返回 null</returns>
|
||||
public async Task<ProtocolContext?> ExecuteSendAsync(string protocol, byte[] data)
|
||||
{
|
||||
var context = new ProtocolContext(protocol, data);
|
||||
|
||||
foreach (var interceptor in _interceptors)
|
||||
{
|
||||
await interceptor.OnSendingAsync(context);
|
||||
if (context.IsBlocked) return null; // 被拦截,终止发送
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行接收管道
|
||||
/// </summary>
|
||||
public async Task<ProtocolContext?> ExecuteReceiveAsync(string protocol, byte[] data)
|
||||
{
|
||||
var context = new ProtocolContext(protocol, data);
|
||||
|
||||
foreach (var interceptor in _interceptors)
|
||||
{
|
||||
await interceptor.OnReceivedAsync(context);
|
||||
if (context.IsBlocked) return null; // 被拦截,丢弃消息
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
37
SHH.CameraService/Interceptors/ProtocolContext.cs
Normal file
37
SHH.CameraService/Interceptors/ProtocolContext.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace SHH.CameraService;
|
||||
|
||||
/// <summary>
|
||||
/// 协议上下文 (用于在拦截器之间传递数据)
|
||||
/// </summary>
|
||||
public class ProtocolContext
|
||||
{
|
||||
public string Protocol { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否拦截/终止 (设为 true 则不再继续传递)
|
||||
/// </summary>
|
||||
public bool IsBlocked { get; set; } = false;
|
||||
|
||||
public ProtocolContext(string protocol, byte[] data)
|
||||
{
|
||||
Protocol = protocol;
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拦截器接口
|
||||
/// </summary>
|
||||
public interface IProtocolInterceptor
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送前触发 (Outbound)
|
||||
/// </summary>
|
||||
Task OnSendingAsync(ProtocolContext context);
|
||||
|
||||
/// <summary>
|
||||
/// 接收后触发 (Inbound)
|
||||
/// </summary>
|
||||
Task OnReceivedAsync(ProtocolContext context);
|
||||
}
|
||||
Reference in New Issue
Block a user