示例#1
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                if (input.Remaining >= _length)
                {
                    Int32 limit = input.Limit;
                    input.Limit = input.Position + _length;
                    IoBuffer product = input.Slice();
                    input.Position = input.Position + _length;
                    input.Limit    = limit;
                    return(FinishDecode(product, output));
                }

                _buffer = IoBuffer.Allocate(_length);
                _buffer.Put(input);
                return(this);
            }

            if (input.Remaining >= _length - _buffer.Position)
            {
                Int32 limit = input.Limit;
                input.Limit = input.Position + _length - _buffer.Position;
                _buffer.Put(input);
                input.Limit = limit;
                IoBuffer product = _buffer;
                _buffer = null;
                return(FinishDecode(product.Flip(), output));
            }

            _buffer.Put(input);
            return(this);
        }
 public void FinishDecode(IoSession session, IProtocolDecoderOutput output)
 {
     lock (_decoder)
     {
         _decoder.FinishDecode(session, output);
     }
 }
 public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     lock (_decoder)
     {
         _decoder.Decode(session, input, output);
     }
 }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.HasRemaining)
                return FinishDecode(input.Get(), output);

            return this;
        }
示例#5
0
        public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            // Try to skip header if not read.
            if (!_readHeader)
            {
                input.GetInt16();               // Skip 'type'.
                _sequence   = input.GetInt32(); // Get 'sequence'.
                _readHeader = true;
            }

            // Try to decode body
            AbstractMessage m = DecodeBody(session, input);

            // Return NEED_DATA if the body is not fully read.
            if (m == null)
            {
                return(MessageDecoderResult.NeedData);
            }
            else
            {
                _readHeader = false; // reset readHeader for the next decode
            }
            m.Sequence = _sequence;
            output.Write(m);

            return(MessageDecoderResult.OK);
        }
        public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            // Try to skip header if not read.
            if (!_readHeader)
            {
                input.GetInt16(); // Skip 'type'.
                _sequence = input.GetInt32(); // Get 'sequence'.
                _readHeader = true;
            }

            // Try to decode body
            AbstractMessage m = DecodeBody(session, input);
            // Return NEED_DATA if the body is not fully read.
            if (m == null)
            {
                return MessageDecoderResult.NeedData;
            }
            else
            {
                _readHeader = false; // reset readHeader for the next decode
            }
            m.Sequence = _sequence;
            output.Write(m);

            return MessageDecoderResult.OK;
        }
        private void DecodeAll(ByteBuffer buf, IProtocolDecoderOutput output)
        {
            for ( ; ;)
            {
                int  oldPos  = buf.Position;
                bool decoded = DoDecode(buf, output);
                if (decoded)
                {
                    if (buf.Position == oldPos)
                    {
                        throw new Exception(
                                  "doDecode() can't return true when buffer is not consumed.");
                    }

                    if (!buf.HasRemaining)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
        }
示例#8
0
        /// <inheritdoc/>
        public override void SessionClosed(INextFilter nextFilter, IoSession session)
        {
            // Call finishDecode() first when a connection is closed.
            IProtocolDecoder       decoder    = _factory.GetDecoder(session);
            IProtocolDecoderOutput decoderOut = GetDecoderOut(session, nextFilter);

            try
            {
                decoder.FinishDecode(session, decoderOut);
            }
            catch (Exception ex)
            {
                ProtocolDecoderException pde = ex as ProtocolDecoderException;
                if (pde == null)
                {
                    pde = new ProtocolDecoderException(null, ex);
                }
                throw pde;
            }
            finally
            {
                // Dispose everything
                DisposeCodec(session);
                decoderOut.Flush(nextFilter, session);
            }

            // Call the next filter
            nextFilter.SessionClosed(session);
        }
 public void FinishDecode(IoSession session, IProtocolDecoderOutput output)
 {
     lock (_decoder)
     {
         _decoder.FinishDecode(session, output);
     }
 }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                if (input.Remaining >= _length)
                {
                    Int32 limit = input.Limit;
                    input.Limit = input.Position + _length;
                    IoBuffer product = input.Slice();
                    input.Position = input.Position + _length;
                    input.Limit = limit;
                    return FinishDecode(product, output);
                }

                _buffer = IoBuffer.Allocate(_length);
                _buffer.Put(input);
                return this;
            }

            if (input.Remaining >= _length - _buffer.Position)
            {
                Int32 limit = input.Limit;
                input.Limit = input.Position + _length - _buffer.Position;
                _buffer.Put(input);
                input.Limit = limit;
                IoBuffer product = _buffer;
                _buffer = null;
                return FinishDecode(product.Flip(), output);
            }

            _buffer.Put(input);
            return this;
        }
        public IDecodingState FinishDecode(IProtocolDecoderOutput output)
        {
            IDecodingState nextState;
            IDecodingState state = CurrentState;

            try
            {
                while (true)
                {
                    IDecodingState oldState = state;
                    state = state.FinishDecode(_childOutput);
                    if (state == null)
                        // Finished
                        break;

                    // Exit if state didn't change.
                    if (oldState == state)
                        break;
                }
            }
            catch (Exception ex)
            {
                state = null;
                Debug.WriteLine("Ignoring the exception caused by a closed session. {0}", ex);
            }
            finally
            {
                _currentState = state;
                nextState = FinishDecode(_childProducts, output);
                if (state == null)
                    Cleanup();
            }
            return nextState;
        }
 public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     lock (_decoder)
     {
         _decoder.Decode(session, input, output);
     }
 }
