Exemple #1
0
 public static void WriteObject(ByteBuffer buffer, object value, bool smallEncoding = true)
 {
     if (value == null)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
     }
     else
     {
         Encode encoder;
         Decode decoder;
         if (TryGetCodec(value.GetType(), out encoder, out decoder))
         {
             encoder(buffer, value, smallEncoding);
         }
         else if (value is Described)
         {
             ((Described)value).Encode(buffer);
         }
         else
         {
             throw new AmqpException(ErrorCode.NotImplemented,
                                     Fx.Format(SRAmqp.EncodingTypeNotSupported, value.GetType()));
         }
     }
 }
Exemple #2
0
        internal static ByteBuffer EncodeCommand(FrameType type, ushort channel, Performative command, int payloadSize)
        {
            int frameSize = HeaderSize;

            if (command != null)
            {
                frameSize += AmqpCodec.GetSerializableEncodeSize(command);
            }

            frameSize += payloadSize;

            ByteBuffer buffer = new ByteBuffer(frameSize, false, false);

            AmqpBitConverter.WriteUInt(buffer, (uint)frameSize);
            AmqpBitConverter.WriteUByte(buffer, DefaultDataOffset);
            AmqpBitConverter.WriteUByte(buffer, (byte)type);
            AmqpBitConverter.WriteUShort(buffer, channel);

            if (command != null)
            {
                AmqpCodec.EncodeSerializable(command, buffer);
            }

            return(buffer);
        }
Exemple #3
0
 public void Encode(ByteBuffer buffer)
 {
     AmqpBitConverter.WriteUInt(buffer, AmqpPrefix);
     AmqpBitConverter.WriteUByte(buffer, (byte)this.protocolId);
     AmqpBitConverter.WriteUByte(buffer, this.version.Major);
     AmqpBitConverter.WriteUByte(buffer, this.version.Minor);
     AmqpBitConverter.WriteUByte(buffer, this.version.Revision);
 }
Exemple #4
0
 static void EncodeFrame(ByteBuffer buffer, FrameType type, ushort channel, DescribedList command)
 {
     AmqpBitConverter.WriteUInt(buffer, 0u);
     AmqpBitConverter.WriteUByte(buffer, DOF);
     AmqpBitConverter.WriteUByte(buffer, (byte)type);
     AmqpBitConverter.WriteUShort(buffer, channel);
     Codec.Encode(command, buffer);
 }
 public static void Encode(ByteBuffer buffer, FrameType type, ushort channel, DescribedList command)
 {
     buffer.Append(4);
     AmqpBitConverter.WriteUByte(buffer, 2);
     AmqpBitConverter.WriteUByte(buffer, (byte)type);
     AmqpBitConverter.WriteUShort(buffer, channel);
     command.Encode(buffer);
     AmqpBitConverter.WriteInt(buffer.Buffer, buffer.Offset, buffer.Length);
 }
 public override void EncodeObject(object value, bool arrayEncoding, ByteBuffer buffer)
 {
     if (arrayEncoding)
     {
         AmqpBitConverter.WriteUByte(buffer, (byte)((bool)value ? 1 : 0));
     }
     else
     {
         BooleanEncoding.Encode((bool)value, buffer);
     }
 }
Exemple #7
0
 public override void WriteObject(ByteBuffer buffer, object value)
 {
     if (value == null)
     {
         AmqpEncoding.EncodeNull(buffer);
         return;
     }
     AmqpBitConverter.WriteUByte(buffer, 0);
     SymbolEncoding.Encode(this.symbol, buffer);
     this.encoder.EncodeObject(this.getter((TValue)value), false, buffer);
 }
 public static void Encode(bool?value, ByteBuffer buffer)
 {
     if (value.HasValue)
     {
         AmqpBitConverter.WriteUByte(buffer, value.Value ? (byte)FormatCode.BooleanTrue : (byte)FormatCode.BooleanFalse);
     }
     else
     {
         AmqpEncoding.EncodeNull(buffer);
     }
 }
Exemple #9
0
 public static void Encode(ushort?value, ByteBuffer buffer)
 {
     if (value.HasValue)
     {
         AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.UShort);
         AmqpBitConverter.WriteUShort(buffer, value.Value);
     }
     else
     {
         AmqpEncoding.EncodeNull(buffer);
     }
 }
