static void HandleWriteTimeout(IdleStateHandler self, IChannelHandlerContext context) { TimeSpan lastWriteTime = self.lastWriteTime; TimeSpan nextDelay = self.writerIdleTime - (self.Ticks() - lastWriteTime); if (nextDelay.Ticks <= 0) { // Writer is idle - set a new timeout and notify the callback. self.writerIdleTimeout = self.Schedule(context, WriteTimeoutAction, self, context, self.writerIdleTime); bool first = self.firstWriterIdleEvent; self.firstWriterIdleEvent = false; try { if (self.HasOutputChanged(context, first)) { return; } IdleStateEvent stateEvent = self.NewIdleStateEvent(IdleState.WriterIdle, first); self.ChannelIdle(context, stateEvent); } catch (Exception ex) { context.FireExceptionCaught(ex); } } else { // Write occurred before the timeout - set a new timeout with shorter delay. self.writerIdleTimeout = self.Schedule(context, WriteTimeoutAction, self, context, nextDelay); } }
static void HandleAllTimeout(IdleStateHandler self, IChannelHandlerContext context) { TimeSpan nextDelay = self.allIdleTime; if (!self.reading) { nextDelay -= self.Ticks() - TimeUtil.Max(self.lastReadTime, self.lastWriteTime); } if (nextDelay.Ticks <= 0) { // Both reader and writer are idle - set a new timeout and // notify the callback. self.allIdleTimeout = self.Schedule(context, AllTimeoutAction, self, context, self.allIdleTime); bool first = self.firstAllIdleEvent; self.firstAllIdleEvent = false; try { if (self.HasOutputChanged(context, first)) { return; } IdleStateEvent stateEvent = self.NewIdleStateEvent(IdleState.AllIdle, first); self.ChannelIdle(context, stateEvent); } catch (Exception ex) { context.FireExceptionCaught(ex); } } else { // Either read or write occurred before the timeout - set a new // timeout with shorter delay. self.allIdleTimeout = self.Schedule(context, AllTimeoutAction, self, context, nextDelay); } }
static void HandleReadTimeout(IdleStateHandler self, IChannelHandlerContext context) { TimeSpan nextDelay = self.readerIdleTime; if (!self.reading) { nextDelay -= self.Ticks() - self.lastReadTime; } if (nextDelay.Ticks <= 0) { // Reader is idle - set a new timeout and notify the callback. self.readerIdleTimeout = self.Schedule(context, ReadTimeoutAction, self, context, self.readerIdleTime); bool first = self.firstReaderIdleEvent; self.firstReaderIdleEvent = false; try { IdleStateEvent stateEvent = self.NewIdleStateEvent(IdleState.ReaderIdle, first); self.ChannelIdle(context, stateEvent); } catch (Exception ex) { context.FireExceptionCaught(ex); } } else { // Read occurred before the timeout - set a new timeout with shorter delay. self.readerIdleTimeout = self.Schedule(context, ReadTimeoutAction, self, context, nextDelay); } }
/// <summary> /// Is called when an <see cref="IdleStateEvent"/> should be fired. This implementation calls /// <see cref="IChannelHandlerContext.FireUserEventTriggered(object)"/>. /// </summary> /// <param name="context">Context.</param> /// <param name="stateEvent">Evt.</param> protected virtual void ChannelIdle(IChannelHandlerContext context, IdleStateEvent stateEvent) { context.FireUserEventTriggered(stateEvent); }
protected override void ChannelIdle(IChannelHandlerContext context, IdleStateEvent stateEvent) { Contract.Requires(stateEvent.State == IdleState.ReaderIdle); this.ReadTimedOut(context); }