Ejemplo n.º 1
0
 public static int WriteTable(Span <byte> span, IDictionary val)
 {
     if (val is null || val.Count == 0)
     {
         NetworkOrderSerializer.WriteUInt32(span, 0);
         return(4);
     }
Ejemplo n.º 2
0
        public static int WriteTable(Span <byte> span, IDictionary <string, object> val)
        {
            if (val == null || val.Count == 0)
            {
                NetworkOrderSerializer.WriteUInt32(span, 0);
                return(4);
            }
            else
            {
                // Let's only write after the length header.
                Span <byte> slice        = span.Slice(4);
                int         bytesWritten = 0;
                if (val is Dictionary <string, object> dict)
                {
                    foreach (KeyValuePair <string, object> entry in dict)
                    {
                        bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key);
                        bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
                    }
                }
                else
                {
                    foreach (KeyValuePair <string, object> entry in val)
                    {
                        bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key);
                        bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
                    }
                }

                NetworkOrderSerializer.WriteUInt32(span, (uint)bytesWritten);
                return(4 + bytesWritten);
            }
        }
Ejemplo n.º 3
0
        internal void WriteTo(Memory <byte> memory)
        {
            memory.Span[0] = (byte)Type;
            NetworkOrderSerializer.WriteUInt16(memory.Slice(1), (ushort)Channel);
            int bytesWritten = WritePayload(memory.Slice(7));

            NetworkOrderSerializer.WriteUInt32(memory.Slice(3), (uint)bytesWritten);
            memory.Span[bytesWritten + 7] = Constants.FrameEnd;
            ByteCount = bytesWritten + 8;
        }
Ejemplo n.º 4
0
            public static int WriteTo(Span <byte> span, ushort channel, MethodBase method)
            {
                const int StartClassId         = StartPayload;
                const int StartMethodArguments = StartPayload + 4;

                NetworkOrderSerializer.WriteUInt32(span.Slice(StartClassId), (uint)method.ProtocolCommandId);
                int offset = method.WriteArgumentsTo(span.Slice(StartMethodArguments));

                return(WriteBaseFrame(span, FrameType.FrameMethod, channel, StartMethodArguments - StartPayload + offset));
            }
Ejemplo n.º 5
0
        public static unsafe int WriteLongstr(Span <byte> span, string val)
        {
            fixed(char *chars = val)
            fixed(byte *bytes = &span.Slice(4).GetPinnableReference())
            {
                int bytesWritten = Encoding.UTF8.GetBytes(chars, val.Length, bytes, span.Length);

                NetworkOrderSerializer.WriteUInt32(span, (uint)bytesWritten);
                return(bytesWritten + 4);
            }
        }
Ejemplo n.º 6
0
            public static int WriteTo <T>(Span <byte> span, ushort channel, ref T method) where T : struct, IOutgoingAmqpMethod
            {
                const int StartClassId         = StartPayload;
                const int StartMethodArguments = StartClassId + 4;

                int payloadLength = method.WriteTo(span.Slice(StartMethodArguments)) + 4;

                NetworkOrderSerializer.WriteUInt64(ref span.GetStart(), ((ulong)Constants.FrameMethod << 56) | ((ulong)channel << 40) | ((ulong)payloadLength << 8));
                NetworkOrderSerializer.WriteUInt32(ref span.GetOffset(StartClassId), (uint)method.ProtocolCommandId);
                span[payloadLength + StartPayload] = Constants.FrameEnd;
                return(payloadLength + BaseFrameSize);
            }
Ejemplo n.º 7
0
        private static int WriteBaseFrame(Span <byte> span, FrameType type, ushort channel, int payloadLength)
        {
            const int StartFrameType   = 0;
            const int StartChannel     = 1;
            const int StartPayloadSize = 3;

            span[StartFrameType] = (byte)type;
            NetworkOrderSerializer.WriteUInt16(span.Slice(StartChannel), channel);
            NetworkOrderSerializer.WriteUInt32(span.Slice(StartPayloadSize), (uint)payloadLength);
            span[StartPayload + payloadLength] = Constants.FrameEnd;
            return(StartPayload + 1 + payloadLength);
        }
Ejemplo n.º 8
0
            public static int WriteTo <T>(Span <byte> span, ushort channel, ref T header, int bodyLength) where T : IAmqpHeader
            {
                const int StartClassId         = StartPayload;
                const int StartBodyLength      = StartPayload + 4;
                const int StartHeaderArguments = StartPayload + 12;

                int payloadLength = 12 + header.WriteTo(span.Slice(StartHeaderArguments));

                NetworkOrderSerializer.WriteUInt64(ref span.GetStart(), ((ulong)Constants.FrameHeader << 56) | ((ulong)channel << 40) | ((ulong)payloadLength << 8));
                NetworkOrderSerializer.WriteUInt32(ref span.GetOffset(StartClassId), (uint)header.ProtocolClassId << 16); // The last 16 bytes (Weight) aren't used
                NetworkOrderSerializer.WriteUInt64(ref span.GetOffset(StartBodyLength), (ulong)bodyLength);
                span[payloadLength + StartPayload] = Constants.FrameEnd;
                return(payloadLength + BaseFrameSize);
            }
