Beispiel #1
0
        static string ReadString(ByteBuffer buffer, byte formatCode, byte code8, byte code32, string type)
        {
            if (formatCode == FormatCode.Null)
            {
                return(null);
            }

            int count;

            if (formatCode == code8)
            {
                count = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (formatCode == code32)
            {
                count = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                throw DecodeException(formatCode, buffer.Offset);
            }

            buffer.Validate(false, count);
            string value = new string(Encoding.UTF8.GetChars(buffer.Buffer, buffer.Offset, count));

            buffer.Complete(count);

            return(value);
        }
Beispiel #2
0
        public static Frame Decode(ByteBuffer buffer, bool fullBody = true)
        {
            Frame frame = new Frame();

            frame.RawBuffer = buffer.Array;

            // Header
            frame.size       = (int)AmqpBitConverter.ReadUInt(buffer);
            frame.dataOffset = AmqpBitConverter.ReadUByte(buffer);
            frame.Type       = (FrameType)AmqpBitConverter.ReadUByte(buffer);
            frame.Channel    = AmqpBitConverter.ReadUShort(buffer);
            // skip extended header
            buffer.Complete(frame.dataOffset * 4 - Frame.HeaderSize);

            // Command
            if (buffer.Length > 0)
            {
                frame.Command = (Performative)AmqpCodec.CreateAmqpDescribed(buffer);
                if (fullBody)
                {
                    frame.Command.DecodeValue(buffer);
                }
            }

            return(frame);
        }
Beispiel #3
0
        public static byte[] ReadBinary(ByteBuffer buffer, byte formatCode)
        {
            if (formatCode == FormatCode.Null)
            {
                return(null);
            }

            int count;

            if (formatCode == FormatCode.Binary8)
            {
                count = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (formatCode == FormatCode.Binary32)
            {
                count = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                throw DecodeException(formatCode, buffer.Offset);
            }

            buffer.Validate(false, count);
            byte[] value = new byte[count];
            Array.Copy(buffer.Buffer, buffer.Offset, value, 0, count);
            buffer.Complete(count);

            return(value);
        }
Beispiel #4
0
 private void DecodeHeader(ByteBuffer buffer)
 {
     this.Size       = (int)AmqpBitConverter.ReadUInt(buffer);
     this.DataOffset = AmqpBitConverter.ReadUByte(buffer);
     this.Type       = (FrameType)AmqpBitConverter.ReadUByte(buffer);
     this.Channel    = AmqpBitConverter.ReadUShort(buffer);
     buffer.Complete(this.DataOffset * 4 - 8);
 }
Beispiel #5
0
            bool HandleReadComplete(TransportAsyncCallbackArgs args)
            {
                bool      completed = true;
                Exception exception = null;

                if (args.Exception != null)
                {
                    exception = args.Exception;
                }
                else if (args.BytesTransfered == 0)
                {
                    exception = new ObjectDisposedException(this.transport.ToString());
                }
                else if (args.BytesTransfered < args.Count)
                {
                    args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered);
                    completed = false;
                }

                if (completed)
                {
                    if (exception != null || object.ReferenceEquals(args.CompletedCallback, onFrameComplete))
                    {
                        ByteBuffer buffer = null;
                        if (exception == null)
                        {
                            buffer = new ByteBuffer(args.Buffer, 0, args.Buffer.Length);
                            this.parent.OnReceiveBuffer(buffer);
                        }
                        else
                        {
                            this.parent.OnIoFault(exception);
                        }
                    }
                    else
                    {
                        // read size completed ok
                        uint size = AmqpBitConverter.ReadUInt(this.sizeBuffer, 0, this.sizeBuffer.Length);
                        if (size > this.maxFrameSize)
                        {
                            completed = true;
                            exception = new AmqpException(AmqpErrorCode.FramingError, CommonResources.GetString(CommonResources.InvalidFrameSize, size, this.maxFrameSize));
                            this.parent.OnIoFault(exception);
                        }
                        else
                        {
                            byte[] frameBuffer = new byte[size];
                            Buffer.BlockCopy(this.sizeBuffer, 0, frameBuffer, 0, this.sizeBuffer.Length);
                            args.SetBuffer(frameBuffer, this.sizeBuffer.Length, (int)size - this.sizeBuffer.Length);
                            args.CompletedCallback = onFrameComplete;
                            completed = false;
                        }
                    }
                }

                return(completed);
            }
Beispiel #6
0
            private void HandleFrameSizeReadComplete(TransportAsyncCallbackArgs args)
            {
                int num = (int)AmqpBitConverter.ReadUInt(this.frameSizeBuffer.Array, this.frameSizeBuffer.Offset, 4);

                if (num <= 0 || num > this.maxFrameSize)
                {
                    throw new AmqpException(AmqpError.FramingError, SRClient.InvalidFrameSize(num, this.maxFrameSize));
                }
                this.SetReadFrameBody(num);
            }
Beispiel #7
0
        void DecodeHeader(ByteBuffer buffer)
        {
            this.Size       = (int)AmqpBitConverter.ReadUInt(buffer);
            this.DataOffset = AmqpBitConverter.ReadUByte(buffer);
            this.Type       = (FrameType)AmqpBitConverter.ReadUByte(buffer);
            this.Channel    = AmqpBitConverter.ReadUShort(buffer);

            // skip extended header
            buffer.Complete(this.DataOffset * 4 - Frame.HeaderSize);
        }
Beispiel #8
0
 int ReadInt(bool smallEncoding)
 {
     if (smallEncoding)
     {
         return(this.stream.ReadByte());
     }
     else
     {
         ArraySegment <byte> buffer = this.ReadBytes(FixedWidth.UInt);
         return((int)AmqpBitConverter.ReadUInt(buffer.Array, buffer.Offset, FixedWidth.UInt));
     }
 }
Beispiel #9
0
        private void OnFrameLengthReceived(ByteBuffer buffer)
        {
            int frameSize = (int)AmqpBitConverter.ReadUInt(buffer);

            if (frameSize > MaxFrameSize)
            {
                throw new AmqpException(ErrorCode.InvalidField, $"Invalid frame size:{frameSize}, maximum frame size:{MaxFrameSize}");
            }
            buffer.ResetReadWrite();             // back to 0 to start reading
            buffer.AppendWrite(FixedWidth.UInt); // advance the write the 4 bytes we already received
            // receive the rest of the frame
            socket.ReceiveAsync((frameSize - FixedWidth.UInt), OnFrameReceived);
        }
Beispiel #10
0
            void HandleFrameSizeReadComplete(TransportAsyncCallbackArgs args)
            {
                Fx.Assert(args.Buffer == this.frameSizeBuffer.Array, "wrong buffer");
                int frameSize = (int)AmqpBitConverter.ReadUInt(this.frameSizeBuffer.Array, this.frameSizeBuffer.Offset, FixedWidth.UInt);

                Fx.Assert(frameSize > 0, "frameSize must be positive");
                if (frameSize <= 0 || frameSize > this.maxFrameSize)
                {
                    throw new AmqpException(AmqpErrorCode.FramingError, CommonResources.GetString(CommonResources.InvalidFrameSize, frameSize, this.maxFrameSize));
                }

                this.SetReadFrameBody(frameSize);
            }
 public void Decode(ByteBuffer buffer)
 {
     if (buffer.Length < this.EncodeSize)
     {
         throw AmqpEncoding.GetEncodingException(SRAmqp.AmqpInsufficientBufferSize(this.EncodeSize, buffer.Length));
     }
     if (AmqpBitConverter.ReadUInt(buffer) != 1095586128)
     {
         throw AmqpEncoding.GetEncodingException("ProtocolName");
     }
     this.protocolId = (Microsoft.ServiceBus.Messaging.Amqp.ProtocolId)AmqpBitConverter.ReadUByte(buffer);
     this.version    = new AmqpVersion(AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer));
 }
