Exemple #1
0
        public static ByteBuffer Encode(FrameType type, ushort channel, Transfer transfer,
            ByteBuffer payload, int maxFrameSize, out int payloadSize)
        {
            int bufferSize = cmdBufferSize + payload.Length;
            if (bufferSize > maxFrameSize)
            {
                bufferSize = maxFrameSize;
            }

            bool more = false;   // estimate it first
            if (payload.Length > bufferSize - 32)
            {
                transfer.More = more = true;
            }

            ByteBuffer buffer = new ByteBuffer(bufferSize, false);
            EncodeFrame(buffer, type, channel, transfer);

            if (more && payload.Length <= buffer.Size)
            {
                // guessed it wrong. correct it
                transfer.More = false;
                buffer.Reset();
                EncodeFrame(buffer, type, channel, transfer);
            }

            payloadSize = Math.Min(payload.Length, buffer.Size);
            AmqpBitConverter.WriteBytes(buffer, payload.Buffer, payload.Offset, payloadSize);
            payload.Complete(payloadSize);
            AmqpBitConverter.WriteInt(buffer.Buffer, 0, buffer.Length);
            return buffer;
        }
Exemple #2
0
 public static sbyte ReadByte(ByteBuffer buffer)
 {
     buffer.Validate(false, FixedWidth.Byte);
     sbyte data = (sbyte)buffer.Buffer[buffer.Offset];
     buffer.Complete(FixedWidth.Byte);
     return data;
 }
Exemple #3
0
 public void Decode(ByteBuffer buffer)
 {
     byte[] bytes = new byte[16];
     Buffer.BlockCopy(buffer.Buffer, buffer.Offset, bytes, 0, bytes.Length);
     this.uuid = new Guid(bytes);
     buffer.Complete(bytes.Length);
 }
 public static byte ReadUByte(ByteBuffer buffer)
 {
     buffer.EnsureLength(FixedWidth.UByte);
     byte data = buffer.Buffer[buffer.Offset];
     buffer.Complete(FixedWidth.UByte);
     return data;
 }
Exemple #5
0
 public static uint ReadUInt(ByteBuffer buffer)
 {
     buffer.Validate(false, FixedWidth.UInt);
     uint data = ReadUInt(buffer.Buffer, buffer.Offset, buffer.Length);
     buffer.Complete(FixedWidth.UInt);
     return data;
 }
Exemple #6
0
        public static ArraySegment<byte> Decode(ByteBuffer buffer, FormatCode formatCode)
        {
            if (formatCode == 0 && (formatCode = AmqpEncoding.ReadFormatCode(buffer)) == FormatCode.Null)
            {
                return AmqpConstants.EmptyBinary;
            }

            int count;
            AmqpEncoding.ReadCount(buffer, formatCode, FormatCode.Binary8, FormatCode.Binary32, out count);
            ArraySegment<byte> value = new ArraySegment<byte>(buffer.Buffer, buffer.Offset, count);
            buffer.Complete(count);
            return value;
        }
Exemple #7
0
        public static short ReadShort(ByteBuffer buffer)
        {
            buffer.Validate(false, FixedWidth.Short);
            short data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[1];
                d[1] = p[0];
            }

            buffer.Complete(FixedWidth.Short);
            return data;
        }
        public static ushort ReadUShort(ByteBuffer buffer)
        {
            buffer.EnsureLength(FixedWidth.UShort);
            ushort data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[1];
                d[1] = p[0];
            }

            buffer.Complete(FixedWidth.UShort);
            return data;
        }
        public static int ReadInt(ByteBuffer buffer)
        {
            buffer.EnsureLength(FixedWidth.Int);
            int data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[3];
                d[1] = p[2];
                d[2] = p[1];
                d[3] = p[0];
            }

            buffer.Complete(FixedWidth.Int);
            return data;
        }
 protected bool TryDecodeNull(ByteBuffer buffer)
 {
     buffer.Validate(false, FixedWidth.FormatCode);
     byte formatCode = buffer.Buffer[buffer.Offset];
     if (formatCode == FormatCode.Null)
     {
         buffer.Complete(FixedWidth.FormatCode);
         return true;
     }
     else
     {
         return false;
     }
 }
Exemple #11
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);
        }
Exemple #12
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;
        }
Exemple #13
0
        public static double ReadDouble(ByteBuffer buffer)
        {
            buffer.EnsureLength(FixedWidth.Double);
            double data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[7];
                d[1] = p[6];
                d[2] = p[5];
                d[3] = p[4];
                d[4] = p[3];
                d[5] = p[2];
                d[6] = p[1];
                d[7] = p[0];
            }

            buffer.Complete(FixedWidth.Double);
            return data;
        }
Exemple #14
0
        public static Guid ReadUuid(ByteBuffer buffer)
        {
            buffer.EnsureLength(FixedWidth.Uuid);
            Guid data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[3];
                d[1] = p[2];
                d[2] = p[1];
                d[3] = p[0];

                d[4] = p[5];
                d[5] = p[4];

                d[6] = p[7];
                d[7] = p[6];

                *((ulong*)&d[8]) = *((ulong*)&p[8]);
            }

            buffer.Complete(FixedWidth.Uuid);
            return data;
        }
