public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
        {
            if ((MessageType)input.Get() != MessageType.Update_ZipFiles)
            {
                return MessageDecoderResult.NotOK;
            }

            var zipFileInfo = JsonConvert.DeserializeObject<TransferingZipFileInfo>(input.GetString(Encoding.UTF8));
            var fileSize = zipFileInfo.FileSize;
            var hashBytes = zipFileInfo.HashBytes;
            if (input.Remaining < fileSize)
            {
                return MessageDecoderResult.NeedData;
            }

            var filesBytes = new byte[fileSize];
            input.Get(filesBytes, 0, (int)fileSize);

            if (FileHashHelper.CompareHashValue(FileHashHelper.ComputeFileHash(filesBytes), hashBytes))
            {
                _zipFileInfoMessage = new TransferingZipFile(filesBytes);
                return MessageDecoderResult.OK;
            }
            return MessageDecoderResult.NotOK;
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output)
        {
            if (input.HasRemaining)
                return FinishDecode(input.Get(), output);

            return this;
        }
Ejemplo n.º 4
0
 private byte ComputeChecksum(IoBuffer buffer)
 {
     buffer.Rewind();
     byte checksum = 0;
     for (int i = 0; i < Length - 1; i++) checksum += buffer.Get();
     return checksum;
 }
 public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
 {
     if ((MessageType)input.Get() != MessageType.Update_UpdateInfo)
     {
         return MessageDecoderResult.NotOK;
     }
     _appUdateInfo = (IClientInfo)JsonConvert.DeserializeObject(input.GetString(Encoding.UTF8));
     return MessageDecoderResult.OK;
 }
        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;
        }
Ejemplo n.º 7
0
        public static void ReadLocator(IoBuffer buffer, ref Locator obj)
        {
            if (obj == null)
                obj = new Locator();
            obj.Kind = (LocatorKind)buffer.GetInt32();
            obj.Port = (int)buffer.GetInt32(); ;
            byte[] tmp = new byte[16];

            buffer.Get(tmp, 0, 16);
            obj.SocketAddressBytes = tmp;
        }
 public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
 {
     var type=(MessageType)input.Get();
     if (type==MessageType.Update_FileHash)
     {
         var message = input.GetString(Encoding.UTF8);
         _decodeMessage=JsonConvert.DeserializeObject<IList<IFileHash>>(message);
         return MessageDecoderResult.OK;
     }
     return MessageDecoderResult.NotOK;
 }
Ejemplo n.º 9
0
        public void TestGetSlice()
        {
            IoBuffer buf = IoBuffer.Allocate(36);

            for (Byte i = 0; i < 36; i++)
            {
                buf.Put(i);
            }

            IoBuffer res = buf.GetSlice(1, 3);

            // The limit should be 3, the pos should be 0 and the bytes read
            // should be 0x01, 0x02 and 0x03
            Assert.AreEqual(0, res.Position);
            Assert.AreEqual(3, res.Limit);
            Assert.AreEqual(0x01, res.Get());
            Assert.AreEqual(0x02, res.Get());
            Assert.AreEqual(0x03, res.Get());

            // Now test after a flip
            buf.Flip();

            res = buf.GetSlice(1, 3);
            // The limit should be 3, the pos should be 0 and the bytes read
            // should be 0x01, 0x02 and 0x03
            Assert.AreEqual(0, res.Position);
            Assert.AreEqual(3, res.Limit);
            Assert.AreEqual(0x01, res.Get());
            Assert.AreEqual(0x02, res.Get());
            Assert.AreEqual(0x03, res.Get());
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Writes an <see cref="IoBuffer"/>. Override this method for better implementation.
        /// </summary>
        protected virtual void PutInternal(IoBuffer src)
        {
            Int32 n = src.Remaining;

            if (n > Remaining)
            {
                throw new OverflowException();
            }
            for (Int32 i = 0; i < n; i++)
            {
                Put(src.Get());
            }
        }
Ejemplo n.º 11
0
        public void TestFillByteSize()
        {
            int      length = 1024 * 1020;
            IoBuffer buffer = IoBuffer.Allocate(length);

            buffer.Fill((byte)0x80, length);

            buffer.Flip();
            for (int i = 0; i < length; i++)
            {
                Assert.AreEqual((byte)0x80, buffer.Get());
            }
        }
Ejemplo n.º 12
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;
        }