Exemple #10
0
 public static void WriteBoolean(ByteBuffer buffer, bool value, bool smallEncoding)
 {
     if (smallEncoding)
     {
         AmqpBitConverter.WriteUByte(buffer, value ? FormatCode.BooleanTrue : FormatCode.BooleanFalse);
     }
     else
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Boolean);
         AmqpBitConverter.WriteUByte(buffer, (byte)(value ? 1 : 0));
     }
 }
Exemple #11
0
 public static void Encode(float?value, ByteBuffer buffer)
 {
     if (value.HasValue)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Float);
         AmqpBitConverter.WriteFloat(buffer, value.Value);
     }
     else
     {
         AmqpEncoding.EncodeNull(buffer);
     }
 }
Exemple #12
0
 public void Encode(ByteBuffer buffer, bool arrayEncoding = false)
 {
     AmqpBitConverter.WriteUByte(buffer, FormatCode.Described);
     if (!string.IsNullOrWhiteSpace(Descriptor.Name) || Descriptor.Code != 0)
     {
         Encoder.WriteULong(buffer, Descriptor.Code, true);
     }
     else
     {
         Encoder.WriteSymbol(buffer, Descriptor.Name, true);
     }
     EncodeValue(buffer, arrayEncoding);
 }
 public static void Encode(DescribedType value, ByteBuffer buffer)
 {
     if (value == null)
     {
         AmqpEncoding.EncodeNull(buffer);
     }
     else
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Described);
         AmqpEncoding.EncodeObject(value.Descriptor, buffer);
         AmqpEncoding.EncodeObject(value.Value, buffer);
     }
 }
Exemple #14
0
 public static void WriteLong(ByteBuffer buffer, long value, bool smallEncoding)
 {
     if (smallEncoding && value >= sbyte.MinValue && value <= sbyte.MaxValue)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.SmallLong);
         AmqpBitConverter.WriteByte(buffer, (sbyte)value);
     }
     else
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Long);
         AmqpBitConverter.WriteLong(buffer, value);
     }
 }
Exemple #15
0
        public static void WriteList(ByteBuffer buffer, IList value, bool smallEncoding)
        {
            if (value == null)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
            }
            else
            {
                // trim tailing nulls
                int last = value.Count - 1;
                while (last >= 0 && value[last] == null)
                {
                    --last;
                }

                if (last < 0 && smallEncoding)
                {
                    AmqpBitConverter.WriteUByte(buffer, FormatCode.List0);
                }
                else
                {
                    int pos = buffer.WritePos;
                    AmqpBitConverter.WriteUByte(buffer, 0);
                    AmqpBitConverter.WriteUInt(buffer, 0);
                    AmqpBitConverter.WriteUInt(buffer, 0);

                    for (int i = 0; i <= last; ++i)
                    {
                        Encoder.WriteObject(buffer, value[i], smallEncoding);
                    }

                    int size  = buffer.WritePos - pos - 9;
                    int count = last + 1;

                    if (smallEncoding && size < byte.MaxValue && count <= byte.MaxValue)
                    {
                        buffer.Buffer[pos]     = FormatCode.List8;
                        buffer.Buffer[pos + 1] = (byte)(size + 1);
                        buffer.Buffer[pos + 2] = (byte)count;
                        Array.Copy(buffer.Buffer, pos + 9, buffer.Buffer, pos + 3, size);
                        buffer.Shrink(6);
                    }
                    else
                    {
                        buffer.Buffer[pos] = FormatCode.List32;
                        AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
                        AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, count);
                    }
                }
            }
        }
Exemple #16
0
        public static void WriteArray(ByteBuffer buffer, Array value)
        {
            if (value == null)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
            }
            else
            {
                int count = value.Length;
                Fx.Assert(count > 0, "must have at least 1 element in array");
                int pos = buffer.WritePos;
                AmqpBitConverter.WriteUByte(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);

                for (int i = 0; i < count; ++i)
                {
                    object item = value.GetValue(i);
                    if (i == 0)
                    {
                        Encoder.WriteObject(buffer, item, false);
                    }
                    else
                    {
                        int  lastPos  = buffer.WritePos - 1;
                        byte lastByte = buffer.Buffer[lastPos];
                        buffer.Shrink(1);
                        Encoder.WriteObject(buffer, item, false);
                        buffer.Buffer[lastPos] = lastByte;
                    }
                }

                int size = buffer.WritePos - pos - 9;

                if (size < byte.MaxValue && count <= byte.MaxValue)
                {
                    buffer.Buffer[pos]     = FormatCode.Array8;
                    buffer.Buffer[pos + 1] = (byte)(size + 1);
                    buffer.Buffer[pos + 2] = (byte)count;
                    Array.Copy(buffer.Buffer, pos + 9, buffer.Buffer, pos + 3, size);
                    buffer.Shrink(6);
                }
                else
                {
                    buffer.Buffer[pos] = FormatCode.Array32;
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, count);
                }
            }
        }
