WriteHeaderCore() static private method

static private WriteHeaderCore ( int fieldNumber, WireType wireType, ProtoWriter writer ) : void
fieldNumber int
wireType WireType
writer ProtoWriter
return void
Ejemplo n.º 1
0
        public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw new InvalidOperationException(string.Concat(new string[]
                {
                    "Cannot write a ",
                    wireType.ToString(),
                    " header until the ",
                    writer.wireType.ToString(),
                    " data has been written"
                }));
            }
            if (fieldNumber < 0)
            {
                throw new ArgumentOutOfRangeException("fieldNumber");
            }
            if (writer.packedFieldNumber == 0)
            {
                writer.fieldNumber = fieldNumber;
                writer.wireType    = wireType;
                ProtoWriter.WriteHeaderCore(fieldNumber, wireType, writer);
            }
            else
            {
                if (writer.packedFieldNumber != fieldNumber)
                {
                    throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString());
                }
                switch (wireType)
                {
                case WireType.Variant:
                case WireType.Fixed64:
                case WireType.Fixed32:
                    goto IL_D7;

                case WireType.String:
                case WireType.StartGroup:
                case WireType.EndGroup:
IL_CB:
                    if (wireType != WireType.SignedVariant)
                    {
                        throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString());
                    }
                    goto IL_D7;
                }
                goto IL_CB;
IL_D7:
                writer.fieldNumber = fieldNumber;
                writer.wireType    = wireType;
            }
        }
Ejemplo n.º 2
0
        internal static void WriteObject(object value, int key, ProtoWriter writer, PrefixStyle style, int fieldNumber)
        {
            if (writer.model == null)
            {
                throw new InvalidOperationException("Cannot serialize sub-objects unless a model is provided");
            }
            if (writer.wireType != ProtoBuf.WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                writer.wireType    = ProtoBuf.WireType.String;
                writer.fieldNumber = fieldNumber;
                if (fieldNumber <= 0)
                {
                    break;
                }
                ProtoWriter.WriteHeaderCore(fieldNumber, ProtoBuf.WireType.String, writer);
                break;
            }

            case PrefixStyle.Fixed32:
            case PrefixStyle.Fixed32BigEndian:
            {
                writer.fieldNumber = 0;
                writer.wireType    = ProtoBuf.WireType.Fixed32;
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException("style");
            }
            }
            SubItemToken subItemToken = ProtoWriter.StartSubItem(value, writer, true);

            if (key >= 0)
            {
                writer.model.Serialize(key, value, writer);
            }
            else if (!writer.model.TrySerializeAuxiliaryType(writer, value.GetType(), DataFormat.Default, 1, value, false))
            {
                TypeModel.ThrowUnexpectedType(value.GetType());
            }
            ProtoWriter.EndSubItem(subItemToken, writer, style);
        }
Ejemplo n.º 3
0
        public static void WriteFieldHeader(int fieldNumber, ProtoBuf.WireType wireType, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != ProtoBuf.WireType.None)
            {
                string[] str = new string[] { "Cannot write a ", wireType.ToString(), " header until the ", writer.wireType.ToString(), " data has been written" };
                throw new InvalidOperationException(string.Concat(str));
            }
            if (fieldNumber < 0)
            {
                throw new ArgumentOutOfRangeException("fieldNumber");
            }
            if (writer.packedFieldNumber == 0)
            {
                writer.fieldNumber = fieldNumber;
                writer.wireType    = wireType;
                ProtoWriter.WriteHeaderCore(fieldNumber, wireType, writer);
                return;
            }
            if (writer.packedFieldNumber != fieldNumber)
            {
                throw new InvalidOperationException(string.Concat("Field mismatch during packed encoding; expected ", writer.packedFieldNumber.ToString(), " but received ", fieldNumber.ToString()));
            }
            ProtoBuf.WireType wireType1 = wireType;
            switch (wireType1)
            {
            case ProtoBuf.WireType.Variant:
            case ProtoBuf.WireType.Fixed64:
            {
                writer.fieldNumber = fieldNumber;
                writer.wireType    = wireType;
                return;
            }

            default:
            {
                if (wireType1 == ProtoBuf.WireType.Fixed32 || wireType1 == ProtoBuf.WireType.SignedVariant)
                {
                    writer.fieldNumber = fieldNumber;
                    writer.wireType    = wireType;
                    return;
                }
                throw new InvalidOperationException(string.Concat("Wire-type cannot be encoded as packed: ", wireType.ToString()));
            }
            }
        }