示例#13
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            while (input.HasRemaining)
            {
                switch (_counter)
                {
                case 0:
                    _firstByte = input.Get() & 0xff;
                    break;

                case 1:
                    _secondByte = input.Get() & 0xff;
                    break;

                case 2:
                    _thirdByte = input.Get() & 0xff;
                    break;

                case 3:
                    _counter = 0;
                    return(FinishDecode((_firstByte << 24) | (_secondByte << 16) | (_thirdByte << 8) | (input.Get() & 0xff), output));
                }

                _counter++;
            }
            return(this);
        }
        public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_session == null)
                _session = session;
            else if (_session != session)
                throw new InvalidOperationException(GetType().Name + " is a stateful decoder.  "
                        + "You have to create one per session.");

            _undecodedBuffers.Enqueue(input);
            while (true)
            {
                IoBuffer b;
                if (!_undecodedBuffers.TryPeek(out b))
                    break;

                Int32 oldRemaining = b.Remaining;
                _state.Decode(b, output);
                Int32 newRemaining = b.Remaining;
                if (newRemaining != 0)
                {
                    if (oldRemaining == newRemaining)
                        throw new InvalidOperationException(_state.GetType().Name
                            + " must consume at least one byte per decode().");
                }
                else
                {
                    _undecodedBuffers.TryDequeue(out b);
                }
            }
        }
示例#15
0
        public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            Context ctx = GetContext(session);

            if (LineDelimiter.Auto.Equals(_delimiter))
                DecodeAuto(ctx, session, input, output);
            else
                DecodeNormal(ctx, session, input, output);
        }
        /// <inheritdoc/>
        protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (!input.PrefixedDataAvailable(4, _maxObjectSize))
                return false;

            input.GetInt32();
            output.Write(input.GetObject());
            return true;
        }
示例#17
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.HasRemaining)
            {
                return(FinishDecode(input.Get(), output));
            }

            return(this);
        }
 /// <summary>
 /// Cumulates content of <tt>in</tt> into internal buffer and forwards
 /// decoding request to {@link #doDecode(IoSession, ByteBuffer, ProtocolDecoderOutput)}.
 /// <tt>doDecode()</tt> is invoked repeatedly until it returns <tt>false</tt>
 /// and the cumulative buffer is compacted after decoding ends.
 /// </summary>
 /// <exception cref="Exception">
 /// if your <tt>doDecode()</tt> returned <tt>true</tt> not consuming the cumulative buffer.
 /// </exception>
 public void Decode(ByteBuffer input, IProtocolDecoderOutput output)
 {
    if ( _remaining.Position != 0 ) // If there were remaining undecoded bytes
    {
       DecodeRemainingAndInput(input, output);
    } else
    {
       DecodeInput(input, output);
    }
 }
