首页 > 解决方案 > 有没有办法禁用 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)
    {
    }
}

标签: c#.netnetty

解决方案


处理程序将为您执行此操作。如果你继承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.


推荐阅读