Ejemplo n.º 13
0
 public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
 {
     if (input.Remaining < MIN_PACKET_LENGTH)
         return MessageDecoderResult.NeedData;
     var symbol = input.GetArray(2);
     if (DataPacket.True(symbol))
     {
         input.Skip(1);
         var len = input.Get();
         input.Rewind();
         if (len > input.Remaining) return MessageDecoderResult.NeedData;
     }
     else if (!CtlPacket.True(symbol)) return MessageDecoderResult.NotOK;
     return MessageDecoderResult.OK;
 }
Ejemplo n.º 14
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;
        }
Ejemplo n.º 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;
 }
Ejemplo n.º 16
0
        public MessageDecoderResult Decodable(IoSession session, IoBuffer input)
        {
            if (input.Remaining < MESSAGE_LENGTH_BYTES_LENGTH)
                return MessageDecoderResult.NeedData;
            var len = input.GetInt32();
            if (len > MAX_MESSAGE_LENGTH)
                return MessageDecoderResult.NotOK;
            if (input.Remaining + MESSAGE_LENGTH_BYTES_LENGTH < len)
                return MessageDecoderResult.NeedData;
            var version = input.Get().ToEnum<MessageVersion>();
            if (version == MessageVersion.BadVersion)
                return MessageDecoderResult.NotOK;
            if (len < FixedHeaderLength(version))
                return MessageDecoderResult.NotOK;

            return MessageDecoderResult.OK;
        }
Ejemplo n.º 17
0
        static ulong ReadVarint64(IoBuffer buffer)
        {
            long result = 0;
            int offset = 0;

            for (; offset < 64; offset += 7)
            {
                int b = buffer.Get();
                if (b == -1)
                    throw new BufferUnderflowException();

                result |= ((long)(b & 0x7f)) << offset;

                if ((b & 0x80) == 0)
                    return (ulong)result;
            }

            throw new InvalidDataException();
        }
Ejemplo n.º 18
0
        public void TestGetUnsigned()
        {
            IoBuffer buf = IoBuffer.Allocate(16);

            buf.Put((byte)0xA4);
            buf.Put((byte)0xD0);
            buf.Put((byte)0xB3);
            buf.Put((byte)0xCD);
            buf.Flip();

            buf.Order = ByteOrder.LittleEndian;

            buf.Mark();
            Assert.AreEqual(0xA4, buf.Get());
            buf.Reset();
            Assert.AreEqual(0xD0A4, (UInt16)buf.GetInt16());
            buf.Reset();
            Assert.AreEqual(0xCDB3D0A4L, (UInt32)buf.GetInt32());
        }
Ejemplo n.º 19
0
        static uint ReadVarint32(IoBuffer buffer)
        {
            int result = 0;
            int offset = 0;

            for (; offset < 32; offset += 7)
            {
                int b = buffer.Get();
                if (b == -1)
                    throw new BufferUnderflowException();

                result |= (b & 0x7f) << offset;

                if ((b & 0x80) == 0)
                    return (uint)result;
            }

            throw new InvalidDataException();
        }
Ejemplo n.º 20
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;
        }