Ejemplo n.º 9
0
        public static int WriteArray(Span <byte> span, IList val)
        {
            if (val is null)
            {
                NetworkOrderSerializer.WriteUInt32(span, 0);
                return(4);
            }

            int bytesWritten = 4;

            for (int index = 0; index < val.Count; index++)
            {
                bytesWritten += WriteFieldValue(span.Slice(bytesWritten), val[index]);
            }

            NetworkOrderSerializer.WriteUInt32(span, (uint)bytesWritten - 4u);
            return(bytesWritten);
        }
Ejemplo n.º 10
0
        public static int WriteArray(ref byte destination, IList val)
        {
            if (val is null || val.Count == 0)
            {
                NetworkOrderSerializer.WriteUInt32(ref destination, 0);
                return(4);
            }

            int bytesWritten = 4;

            for (int index = 0; index < val.Count; index++)
            {
                bytesWritten += WriteFieldValue(ref destination.GetOffset(bytesWritten), val[index]);
            }

            NetworkOrderSerializer.WriteUInt32(ref destination, (uint)bytesWritten - 4u);
            return(bytesWritten);
        }
Ejemplo n.º 11
0
        public static int WriteArray(Memory <byte> memory, IList val)
        {
            if (val == null)
            {
                NetworkOrderSerializer.WriteUInt32(memory, 0);
                return(4);
            }
            else
            {
                int bytesWritten = 0;
                foreach (object entry in val)
                {
                    bytesWritten += WriteFieldValue(memory.Slice(4 + bytesWritten), entry);;
                }

                NetworkOrderSerializer.WriteUInt32(memory, (uint)bytesWritten);
                return(4 + bytesWritten);
            }
        }
        public static int WriteArray(Memory <byte> memory, IList val)
        {
            if (val == null)
            {
                NetworkOrderSerializer.WriteUInt32(memory.Span, 0);
                return(4);
            }
            else
            {
                int bytesWritten = 0;
                for (int index = 0; index < val.Count; index++)
                {
                    bytesWritten += WriteFieldValue(memory.Slice(4 + bytesWritten), val[index]);
                }

                NetworkOrderSerializer.WriteUInt32(memory.Span, (uint)bytesWritten);
                return(4 + bytesWritten);
            }
        }
Ejemplo n.º 13
0
        public static int WriteTable(Span <byte> span, IDictionary val)
        {
            if (val is null || val.Count == 0)
            {
                NetworkOrderSerializer.WriteUInt32(span, 0u);
                return(4);
            }

            // Let's only write after the length header.
            Span <byte> slice        = span.Slice(4);
            int         bytesWritten = 0;

            foreach (DictionaryEntry entry in val)
            {
                bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key.ToString());
                bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
            }

            NetworkOrderSerializer.WriteUInt32(span, (uint)bytesWritten);
            return(4 + bytesWritten);
        }
Ejemplo n.º 14
0
        public static int WriteTable(Memory <byte> memory, IDictionary <string, object> val)
        {
            if (val == null)
            {
                NetworkOrderSerializer.WriteUInt32(memory, 0);
                return(4);
            }
            else
            {
                // Let's only write after the length header.
                Memory <byte> slice        = memory.Slice(4);
                int           bytesWritten = 0;
                foreach (KeyValuePair <string, object> entry in val)
                {
                    bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key.ToString());
                    bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value);
                }

                NetworkOrderSerializer.WriteUInt32(memory, (uint)bytesWritten);
                return(4 + bytesWritten);
            }
        }
 public void TestWriteUInt32()
 {
     byte[] bytes = new byte[4];
     NetworkOrderSerializer.WriteUInt32(bytes, 0x89ABCDEF);
     Check(bytes, new byte[] { 0x89, 0xAB, 0xCD, 0xEF });
 }