示例#19
0
        public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            var instr = session.GetAttribute <IInstruction>(KeyName.INSTRUCTION);

            if (instr != null)
            {
                output.Write(instr.CreateDecoder().Decode(input));
            }
            return(MessageDecoderResult.OK);
        }
示例#20
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            IDecodingState state = CurrentState;

            Int32 limit = input.Limit, pos = input.Position;

            try
            {
                while (true)
                {
                    // Wait for more data if all data is consumed.
                    if (pos == limit)
                    {
                        break;
                    }

                    IDecodingState oldState = state;
                    state = state.Decode(input, _childOutput);

                    // If finished, call finishDecode
                    if (state == null)
                    {
                        return(FinishDecode(_childProducts, output));
                    }

                    Int32 newPos = input.Position;

                    // Wait for more data if nothing is consumed and state didn't change.
                    if (newPos == pos && oldState == state)
                    {
                        break;
                    }

                    pos = newPos;
                }

                return(this);
            }
            catch (Exception)
            {
                state = null;
                throw;
            }
            finally
            {
                _currentState = state;

                // Destroy if decoding is finished or failed.
                if (state == null)
                {
                    Cleanup();
                }
            }
        }
        protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.PrefixedDataAvailable(PrefixLength, MaxDataLength))
            {
                String msg = input.GetPrefixedString(PrefixLength, Encoding);
                output.Write(msg);
                return true;
            }

            return false;
        }
 /// <summary>
 /// Cumulates content of <tt>in</tt> into internal buffer and forwards
 /// decoding request to {@link #doDecode(IoSession, ByteBuffer, ProtocolDecoderOutput)}.
 /// <tt>doDecode()</tt> is invoked repeatedly until it returns <tt>false</tt>
 /// and the cumulative buffer is compacted after decoding ends.
 /// </summary>
 /// <exception cref="Exception">
 /// if your <tt>doDecode()</tt> returned <tt>true</tt> not consuming the cumulative buffer.
 /// </exception>
 public void Decode(ByteBuffer input, IProtocolDecoderOutput output)
 {
     if (_remaining.Position != 0) // If there were remaining undecoded bytes
     {
         DecodeRemainingAndInput(input, output);
     }
     else
     {
         DecodeInput(input, output);
     }
 }
示例#23
0
        protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.PrefixedDataAvailable(PrefixLength, MaxDataLength))
            {
                String msg = input.GetPrefixedString(PrefixLength, Encoding);
                output.Write(msg);
                return(true);
            }

            return(false);
        }
示例#24
0
        /// <inheritdoc/>
        protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            if (!input.PrefixedDataAvailable(4, _maxObjectSize))
            {
                return(false);
            }

            input.GetInt32();
            output.Write(input.GetObject());
            return(true);
        }
            protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
            {
                Assert.IsTrue(input.HasRemaining);

                if (input.Remaining < 4)
                {
                    return(false);
                }

                output.Write(input.GetInt32());
                return(true);
            }
示例#26
0
        /// <inheritdoc/>
        public override void FinishDecode(IoSession session, IProtocolDecoderOutput output)
        {
            base.FinishDecode(session, output);
            State           state          = GetState(session);
            IMessageDecoder currentDecoder = state.currentDecoder;

            if (currentDecoder == null)
            {
                return;
            }
            currentDecoder.FinishDecode(session, output);
        }
        public AmqpChannel(IByteChannel byteChannel, IProtocolDecoderOutput decoderOutput)
        {
            _byteChannel   = byteChannel;
            _decoderOutput = decoderOutput;
            _syncLock      = new object();

            AMQProtocolProvider   protocolProvider = new AMQProtocolProvider();
            IProtocolCodecFactory factory          = protocolProvider.CodecFactory;

            _encoder = factory.Encoder;
            _decoder = factory.Decoder;
        }
