c# - 有没有办法禁用 IdleState?
问题描述
我正在设置 DotNetty 服务器。我的第一个处理程序是 IdleStateHandler 。有没有办法在收到数据时禁用此处理程序,然后在之后启用它WriteAndFlushAsync
?
public class TimeoutHandler :IdleStateHandler
{
private bool enable = true;
protected override void ChannelIdle(IChannelHandlerContext context, IdleStateEvent stateEvent)
{
base.ChannelIdle(context, stateEvent);
}
public override void ChannelRead(IChannelHandlerContext context, object message)
{
Console.WriteLine(IdleState.ReaderIdle);
NewIdleStateEvent(IdleState.ReaderIdle, false);
base.ChannelRead(context, message);
}
public override Task WriteAsync(IChannelHandlerContext context, object message)
{
return context.WriteAndFlushAsync(message as IByteBuffer);
}
public TimeoutHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) : base(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds)
{
}
public TimeoutHandler(TimeSpan readerIdleTime, TimeSpan writerIdleTime, TimeSpan allIdleTime) : base(readerIdleTime, writerIdleTime, allIdleTime)
{
}
public TimeoutHandler(bool observeOutput, TimeSpan readerIdleTime, TimeSpan writerIdleTime, TimeSpan allIdleTime) : base(observeOutput, readerIdleTime, writerIdleTime, allIdleTime)
{
}
}
解决方案
处理程序将为您执行此操作。如果你继承IdleStateHandler
了你真正需要做的就是实现它的一个或多个构造函数并覆盖ChannelIdle
。这是一个简单的示例,它将在指定的不活动时间后关闭连接;当有活动时它会自动重置:
public class TimeoutHandler : IdleStateHandler
{
/// <summary>
/// Initializes a new instance firing <see cref="T:DotNetty.Handlers.Timeout.IdleStateEvent" />s.
/// </summary>
/// <param name="readerIdleTimeSeconds">
/// an <see cref="T:DotNetty.Handlers.Timeout.IdleStateEvent" /> whose state is <see cref="F:DotNetty.Handlers.Timeout.IdleState.ReaderIdle" />
/// will be triggered when no read was performed for the specified
/// period of time. Specify <code>0</code> to disable.
/// </param>
/// <param name="writerIdleTimeSeconds">
/// an <see cref="T:DotNetty.Handlers.Timeout.IdleStateEvent" /> whose state is <see cref="F:DotNetty.Handlers.Timeout.IdleState.WriterIdle" />
/// will be triggered when no write was performed for the specified
/// period of time. Specify <code>0</code> to disable.
/// </param>
/// <param name="allIdleTimeSeconds">
/// an <see cref="T:DotNetty.Handlers.Timeout.IdleStateEvent" /> whose state is <see cref="F:DotNetty.Handlers.Timeout.IdleState.AllIdle" />
/// will be triggered when neither read nor write was performed for
/// the specified period of time. Specify <code>0</code> to disable.
/// </param>
public TimeoutHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) : base(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds)
{
}
protected override void ChannelIdle(IChannelHandlerContext context, IdleStateEvent stateEvent)
{
context.CloseAsync();
}
}
要将其添加到不活动期为 60 秒的管道(对于任何类型的不活动):
pipeline.AddLast(name: "idle-handler", new
TimeoutHandler(readerIdleTimeSeconds: 60, writerIdleTimeSeconds: 60, allIdleTimeSeconds: 60));
值得查看 DotNetty 的源代码IdleStateHandler
(鉴于缺乏文档)。例如,您将看到,WriteAsync
在完成写入任务后,它会重置lastWriteTime
.
推荐阅读
- android - Room @Transaction 和 Rx 可完成
- microsoft-graph-api - 无法使用图形 api 获取频道
- spring - Apache Camel:帮助理解 SimpleScheduledRoutePolicy 的使用
- nestjs - 如何处理 GRPC 断开连接?
- sql - 如何从 AFTER 触发器制作等效的 INSTEAD OF 触发器?
- c# - 样式化确认电子邮件身份 2.0 MVC 5
- python - NoSuchElementException:无法单击 div 元素
- .net - OrderedDictionary 的替代或解决方法以避免重复键错误
- javascript - 如何在 AVA 中禁用堆栈跟踪并在错误时显示我自己的错误消息
- r - 将 tibble 中的复杂列表对象扩展为更大的 tibble