예제 #1
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);
        }
예제 #2
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;
        }
        /// <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;
        }
예제 #4
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);
        }
예제 #5
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)
        {
            if (input.PrefixedDataAvailable(PrefixLength, MaxDataLength))
            {
                String msg = input.GetPrefixedString(PrefixLength, Encoding);
                output.Write(msg);
                return true;
            }

            return false;
        }
예제 #7
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);
        }
            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);
            }
예제 #9
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;
     }
 }
예제 #10
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);
        }
예제 #11
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);
            }
        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);
        }
예제 #13
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;
 }
예제 #14
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;
     }
 }
예제 #15
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);
        }
예제 #16
0
        private void DecodeVoltronStylePackets(IoBuffer buffer, ref uint payloadSize, IProtocolDecoderOutput output, Func <ushort, Type> typeResolver)
        {
            while (payloadSize > 0)
            {
                /** Voltron packet **/
                buffer.Order = ByteOrder.BigEndian;
                ushort type             = buffer.GetUInt16();
                uint   innerPayloadSize = buffer.GetUInt32() - 6;

                byte[] data = new byte[(int)innerPayloadSize];
                buffer.Get(data, 0, (int)innerPayloadSize);

                var packetClass = typeResolver(type);
                if (packetClass != null)
                {
                    IoBufferDeserializable packet = (IoBufferDeserializable)Activator.CreateInstance(packetClass);
                    packet.Deserialize(IoBuffer.Wrap(data), Context);
                    output.Write(packet);
                }

                payloadSize -= innerPayloadSize + 6;
            }
        }
예제 #17
0
        protected override bool DoDecode(IoSession session, IoBuffer buffer, IProtocolDecoderOutput output)
        {
            if (buffer.Remaining < 8)
            {
                return(false);
            }

            /**
             * We expect aries, voltron or electron packets
             */
            var startPosition = buffer.Position;

            buffer.Order = ByteOrder.LittleEndian;
            uint packetType  = buffer.GetUInt32(); //currently unused
            uint payloadSize = buffer.GetUInt32();

            if (buffer.Remaining < payloadSize)
            {
                /** Not all here yet **/
                buffer.Position = startPosition;
                return(false);
            }

            var type = (VMNetMessageType)buffer.Get();
            var data = new List <byte>();

            for (int i = 0; i < payloadSize - 1; i++)
            {
                data.Add(buffer.Get());
            }
            var packet = new VMNetMessage(type, data.ToArray());

            output.Write(packet);

            return(true);
        }
예제 #18
0
 /// <summary>
 /// By default, this method propagates the decoded line of text to <see cref="IProtocolDecoderOutput"/>.
 /// You may override this method to modify the default behavior.
 /// </summary>
 protected virtual void WriteText(IoSession session, String text, IProtocolDecoderOutput output)
 {
     output.Write(text);
 }
예제 #19
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;
 }
예제 #20
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;
 }
예제 #21
0
 public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     output.Write(input);
     return(MessageDecoderResult.OK);
 }
예제 #22
0
        protected override bool DoDecode(IoSession session, IoBuffer buffer, IProtocolDecoderOutput output)
        {
            if (buffer.Remaining < 12)
            {
                return(false);
            }

            /**
             * We expect aries, voltron or electron packets
             */
            var startPosition = buffer.Position;

            buffer.Order = ByteOrder.LittleEndian;
            uint packetType  = buffer.GetUInt32();
            uint timestamp   = buffer.GetUInt32();
            uint payloadSize = buffer.GetUInt32();

            if (buffer.Remaining < payloadSize)
            {
                /** Not all here yet **/
                buffer.Position = startPosition;
                return(false);
            }

            //LOG.Info("[ARIES] " + packetType + " (" + payloadSize + ")");

            if (packetType == AriesPacketType.Voltron.GetPacketCode())
            {
                DecodeVoltronStylePackets(buffer, ref payloadSize, output, VoltronPackets.GetByPacketCode);
            }
            else if (packetType == AriesPacketType.Electron.GetPacketCode())
            {
                DecodeVoltronStylePackets(buffer, ref payloadSize, output, ElectronPackets.GetByPacketCode);
            }
            else if (packetType == AriesPacketType.Gluon.GetPacketCode())
            {
                DecodeVoltronStylePackets(buffer, ref payloadSize, output, GluonPackets.GetByPacketCode);
            }
            else
            {
                //Aries
                var packetClass = AriesPackets.GetByPacketCode(packetType);
                if (packetClass != null)
                {
                    byte[] data = new byte[(int)payloadSize];
                    buffer.Get(data, 0, (int)payloadSize);

                    IAriesPacket packet = (IAriesPacket)Activator.CreateInstance(packetClass);
                    packet.Deserialize(IoBuffer.Wrap(data), Context);
                    output.Write(packet);

                    payloadSize = 0;
                }
                else
                {
                    buffer.Skip((int)payloadSize);
                    payloadSize = 0;
                }
            }

            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;
            }
 public MessageDecoderResult Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     output.Write(_decodeMessage);
     return MessageDecoderResult.OK;
 }
        public MessageDecoderResult Decode(ByteBuffer input, IProtocolDecoderOutput output)
        {
            output.Write(CreateAndPopulateFrame(input));

            return(MessageDecoderResult.OK);
        }
예제 #26
0
 protected virtual void WriteText(IoSession session, String text, IProtocolDecoderOutput output)
 {
     output.Write(text);
 }
예제 #27
0
        public MessageDecoderResult Decode(ByteBuffer input, IProtocolDecoderOutput output)
        {

            output.Write(CreateAndPopulateFrame(input));

            return MessageDecoderResult.OK;
        }