Beispiel #1
0
        public void Basics()
        {
            var buffer = Unpooled.Buffer(1024);
            var writer = new ByteBufferWriter(buffer);

            writer.WriteByte(1);
            writer.WriteShort(12);
            writer.WriteShortLE(21);

            writer.WriteMedium(13);
            writer.WriteMediumLE(31);

            writer.WriteInt(14);
            writer.WriteIntLE(41);
            writer.WriteLong(15);
            writer.WriteLongLE(51);

            writer.WriteDecimal(168.86m);
            writer.WriteDecimalLE(188.88m);

            writer.Flush();

            var reader = new ByteBufferReader(buffer);

            reader.TryRead(out byte b);
            Assert.Equal(1, b);
            reader.TryReadShort(out var sV);
            Assert.Equal(12, sV);
            reader.TryReadShortLE(out sV);
            Assert.Equal(21, sV);

            reader.TryReadMedium(out var iV);
            Assert.Equal(13, iV);
            reader.TryReadMediumLE(out iV);
            Assert.Equal(31, iV);

            reader.TryReadInt(out iV);
            Assert.Equal(14, iV);
            reader.TryReadIntLE(out iV);
            Assert.Equal(41, iV);
            reader.TryReadLong(out var lV);
            Assert.Equal(15, lV);
            reader.TryReadLongLE(out lV);
            Assert.Equal(51, lV);

            reader.TryReadDecimal(out var mV);
            Assert.Equal(168.86m, mV);
            reader.TryReadDecimalLE(out mV);
            Assert.Equal(188.88m, mV);
        }
Beispiel #2
0
        private void writeDataByteBuffer(ByteBufferWriter writer, int index, object data)
        {
            Assert.IsNotNull(writer);
            Assert.IsNotNull(data);
            writer.WriteVarint32((uint)index);
            switch (index)
            {
            case 0:
                writer.WriteBool((bool)data);
                break;

            case 1:
                writer.WriteByte((byte)data);
                break;

            case 2:
                writer.WriteUint16((ushort)data);
                break;

            case 3:
                writer.WriteUint32((uint)data);
                break;

            case 4:
                writer.WriteUint64((ulong)data);
                break;

            case 5:
                writer.WriteInt16((short)data);
                break;

            case 6:
                writer.WriteInt32((int)data);
                break;

            case 7:
                writer.WriteInt64((long)data);
                break;

            case 8:
                writer.WriteVarint32((uint)data);
                break;

            case 9:
                writer.WriteVarint64((ulong)data);
                break;

            case 10:
                writer.WriteSVarint32((int)data);
                break;

            case 11:
                writer.WriteSVarint64((long)data);
                break;

            case 12:
                writer.WriteFloat((float)data);
                break;

            case 13:
                writer.WriteDouble((double)data);
                break;

            case 14:
                writer.WriteString((string)data);
                break;

            case 15: {
                var buffer = (byte[])data;
                writer.WriteVarint32((uint)buffer.Length);
                writer.WriteBuffer(buffer, 0, buffer.Length);
            }
            break;

            case 16: {
                writer.WriteVarint32((uint)(int)data);
                writer.Skip((int)data);
            }
            break;

            default:
                Assert.Fail();
                break;
            }
        }