Exemple #17
0
            protected override void EncodeBody(ByteBuffer buffer)
            {
                int pos = buffer.WritePos;

                buffer.Validate(true, 8);
                buffer.Append(8);
                int  length    = 0;
                long streamPos = this.bodyStream.CanSeek ? this.bodyStream.Position : -1;

                try
                {
                    while (true)
                    {
                        buffer.Validate(true, 512);
                        int size = this.bodyStream.Read(buffer.Buffer, buffer.WritePos, 512);
                        if (size == 0)
                        {
                            break;
                        }

                        buffer.Append(size);
                        length += size;
                    }
                }
                finally
                {
                    if (this.bodyStream.CanSeek)
                    {
                        this.bodyStream.Position = streamPos;
                    }
                }

                AmqpBitConverter.WriteUByte(buffer.Buffer, pos, FormatCode.Described);
                AmqpBitConverter.WriteUByte(buffer.Buffer, pos + 1, FormatCode.SmallULong);
                AmqpBitConverter.WriteUByte(buffer.Buffer, pos + 2, (byte)Data.Code);
                AmqpBitConverter.WriteUByte(buffer.Buffer, pos + 3, FormatCode.Binary32);
                AmqpBitConverter.WriteUInt(buffer.Buffer, pos + 4, (uint)length);

                this.bodyOffset = pos;
                this.bodyLength = buffer.WritePos - pos;
                this.dataList   = new Data[]
                {
                    new Data()
                    {
                        Value  = new ArraySegment <byte>(buffer.Buffer, pos, length),
                        Offset = pos,
                        Length = length
                    }
                };
            }
Exemple #18
0
 protected override bool WriteFormatCode(ByteBuffer buffer)
 {
     AmqpBitConverter.WriteUByte(buffer, 0);
     if (!this.descriptorCode.HasValue)
     {
         SymbolEncoding.Encode(this.descriptorName, buffer);
     }
     else
     {
         ULongEncoding.Encode(this.descriptorCode, buffer);
     }
     AmqpBitConverter.WriteUByte(buffer, this.Code);
     return(true);
 }
Exemple #19
0
        public void Encode(ByteBuffer buffer)
        {
            AmqpBitConverter.WriteUInt(buffer, (uint)this.size);
            AmqpBitConverter.WriteUByte(buffer, this.dataOffset);
            AmqpBitConverter.WriteUByte(buffer, (byte)this.Type);
            AmqpBitConverter.WriteUShort(buffer, this.Channel);

            if (this.Command != null)
            {
                AmqpCodec.EncodeSerializable(this.Command, buffer);
            }

            this.frameBuffer = buffer.Array;
        }
Exemple #20
0
            protected override bool WriteFormatCode(ByteBuffer buffer)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Described);
                if (this.descriptorCode != null)
                {
                    Encoder.WriteULong(buffer, this.descriptorCode.Value, true);
                }
                else
                {
                    Encoder.WriteSymbol(buffer, this.descriptorName, true);
                }

                AmqpBitConverter.WriteUByte(buffer, this.Code);
                return(true);
            }
            protected override bool WriteFormatCode(ByteBuffer buffer)
            {
                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Described);
                if (this.descriptorCode != null)
                {
                    ULongEncoding.Encode(this.descriptorCode, buffer);
                }
                else
                {
                    SymbolEncoding.Encode(this.descriptorName, buffer);
                }

                AmqpBitConverter.WriteUByte(buffer, this.Code);
                return(true);
            }
Exemple #22
0
 public static void WriteULong(ByteBuffer buffer, ulong value, bool smallEncoding)
 {
     if (!smallEncoding || value > byte.MaxValue)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.ULong);
         AmqpBitConverter.WriteULong(buffer, value);
     }
     else if (value == 0)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.ULong0);
     }
     else if (value <= byte.MaxValue)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.SmallULong);
         AmqpBitConverter.WriteUByte(buffer, (byte)value);
     }
 }