Ejemplo n.º 21
0
        public void TestAutoShrink()
        {
            IoBuffer buf = ByteBufferAllocator.Instance.Allocate(8);

            buf.AutoShrink = true;

            // Make sure the buffer doesn't shrink too much (less than the initial
            // capacity.)
            buf.Sweep((byte)1);
            buf.Fill(7);
            buf.Compact();
            Assert.AreEqual(8, buf.Capacity);
            Assert.AreEqual(1, buf.Position);
            Assert.AreEqual(8, buf.Limit);
            buf.Clear();
            Assert.AreEqual(1, buf.Get());

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer shrinks when only 1/4 is being used.
            buf.Sweep((byte)1);
            buf.Fill(24);
            buf.Compact();
            Assert.AreEqual(16, buf.Capacity);
            Assert.AreEqual(8, buf.Position);
            Assert.AreEqual(16, buf.Limit);
            buf.Clear();
            for (int i = 0; i < 8; i++)
            {
                Assert.AreEqual(1, buf.Get());
            }

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer shrinks when only 1/8 is being used.
            buf.Sweep((byte)1);
            buf.Fill(28);
            buf.Compact();
            Assert.AreEqual(8, buf.Capacity);
            Assert.AreEqual(4, buf.Position);
            Assert.AreEqual(8, buf.Limit);
            buf.Clear();
            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(1, buf.Get());
            }

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer shrinks when 0 byte is being used.
            buf.Fill(32);
            buf.Compact();
            Assert.AreEqual(8, buf.Capacity);
            Assert.AreEqual(0, buf.Position);
            Assert.AreEqual(8, buf.Limit);

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer doesn't shrink when more than 1/4 is being used.
            buf.Sweep((byte)1);
            buf.Fill(23);
            buf.Compact();
            Assert.AreEqual(32, buf.Capacity);
            Assert.AreEqual(9, buf.Position);
            Assert.AreEqual(32, buf.Limit);
            buf.Clear();
            for (int i = 0; i < 9; i++)
            {
                Assert.AreEqual(1, buf.Get());
            }
        }
Ejemplo n.º 22
0
        private void ManipulateIoBuffer(IoSession session, IoBuffer buffer)
        {
            if ((buffer.Remaining > 0) && (_removeByteProbability > _rng.Next(1000)))
            {
                if (log.IsInfoEnabled)
                    log.Info(buffer.GetHexDump());

                // where to remove bytes ?
                int pos = _rng.Next(buffer.Remaining);
                // how many byte to remove ?
                int count = _rng.Next(buffer.Remaining - pos) + 1;
                if (count == buffer.Remaining)
                    count = buffer.Remaining - 1;

                IoBuffer newBuff = IoBuffer.Allocate(buffer.Remaining - count);
                for (int i = 0; i < pos; i++)
                    newBuff.Put(buffer.Get());

                buffer.Skip(count); // hole
                while (newBuff.Remaining > 0)
                    newBuff.Put(buffer.Get());
                newBuff.Flip();
                // copy the new buffer in the old one
                buffer.Rewind();
                buffer.Put(newBuff);
                buffer.Flip();

                if (log.IsInfoEnabled)
                {
                    log.Info("Removed " + count + " bytes at position " + pos + ".");
                    log.Info(buffer.GetHexDump());
                }
            }
            if ((buffer.Remaining > 0) && (_changeByteProbability > _rng.Next(1000)))
            {
                if (log.IsInfoEnabled)
                    log.Info(buffer.GetHexDump());

                // how many byte to change ?
                int count = _rng.Next(buffer.Remaining - 1) + 1;

                byte[] values = new byte[count];
                _rng.NextBytes(values);
                for (int i = 0; i < values.Length; i++)
                {
                    int pos = _rng.Next(buffer.Remaining);
                    buffer.Put(pos, values[i]);
                }

                if (log.IsInfoEnabled)
                {
                    log.Info("Modified " + count + " bytes.");
                    log.Info(buffer.GetHexDump());
                }
            }
        }
Ejemplo n.º 23
0
        private IoBuffer InsertBytesToNewIoBuffer(IoSession session, IoBuffer buffer)
        {
            if (_insertByteProbability > _rng.Next(1000))
            {
                if (log.IsInfoEnabled)
                    log.Info(buffer.GetHexDump());

                // where to insert bytes ?
                int pos = _rng.Next(buffer.Remaining) - 1;

                // how many byte to insert ?
                int count = _rng.Next(_maxInsertByte - 1) + 1;

                IoBuffer newBuff = IoBuffer.Allocate(buffer.Remaining + count);
                for (int i = 0; i < pos; i++)
                    newBuff.Put(buffer.Get());
                for (int i = 0; i < count; i++)
                {
                    newBuff.Put((byte)(_rng.Next(256)));
                }
                while (buffer.Remaining > 0)
                {
                    newBuff.Put(buffer.Get());
                }
                newBuff.Flip();

                if (log.IsInfoEnabled)
                {
                    log.Info("Inserted " + count + " bytes.");
                    log.Info(newBuff.GetHexDump());
                }
                return newBuff;
            }
            return null;
        }