Beispiel #12
0
        public static AmqpFrame DecodeFrame(ByteBuffer buffer, out ushort channelNumber)
        {
#if DEBUG
            byte[] debugFrameBuffer = new byte[buffer.LengthAvailableToRead];
            Buffer.BlockCopy(buffer.Buffer, buffer.ReadOffset, debugFrameBuffer, 0, buffer.LengthAvailableToRead);
#endif

            int frameStartOffset = buffer.ReadOffset;

            // frame header
            uint frameSize  = AmqpBitConverter.ReadUInt(buffer);
            byte dataOffset = AmqpBitConverter.ReadUByte(buffer);
            byte frameType  = AmqpBitConverter.ReadUByte(buffer);
            channelNumber = AmqpBitConverter.ReadUShort(buffer); // out param

            if (dataOffset < 2)
            {
                throw new AmqpException(ErrorCode.FramingError, $"DOFF must be >= 2. Value is {dataOffset.ToHex()}");
            }

            // data offset is always counted in 4-byte words. header total length is 8 bytes
            int bodyStartOffset = 4 * dataOffset;
            // forward the reader the number of bytes needed to reach the frame body
            buffer.CompleteRead((bodyStartOffset - 8));

            if (frameSize == bodyStartOffset)
            {
                // empty frame body
                return(null);
            }

            // we're expecting a described list...
            var formatCode = DecodeFormatCode(buffer);
            if (formatCode != FormatCode.Described)
            {
                throw new AmqpException(ErrorCode.FramingError, $"Expected Format Code = {FormatCode.Described.ToHex()} but was {formatCode.ToHex()}");
            }

            try
            {
                // decode
                return((AmqpFrame)DecodeDescribedType(buffer, formatCode));
            }
            catch (Exception)
            {
#if DEBUG
                TraceSource.FromClass().Debug(Environment.NewLine + debugFrameBuffer.ToHex());
#endif
                throw;
            }
        }
