Esempio n. 1
0
 /// <summary>
 ///     Called one last time when the <see cref="IChannelHandlerContext" /> goes inactive, which means the
 ///     <see cref="IChannelHandler.ChannelInactive" /> was triggered.
 ///     By default this will jsut call <see cref="Decode" /> but sub-classes may override this for special cleanup
 ///     operations.
 /// </summary>
 protected virtual void DecodeLast(IChannelHandlerContext context, IByteBuf input, List <object> output)
 {
     if (input.IsReadable())
     {
         // Only call Decode if there is something left in the buffer to decode
         Decode(context, input, output);
     }
 }
Esempio n. 2
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            if (message is IByteBuf)
            {
                var output = RecyclableArrayList.Take();
                try
                {
                    var data = (IByteBuf)message;
                    _first = _cumulation == null;
                    if (_first)
                    {
                        _cumulation = data;
                    }
                    else
                    {
                        _cumulation = _cumulator(context.Allocator, _cumulation, data);
                    }
                    CallDecode(context, _cumulation, output);
                }
                catch (DecoderException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new DecoderException(ex);
                }
                finally
                {
                    if (_cumulation != null && !_cumulation.IsReadable())
                    {
                        _numReads = 0;
                        _cumulation.Release();
                        _cumulation = null;
                    }
                    else if (++_numReads >= _discardAfterReads)
                    {
                        _numReads = 0;
                        DiscardSomeReadBytes();
                    }

                    var size = output.Count;
                    _decodeWasNull = size == 0;
                    FireChannelRead(context, output, size);
                    output.Return();
                }
            }
            else
            {
                // not a byte buffer? then we can't handle it. Forward it along
                context.FireChannelRead(message);
            }
        }
 private void HandleReadException(IChannelPipeline pipeline, IByteBuf byteBuf, Exception cause, bool close)
 {
     if (byteBuf != null)
     {
         if (byteBuf.IsReadable())
         {
             Channel.ReadPending = false;
             pipeline.FireChannelRead(byteBuf);
         }
         else
         {
             byteBuf.Release();
         }
     }
     pipeline.FireChannelReadComplete();
     pipeline.FireExceptionCaught(cause);
     if (close || cause is SocketException)
     {
         CloseOnRead();
     }
 }
Esempio n. 4
0
 public bool IsReadable()
 {
     return(_buf.IsReadable());
 }
Esempio n. 5
0
        protected void CallDecode(IChannelHandlerContext context, IByteBuf input, List <object> output)
        {
            try
            {
                while (input.IsReadable())
                {
                    var outSize = output.Count;
                    if (outSize > 0)
                    {
                        FireChannelRead(context, output, outSize);
                        output.Clear();

                        // Check if this handler was removed before continuing with decoding
                        // If it was removed, it is no longer safe to keep operating on the buffer
                        if (context.Removed)
                        {
                            break;
                        }

                        outSize = 0;
                    }

                    var oldInputLength = input.ReadableBytes;
                    Decode(context, input, output);

                    // Check if this handler was removed before continuing with decoding
                    // If it was removed, it is no longer safe to keep operating on the buffer
                    if (context.Removed)
                    {
                        break;
                    }

                    if (outSize == output.Count)
                    {
                        if (oldInputLength == input.ReadableBytes)
                        {
                            break;
                        }
                        continue;
                    }

                    if (oldInputLength == input.ReadableBytes)
                    {
                        throw new DecoderException($"{GetType()}.Decode() did not read anything but decoded a message.");
                    }

                    if (IsSingleDecode)
                    {
                        break;
                    }
                }
            }
            catch (DecoderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DecoderException(ex);
            }
        }
 private void HandleReadException(IChannelPipeline pipeline, IByteBuf byteBuf, Exception cause, bool close)
 {
     if (byteBuf != null)
     {
         if (byteBuf.IsReadable())
         {
             Channel.ReadPending = false;
             pipeline.FireChannelRead(byteBuf);
         }
         else
         {
             byteBuf.Release();
         }
     }
     pipeline.FireChannelReadComplete();
     pipeline.FireExceptionCaught(cause);
     if (close || cause is SocketException)
     {
         CloseOnRead();
     }
 }
Esempio n. 7
0
 /// <summary>
 ///     Called one last time when the <see cref="IChannelHandlerContext" /> goes inactive, which means the
 ///     <see cref="IChannelHandler.ChannelInactive" /> was triggered.
 ///     By default this will jsut call <see cref="Decode" /> but sub-classes may override this for special cleanup
 ///     operations.
 /// </summary>
 protected virtual void DecodeLast(IChannelHandlerContext context, IByteBuf input, List<object> output)
 {
     if (input.IsReadable())
     {
         // Only call Decode if there is something left in the buffer to decode
         Decode(context, input, output);
     }
 }
Esempio n. 8
0
        protected void CallDecode(IChannelHandlerContext context, IByteBuf input, List<object> output)
        {
            try
            {
                while (input.IsReadable())
                {
                    var outSize = output.Count;
                    if (outSize > 0)
                    {
                        FireChannelRead(context, output, outSize);
                        output.Clear();

                        // Check if this handler was removed before continuing with decoding
                        // If it was removed, it is no longer safe to keep operating on the buffer
                        if (context.Removed)
                            break;

                        outSize = 0;
                    }

                    var oldInputLength = input.ReadableBytes;
                    Decode(context, input, output);

                    // Check if this handler was removed before continuing with decoding
                    // If it was removed, it is no longer safe to keep operating on the buffer
                    if (context.Removed)
                        break;

                    if (outSize == output.Count)
                    {
                        if (oldInputLength == input.ReadableBytes)
                        {
                            break;
                        }
                        continue;
                    }

                    if (oldInputLength == input.ReadableBytes)
                    {
                        throw new DecoderException($"{GetType()}.Decode() did not read anything but decoded a message.");
                    }

                    if (IsSingleDecode)
                        break;
                }
            }
            catch (DecoderException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DecoderException(ex);
            }
        }