示例#28
0
        private void TestDecoderAndInputStream(String expected, IoBuffer input)
        {
            // Test ProtocolDecoder
            IProtocolDecoder       decoder    = new ObjectSerializationDecoder();
            ProtocolCodecSession   session    = new ProtocolCodecSession();
            IProtocolDecoderOutput decoderOut = session.DecoderOutput;

            decoder.Decode(session, input.Duplicate(), decoderOut);

            Assert.AreEqual(1, session.DecoderOutputQueue.Count);
            Assert.AreEqual(expected, session.DecoderOutputQueue.Dequeue());
        }
示例#29
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 terminatorPos = input.IndexOf(_terminator);

            if (terminatorPos >= 0)
            {
                Int32    limit = input.Limit;
                IoBuffer product;

                if (input.Position < terminatorPos)
                {
                    input.Limit = terminatorPos;

                    if (_buffer == null)
                    {
                        product = input.Slice();
                    }
                    else
                    {
                        _buffer.Put(input);
                        product = _buffer.Flip();
                        _buffer = null;
                    }

                    input.Limit = limit;
                }
                else
                {
                    // When input contained only terminator rather than actual data...
                    if (_buffer == null)
                    {
                        product = IoBuffer.Allocate(0);
                    }
                    else
                    {
                        product = _buffer.Flip();
                        _buffer = null;
                    }
                }
                input.Position = terminatorPos + 1;
                return(FinishDecode(product, output));
            }

            if (_buffer == null)
            {
                _buffer            = IoBuffer.Allocate(input.Remaining);
                _buffer.AutoExpand = true;
            }

            _buffer.Put(input);
            return(this);
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 terminatorPos = input.IndexOf(_terminator);

            if (terminatorPos >= 0)
            {
                Int32 limit = input.Limit;
                IoBuffer product;

                if (input.Position < terminatorPos)
                {
                    input.Limit = terminatorPos;

                    if (_buffer == null)
                    {
                        product = input.Slice();
                    }
                    else
                    {
                        _buffer.Put(input);
                        product = _buffer.Flip();
                        _buffer = null;
                    }

                    input.Limit = limit;
                }
                else
                {
                    // When input contained only terminator rather than actual data...
                    if (_buffer == null)
                    {
                        product = IoBuffer.Allocate(0);
                    }
                    else
                    {
                        product = _buffer.Flip();
                        _buffer = null;
                    }
                }
                input.Position = terminatorPos + 1;
                return FinishDecode(product, output);
            }

            if (_buffer == null)
            {
                _buffer = IoBuffer.Allocate(input.Remaining);
                _buffer.AutoExpand = true;
            }

            _buffer.Put(input);
            return this;
        }
示例#31
0
        private IProtocolDecoderOutput GetDecoderOut(IoSession session, INextFilter nextFilter)
        {
            IProtocolDecoderOutput output = session.GetAttribute <IProtocolDecoderOutput>(DECODER_OUT);

            if (output == null)
            {
                // Create a new instance, and stores it into the session
                output = new ProtocolDecoderOutputImpl();
                session.SetAttribute(DECODER_OUT, output);
            }

            return(output);
        }
示例#32
0
        /// <inheritdoc/>
        public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            Context ctx = GetContext(session);

            if (LineDelimiter.Auto.Equals(_delimiter))
            {
                DecodeAuto(ctx, session, input, output);
            }
            else
            {
                DecodeNormal(ctx, session, input, output);
            }
        }
示例#33
0
 protected override Boolean DoDecode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     if (input.Remaining >= 4)
     {
         Message request = input.GetMessage();
         output.Write(request);
         return true;
     }
     else
     {
         return false;
     }
 }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                _buffer = IoBuffer.Allocate(256);
                _buffer.AutoExpand = true;
            }

            if (_buffer.Position + input.Remaining > _maxLength)
                throw new ProtocolDecoderException("Received data exceeds " + _maxLength + " byte(s).");
         
            _buffer.Put(input);
            return this;
        }
 public IDecodingState FinishDecode(IProtocolDecoderOutput output)
 {
     IoBuffer readData;
     if (_buffer == null)
     {
         readData = IoBuffer.Allocate(0);
     }
     else
     {
         readData = _buffer.Flip();
         _buffer = null;
     }
     return FinishDecode(readData, output);
 }
