public void ReadDisconnectPayloadTest()
        {
            var buffer = ByteBufferAllocator.NewBuffer(DataSerializer.Endian);

            var detailMessage = "Invalid CommandType";
            var srcDetail     = Encoding.UTF8.GetBytes(detailMessage);

            buffer.WriteInt((int)DisconnectReason.InvalidDataFormat);
            buffer.WriteInt(srcDetail.Length);
            buffer.WriteBytes(srcDetail);

            byte[] payload = buffer.ToArray();
            int    offset  = 0;

            DisconnectReason disconnectType = (DisconnectReason)ByteRead.GetInt(payload, ref offset);

            int detailLength = ByteRead.GetInt(payload, ref offset);

            byte[] dstDetail = new byte[detailLength];
            ByteRead.ReadBytes(payload, dstDetail, offset, 0, detailLength);

            Assert.AreEqual(DisconnectReason.InvalidDataFormat, disconnectType);
            Assert.AreEqual(srcDetail.Length, detailLength);
            Assert.AreEqual(detailMessage, Encoding.UTF8.GetString(dstDetail));
        }
Пример #2
0
        public void DataObjectSerializeTest()
        {
            DataObject dataObject = new DataObject();

            dataObject.SetByte(0, 119);
            var buffer = ByteBufferAllocator.NewBuffer(Endian.BigEndian);

            DataSerializer.Write(buffer, dataObject);
            _output.WriteLine(TestUtil.ToString(buffer.ToArray()));
        }
        public void GetTest()
        {
            var bigEndianBuffer = ByteBufferAllocator.NewBuffer(Endian.BigEndian);

            bigEndianBuffer.WriteInt(10);

            var littleEndianBuffer = ByteBufferAllocator.NewReadOnlyBuffer(Endian.LittleEndian, bigEndianBuffer.ToArray());

            Assert.AreEqual(10.Reverse(), littleEndianBuffer.ReadInt());
        }
        public void WritableBufferTest()
        {
            var buffer = ByteBufferAllocator.NewBuffer(Endian.BigEndian, new byte[1] {
                1
            });

            buffer.WriteByte(2);

            Assert.AreEqual(1, buffer.ReadByte());
            Assert.AreEqual(2, buffer.ReadByte());
        }
        public void ReadStringTest()
        {
            var buffer = ByteBufferAllocator.NewBuffer(DataSerializer.Endian);

            var detailMessage = "Hello,World!!!";
            var detailBytes   = Encoding.UTF8.GetBytes(detailMessage);

            buffer.WriteInt(detailBytes.Length);
            buffer.WriteBytes(detailBytes);

            int offset = 0;
            var result = ByteRead.GetString <int>(buffer.ToArray(), ref offset, Encoding.UTF8);

            Assert.AreEqual(detailMessage, result);
            Assert.AreEqual(sizeof(int) + detailMessage.Length, offset);
        }
        private void EndianTest(Endian endian)
        {
            var buffer = ByteBufferAllocator.NewBuffer(endian);

            buffer.WriteBool(true);
            buffer.WriteByte(byte.MaxValue);
            buffer.WriteShort(short.MaxValue);
            buffer.WriteInt(int.MaxValue);
            buffer.WriteLong(long.MaxValue);
            buffer.WriteFloat(float.MaxValue);
            buffer.WriteDouble(double.MaxValue);

            Assert.AreEqual(true, buffer.ReadBool());
            Assert.AreEqual(byte.MaxValue, buffer.ReadByte());
            Assert.AreEqual(short.MaxValue, buffer.ReadShort());
            Assert.AreEqual(int.MaxValue, buffer.ReadInt());
            Assert.AreEqual(long.MaxValue, buffer.ReadLong());
            Assert.AreEqual(float.MaxValue, buffer.ReadFloat());
            Assert.AreEqual(double.MaxValue, buffer.ReadDouble());
        }
 public static IByteBuffer NewBuffer(int capacity)
 {
     return(ByteBufferAllocator.NewBuffer(DataSerializer.Endian, capacity));
 }
 public static IByteBuffer NewBuffer(byte[] data)
 {
     return(ByteBufferAllocator.NewBuffer(DataSerializer.Endian, data));
 }
 public static IByteBuffer NewBuffer()
 {
     return(ByteBufferAllocator.NewBuffer(DataSerializer.Endian));
 }