Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        //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);
            }
        }
Esempio n. 4
0
 /// <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);
 }
Esempio n. 5
0
 protected override void ChannelIdle(IChannelHandlerContext context, IdleStateEvent stateEvent)
 {
     Debug.Assert(stateEvent.State == IdleState.ReaderIdle);
     ReadTimedOut(context);
 }
Esempio n. 6
0
 /// <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 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);
 }