示例#36
0
            /// <summary>
            /// Decodes the specified session.
            /// </summary>
            /// <param name="inbuf">The inbuf.</param>
            /// <param name="output">The protocol output.</param>
            /// <returns></returns>
            public MessageDecoderResult Decode(ByteBuffer inbuf, IProtocolDecoderOutput output)
            {
                byte[] header = new byte[4];
                inbuf.GetBytes(header);
                ProtocolInitiation pi = new ProtocolInitiation();

                pi.Header           = new char[] { 'A', 'M', 'Q', 'P' };
                pi.ProtocolClass    = inbuf.GetByte();
                pi.ProtocolInstance = inbuf.GetByte();
                pi.ProtocolMajor    = inbuf.GetByte();
                pi.ProtocolMinor    = inbuf.GetByte();
                output.Write(pi);
                return(MessageDecoderResult.OK);
            }
示例#37
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Boolean found    = false;
            Boolean finished = false;

            while (input.HasRemaining)
            {
                Byte b = input.Get();
                if (!_hasCR)
                {
                    if (b == CR)
                    {
                        _hasCR = true;
                    }
                    else
                    {
                        if (b == LF)
                        {
                            found = true;
                        }
                        else
                        {
                            input.Position = input.Position - 1;
                            found          = false;
                        }
                        finished = true;
                        break;
                    }
                }
                else
                {
                    if (b == LF)
                    {
                        found    = true;
                        finished = true;
                        break;
                    }

                    throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff));
                }
            }

            if (finished)
            {
                _hasCR = false;
                return(FinishDecode(found, output));
            }

            return(this);
        }
示例#38
0
        public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            var value = input.GetRemaining().Array;

            input.Position = value.Length;
            if (value == null)
            {
                return(MessageDecoderResult.NeedData);
            }

            output.Write(value);

            return(MessageDecoderResult.OK);
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Boolean found = false;
            Boolean finished = false;
            while (input.HasRemaining)
            {
                Byte b = input.Get();
                if (!_hasCR)
                {
                    if (b == CR)
                    {
                        _hasCR = true;
                    }
                    else
                    {
                        if (b == LF)
                        {
                            found = true;
                        }
                        else
                        {
                            input.Position = input.Position - 1;
                            found = false;
                        }
                        finished = true;
                        break;
                    }
                }
                else
                {
                    if (b == LF)
                    {
                        found = true;
                        finished = true;
                        break;
                    }

                    throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff));
                }
            }

            if (finished)
            {
                _hasCR = false;
                return FinishDecode(found, output);
            }

            return this;
        }
 public IDecodingState FinishDecode(IProtocolDecoderOutput output)
 {
     IoBuffer product;
     // When input contained only terminator rather than actual data...
     if (_buffer == null)
     {
         product = IoBuffer.Allocate(0);
     }
     else
     {
         product = _buffer.Flip();
         _buffer = null;
     }
     return FinishDecode(product, output);
 }
 private void DecodeInput(ByteBuffer input, IProtocolDecoderOutput output)
 {
    _logger.Debug(string.Format("DecodeInput: input {0}", input.Remaining));
    // Just decode the input buffer and remember any remaining undecoded bytes.
    try
    {
       DecodeAll(input, output);
    } finally
    {
       if ( input.HasRemaining )
       {
          _remaining.Put(input);
       }
    }
 }
 private void DecodeInput(ByteBuffer input, IProtocolDecoderOutput output)
 {
     _logger.Debug(string.Format("DecodeInput: input {0}", input.Remaining));
     // Just decode the input buffer and remember any remaining undecoded bytes.
     try
     {
         DecodeAll(input, output);
     } finally
     {
         if (input.HasRemaining)
         {
             _remaining.Put(input);
         }
     }
 }