Beispiel #13
0
        static Error ScanDataSection(byte formatCode, ByteBuffer buffer)
        {
            switch (formatCode)
            {
            case FormatCode.Binary8:
                return(AdvanceBuffer(buffer, AmqpBitConverter.ReadUByte(buffer)));

            case FormatCode.Binary32:
                return(AdvanceBuffer(buffer, AmqpBitConverter.ReadUInt(buffer)));

            default:
                return(GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidFormatCode, formatCode, buffer.Offset)));
            }
        }
Beispiel #14
0
        private static void SkipBinaryBuffer(ByteBuffer buffer)
        {
            var binaryFormatCode = AmqpCodec.DecodeFormatCode(buffer);
            int size             = 0;

            if (binaryFormatCode == FormatCode.Binary8)
            {
                size = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (binaryFormatCode == FormatCode.Binary32)
            {
                size = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            buffer.CompleteRead(size);
        }
Beispiel #15
0
 public static void Decode(ByteBuffer buffer, out ushort channel, out DescribedList command)
 {
     AmqpBitConverter.ReadUInt(buffer);
     AmqpBitConverter.ReadUByte(buffer);
     AmqpBitConverter.ReadUByte(buffer);
     channel = AmqpBitConverter.ReadUShort(buffer);
     if (buffer.Length > 0)
     {
         command = (DescribedList)Codec.Decode(buffer);
     }
     else
     {
         command = null;
     }
 }
Beispiel #16
0
 private bool HandleReadComplete(TransportAsyncCallbackArgs args)
 {
     unsafe
     {
         bool      flag      = true;
         Exception exception = null;
         if (args.Exception != null)
         {
             exception = args.Exception;
         }
         else if (args.BytesTransfered == 0)
         {
             exception = new ObjectDisposedException(this.transport.ToString());
         }
         else if (args.BytesTransfered < args.Count)
         {
             args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered);
             flag = false;
         }
         if (flag)
         {
             if (exception != null || object.ReferenceEquals(args.CompletedCallback, AsyncIO.FrameBufferReader.onFrameComplete))
             {
                 ByteBuffer byteBuffer = null;
                 if (exception != null)
                 {
                     this.parent.OnIoFault(exception);
                 }
                 else
                 {
                     byteBuffer = new ByteBuffer(args.Buffer, 0, (int)args.Buffer.Length);
                     this.parent.OnReceiveBuffer(byteBuffer);
                 }
             }
             else
             {
                 uint   num      = AmqpBitConverter.ReadUInt(this.sizeBuffer, 0, (int)this.sizeBuffer.Length);
                 byte[] numArray = new byte[num];
                 Buffer.BlockCopy(this.sizeBuffer, 0, numArray, 0, (int)this.sizeBuffer.Length);
                 args.SetBuffer(numArray, (int)this.sizeBuffer.Length, (int)(num - (int)this.sizeBuffer.Length));
                 args.CompletedCallback = AsyncIO.FrameBufferReader.onFrameComplete;
                 flag = false;
             }
         }
         return(flag);
     }
 }
Beispiel #17
0
            bool HandleReadComplete(TransportAsyncCallbackArgs args)
            {
                bool      completed = true;
                Exception exception = null;

                if (args.Exception != null)
                {
                    exception = args.Exception;
                }
                else if (args.BytesTransfered == 0)
                {
                    exception = new ObjectDisposedException(this.transport.ToString());
                }
                else if (args.BytesTransfered < args.Count)
                {
                    args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered);
                    completed = false;
                }

                if (completed)
                {
                    if (exception != null || object.ReferenceEquals(args.CompletedCallback, this.onFrameComplete))
                    {
                        Action <ByteBuffer, Exception> callback = (Action <ByteBuffer, Exception>)args.UserToken;
                        ByteBuffer buffer = null;
                        if (exception == null)
                        {
                            buffer = ByteBuffer.Wrap(args.Buffer, 0, args.Buffer.Length);
                        }

                        callback(buffer, exception);
                    }
                    else
                    {
                        // read size completed ok
                        uint   size        = AmqpBitConverter.ReadUInt(this.sizeBuffer, 0, this.sizeBuffer.Length);
                        byte[] frameBuffer = new byte[size];
                        Buffer.BlockCopy(this.sizeBuffer, 0, frameBuffer, 0, this.sizeBuffer.Length);
                        args.SetBuffer(frameBuffer, this.sizeBuffer.Length, (int)size - this.sizeBuffer.Length);
                        args.CompletedCallback = this.onFrameComplete;
                        completed = false;
                    }
                }

                return(completed);
            }