Exemple #23
0
        internal override void EncodeValue(ByteBuffer buffer)
        {
            if (this.FieldCount == 0)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.List0);
            }
            else
            {
                int valueSize   = this.OnValueSize();
                int encodeWidth = AmqpEncoding.GetEncodeWidthByCountAndSize(this.FieldCount, valueSize);
                int sizeOffset;
                if (encodeWidth == FixedWidth.UByte)
                {
                    AmqpBitConverter.WriteUByte(buffer, FormatCode.List8);
                    sizeOffset = buffer.Length;
                    AmqpBitConverter.WriteUByte(buffer, 0);
                    AmqpBitConverter.WriteUByte(buffer, (byte)this.FieldCount);
                }
                else
                {
                    AmqpBitConverter.WriteUByte(buffer, FormatCode.List32);
                    sizeOffset = buffer.Length;
                    AmqpBitConverter.WriteUInt(buffer, 0);
                    AmqpBitConverter.WriteUInt(buffer, (uint)this.FieldCount);
                }

                this.OnEncode(buffer);

                // the actual encoded value size may be different from the calculated
                // valueSize. However, it can only become smaller. This allows for
                // reserving space in the buffer using the longest encoding form of a
                // value. For example, if the delivery id of a transfer is unknown, we
                // can use uint.Max for calculating encode size, but the actual encoding
                // could be small uint.
                int size = buffer.Length - sizeOffset - encodeWidth;
                if (encodeWidth == FixedWidth.UByte)
                {
                    AmqpBitConverter.WriteUByte(buffer.Buffer, sizeOffset, (byte)size);
                }
                else
                {
                    AmqpBitConverter.WriteUInt(buffer.Buffer, sizeOffset, (uint)size);
                }
            }
        }
        public static void FRM(Stream stream, ulong code, byte type, ushort channel, params object[] value)
        {
            List list = new List();

            if (value != null)
            {
                list.AddRange(value);
            }
            ByteBuffer buffer = new ByteBuffer(256, true);

            buffer.Append(4);
            AmqpBitConverter.WriteUByte(buffer, 2);
            AmqpBitConverter.WriteUByte(buffer, type);
            AmqpBitConverter.WriteUShort(buffer, channel);
            Encoder.WriteObject(buffer, new DescribedValue(code, list));
            AmqpBitConverter.WriteInt(buffer.Buffer, 0, buffer.Length);
            stream.Write(buffer.Buffer, buffer.Offset, buffer.Length);
        }
Exemple #25
0
 public static void WriteBinary(ByteBuffer buffer, byte[] value, bool smallEncoding)
 {
     if (value == null)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
     }
     else if (smallEncoding && value.Length <= byte.MaxValue)
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Binary8);
         AmqpBitConverter.WriteUByte(buffer, (byte)value.Length);
         AmqpBitConverter.WriteBytes(buffer, value, 0, value.Length);
     }
     else
     {
         AmqpBitConverter.WriteUByte(buffer, FormatCode.Binary32);
         AmqpBitConverter.WriteUInt(buffer, (uint)value.Length);
         AmqpBitConverter.WriteBytes(buffer, value, 0, value.Length);
     }
 }
Exemple #26
0
 public static void Encode(int?value, ByteBuffer buffer)
 {
     if (value.HasValue)
     {
         if (value < sbyte.MinValue || value > sbyte.MaxValue)
         {
             AmqpBitConverter.WriteUByte(buffer, FormatCode.Int);
             AmqpBitConverter.WriteInt(buffer, value.Value);
         }
         else
         {
             AmqpBitConverter.WriteUByte(buffer, FormatCode.SmallInt);
             AmqpBitConverter.WriteByte(buffer, (sbyte)value.Value);
         }
     }
     else
     {
         AmqpEncoding.EncodeNull(buffer);
     }
 }
Exemple #27
0
        public static ByteBuffer EncodeCommand(FrameType type, ushort channel, Performative command, int payloadSize)
        {
            int serializableEncodeSize = 8;

            if (command != null)
            {
                serializableEncodeSize = serializableEncodeSize + AmqpCodec.GetSerializableEncodeSize(command);
            }
            serializableEncodeSize = serializableEncodeSize + payloadSize;
            ByteBuffer byteBuffer = new ByteBuffer(serializableEncodeSize, false, false);

            AmqpBitConverter.WriteUInt(byteBuffer, (uint)serializableEncodeSize);
            AmqpBitConverter.WriteUByte(byteBuffer, 2);
            AmqpBitConverter.WriteUByte(byteBuffer, (byte)type);
            AmqpBitConverter.WriteUShort(byteBuffer, channel);
            if (command != null)
            {
                AmqpCodec.EncodeSerializable(command, byteBuffer);
            }
            return(byteBuffer);
        }