示例#43
0
        public IDecodingState FinishDecode(IProtocolDecoderOutput output)
        {
            IoBuffer readData;

            if (_buffer == null)
            {
                readData = IoBuffer.Allocate(0);
            }
            else
            {
                readData = _buffer.Flip();
                _buffer  = null;
            }
            return(FinishDecode(readData, output));
        }
        public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            int totalLen = input.GetInt32();
            int len      = totalLen - 4;

            if (input.Remaining < len)
            {
                return(MessageDecoderResult.NeedData);
            }
            byte[] jsonBuffer = new byte[len];
            input.Get(jsonBuffer, 0, len);
            string msg = System.Text.Encoding.UTF8.GetString(jsonBuffer);

            output.Write(msg);
            return(MessageDecoderResult.OK);
        }
        public IDecodingState FinishDecode(IProtocolDecoderOutput output)
        {
            IoBuffer product;

            // When input contained only CR or LF rather than actual data...
            if (_buffer == null)
            {
                product = IoBuffer.Allocate(0);
            }
            else
            {
                product = _buffer.Flip();
                _buffer = null;
            }
            return(FinishDecode(product, output));
        }
 public IDecodingState FinishDecode(IProtocolDecoderOutput output)
 {
     try
     {
         if (_buffer == null)
         {
             _buffer = IoBuffer.Allocate(0);
         }
         _buffer.Flip();
         return(FinishDecode(_buffer, output));
     }
     finally
     {
         _buffer = null;
     }
 }
示例#47
0
 public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     int limit = input.Limit;
     int position = input.Position;
     var len = input.GetInt32();
     var version = input.Get();
     input.Position = position;
     input.Limit = input.Position + len;
     var buffer = input.Slice();
     input.Position = input.Limit;
     input.Limit = limit;
     var message = DoDecode(version.ToEnum<MessageVersion>(), buffer);
     if (message != null)
         output.Write(message);
     return MessageDecoderResult.OK;
 }
 public IDecodingState FinishDecode(IProtocolDecoderOutput output)
 {
     try
     {
         if (_buffer == null)
         {
             _buffer = IoBuffer.Allocate(0);
         }
         _buffer.Flip();
         return FinishDecode(_buffer, output);
     }
     finally
     {
         _buffer = null;
     }
 }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (_buffer == null)
            {
                _buffer            = IoBuffer.Allocate(256);
                _buffer.AutoExpand = true;
            }

            if (_buffer.Position + input.Remaining > _maxLength)
            {
                throw new ProtocolDecoderException("Received data exceeds " + _maxLength + " byte(s).");
            }

            _buffer.Put(input);
            return(this);
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            IDecodingState state = CurrentState;

            Int32 limit = input.Limit, pos = input.Position;

            try
            {
                while (true)
                {
                    // Wait for more data if all data is consumed.
                    if (pos == limit)
                        break;

                    IDecodingState oldState = state;
                    state = state.Decode(input, _childOutput);

                    // If finished, call finishDecode
                    if (state == null)
                        return FinishDecode(_childProducts, output);

                    Int32 newPos = input.Position;

                    // Wait for more data if nothing is consumed and state didn't change.
                    if (newPos == pos && oldState == state)
                        break;

                    pos = newPos;
                }

                return this;
            }
            catch (Exception)
            {
                state = null;
                throw;
            }
            finally
            {
                _currentState = state;

                // Destroy if decoding is finished or failed.
                if (state == null)
                    Cleanup();
            }
        }