Exemple #15
0
            public override object ReadObject(ByteBuffer buffer)
            {
                FormatCode formatCode = AmqpEncoding.ReadFormatCode(buffer);
                if (formatCode == FormatCode.Null)
                {
                    return null;
                }

                int size;
                int count;
                int encodeWidth;
                Collection effectiveType;
                this.Initialize(buffer, formatCode, out size, out count, out encodeWidth, out effectiveType);
                int offset = buffer.Offset;

                object container = effectiveType.CreateInstance();
                if (count > 0)
                {
                    effectiveType.ReadMembers(buffer, container, ref count);

                    if (count > 0)
                    {
                        // skip unknow members: todo: support IExtensibleDataObject
                        buffer.Complete(size - (buffer.Offset - offset) - encodeWidth);
                    }
                }

                return container;
            }
Exemple #16
0
 public static ulong ReadULong(ByteBuffer buffer)
 {
     buffer.EnsureLength(FixedWidth.ULong);
     ulong data = ReadULong(buffer.Buffer, buffer.Offset, buffer.Length);
     buffer.Complete(FixedWidth.ULong);
     return data;
 }
Exemple #17
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;
        }
Exemple #18
0
 public override object ReadObject(ByteBuffer buffer)
 {
     buffer.Validate(false, FixedWidth.FormatCode);
     FormatCode formatCode = buffer.Buffer[buffer.Offset];
     if (formatCode == FormatCode.Null)
     {
         buffer.Complete(FixedWidth.FormatCode);
         return null;
     }
     else
     {
         object container = this.CreateInstance();
         ((IAmqpSerializable)container).Decode(buffer);
         return container;
     }
 }
Exemple #19
0
        public static long ReadLong(ByteBuffer buffer)
        {
            buffer.Validate(false, FixedWidth.Long);
            long data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[7];
                d[1] = p[6];
                d[2] = p[5];
                d[3] = p[4];
                d[4] = p[3];
                d[5] = p[2];
                d[6] = p[1];
                d[7] = p[0];
            }

            buffer.Complete(FixedWidth.Long);
            return data;
        }
Exemple #20
0
        /// <summary>
        /// Reads a binary value from a buffer.
        /// </summary>
        /// <param name="buffer">The buffer to read.</param>
        /// <param name="formatCode">The format code of the value.</param>
        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;
        }
            public override object ReadObject(ByteBuffer buffer)
            {
                byte formatCode = Encoder.ReadFormatCode(buffer);
                if (formatCode == FormatCode.Null)
                {
                    return null;
                }

                int size;
                int count;
                int encodeWidth;
                CollectionType effectiveType;
                this.Initialize(buffer, formatCode, out size, out count, out encodeWidth, out effectiveType);
                int offset = buffer.Offset;

                object container = effectiveType.hasDefaultCtor ?
                    Activator.CreateInstance(effectiveType.type) :
                    FormatterServices.GetUninitializedObject(effectiveType.type);

                if (count > 0)
                {
                    effectiveType.ReadMembers(buffer, container, ref count);

                    if (count > 0)
                    {
                        // skip unknow members
                        buffer.Complete(size - (buffer.Offset - offset) - encodeWidth);
                    }
                }

                return container;
            }
 public override object ReadObject(ByteBuffer buffer)
 {
     buffer.Validate(false, FixedWidth.FormatCode);
     byte formatCode = buffer.Buffer[buffer.Offset];
     if (formatCode == FormatCode.Null)
     {
         buffer.Complete(FixedWidth.FormatCode);
         return null;
     }
     else
     {
         object value = this.type.CreateInstance(this.hasDefaultCtor);
         ((IAmqpSerializable)value).Decode(buffer);
         return value;
     }
 }
Exemple #23
0
        internal static ByteBuffer ReadBinaryBuffer(ByteBuffer buffer)
        {
            byte formatCode = Encoder.ReadFormatCode(buffer);
            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 InvalidFormatCodeException(formatCode, buffer.Offset);
            }

            buffer.Validate(false, count);
            ByteBuffer result = new ByteBuffer(buffer.Buffer, buffer.Offset, count, count);
            buffer.Complete(count);

            return result;
        }
Exemple #24
0
        public static float ReadFloat(ByteBuffer buffer)
        {
            buffer.Validate(false, FixedWidth.Float);
            float data;
            fixed (byte* p = &buffer.Buffer[buffer.Offset])
            {
                byte* d = (byte*)&data;
                d[0] = p[3];
                d[1] = p[2];
                d[2] = p[1];
                d[3] = p[0];
            }

            buffer.Complete(FixedWidth.Float);
            return data;
        }
Exemple #25
0
 public static void ReadBytes(ByteBuffer buffer, byte[] data, int offset, int count)
 {
     buffer.Validate(false, count);
     Buffer.BlockCopy(buffer.Buffer, buffer.Offset, data, offset, count);
     buffer.Complete(count);
 }