Beispiel #18
0
        protected override void DecodeValue(ByteBuffer buffer)
        {
            var formatCode = AmqpCodec.DecodeFormatCode(buffer);

            if (formatCode == FormatCode.Null)
            {
                return; // nothing to decode
            }

            int size;
            int count;

            if (formatCode == FormatCode.List0)
            {
                size = count = 0;
            }
            else if (formatCode == FormatCode.List8)
            {
                size  = AmqpBitConverter.ReadUByte(buffer);
                count = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (formatCode == FormatCode.List32)
            {
                size  = (int)AmqpBitConverter.ReadUInt(buffer);
                count = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                throw new AmqpException(ErrorCode.DecodeError, $"The format code '{formatCode}' at frame buffer offset '{buffer.ReadOffset}' is invalid.");
            }

            var thisType = GetType();

            for (int i = 0; i < count; i++)
            {
                var itemFormatCode = AmqpCodec.DecodeFormatCode(buffer);
                if (itemFormatCode == FormatCode.Null)
                {
                    continue; // null value, ignore and continue
                }
                GetDecoderDelegate(thisType, i)(this, buffer, itemFormatCode);
            }
        }
Beispiel #19
0
 public static uint ReadUInt(ByteBuffer buffer, byte formatCode)
 {
     if (formatCode == FormatCode.UInt0)
     {
         return(0);
     }
     else if (formatCode == FormatCode.SmallUInt)
     {
         return(AmqpBitConverter.ReadUByte(buffer));
     }
     else if (formatCode == FormatCode.UInt)
     {
         return(AmqpBitConverter.ReadUInt(buffer));
     }
     else
     {
         throw DecodeException(formatCode, buffer.Offset);
     }
 }
Beispiel #20
0
        public static uint?Decode(ByteBuffer buffer, FormatCode formatCode)
        {
            if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null)
            {
                return(null);
            }

            VerifyFormatCode(formatCode, buffer.Offset, FormatCode.UInt, FormatCode.SmallUInt, FormatCode.UInt0);
            if (formatCode == FormatCode.UInt0)
            {
                return(0);
            }
            else
            {
                return(formatCode == FormatCode.SmallUInt ?
                       AmqpBitConverter.ReadUByte(buffer) :
                       AmqpBitConverter.ReadUInt(buffer));
            }
        }