Ejemplo n.º 24
0
 public static void ReadGuidPrefix(IoBuffer buffer, ref GuidPrefix obj)
 {
     buffer.Get(obj.Prefix, 0, GuidPrefix.GUID_PREFIX_SIZE);
 }
Ejemplo n.º 25
0
 public static void ReadPrimitive(IoBuffer buffer, out sbyte value)
 {
     value = (sbyte)buffer.Get();
 }
Ejemplo n.º 26
0
        public static void ReadPrimitive(IoBuffer buffer, out string value)
        {
            uint totalBytes;
            ReadPrimitive(buffer, out totalBytes);

            if (totalBytes == 0)
            {
                value = null;
                return;
            }
            else if (totalBytes == 1)
            {
                value = string.Empty;
                return;
            }

            totalBytes -= 1;

            uint totalChars;
            ReadPrimitive(buffer, out totalChars);

            var helper = s_stringHelper;
            if (helper == null)
                s_stringHelper = helper = new StringHelper();

            var decoder = helper.Decoder;
            var buf = helper.ByteBuffer;
            char[] chars;
            if (totalChars <= StringHelper.CHARBUFFERLEN)
                chars = helper.CharBuffer;
            else
                chars = new char[totalChars];

            int bufferBytesLeft = (int)totalBytes;

            int cp = 0;

            while (bufferBytesLeft > 0)
            {
                int bytesInBuffer = Math.Min(buf.Length, bufferBytesLeft);
                buffer.Get(buf, 0, bytesInBuffer);
                if (bytesInBuffer == 0)
                    throw new BufferUnderflowException();

                bufferBytesLeft -= bytesInBuffer;
                bool flush = bufferBytesLeft == 0 ? true : false;

                bool completed = false;

                int p = 0;

                while (completed == false)
                {
                    int charsConverted;
                    int bytesConverted;

                    decoder.Convert(buf, p, bytesInBuffer - p,
                        chars, cp, (int)totalChars - cp,
                        flush,
                        out bytesConverted, out charsConverted, out completed);

                    p += bytesConverted;
                    cp += charsConverted;
                }
            }

            value = new string(chars, 0, (int)totalChars);
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Writes an <see cref="IoBuffer"/>. Override this method for better implementation.
 /// </summary>
 protected virtual void PutInternal(IoBuffer src)
 {
     Int32 n = src.Remaining;
     if (n > Remaining)
         throw new OverflowException();
     for (Int32 i = 0; i < n; i++)
     {
         Put(src.Get());
     }
 }
        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;
        }
Ejemplo n.º 29
0
        public static void ReadPrimitive(IoBuffer buffer, out byte[] value)
        {
            uint len;
            ReadPrimitive(buffer, out len);

            if (len == 0)
            {
                value = null;
                return;
            }
            else if (len == 1)
            {
                value = s_emptyByteArray;
                return;
            }

            len -= 1;

            value = new byte[len];
            int l = 0;

            while (l < len)
            {
                int r = (int)len - l;
                buffer.Get(value, l, r);
                if (r == 0)
                    throw new BufferUnderflowException();
                l += r;
            }
        }