Ejemplo n.º 4
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            int    num;
            UInt32 num1;

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != ProtoBuf.WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            int num2 = token.@value;

            if (writer.depth <= 0)
            {
                throw ProtoWriter.CreateException(writer);
            }
            ProtoWriter protoWriter = writer;
            int         num3        = protoWriter.depth;
            int         num4        = num3;

            protoWriter.depth = num3 - 1;
            if (num4 > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (num2 < 0)
            {
                ProtoWriter.WriteHeaderCore(-num2, ProtoBuf.WireType.EndGroup, writer);
                writer.wireType = ProtoBuf.WireType.None;
                return;
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                num = writer.ioIndex - num2 - 1;
                int  num5 = 0;
                uint num6 = (uint)num;
                while (true)
                {
                    UInt32 num7 = num6 >> 7;
                    num6 = num7;
                    if (num7 == 0)
                    {
                        break;
                    }
                    num5++;
                }
                if (num5 != 0)
                {
                    ProtoWriter.DemandSpace(num5, writer);
                    byte[] numArray = writer.ioBuffer;
                    Helpers.BlockCopy(numArray, num2 + 1, numArray, num2 + 1 + num5, num);
                    num6 = (uint)num;
                    do
                    {
                        int num8 = num2;
                        num2           = num8 + 1;
                        numArray[num8] = (byte)(num6 & 127 | 128);
                        num1           = num6 >> 7;
                        num6           = num1;
                    }while (num1 != 0);
                    numArray[num2 - 1] = (byte)(numArray[num2 - 1] & -129);
                    writer.position   += num5;
                    writer.ioIndex    += num5;
                    break;
                }
                else
                {
                    writer.ioBuffer[num2] = (byte)(num & 127);
                    break;
                }
            }

            case PrefixStyle.Fixed32:
            {
                num = writer.ioIndex - num2 - 4;
                ProtoWriter.WriteInt32ToBuffer(num, writer.ioBuffer, num2);
                break;
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                num = writer.ioIndex - num2 - 4;
                byte[] numArray1 = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(num, numArray1, num2);
                byte num9 = numArray1[num2];
                numArray1[num2]     = numArray1[num2 + 3];
                numArray1[num2 + 3] = num9;
                num9 = numArray1[num2 + 1];
                numArray1[num2 + 1] = numArray1[num2 + 2];
                numArray1[num2 + 2] = num9;
                break;
            }

            default:
            {
                throw new ArgumentOutOfRangeException("style");
            }
            }
            ProtoWriter protoWriter1 = writer;
            int         num10        = protoWriter1.flushLock - 1;
            int         num11        = num10;

            protoWriter1.flushLock = num10;
            if (num11 == 0 && writer.ioIndex >= 1024)
            {
                ProtoWriter.Flush(writer);
            }
        }
Ejemplo n.º 5
0
        private static void EndSubItem(SubItemToken token, ProtoWriter writer, PrefixStyle style)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.wireType != WireType.None)
            {
                throw ProtoWriter.CreateException(writer);
            }
            int value = token.value;

            if (writer.depth <= 0)
            {
                throw ProtoWriter.CreateException(writer);
            }
            if (writer.depth-- > 25)
            {
                writer.PopRecursionStack();
            }
            writer.packedFieldNumber = 0;
            if (value < 0)
            {
                ProtoWriter.WriteHeaderCore(-value, WireType.EndGroup, writer);
                writer.wireType = WireType.None;
                return;
            }
            switch (style)
            {
            case PrefixStyle.Base128:
            {
                int  num  = writer.ioIndex - value - 1;
                int  num2 = 0;
                uint num3 = (uint)num;
                while ((num3 >>= 7) != 0u)
                {
                    num2++;
                }
                if (num2 == 0)
                {
                    writer.ioBuffer[value] = (byte)(num & 127);
                }
                else
                {
                    ProtoWriter.DemandSpace(num2, writer);
                    byte[] array = writer.ioBuffer;
                    Helpers.BlockCopy(array, value + 1, array, value + 1 + num2, num);
                    num3 = (uint)num;
                    do
                    {
                        array[value++] = (byte)((num3 & 127u) | 128u);
                    }while ((num3 >>= 7) != 0u);
                    array[value - 1] = (byte)((int)array[value - 1] & -129);
                    writer.position += num2;
                    writer.ioIndex  += num2;
                }
                break;
            }

            case PrefixStyle.Fixed32:
            {
                int num = writer.ioIndex - value - 4;
                ProtoWriter.WriteInt32ToBuffer(num, writer.ioBuffer, value);
                break;
            }

            case PrefixStyle.Fixed32BigEndian:
            {
                int    num    = writer.ioIndex - value - 4;
                byte[] array2 = writer.ioBuffer;
                ProtoWriter.WriteInt32ToBuffer(num, array2, value);
                byte b = array2[value];
                array2[value]     = array2[value + 3];
                array2[value + 3] = b;
                b = array2[value + 1];
                array2[value + 1] = array2[value + 2];
                array2[value + 2] = b;
                break;
            }

            default:
                throw new ArgumentOutOfRangeException("style");
            }
            if (--writer.flushLock == 0 && writer.ioIndex >= 1024)
            {
                ProtoWriter.Flush(writer);
            }
        }