示例#51
0
 public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     try
     {
         input.Skip(MIN_REQUIRED_FRAME_LENGTH);
         var command = commandParser.Parse(input);
         output.Write(command);
         return MessageDecoderResult.OK;
     }
     catch (Exception ex)
     {
         var remoteAddr = string.Empty;
         if (session != null && session.RemoteEndPoint != null) remoteAddr = session.RemoteEndPoint.ToString();
         log.Error(ex, new Dictionary<string, string> { { "RemoteAddr", remoteAddr } });
         return MessageDecoderResult.NotOK;
     }
 }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            while (input.HasRemaining)
            {
                switch (_counter)
                {
                    case 0:
                        _highByte = input.Get() & 0xff;
                        break;
                    case 1:
                        _counter = 0;
                        return FinishDecode((Int16)((_highByte << 8) | (input.Get() & 0xff)), output);
                }

                _counter++;
            }
            return this;
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            while (input.HasRemaining)
            {
                switch (_counter)
                {
                case 0:
                    _highByte = input.Get() & 0xff;
                    break;

                case 1:
                    _counter = 0;
                    return(FinishDecode((Int16)((_highByte << 8) | (input.Get() & 0xff)), output));
                }

                _counter++;
            }
            return(this);
        }
 private void DecodeRemainingAndInput(ByteBuffer input, IProtocolDecoderOutput output)
 {
    _logger.Debug(string.Format("DecodeRemainingAndInput: input {0}, remaining {1}", input.Remaining, _remaining.Position));
    // replace the _remainder buffer, so that we can leave the 
    // original one alone. Necessary because some consumer splice
    // the buffer and only consume it until later, causing
    // a race condition if we compact it too soon.
    ByteBuffer newRemainding = AllocateBuffer();
    ByteBuffer temp = _remaining;
    _remaining = newRemainding;
    temp.Put(input);
    temp.Flip();
    try
    {
       DecodeAll(temp, output);
    } finally
    {
       if ( temp.Remaining > 0 )
          _remaining.Put(temp);
    }
 }
示例#55
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 beginPos = input.Position;
            Int32 limit = input.Limit;
            for (Int32 i = beginPos; i < limit; i++)
            {
                Byte b = input.Get(i);
                if (!CanSkip(b))
                {
                    input.Position = i;
                    Int32 answer = _skippedBytes;
                    _skippedBytes = 0;
                    return FinishDecode(answer);
                }

                _skippedBytes++;
            }

            input.Position = limit;
            return this;
        }
示例#56
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            while (input.HasRemaining)
            {
                switch (_counter)
                {
                    case 0:
                        _firstByte = input.Get() & 0xff;
                        break;
                    case 1:
                        _secondByte = input.Get() & 0xff;
                        break;
                    case 2:
                        _thirdByte = input.Get() & 0xff;
                        break;
                    case 3:
                        _counter = 0;
                        return FinishDecode((_firstByte << 24) | (_secondByte << 16) | (_thirdByte << 8) | (input.Get() & 0xff), output);
                }

                _counter++;
            }
            return this;
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 beginPos = input.Position;
            Int32 limit = input.Limit;
            Int32 terminatorPos = -1;

            for (Int32 i = beginPos; i < limit; i++)
            {
                Byte b = input.Get(i);
                if (b == CR)
                {
                    _lastIsCR = true;
                }
                else
                {
                    if (b == LF && _lastIsCR)
                    {
                        terminatorPos = i;
                        break;
                    }
                    _lastIsCR = false;
                }
            }

            if (terminatorPos >= 0)
            {
                IoBuffer product;

                Int32 endPos = terminatorPos - 1;

                if (beginPos < endPos)
                {
                    input.Limit = endPos;

                    if (_buffer == null)
                    {
                        product = input.Slice();
                    }
                    else
                    {
                        _buffer.Put(input);
                        product = _buffer.Flip();
                        _buffer = null;
                    }

                    input.Limit = limit;
                }
                else
                {
                    // When input contained only CR or LF rather than actual data...
                    if (_buffer == null)
                    {
                        product = IoBuffer.Allocate(0);
                    }
                    else
                    {
                        product = _buffer.Flip();
                        _buffer = null;
                    }
                }
                input.Position = terminatorPos + 1;
                return FinishDecode(product, output);
            }

            input.Position = beginPos;

            if (_buffer == null)
            {
                _buffer = IoBuffer.Allocate(input.Remaining);
                _buffer.AutoExpand = true;
            }

            _buffer.Put(input);

            if (_lastIsCR)
            {
                _buffer.Position = _buffer.Position - 1;
            }

            return this;
        }
 protected abstract IDecodingState FinishDecode(IoBuffer product, IProtocolDecoderOutput output);
示例#59
0
 public void FinishDecode(IoSession session, IProtocolDecoderOutput output)
 {
 }
 protected abstract IDecodingState FinishDecode(Boolean foundCRLF, IProtocolDecoderOutput output);