我正在设置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)
{
}
}
答案 0 :(得分:0)
处理程序将为您执行此操作。如果您要从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
。