有没有办法禁用 IdleState?
Is there a way to disable 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
。这是一个简单的例子,它将在指定的时间 inactivity 后关闭连接;它会在 activity:
时自动重置
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 秒 inactivity 周期的管道(对于 any 类型的 inactivity):
pipeline.AddLast(name: "idle-handler", new
TimeoutHandler(readerIdleTimeSeconds: 60, writerIdleTimeSeconds: 60, allIdleTimeSeconds: 60));
值得查看 DotNetty 的 IdleStateHandler
的源代码(鉴于缺少文档)。例如,您将在 WriteAsync
中看到,在完成写入任务后,它会重置 lastWriteTime
。
我正在设置 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
。这是一个简单的例子,它将在指定的时间 inactivity 后关闭连接;它会在 activity:
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 秒 inactivity 周期的管道(对于 any 类型的 inactivity):
pipeline.AddLast(name: "idle-handler", new
TimeoutHandler(readerIdleTimeSeconds: 60, writerIdleTimeSeconds: 60, allIdleTimeSeconds: 60));
值得查看 DotNetty 的 IdleStateHandler
的源代码(鉴于缺少文档)。例如,您将在 WriteAsync
中看到,在完成写入任务后,它会重置 lastWriteTime
。