protected override void ToBytes0(IoBuffer buf)
 {
     HermesPrimitiveCodec codec = new HermesPrimitiveCodec(buf);
     codec.WriteString(Topic);
     codec.WriteInt(Partition);
     codec.WriteString(GroupId);
 }
        public PartialDecodedMessage DecodePartial(IoBuffer buf)
        {
            HermesPrimitiveCodec codec = new HermesPrimitiveCodec(buf);

            // skip whole length
            codec.ReadInt();
            // skip header length
            int headerLen = codec.ReadInt();
            // skip body length
            int bodyLen = codec.ReadInt();
            verifyChecksum(buf, headerLen + bodyLen);
            PartialDecodedMessage msg = new PartialDecodedMessage();
            msg.Key = codec.ReadString();
            msg.BornTime = codec.ReadLong();
            msg.RemainingRetries = codec.ReadInt();
            msg.BodyCodecType = codec.ReadString();

            int len = codec.ReadInt();
            msg.DurableProperties = buf.GetSlice(len);

            len = codec.ReadInt();
            msg.VolatileProperties = buf.GetSlice(len);

            msg.Body = buf.GetSlice(bodyLen);

            // skip crc
            codec.ReadLong();

            return msg;
        }
 protected override void ToBytes0(IoBuffer buf)
 {
     HermesPrimitiveCodec codec = new HermesPrimitiveCodec(buf);
     writeMsgSeqMap(codec, m_ackMsgSeqs);
     writeMsgSeqMap(codec, m_nackMsgSeqs);
     codec.WriteInt(Type);
 }
 public void Decode(IoSession session, IoBuffer input, IProtocolDecoderOutput output)
 {
     lock (_decoder)
     {
         _decoder.Decode(session, input, output);
     }
 }
 protected override void BeginSend(IWriteRequest request, IoBuffer buf)
 {
     EndPoint destination = request.Destination;
     if (destination == null)
         destination = this.RemoteEndPoint;
     BeginSend(buf, destination);
 }
        private void Write(IoSession session, IoBuffer data, IoBuffer buf)
        {
            try
            {
                Int32 len = data.Remaining;
                if (len >= buf.Capacity)
                {
                    /*
                     * If the request length exceeds the size of the output buffer,
                     * flush the output buffer and then write the data directly.
                     */
                    INextFilter nextFilter = session.FilterChain.GetNextFilter(this);
                    InternalFlush(nextFilter, session, buf);
                    nextFilter.FilterWrite(session, new DefaultWriteRequest(data));
                    return;
                }
                if (len > (buf.Limit - buf.Position))
                {
                    InternalFlush(session.FilterChain.GetNextFilter(this), session, buf);
                }

                lock (buf)
                {
                    buf.Put(data);
                }
            }
            catch (Exception e)
            {
                session.FilterChain.FireExceptionCaught(e);
            }
        }
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.HasRemaining)
                return FinishDecode(input.Get(), output);

            return this;
        }
        public static String GetHexdump(IoBuffer buf, Int32 lengthLimit)
        {
            if (lengthLimit <= 0)
                throw new ArgumentException("lengthLimit: " + lengthLimit + " (expected: 1+)");
            Boolean truncate = buf.Remaining > lengthLimit;
            Int32 size = truncate ? lengthLimit : buf.Remaining;

            if (size == 0)
                return "empty";

            StringBuilder sb = new StringBuilder(size * 3 + 3);
            Int32 oldPos = buf.Position;

            // fill the first
            Int32 byteValue = buf.Get() & 0xFF;
            sb.Append((char)highDigits[byteValue]);
            sb.Append((char)lowDigits[byteValue]);
            size--;

            // and the others, too
            for (; size > 0; size--)
            {
                sb.Append(' ');
                byteValue = buf.Get() & 0xFF;
                sb.Append((char)highDigits[byteValue]);
                sb.Append((char)lowDigits[byteValue]);
            }

            buf.Position = oldPos;

            if (truncate)
                sb.Append("...");

            return sb.ToString();
        }
        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);
                }
            }
        }
 public IoSessionStream()
 {
     _syncRoot = new Byte[0];
     _buf = IoBuffer.Allocate(16);
     _buf.AutoExpand = true;
     _buf.Limit = 0;
 }
 protected override void Parse0(IoBuffer buf)
 {
     HermesPrimitiveCodec codec = new HermesPrimitiveCodec(buf);
     m_ackMsgSeqs = readMsgSeqMap(codec);
     m_nackMsgSeqs = readMsgSeqMap(codec);
     Type = codec.ReadInt();
 }
        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;
        }
 protected IoBufferWrapper(IoBuffer buf)
     : base(-1, 0, 0, 0)
 {
     if (buf == null)
         throw new ArgumentNullException("buf");
     _buf = buf;
 }
 protected override void Parse0(IoBuffer buf)
 {
     HermesPrimitiveCodec codec = new HermesPrimitiveCodec(buf);
     Topic = codec.ReadString();
     Partition = codec.ReadInt();
     GroupId = codec.ReadString();
 }