Ejemplo n.º 16
0
        public static int WriteFieldValue(Span <byte> span, object value)
        {
            if (value is null)
            {
                span[0] = (byte)'V';
                return(1);
            }

            Span <byte> slice = span.Slice(1);

            switch (value)
            {
            case string val:
                span[0] = (byte)'S';
                return(1 + WriteLongstr(slice, val));

            case byte[] val:
                span[0] = (byte)'S';
                return(1 + WriteLongstr(slice, val));

            case int val:
                span[0] = (byte)'I';
                NetworkOrderSerializer.WriteInt32(slice, val);
                return(5);

            case uint val:
                span[0] = (byte)'i';
                NetworkOrderSerializer.WriteUInt32(slice, val);
                return(5);

            case decimal val:
                span[0] = (byte)'D';
                return(1 + WriteDecimal(slice, val));

            case AmqpTimestamp val:
                span[0] = (byte)'T';
                return(1 + WriteTimestamp(slice, val));

            case IDictionary val:
                span[0] = (byte)'F';
                return(1 + WriteTable(slice, val));

            case IList val:
                span[0] = (byte)'A';
                return(1 + WriteArray(slice, val));

            case byte val:
                span[0] = (byte)'B';
                span[1] = val;
                return(2);

            case sbyte val:
                span[0] = (byte)'b';
                span[1] = (byte)val;
                return(2);

            case double val:
                span[0] = (byte)'d';
                NetworkOrderSerializer.WriteDouble(slice, val);
                return(9);

            case float val:
                span[0] = (byte)'f';
                NetworkOrderSerializer.WriteSingle(slice, val);
                return(5);

            case long val:
                span[0] = (byte)'l';
                NetworkOrderSerializer.WriteInt64(slice, val);
                return(9);

            case short val:
                span[0] = (byte)'s';
                NetworkOrderSerializer.WriteInt16(slice, val);
                return(3);

            case bool val:
                span[0] = (byte)'t';
                span[1] = (byte)(val ? 1 : 0);
                return(2);

            case BinaryTableValue val:
                span[0] = (byte)'x';
                return(1 + WriteLongstr(slice, val.Bytes));

            default:
                throw new WireFormattingException($"Value of type '{value.GetType().Name}' cannot appear as table value", value);
            }
        }
Ejemplo n.º 17
0
 public static int WriteLong(Memory <byte> memory, uint val)
 {
     NetworkOrderSerializer.WriteUInt32(memory, val);
     return(4);
 }
Ejemplo n.º 18
0
        public static int WriteFieldValue(Memory <byte> memory, object value)
        {
            if (value == null)
            {
                memory.Span[0] = (byte)'V';
                return(1);
            }

            Memory <byte> slice = memory.Slice(1);

            switch (value)
            {
            case string val:
                memory.Span[0] = (byte)'S';
                if (MemoryMarshal.TryGetArray(memory.Slice(5, Encoding.UTF8.GetByteCount(val)), out ArraySegment <byte> segment))
                {
                    NetworkOrderSerializer.WriteUInt32(slice, (uint)segment.Count);
                    Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset);
                    return(segment.Count + 5);
                }

                throw new WireFormattingException("Unable to get array segment from memory.");

            case byte[] val:
                memory.Span[0] = (byte)'S';
                return(1 + WriteLongstr(slice, val));

            case int val:
                memory.Span[0] = (byte)'I';
                NetworkOrderSerializer.WriteInt32(slice, val);
                return(5);

            case uint val:
                memory.Span[0] = (byte)'i';
                NetworkOrderSerializer.WriteUInt32(slice, val);
                return(5);

            case decimal val:
                memory.Span[0] = (byte)'D';
                return(1 + WriteDecimal(slice, val));

            case AmqpTimestamp val:
                memory.Span[0] = (byte)'T';
                return(1 + WriteTimestamp(slice, val));

            case IDictionary val:
                memory.Span[0] = (byte)'F';
                return(1 + WriteTable(slice, val));

            case IList val:
                memory.Span[0] = (byte)'A';
                return(1 + WriteArray(slice, val));

            case byte val:
                memory.Span[0] = (byte)'B';
                memory.Span[1] = val;
                return(2);

            case sbyte val:
                memory.Span[0] = (byte)'b';
                memory.Span[1] = (byte)val;
                return(2);

            case double val:
                memory.Span[0] = (byte)'d';
                NetworkOrderSerializer.WriteDouble(slice, val);
                return(9);

            case float val:
                memory.Span[0] = (byte)'f';
                NetworkOrderSerializer.WriteSingle(slice, val);
                return(5);

            case long val:
                memory.Span[0] = (byte)'l';
                NetworkOrderSerializer.WriteInt64(slice, val);
                return(9);

            case short val:
                memory.Span[0] = (byte)'s';
                NetworkOrderSerializer.WriteInt16(slice, val);
                return(3);

            case bool val:
                memory.Span[0] = (byte)'t';
                memory.Span[1] = (byte)(val ? 1 : 0);
                return(2);

            case BinaryTableValue val:
                memory.Span[0] = (byte)'x';
                return(1 + WriteLongstr(slice, val.Bytes));

            default:
                throw new WireFormattingException($"Value of type '{value.GetType().Name}' cannot appear as table value", value);
            }
        }
Ejemplo n.º 19
0
 public static int WriteLong(Span <byte> span, uint val)
 {
     NetworkOrderSerializer.WriteUInt32(span, val);
     return(4);
 }