Beispiel #21
0
        public static Array ReadArray(ByteBuffer buffer, byte formatCode)
        {
            if (formatCode == FormatCode.Null)
            {
                return(null);
            }

            int size;
            int count;

            if (formatCode == FormatCode.Array8)
            {
                size  = AmqpBitConverter.ReadUByte(buffer);
                count = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (formatCode == FormatCode.Array32)
            {
                size  = (int)AmqpBitConverter.ReadUInt(buffer);
                count = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                throw DecodeException(formatCode, buffer.Offset);
            }

            formatCode = Encoder.ReadFormatCode(buffer);
            Serializer codec = GetSerializer(formatCode);

            if (codec == null)
            {
                throw DecodeException(formatCode, buffer.Offset);
            }

            Array value = Array.CreateInstance(codec.Type, count);
            IList list  = value;

            for (int i = 0; i < count; ++i)
            {
                list[i] = codec.Decoder(buffer, formatCode);
            }

            return(value);
        }
Beispiel #22
0
        private static void SkipDescribedList(ByteBuffer buffer)
        {
            // read the list length and move forward
            var listFormatCode = AmqpCodec.DecodeFormatCode(buffer);
            int size           = 0;

            if (listFormatCode == FormatCode.List0)
            {
                size = 0;
            }
            else if (listFormatCode == FormatCode.List8)
            {
                size = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (listFormatCode == FormatCode.List32)
            {
                size = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            buffer.CompleteRead(size);
        }
Beispiel #23
0
        public static ProtocolHeader Decode(ByteBuffer buffer)
        {
            if (buffer.Length < ProtocolHeader.Size)
            {
                throw AmqpEncoding.GetEncodingException("BufferSize");
            }

            uint prefix = AmqpBitConverter.ReadUInt(buffer);

            if (prefix != ProtocolHeader.AmqpPrefix)
            {
                throw AmqpEncoding.GetEncodingException("ProtocolName");
            }

            ProtocolHeader header = new ProtocolHeader();

            header.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer);
            header.version    = new AmqpVersion(AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer), AmqpBitConverter.ReadUByte(buffer));
            header.Buffer     = new ArraySegment <byte>(buffer.Buffer, buffer.Offset - ProtocolHeader.Size, ProtocolHeader.Size);
            return(header);
        }
Beispiel #24
0
        public void Decode(ByteBuffer buffer)
        {
            if (buffer.Length < this.EncodeSize)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, this.EncodeSize, buffer.Length));
            }

            uint prefix = AmqpBitConverter.ReadUInt(buffer);

            if (prefix != ProtocolHeader.AmqpPrefix)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, "ProtocolName" + prefix.ToString("X8"));
            }

            this.protocolId = (ProtocolId)AmqpBitConverter.ReadUByte(buffer);

            this.version = new AmqpVersion(
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer),
                AmqpBitConverter.ReadUByte(buffer));
        }