Beispiel #15
0
 public static void ReadAndCheckMagic(IoBuffer buf)
 {
     var magic = new byte[Magic.Length];
     buf.Get(magic, 0, magic.Length);
     if (!magic.SequenceEqual(MAGIC))
         throw new ArgumentException("Magic number mismatch");
 }
		private MessageCodecVersion GetVersion (IoBuffer buf)
		{
			var versionByte = buf.Get ();
			var version = MessageCodecVersion.ValueOf (versionByte);
			if (version == null)
				throw new ArgumentException (string.Format ("Unknown version {0}", versionByte));
			return version;
		}
 protected override void Parse0(IoBuffer buf)
 {
     Buf = buf;
     var codec = new HermesPrimitiveCodec(buf);
     msgCounter.WriteFullFence(codec.ReadInt());
     Topic = codec.ReadString();
     Partition = codec.ReadInt();
     
 }
        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;
        }
        private void readBatchDatas(IoBuffer buf, HermesPrimitiveCodec codec, List<TppConsumerMessageBatch> batches)
        {
            foreach (TppConsumerMessageBatch batch in batches)
            {
                int len = codec.ReadInt();
                batch.Data = buf.GetSlice(len);
            }

        }
        private byte[] ReadByteBuf(IoBuffer buf)
        {
            if (buf == null)
            {
                return null;
            }

            return buf.GetRemainingArray();
        }
        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);
        }
        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;
        }
 public Int32 Read(IoBuffer buffer)
 {
     using (FileStream fs = _file.OpenRead())
     {
         fs.Position = _position;
         Byte[] bytes = new Byte[buffer.Remaining];
         Int32 read = fs.Read(bytes, 0, bytes.Length);
         buffer.Put(bytes, 0, read);
         Update(read);
         return read;
     }
 }
        protected override void  ToBytes0(IoBuffer buf)
        {
            var codec = new HermesPrimitiveCodec(buf);

            codec.WriteInt(PullType);
            codec.WriteString(Topic);
            codec.WriteInt(Partition);
            codec.WriteString(GroupId);
            codec.WriteOffset(Offset);
            codec.WriteInt(Size);
            codec.WriteLong(ExpireTime);
        }
        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;
        }
        protected override void Parse0(IoBuffer buf)
        {
            var codec = new HermesPrimitiveCodec(buf);
            List<TppConsumerMessageBatch> batches = new List<TppConsumerMessageBatch>();

            readBatchMetas(codec, batches);

            readBatchDatas(buf, codec, batches);

            Batches = batches;

            Offset = codec.ReadOffset();
        }
        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);
 }
        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;
        }
        protected override void BeginSend(IWriteRequest request, IoBuffer buf)
        {
            _writeBuffer.Clear();

            SocketAsyncEventArgs saea;
            SocketAsyncEventArgsBuffer saeaBuf = buf as SocketAsyncEventArgsBuffer;
            if (saeaBuf == null)
            {
                if (_writeBuffer.Remaining < buf.Remaining)
                {
                    Int32 oldLimit = buf.Limit;
                    buf.Limit = buf.Position + _writeBuffer.Remaining;
                    _writeBuffer.Put(buf);
                    buf.Limit = oldLimit;
                }
                else
                {
                    _writeBuffer.Put(buf);
                }
                _writeBuffer.Flip();
                saea = _writeBuffer.SocketAsyncEventArgs;
                saea.SetBuffer(saea.Offset + _writeBuffer.Position, _writeBuffer.Limit);
            }
            else
            {
                saea = saeaBuf.SocketAsyncEventArgs;
                saea.Completed += _completeHandler;
            }

            Boolean willRaiseEvent;
            try
            {
                willRaiseEvent = Socket.SendAsync(saea);
            }
            catch (ObjectDisposedException)
            {
                // do nothing
                return;
            }
            catch (Exception ex)
            {
                EndSend(ex);
                return;
            }
            if (!willRaiseEvent)
            {
                ProcessSend(saea);
            }
        }