Ejemplo n.º 30
0
        public void TestPutString()
        {
            IoBuffer buf      = ByteBufferAllocator.Instance.Allocate(16);
            Encoding encoding = Encoding.GetEncoding("ISO-8859-1");

            buf.PutString("ABC", encoding);
            Assert.AreEqual(3, buf.Position);
            buf.Clear();
            Assert.AreEqual((Byte)'A', buf.Get(0));
            Assert.AreEqual((Byte)'B', buf.Get(1));
            Assert.AreEqual((Byte)'C', buf.Get(2));

            buf.PutString("D", 5, encoding);
            Assert.AreEqual(5, buf.Position);
            buf.Clear();
            Assert.AreEqual((Byte)'D', buf.Get(0));
            Assert.AreEqual(0, buf.Get(1));

            buf.PutString("EFG", 2, encoding);
            Assert.AreEqual(2, buf.Position);
            buf.Clear();
            Assert.AreEqual((Byte)'E', buf.Get(0));
            Assert.AreEqual((Byte)'F', buf.Get(1));
            Assert.AreEqual((Byte)'C', buf.Get(2)); // C may not be overwritten

            // UTF-16: We specify byte order to omit BOM.
            encoding = Encoding.GetEncoding("UTF-16BE");
            buf.Clear();

            buf.PutString("ABC", encoding);
            Assert.AreEqual(6, buf.Position);
            buf.Clear();

            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual((Byte)'A', buf.Get(1));
            Assert.AreEqual(0, buf.Get(2));
            Assert.AreEqual((Byte)'B', buf.Get(3));
            Assert.AreEqual(0, buf.Get(4));
            Assert.AreEqual((Byte)'C', buf.Get(5));

            buf.PutString("D", 10, encoding);
            Assert.AreEqual(10, buf.Position);
            buf.Clear();
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual((Byte)'D', buf.Get(1));
            Assert.AreEqual(0, buf.Get(2));
            Assert.AreEqual(0, buf.Get(3));

            buf.PutString("EFG", 4, encoding);
            Assert.AreEqual(4, buf.Position);
            buf.Clear();
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual((Byte)'E', buf.Get(1));
            Assert.AreEqual(0, buf.Get(2));
            Assert.AreEqual((Byte)'F', buf.Get(3));
            Assert.AreEqual(0, buf.Get(4));         // C may not be overwritten
            Assert.AreEqual((Byte)'C', buf.Get(5)); // C may not be overwritten

            // Test putting an emptry string
            buf.PutString("", encoding);
            Assert.AreEqual(0, buf.Position);
            buf.PutString("", 4, encoding);
            Assert.AreEqual(4, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(0, buf.Get(1));
        }
Ejemplo n.º 31
0
        public void TestPutPrefixedString()
        {
            Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
            IoBuffer buf      = IoBuffer.Allocate(16);

            buf.FillAndReset(buf.Remaining);

            // Without autoExpand
            buf.PutPrefixedString("ABC", encoding);
            Assert.AreEqual(5, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(3, buf.Get(1));
            Assert.AreEqual((byte)'A', buf.Get(2));
            Assert.AreEqual((byte)'B', buf.Get(3));
            Assert.AreEqual((byte)'C', buf.Get(4));

            buf.Clear();
            try
            {
                buf.PutPrefixedString("123456789012345", encoding);
                Assert.Fail();
            }
            catch (OverflowException)
            {
                // Expected an Exception, signifies test success
                Assert.IsTrue(true);
            }

            // With autoExpand
            buf.Clear();
            buf.AutoExpand = true;
            buf.PutPrefixedString("123456789012345", encoding);
            Assert.AreEqual(17, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(15, buf.Get(1));
            Assert.AreEqual((byte)'1', buf.Get(2));
            Assert.AreEqual((byte)'2', buf.Get(3));
            Assert.AreEqual((byte)'3', buf.Get(4));
            Assert.AreEqual((byte)'4', buf.Get(5));
            Assert.AreEqual((byte)'5', buf.Get(6));
            Assert.AreEqual((byte)'6', buf.Get(7));
            Assert.AreEqual((byte)'7', buf.Get(8));
            Assert.AreEqual((byte)'8', buf.Get(9));
            Assert.AreEqual((byte)'9', buf.Get(10));
            Assert.AreEqual((byte)'0', buf.Get(11));
            Assert.AreEqual((byte)'1', buf.Get(12));
            Assert.AreEqual((byte)'2', buf.Get(13));
            Assert.AreEqual((byte)'3', buf.Get(14));
            Assert.AreEqual((byte)'4', buf.Get(15));
            Assert.AreEqual((byte)'5', buf.Get(16));
        }
Ejemplo n.º 32
0
 public static void ReadPrimitive(IoBuffer buffer, out bool value)
 {
     var b = buffer.Get();
     value = b != 0;
 }
Ejemplo n.º 33
0
        private void DecodeAuto(Context ctx, IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 matchCount = ctx.MatchCount;

            // Try to find a match
            Int32 oldPos = input.Position, oldLimit = input.Limit;

            while (input.HasRemaining)
            {
                Byte b = input.Get();
                Boolean matched = false;
                
                switch (b)
                {
                    case 0x0d: // \r
                        // Might be Mac, but we don't auto-detect Mac EOL
                        // to avoid confusion.
                        matchCount++;
                        break;
                    case 0x0a: // \n
                        // UNIX
                        matchCount++;
                        matched = true;
                        break;
                    default:
                        matchCount = 0;
                        break;
                }

                if (matched)
                {
                    // Found a match.
                    Int32 pos = input.Position;
                    input.Limit = pos;
                    input.Position = oldPos;

                    ctx.Append(input);

                    input.Limit = oldLimit;
                    input.Position = pos;

                    if (ctx.OverflowPosition == 0)
                    {
                        IoBuffer buf = ctx.Buffer;
                        buf.Flip();
                        buf.Limit -= matchCount;
                        ArraySegment<Byte> bytes = buf.GetRemaining();
                        try
                        {
                            String str = _encoding.GetString(bytes.Array, bytes.Offset, bytes.Count);
                            WriteText(session, str, output);
                        }
                        finally
                        {
                            buf.Clear();
                        }
                    }
                    else
                    {
                        Int32 overflowPosition = ctx.OverflowPosition;
                        ctx.Reset();
                        throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
                    }

                    oldPos = pos;
                    matchCount = 0;
                }
            }

            // Put remainder to buf.
            input.Position = oldPos;
            ctx.Append(input);
            ctx.MatchCount = matchCount;
        }
Ejemplo n.º 34
0
        private void DecodeNormal(Context ctx, IoSession session, IoBuffer input, IProtocolDecoderOutput output)
        {
            Int32 matchCount = ctx.MatchCount;

            // Try to find a match
            Int32 oldPos = input.Position, oldLimit = input.Limit;

            while (input.HasRemaining)
            {
                Byte b = input.Get();

                if (_delimBuf[matchCount] == b)
                {
                    matchCount++;

                    if (matchCount == _delimBuf.Length)
                    {
                        // Found a match.
                        Int32 pos = input.Position;
                        input.Limit = pos;
                        input.Position = oldPos;

                        ctx.Append(input);

                        input.Limit = oldLimit;
                        input.Position = pos;

                        if (ctx.OverflowPosition == 0)
                        {
                            IoBuffer buf = ctx.Buffer;
                            buf.Flip();
                            buf.Limit -= matchCount;
                            ArraySegment<Byte> bytes = buf.GetRemaining();
                            try
                            {
                                String str = _encoding.GetString(bytes.Array, bytes.Offset, bytes.Count);
                                WriteText(session, str, output);
                            }
                            finally
                            {
                                buf.Clear();
                            }
                        }
                        else
                        {
                            Int32 overflowPosition = ctx.OverflowPosition;
                            ctx.Reset();
                            throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
                        }

                        oldPos = pos;
                        matchCount = 0;
                    }
                }
                else
                {
                    input.Position = Math.Max(0, input.Position - matchCount);
                    matchCount = 0;
                }
            }

            // Put remainder to buf.
            input.Position = oldPos;
            ctx.Append(input);
            ctx.MatchCount = matchCount;
        }
Ejemplo n.º 35
0
 public static void ReadProtocolId( IoBuffer buffer, ref ProtocolId obj)
 {
     if (obj == null)
         obj = new ProtocolId();
     buffer.Get(obj.Id, 0, ProtocolId.PROTOID_SIZE);
 }
Ejemplo n.º 36
0
 /// <inheritdoc/>
 public override Byte Get()
 {
     return(_buf.Get());
 }