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 Action<object, object> WrapperTimeoutHandler(Action<IdleStateHandler, IChannelHandlerContext> action) //{ // return (handler, ctx) => // { // var self = (IdleStateHandler)handler; // instead of this // var context = (IChannelHandlerContext)ctx; // if (!context.Channel.Open) // { // return; // } // action(self, context); // }; //} 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); } }