Beispiel #25
0
        public static Map ReadMap(ByteBuffer buffer, byte formatCode)
        {
            if (formatCode == FormatCode.Null)
            {
                return(null);
            }

            int size;
            int count;

            if (formatCode == FormatCode.Map8)
            {
                size  = AmqpBitConverter.ReadUByte(buffer);
                count = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (formatCode == FormatCode.Map32)
            {
                size  = (int)AmqpBitConverter.ReadUInt(buffer);
                count = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                throw DecodeException(formatCode, buffer.Offset);
            }

            if (count % 2 > 0)
            {
                throw new AmqpException(ErrorCode.DecodeError,
                                        Fx.Format(SRAmqp.InvalidMapCount, count));
            }

            Map value = new Map();

            for (int i = 0; i < count; i += 2)
            {
                value.Add(ReadObject(buffer), ReadObject(buffer));
            }

            return(value);
        }
Beispiel #26
0
        public static List ReadList(ByteBuffer buffer, byte formatCode)
        {
            if (formatCode == FormatCode.Null)
            {
                return(null);
            }

            int size;
            int count;

            if (formatCode == FormatCode.List0)
            {
                size = count = 0;
            }
            else if (formatCode == FormatCode.List8)
            {
                size  = AmqpBitConverter.ReadUByte(buffer);
                count = AmqpBitConverter.ReadUByte(buffer);
            }
            else if (formatCode == FormatCode.List32)
            {
                size  = (int)AmqpBitConverter.ReadUInt(buffer);
                count = (int)AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                throw DecodeException(formatCode, buffer.Offset);
            }

            List value = new List();

            for (int i = 0; i < count; ++i)
            {
                value.Add(ReadObject(buffer));
            }

            return(value);
        }
Beispiel #27
0
 protected static void ReadSizeAndCount(ByteBuffer buffer, byte formatCode, out int size, out int count, out int width)
 {
     if (formatCode == FormatCode.List0)
     {
         size = count = width = 0;
     }
     else if (formatCode == FormatCode.List8 || formatCode == FormatCode.Map8)
     {
         width = FixedWidth.UByte;
         size  = AmqpBitConverter.ReadUByte(buffer);
         count = AmqpBitConverter.ReadUByte(buffer);
     }
     else if (formatCode == FormatCode.List32 || formatCode == FormatCode.Map32)
     {
         width = FixedWidth.UInt;
         size  = (int)AmqpBitConverter.ReadUInt(buffer);
         count = (int)AmqpBitConverter.ReadUInt(buffer);
     }
     else
     {
         throw new AmqpException(ErrorCode.InvalidField, Fx.Format(SRAmqp.AmqpInvalidFormatCode, formatCode, buffer.Offset));
     }
 }
Beispiel #28
0
        static Error ScanValueSection(byte formatCode, ByteBuffer buffer, int depth = 0)
        {
            if (formatCode == FormatCode.Described)
            {
                if (depth > 10)
                {
                    // protection for stack overflow
                    return(GetDecodeError(AmqpResources.GetString(Resources.AmqpInvalidFormatCode, formatCode, buffer.Offset)));
                }

                Error error = ScanValueSection(AmqpEncoding.ReadFormatCode(buffer), buffer, depth + 1);
                if (error != null)
                {
                    return(error);
                }

                formatCode = AmqpEncoding.ReadFormatCode(buffer);
            }

            uint size;

            if (formatCode >= FormatCode.Binary8)
            {
                // variable width
                size = (formatCode & 0x10) == 0 ?
                       AmqpBitConverter.ReadUByte(buffer) :
                       AmqpBitConverter.ReadUInt(buffer);
            }
            else
            {
                // fixed width
                size = (uint)((1 << ((formatCode >> 4) - 4)) >> 1);
            }

            return(AdvanceBuffer(buffer, size));
        }
Beispiel #29
0
        public void TestMethod_AmqpBitConverter()
        {
            ByteBuffer buffer = new ByteBuffer(128, true);

            AmqpBitConverter.WriteByte(buffer, 0x22);
            AmqpBitConverter.WriteByte(buffer, -0x22);

            AmqpBitConverter.WriteUByte(buffer, 0x22);
            AmqpBitConverter.WriteUByte(buffer, 0xB2);

            AmqpBitConverter.WriteShort(buffer, 0x22B7);
            AmqpBitConverter.WriteShort(buffer, -0x22B7);

            AmqpBitConverter.WriteUShort(buffer, 0x22B7);
            AmqpBitConverter.WriteUShort(buffer, 0xC2B7);

            AmqpBitConverter.WriteInt(buffer, 0x340da287);
            AmqpBitConverter.WriteInt(buffer, -0x340da287);

            AmqpBitConverter.WriteUInt(buffer, 0x340da287);
            AmqpBitConverter.WriteUInt(buffer, 0xF40da287);

            AmqpBitConverter.WriteLong(buffer, 0x5d00BB9A340da287);
            AmqpBitConverter.WriteLong(buffer, -0x5d00BB9A340da287);

            AmqpBitConverter.WriteULong(buffer, 0x5d00BB9A340da287);
            AmqpBitConverter.WriteULong(buffer, 0xad00BB9A340da287);

            AmqpBitConverter.WriteFloat(buffer, 12344.4434F);
            AmqpBitConverter.WriteFloat(buffer, -12344.4434F);

            AmqpBitConverter.WriteDouble(buffer, 39432123244.44352334);
            AmqpBitConverter.WriteDouble(buffer, -39432123244.44352334);

            Guid uuid = Guid.NewGuid();

            AmqpBitConverter.WriteUuid(buffer, uuid);

            sbyte b  = AmqpBitConverter.ReadByte(buffer);
            sbyte b2 = AmqpBitConverter.ReadByte(buffer);

            byte ub  = AmqpBitConverter.ReadUByte(buffer);
            byte ub2 = AmqpBitConverter.ReadUByte(buffer);

            short s  = AmqpBitConverter.ReadShort(buffer);
            short s2 = AmqpBitConverter.ReadShort(buffer);

            ushort us  = AmqpBitConverter.ReadUShort(buffer);
            ushort us2 = AmqpBitConverter.ReadUShort(buffer);

            int i  = AmqpBitConverter.ReadInt(buffer);
            int i2 = AmqpBitConverter.ReadInt(buffer);

            uint ui  = AmqpBitConverter.ReadUInt(buffer);
            uint ui2 = AmqpBitConverter.ReadUInt(buffer);

            long l  = AmqpBitConverter.ReadLong(buffer);
            long l2 = AmqpBitConverter.ReadLong(buffer);

            ulong ul  = AmqpBitConverter.ReadULong(buffer);
            ulong ul2 = AmqpBitConverter.ReadULong(buffer);

            float f  = AmqpBitConverter.ReadFloat(buffer);
            float f2 = AmqpBitConverter.ReadFloat(buffer);

            double d  = AmqpBitConverter.ReadDouble(buffer);
            double d2 = AmqpBitConverter.ReadDouble(buffer);

            Guid uuid2 = AmqpBitConverter.ReadUuid(buffer);
        }
Beispiel #30
0
        public void AmqpCodecMapTest()
        {
            byte[]     workBuffer = new byte[4096];
            ByteBuffer buffer     = new ByteBuffer(workBuffer);
            string     strBig     = new string('A', 512);

            AmqpMap map = new AmqpMap();

            map.Add(new MapKey("boolTrue"), boolTrue);
            map.Add(new MapKey("boolFalse"), boolFalse);
            map.Add(new MapKey("ubyte"), ubyteValue);
            map.Add(new MapKey("ushort"), ushortValue);
            map.Add(new MapKey("uint"), uintValue);
            map.Add(new MapKey("ulong"), ulongValue);
            map.Add(new MapKey("byte"), byteValue);
            map.Add(new MapKey("short"), shortValue);
            map.Add(new MapKey("int"), intValue);
            map.Add(new MapKey("long"), longValue);
            map.Add(new MapKey("null"), null);
            map.Add(new MapKey("float"), floatValue);
            map.Add(new MapKey("double"), doubleValue);
            map.Add(new MapKey("decimal32"), decimal32Value);
            map.Add(new MapKey("decimal64"), decimal64Value);
            map.Add(new MapKey("decimal128"), decimal128Value);
            map.Add(new MapKey("char"), charValue);
            map.Add(new MapKey("datetime"), dtValue);
            map.Add(new MapKey("uuid"), uuidValue);
            map.Add(new MapKey("binaryNull"), new ArraySegment <byte>());
            map.Add(new MapKey("binary8"), bin8Value);
            map.Add(new MapKey("binary32"), bin32Value);
            map.Add(new MapKey("symbolNull"), new AmqpSymbol());
            map.Add(new MapKey("symbol8"), new AmqpSymbol(strValue));
            map.Add(new MapKey("symbol32"), new AmqpSymbol(strBig));
            map.Add(new MapKey("string8"), strValue);
            map.Add(new MapKey("string32"), strBig);
            map.Add(new MapKey("described1"), described1);

            AmqpCodec.EncodeMap(map, buffer);

            // make sure the size written is correct (it has to be Map32)
            // the first byte is FormatCode.Map32
            int mapSize = (int)AmqpBitConverter.ReadUInt(workBuffer, 1, 4);

            Assert.Equal(buffer.Length - 5, mapSize);

            AmqpMap decMap = AmqpCodec.DecodeMap(buffer);

            Assert.True(decMap[new MapKey("boolTrue")].Equals(true), "Boolean true expected.");
            Assert.True(decMap[new MapKey("boolFalse")].Equals(false), "Boolean false expected.");
            Assert.True(decMap[new MapKey("ubyte")].Equals(ubyteValue), "UByte value not equal.");
            Assert.True(decMap[new MapKey("ushort")].Equals(ushortValue), "UShort value not equal.");
            Assert.True(decMap[new MapKey("uint")].Equals(uintValue), "UInt value not equal.");
            Assert.True(decMap[new MapKey("ulong")].Equals(ulongValue), "ULong value not equal.");
            Assert.True(decMap[new MapKey("byte")].Equals(byteValue), "Byte value not equal.");
            Assert.True(decMap[new MapKey("short")].Equals(shortValue), "Short value not equal.");
            Assert.True(decMap[new MapKey("int")].Equals(intValue), "Int value not equal.");
            Assert.True(decMap[new MapKey("long")].Equals(longValue), "Long value not equal.");
            Assert.True(decMap[new MapKey("null")] == null, "Null object expected.");
            Assert.True(decMap[new MapKey("float")].Equals(floatValue), "Float value not equal.");
            Assert.True(decMap[new MapKey("double")].Equals(doubleValue), "Double value not equal.");
            Assert.True(decMap[new MapKey("decimal32")].Equals(decimal32Value), "Decimal32 value not equal.");
            Assert.True(decMap[new MapKey("decimal64")].Equals(decimal64Value), "Decimal64 value not equal.");
            Assert.True(decMap[new MapKey("decimal128")].Equals(decimal128Value), "Decimal128 value not equal.");
            Assert.True(decMap[new MapKey("char")].Equals(charValue), "Char value not equal.");
            Assert.True(decMap[new MapKey("datetime")].Equals(dtValue), "TimeStamp value not equal.");
            Assert.True(decMap[new MapKey("uuid")].Equals(uuidValue), "Uuid value not equal.");
            Assert.True(decMap[new MapKey("binaryNull")] == null, "Null binary expected.");
            ArraySegment <byte> bin8 = (ArraySegment <byte>)decMap[new MapKey("binary8")];

            EnsureEqual(bin8.Array, bin8.Offset, bin8.Count, bin8Value.Array, bin8Value.Offset, bin8Value.Count);
            ArraySegment <byte> bin32 = (ArraySegment <byte>)decMap[new MapKey("binary32")];

            EnsureEqual(bin32.Array, bin32.Offset, bin32.Count, bin32Value.Array, bin32Value.Offset, bin32Value.Count);

            Assert.True(decMap[new MapKey("symbolNull")] == null, "Null symbol expected.");
            AmqpSymbol symDecode = (AmqpSymbol)decMap[new MapKey("symbol8")];

            Assert.True(symDecode.Equals(strValue), "AmqpSymbol value not equal.");
            symDecode = (AmqpSymbol)decMap[new MapKey("symbol32")];
            Assert.True(symDecode.Equals(strBig), "AmqpSymbol value (big) not equal.");

            string strDecode = (string)decMap[new MapKey("string8")];

            Assert.True(strDecode.Equals(strValue), "string value not equal.");
            strDecode = (string)decMap[new MapKey("string32")];
            Assert.True(strDecode.Equals(strBig), "string value (big) not equal.");

            DescribedType described = (DescribedType)decMap[new MapKey("described1")];

            Assert.True(described.Descriptor.Equals(described1.Descriptor), "Described value 1 descriptor is different");
            Assert.True(described.Value.Equals(described1.Value), "Described value 1 value is different");
        }