Exemple #28
0
            protected override void OnWriteObject(ByteBuffer buffer, object graph)
            {
                if (graph == null)
                {
                    AmqpEncoding.EncodeNull(buffer);
                    return;
                }

                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.Described);
                if (this.descriptorName.Value != null)
                {
                    SymbolEncoding.Encode(this.descriptorName, buffer);
                }
                else
                {
                    ULongEncoding.Encode(this.descriptorCode, buffer);
                }

                if (this.members.Length == 0)
                {
                    AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.List0);
                    return;
                }

                AmqpBitConverter.WriteUByte(buffer, (byte)FormatCode.List32);
                int endCopy = buffer.End;               // remember the current position

                AmqpBitConverter.WriteUInt(buffer, 0);  // reserve space for list size
                AmqpBitConverter.WriteUInt(buffer, (uint)this.members.Length);

                for (int i = 0; i < this.members.Length; ++i)
                {
                    object memberValue = this.members[i].Accessor.ReadObject(graph);
                    this.members[i].Type.OnWriteObject(buffer, memberValue);
                }

                // write the correct size
                AmqpBitConverter.WriteUInt(buffer.Buffer, endCopy, (uint)(buffer.End - endCopy - FixedWidth.UInt));
            }
Exemple #29
0
        protected override void EncodeValue(ByteBuffer buffer, bool arrayEncoding)
        {
            int pos = buffer.WriteOffset;

            // initial values for ctor, size, length
            AmqpBitConverter.WriteUByte(buffer, 0);
            AmqpBitConverter.WriteUInt(buffer, 0);
            AmqpBitConverter.WriteUInt(buffer, 0);

            var propertyCount                = GetEncodablePropertyCount(GetType());
            int lastNotNullIndex             = -1;
            int lastNotNullBufferWriteOffset = buffer.WriteOffset;

            var thisType = GetType();

            for (int i = 0; i < propertyCount; i++)
            {
                var encoderFunc  = GetEncoderDelegate(thisType, i);
                var valueWasNull = encoderFunc(this, buffer, arrayEncoding); // returns true if the value == null, false otherwise
                if (!valueWasNull || i == 0)
                {
                    lastNotNullIndex             = i;
                    lastNotNullBufferWriteOffset = buffer.WriteOffset;
                }
            }

            // rewind the buffer to just after we wrote the last not-null value
            var listLength = lastNotNullIndex + 1;

            buffer.AdjustWriteOffset(lastNotNullBufferWriteOffset);

            int size = buffer.WriteOffset - pos - 9; // recalculate list size

            // set ctor, size, length
            buffer.Buffer[pos] = FormatCode.List32;
            AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
            AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, listLength);
        }
Exemple #30
0
        public static void WriteMap(ByteBuffer buffer, Map value, bool smallEncoding)
        {
            if (value == null)
            {
                AmqpBitConverter.WriteUByte(buffer, FormatCode.Null);
            }
            else
            {
                int pos = buffer.WritePos;
                AmqpBitConverter.WriteUByte(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);
                AmqpBitConverter.WriteUInt(buffer, 0);

                foreach (var key in value.Keys)
                {
                    Encoder.WriteObject(buffer, key);
                    Encoder.WriteObject(buffer, value[key]);
                }

                int size  = buffer.WritePos - pos - 9;
                int count = value.Count * 2;

                if (smallEncoding && size < byte.MaxValue && count <= byte.MaxValue)
                {
                    buffer.Buffer[pos]     = FormatCode.Map8;
                    buffer.Buffer[pos + 1] = (byte)(size + 1);
                    buffer.Buffer[pos + 2] = (byte)count;
                    Array.Copy(buffer.Buffer, pos + 9, buffer.Buffer, pos + 3, size);
                    buffer.Shrink(6);
                }
                else
                {
                    buffer.Buffer[pos] = FormatCode.Map32;
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 1, size + 4);
                    AmqpBitConverter.WriteInt(buffer.Buffer, pos + 5, count);
                }
            }
        }