WriteInt32() public static method

Writes a signed 32-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
public static WriteInt32 ( int value, ProtoWriter writer ) : void
value int
writer ProtoWriter
return void
Exemplo n.º 1
0
        /// <summary>
        /// Writes an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
        /// </summary>
        public static void WriteNetObject(object value, ProtoWriter dest, int key, NetObjectOptions options)
        {
#if FEAT_IKVM
            throw new NotSupportedException();
#else
            Helpers.DebugAssert(value != null);
            bool dynamicType         = (options & NetObjectOptions.DynamicType) != 0,
                 asReference         = (options & NetObjectOptions.AsReference) != 0;
            WireType     wireType    = dest.WireType;
            SubItemToken token       = ProtoWriter.StartSubItem(null, dest);
            bool         writeObject = true;
            if (asReference)
            {
                bool existing;
                int  objectKey = dest.NetCache.AddObjectKey(value, out existing);
                ProtoWriter.WriteFieldHeader(existing ? FieldExistingObjectKey : FieldNewObjectKey, WireType.Variant, dest);
                ProtoWriter.WriteInt32(objectKey, dest);
                if (existing)
                {
                    writeObject = false;
                }
            }

            if (writeObject)
            {
                if (dynamicType)
                {
                    bool existing;
                    Type type = value.GetType();

                    if (!(value is string))
                    {
                        key = dest.GetTypeKey(ref type);
                        if (key < 0)
                        {
                            throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
                        }
                    }
                    int typeKey = dest.NetCache.AddObjectKey(type, out existing);
                    ProtoWriter.WriteFieldHeader(existing ? FieldExistingTypeKey : FieldNewTypeKey, WireType.Variant, dest);
                    ProtoWriter.WriteInt32(typeKey, dest);
                    if (!existing)
                    {
                        ProtoWriter.WriteFieldHeader(FieldTypeName, WireType.String, dest);
                        ProtoWriter.WriteString(dest.SerializeType(type), dest);
                    }
                }
                ProtoWriter.WriteFieldHeader(FieldObject, wireType, dest);
                if (value is string)
                {
                    ProtoWriter.WriteString((string)value, dest);
                }
                else
                {
                    ProtoWriter.WriteObject(value, key, dest);
                }
            }
            ProtoWriter.EndSubItem(token, dest);
#endif
        }
        /// <summary>
        /// Writes an unsigned 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64
        /// </summary>
        public static void WriteUInt32(uint value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            switch (writer.wireType)
            {
            case WireType.Fixed32:
                ProtoWriter.WriteInt32((int)value, writer);
                return;

            case WireType.Fixed64:
                ProtoWriter.WriteInt64((int)value, writer);
                return;

            case WireType.Variant:
                WriteUInt32Variant(value, writer);
                writer.wireType = WireType.None;
                return;

            default:
                throw CreateException(writer);
            }
        }
Exemplo n.º 3
0
        static void WriteSingle(float value, ProtoWriter writer)

        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            switch (writer.wireType)

            {
            case WireType.Fixed32:

#if FEAT_SAFE
                ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
#else
                ProtoWriter.WriteInt32(*(int *)&value, writer);
#endif

                return;

            case WireType.Fixed64:

                ProtoWriter.WriteDouble((double)value, writer);

                return;

            default:

                throw CreateException(writer);
            }
        }
Exemplo n.º 4
0
        // Token: 0x0600015A RID: 346 RVA: 0x0000DA5C File Offset: 0x0000BC5C
        public static void WriteUInt32(uint value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            WireType wireType = writer.wireType;

            if (wireType == WireType.Variant)
            {
                ProtoWriter.WriteUInt32Variant(value, writer);
                writer.wireType = WireType.None;
                return;
            }
            if (wireType == WireType.Fixed64)
            {
                ProtoWriter.WriteInt64((long)value, writer);
                return;
            }
            if (wireType == WireType.Fixed32)
            {
                ProtoWriter.WriteInt32((int)value, writer);
                return;
            }
            throw ProtoWriter.CreateException(writer);
        }
Exemplo n.º 5
0
        public static void WriteNetObject(object value, ProtoWriter dest, int key, BclHelpers.NetObjectOptions options)
        {
            bool flag;
            bool flag1;

            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            bool         flag2        = (byte)(options & BclHelpers.NetObjectOptions.DynamicType) != 0;
            bool         flag3        = (byte)(options & BclHelpers.NetObjectOptions.AsReference) != 0;
            WireType     wireType     = dest.WireType;
            SubItemToken subItemToken = ProtoWriter.StartSubItem(null, dest);
            bool         flag4        = true;

            if (flag3)
            {
                int num = dest.NetCache.AddObjectKey(value, out flag);
                ProtoWriter.WriteFieldHeader((flag ? 1 : 2), WireType.Variant, dest);
                ProtoWriter.WriteInt32(num, dest);
                if (flag)
                {
                    flag4 = false;
                }
            }
            if (flag4)
            {
                if (flag2)
                {
                    Type type = value.GetType();
                    if (!(value is string))
                    {
                        key = dest.GetTypeKey(ref type);
                        if (key < 0)
                        {
                            throw new InvalidOperationException(string.Concat("Dynamic type is not a contract-type: ", type.Name));
                        }
                    }
                    int num1 = dest.NetCache.AddObjectKey(type, out flag1);
                    ProtoWriter.WriteFieldHeader((flag1 ? 3 : 4), WireType.Variant, dest);
                    ProtoWriter.WriteInt32(num1, dest);
                    if (!flag1)
                    {
                        ProtoWriter.WriteFieldHeader(8, WireType.String, dest);
                        ProtoWriter.WriteString(dest.SerializeType(type), dest);
                    }
                }
                ProtoWriter.WriteFieldHeader(10, wireType, dest);
                if (!(value is string))
                {
                    ProtoWriter.WriteObject(value, key, dest);
                }
                else
                {
                    ProtoWriter.WriteString((string)value, dest);
                }
            }
            ProtoWriter.EndSubItem(subItemToken, dest);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Writes a TimeSpan to a protobuf stream
        /// </summary>
        public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
        {
            TimeSpanScale scale;
            long          value = timeSpan.Ticks;

            if (timeSpan == TimeSpan.MaxValue)
            {
                value = 1;
                scale = TimeSpanScale.MinMax;
            }
            else if (timeSpan == TimeSpan.MinValue)
            {
                value = -1;
                scale = TimeSpanScale.MinMax;
            }
            else if (value % TimeSpan.TicksPerDay == 0)
            {
                scale  = TimeSpanScale.Days;
                value /= TimeSpan.TicksPerDay;
            }
            else if (value % TimeSpan.TicksPerHour == 0)
            {
                scale  = TimeSpanScale.Hours;
                value /= TimeSpan.TicksPerHour;
            }
            else if (value % TimeSpan.TicksPerMinute == 0)
            {
                scale  = TimeSpanScale.Minutes;
                value /= TimeSpan.TicksPerMinute;
            }
            else if (value % TimeSpan.TicksPerSecond == 0)
            {
                scale  = TimeSpanScale.Seconds;
                value /= TimeSpan.TicksPerSecond;
            }
            else if (value % TimeSpan.TicksPerMillisecond == 0)
            {
                scale  = TimeSpanScale.Milliseconds;
                value /= TimeSpan.TicksPerMillisecond;
            }
            else
            {
                scale = TimeSpanScale.Ticks;
            }

            SubItemToken token = ProtoWriter.StartSubItem(null, dest);

            if (value != 0)
            {
                ProtoWriter.WriteFieldHeader(FieldTimeSpanValue, WireType.SignedVariant, dest);
                ProtoWriter.WriteInt64(value, dest);
            }
            if (scale != TimeSpanScale.Days)
            {
                ProtoWriter.WriteFieldHeader(FieldTimeSpanScale, WireType.Variant, dest);
                ProtoWriter.WriteInt32((int)scale, dest);
            }
            ProtoWriter.EndSubItem(token, dest);
        }
Exemplo n.º 7
0
        public static void WriteNetObject(object value, ProtoWriter dest, int key, NetObjectOptions options)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            bool         flag     = (options & NetObjectOptions.DynamicType) != 0;
            bool         num      = (options & NetObjectOptions.AsReference) != 0;
            WireType     wireType = dest.WireType;
            SubItemToken token    = ProtoWriter.StartSubItem(null, dest);
            bool         flag2    = true;

            if (num)
            {
                bool existing;
                int  value2 = dest.NetCache.AddObjectKey(value, out existing);
                ProtoWriter.WriteFieldHeader(existing ? 1 : 2, WireType.Variant, dest);
                ProtoWriter.WriteInt32(value2, dest);
                if (existing)
                {
                    flag2 = false;
                }
            }
            if (flag2)
            {
                if (flag)
                {
                    Type type = value.GetType();
                    if (!(value is string))
                    {
                        key = dest.GetTypeKey(ref type);
                        if (key < 0)
                        {
                            throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
                        }
                    }
                    bool existing2;
                    int  value3 = dest.NetCache.AddObjectKey(type, out existing2);
                    ProtoWriter.WriteFieldHeader(existing2 ? 3 : 4, WireType.Variant, dest);
                    ProtoWriter.WriteInt32(value3, dest);
                    if (!existing2)
                    {
                        ProtoWriter.WriteFieldHeader(8, WireType.String, dest);
                        ProtoWriter.WriteString(dest.SerializeType(type), dest);
                    }
                }
                ProtoWriter.WriteFieldHeader(10, wireType, dest);
                if (value is string)
                {
                    ProtoWriter.WriteString((string)value, dest);
                }
                else
                {
                    ProtoWriter.WriteObject(value, key, dest);
                }
            }
            ProtoWriter.EndSubItem(token, dest);
        }
Exemplo n.º 8
0
        public static void WriteNetObject(object value, ProtoWriter dest, int key, BclHelpers.NetObjectOptions options)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            bool         flag     = (byte)(options & BclHelpers.NetObjectOptions.DynamicType) != 0;
            bool         flag2    = (byte)(options & BclHelpers.NetObjectOptions.AsReference) != 0;
            WireType     wireType = dest.WireType;
            SubItemToken token    = ProtoWriter.StartSubItem(null, dest);
            bool         flag3    = true;

            if (flag2)
            {
                bool flag4;
                int  value2 = dest.NetCache.AddObjectKey(value, out flag4);
                ProtoWriter.WriteFieldHeader((!flag4) ? 2 : 1, WireType.Variant, dest);
                ProtoWriter.WriteInt32(value2, dest);
                if (flag4)
                {
                    flag3 = false;
                }
            }
            if (flag3)
            {
                if (flag)
                {
                    Type type = value.GetType();
                    if (!(value is string))
                    {
                        key = dest.GetTypeKey(ref type);
                        if (key < 0)
                        {
                            throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.get_Name());
                        }
                    }
                    bool flag5;
                    int  value3 = dest.NetCache.AddObjectKey(type, out flag5);
                    ProtoWriter.WriteFieldHeader((!flag5) ? 4 : 3, WireType.Variant, dest);
                    ProtoWriter.WriteInt32(value3, dest);
                    if (!flag5)
                    {
                        ProtoWriter.WriteFieldHeader(8, WireType.String, dest);
                        ProtoWriter.WriteString(dest.SerializeType(type), dest);
                    }
                }
                ProtoWriter.WriteFieldHeader(10, wireType, dest);
                if (value is string)
                {
                    ProtoWriter.WriteString((string)value, dest);
                }
                else
                {
                    ProtoWriter.WriteObject(value, key, dest);
                }
            }
            ProtoWriter.EndSubItem(token, dest);
        }
Exemplo n.º 9
0
        private static void WriteSecondsNanos(long seconds, int nanos, ProtoWriter dest, ref ProtoWriter.State state)
        {
            SubItemToken token = ProtoWriter.StartSubItem(null, dest, ref state);

            if (seconds != 0)
            {
                ProtoWriter.WriteFieldHeader(1, WireType.Variant, dest, ref state);
                ProtoWriter.WriteInt64(seconds, dest, ref state);
            }
            if (nanos != 0)
            {
                ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest, ref state);
                ProtoWriter.WriteInt32(nanos, dest, ref state);
            }
            ProtoWriter.EndSubItem(token, dest, ref state);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Writes an *implementation specific* bundled .NET object, including (as options) type-metadata, identity/re-use, etc.
        /// </summary>
        public static void WriteNetObject(object value, ProtoWriter dest, int key, bool dynamicType, bool asReference)
        {
            Helpers.DebugAssert(value != null);
            WireType     wireType    = dest.WireType;
            SubItemToken token       = ProtoWriter.StartSubItem(null, dest);
            bool         writeObject = true;

            if (asReference)
            {
                bool existing;
                int  objectKey = dest.NetCache.AddObjectKey(value, out existing);
                ProtoWriter.WriteFieldHeader(existing ? FieldExistingObjectKey : FieldNewObjectKey, WireType.Variant, dest);
                ProtoWriter.WriteInt32(objectKey, dest);
                if (existing)
                {
                    writeObject = false;
                }
            }

            if (writeObject)
            {
                if (dynamicType)
                {
                    bool existing;
                    Type type = value.GetType();
                    key = dest.GetTypeKey(ref type);
                    int typeKey = dest.NetCache.AddObjectKey(type, out existing);
                    ProtoWriter.WriteFieldHeader(existing ? FieldExistingTypeKey : FieldNewTypeKey, WireType.Variant, dest);
                    ProtoWriter.WriteInt32(typeKey, dest);
                    if (!existing)
                    {
                        ProtoWriter.WriteFieldHeader(FieldTypeName, WireType.String, dest);
                        ProtoWriter.WriteString(dest.SerializeType(type), dest);
                    }
                }
                ProtoWriter.WriteFieldHeader(FieldObject, wireType, dest);
                if (value is string)
                {
                    ProtoWriter.WriteString((string)value, dest);
                }
                else
                {
                    ProtoWriter.WriteObject(value, key, dest);
                }
            }
            ProtoWriter.EndSubItem(token, dest);
        }
Exemplo n.º 11
0
 public static void WriteSingle(float value, ProtoWriter writer)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     ProtoBuf.WireType wireType = writer.wireType;
     if (wireType == ProtoBuf.WireType.Fixed64)
     {
         ProtoWriter.WriteDouble((double)value, writer);
         return;
     }
     if (wireType != ProtoBuf.WireType.Fixed32)
     {
         throw ProtoWriter.CreateException(writer);
     }
     ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
 }
Exemplo n.º 12
0
        // Token: 0x060034E4 RID: 13540 RVA: 0x00132E38 File Offset: 0x00131238
        public static void WriteSingle(float value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            WireType wireType = writer.wireType;

            if (wireType == WireType.Fixed32)
            {
                ProtoWriter.WriteInt32((int)value, writer);
                return;
            }
            if (wireType != WireType.Fixed64)
            {
                throw ProtoWriter.CreateException(writer);
            }
            ProtoWriter.WriteDouble((double)value, writer);
        }
Exemplo n.º 13
0
        public static void WriteUnityObject(ProtoWriter writer, UnityEngine.Object obj)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writer.unityReferences == null)
            {
                throw new Exception("unityReferences is null");
            }
            int index = writer.unityReferences.IndexOf(obj);

            if (index < 0)
            {
                writer.unityReferences.Add(obj);
                index = writer.unityReferences.Count - 1;
            }
            ProtoWriter.WriteInt32(index, writer);
        }
Exemplo n.º 14
0
        static void WriteSingle(float value, ProtoWriter writer)
        {
            switch (writer.wireType)
            {
            case WireType.Fixed32:
#if true
                ProtoWriter.WriteInt32(BitConverter.ToInt32(BitConverter.GetBytes(value), 0), writer);
#else
                ProtoWriter.WriteInt32(*(int *)&value, writer);
#endif
                return;

            case WireType.Fixed64:
                ProtoWriter.WriteDouble((double)value, writer);
                return;

            default:
                throw CreateException(writer);
            }
        }
Exemplo n.º 15
0
        static void WriteSingle(float value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            switch (writer.wireType)
            {
            case WireType.Fixed32:

                ProtoWriter.WriteInt32(*(int *)&value, writer);
                return;

            case WireType.Fixed64:
                ProtoWriter.WriteDouble((double)value, writer);
                return;

            default:
                throw CreateException(writer);
            }
        }
Exemplo n.º 16
0
        private void AppendExtensionField(ProtoWriter writer)
        {
            //TODO: replace this with stream-based, buffered raw copying
            ProtoWriter.WriteFieldHeader(fieldNumber, wireType, writer);
            switch (wireType)
            {
            case WireType.Fixed32:
                ProtoWriter.WriteInt32(ReadInt32(), writer);
                return;

            case WireType.Variant:
            case WireType.SignedVariant:
            case WireType.Fixed64:
                ProtoWriter.WriteInt64(ReadInt64(), writer);
                return;

            case WireType.String:
                ProtoWriter.WriteBytes(AppendBytes(null, this), writer);
                return;

            case WireType.StartGroup:
                SubItemToken readerToken = StartSubItem(this),
                             writerToken = ProtoWriter.StartSubItem(null, writer);
                while (ReadFieldHeader() > 0)
                {
                    AppendExtensionField(writer);
                }
                EndSubItem(readerToken, this);
                ProtoWriter.EndSubItem(writerToken, writer);
                return;

            case WireType.None:     // treat as explicit errorr
            case WireType.EndGroup: // treat as explicit error
            default:                // treat as implicit error
                throw CreateException();
            }
        }
Exemplo n.º 17
0
        public static void WriteUInt32(uint value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            ProtoBuf.WireType wireType = writer.wireType;
            switch (wireType)
            {
            case ProtoBuf.WireType.Variant:
            {
                ProtoWriter.WriteUInt32Variant(value, writer);
                writer.wireType = ProtoBuf.WireType.None;
                return;
            }

            case ProtoBuf.WireType.Fixed64:
            {
                ProtoWriter.WriteInt64((long)value, writer);
                return;
            }

            default:
            {
                if (wireType != ProtoBuf.WireType.Fixed32)
                {
                    break;
                }
                else
                {
                    ProtoWriter.WriteInt32((int)value, writer);
                    return;
                }
            }
            }
            throw ProtoWriter.CreateException(writer);
        }
Exemplo n.º 18
0
        private void AppendExtensionField(ProtoWriter writer)
        {
            ProtoWriter.WriteFieldHeader(fieldNumber, wireType, writer);
            switch (wireType)
            {
            case WireType.Fixed32:
                ProtoWriter.WriteInt32(ReadInt32(), writer);
                break;

            case WireType.Variant:
            case WireType.Fixed64:
            case WireType.SignedVariant:
                ProtoWriter.WriteInt64(ReadInt64(), writer);
                break;

            case WireType.String:
                ProtoWriter.WriteBytes(AppendBytes(null, this), writer);
                break;

            case WireType.StartGroup:
            {
                SubItemToken token  = StartSubItem(this);
                SubItemToken token2 = ProtoWriter.StartSubItem(null, writer);
                while (ReadFieldHeader() > 0)
                {
                    AppendExtensionField(writer);
                }
                EndSubItem(token, this);
                ProtoWriter.EndSubItem(token2, writer);
                break;
            }

            default:
                throw CreateWireTypeException();
            }
        }
Exemplo n.º 19
0
        private void AppendExtensionField(ProtoWriter writer)
        {
            ProtoWriter.WriteFieldHeader(this.fieldNumber, this.wireType, writer);
            WireType wireType = this.wireType;

            switch (wireType + 1)
            {
            case WireType.Fixed64:
            case WireType.String:
            case (WireType)9:
                ProtoWriter.WriteInt64(this.ReadInt64(), writer);
                return;

            case WireType.StartGroup:
                ProtoWriter.WriteBytes(ProtoReader.AppendBytes(null, this), writer);
                return;

            case WireType.EndGroup:
            {
                SubItemToken token  = ProtoReader.StartSubItem(this);
                SubItemToken token2 = ProtoWriter.StartSubItem(null, writer);
                while (this.ReadFieldHeader() > 0)
                {
                    this.AppendExtensionField(writer);
                }
                ProtoReader.EndSubItem(token, this);
                ProtoWriter.EndSubItem(token2, writer);
                return;
            }

            case (WireType)6:
                ProtoWriter.WriteInt32(this.ReadInt32(), writer);
                return;
            }
            throw this.CreateWireTypeException();
        }
Exemplo n.º 20
0
 /// <summary>
 /// Writes a signed 16-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
 /// </summary>
 public static void WriteInt16(short value, ProtoWriter writer)
 {
     ProtoWriter.WriteInt32(value, writer);
 }
Exemplo n.º 21
0
        public static void WriteInt64(long value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            WireType wireType = writer.wireType;

            switch (wireType)
            {
            case WireType.Variant:
                if (value >= 0L)
                {
                    ProtoWriter.WriteUInt64Variant((ulong)value, writer);
                    writer.wireType = WireType.None;
                }
                else
                {
                    ProtoWriter.DemandSpace(10, writer);
                    byte[] array = writer.ioBuffer;
                    int    num   = writer.ioIndex;
                    array[num]     = (byte)(value | 128L);
                    array[num + 1] = (byte)((int)(value >> 7) | 128);
                    array[num + 2] = (byte)((int)(value >> 14) | 128);
                    array[num + 3] = (byte)((int)(value >> 21) | 128);
                    array[num + 4] = (byte)((int)(value >> 28) | 128);
                    array[num + 5] = (byte)((int)(value >> 35) | 128);
                    array[num + 6] = (byte)((int)(value >> 42) | 128);
                    array[num + 7] = (byte)((int)(value >> 49) | 128);
                    array[num + 8] = (byte)((int)(value >> 56) | 128);
                    array[num + 9] = 1;
                    ProtoWriter.IncrementedAndReset(10, writer);
                }
                return;

            case WireType.Fixed64:
            {
                ProtoWriter.DemandSpace(8, writer);
                byte[] array = writer.ioBuffer;
                int    num   = writer.ioIndex;
                array[num]     = (byte)value;
                array[num + 1] = (byte)(value >> 8);
                array[num + 2] = (byte)(value >> 16);
                array[num + 3] = (byte)(value >> 24);
                array[num + 4] = (byte)(value >> 32);
                array[num + 5] = (byte)(value >> 40);
                array[num + 6] = (byte)(value >> 48);
                array[num + 7] = (byte)(value >> 56);
                ProtoWriter.IncrementedAndReset(8, writer);
                return;
            }

            case WireType.String:
            case WireType.StartGroup:
            case WireType.EndGroup:
IL_36:
                if (wireType != WireType.SignedVariant)
                {
                    throw ProtoWriter.CreateException(writer);
                }
                ProtoWriter.WriteUInt64Variant(ProtoWriter.Zig(value), writer);
                writer.wireType = WireType.None;
                return;

            case WireType.Fixed32:
                ProtoWriter.WriteInt32(checked ((int)value), writer);
                return;
            }
            goto IL_36;
        }
Exemplo n.º 22
0
        public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
        {
            TimeSpanScale timeSpanScale;

            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            switch (dest.WireType)
            {
            case WireType.Fixed64:
            {
                ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
                return;
            }

            case WireType.String:
            case WireType.StartGroup:
            {
                long ticks = timeSpan.Ticks;
                if (timeSpan == TimeSpan.MaxValue)
                {
                    ticks         = (long)1;
                    timeSpanScale = TimeSpanScale.MinMax;
                }
                else if (timeSpan == TimeSpan.MinValue)
                {
                    ticks         = (long)-1;
                    timeSpanScale = TimeSpanScale.MinMax;
                }
                else if (ticks % 864000000000L == (long)0)
                {
                    timeSpanScale = TimeSpanScale.Days;
                    ticks        /= 864000000000L;
                }
                else if (ticks % 36000000000L == (long)0)
                {
                    timeSpanScale = TimeSpanScale.Hours;
                    ticks        /= 36000000000L;
                }
                else if (ticks % (long)600000000 == (long)0)
                {
                    timeSpanScale = TimeSpanScale.Minutes;
                    ticks        /= (long)600000000;
                }
                else if (ticks % (long)10000000 == (long)0)
                {
                    timeSpanScale = TimeSpanScale.Seconds;
                    ticks        /= (long)10000000;
                }
                else if (ticks % (long)10000 != (long)0)
                {
                    timeSpanScale = TimeSpanScale.Ticks;
                }
                else
                {
                    timeSpanScale = TimeSpanScale.Milliseconds;
                    ticks        /= (long)10000;
                }
                SubItemToken subItemToken = ProtoWriter.StartSubItem(null, dest);
                if (ticks != (long)0)
                {
                    ProtoWriter.WriteFieldHeader(1, WireType.SignedVariant, dest);
                    ProtoWriter.WriteInt64(ticks, dest);
                }
                if (timeSpanScale != TimeSpanScale.Days)
                {
                    ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest);
                    ProtoWriter.WriteInt32((int)timeSpanScale, dest);
                }
                ProtoWriter.EndSubItem(subItemToken, dest);
                return;
            }
            }
            throw new ProtoException(string.Concat("Unexpected wire-type: ", dest.WireType.ToString()));
        }
Exemplo n.º 23
0
        public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            switch (dest.WireType)
            {
            case WireType.String:
            case WireType.StartGroup:
            {
                long          num = timeSpan.Ticks;
                TimeSpanScale timeSpanScale;
                if (timeSpan == TimeSpan.MaxValue)
                {
                    num           = 1L;
                    timeSpanScale = TimeSpanScale.MinMax;
                }
                else if (timeSpan == TimeSpan.MinValue)
                {
                    num           = -1L;
                    timeSpanScale = TimeSpanScale.MinMax;
                }
                else if (num % 864000000000L == 0L)
                {
                    timeSpanScale = TimeSpanScale.Days;
                    num          /= 864000000000L;
                }
                else if (num % 36000000000L == 0L)
                {
                    timeSpanScale = TimeSpanScale.Hours;
                    num          /= 36000000000L;
                }
                else if (num % 600000000 == 0L)
                {
                    timeSpanScale = TimeSpanScale.Minutes;
                    num          /= 600000000;
                }
                else if (num % 10000000 == 0L)
                {
                    timeSpanScale = TimeSpanScale.Seconds;
                    num          /= 10000000;
                }
                else if (num % 10000 == 0L)
                {
                    timeSpanScale = TimeSpanScale.Milliseconds;
                    num          /= 10000;
                }
                else
                {
                    timeSpanScale = TimeSpanScale.Ticks;
                }
                SubItemToken token = ProtoWriter.StartSubItem(null, dest);
                if (num != 0L)
                {
                    ProtoWriter.WriteFieldHeader(1, WireType.SignedVariant, dest);
                    ProtoWriter.WriteInt64(num, dest);
                }
                if (timeSpanScale != 0)
                {
                    ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest);
                    ProtoWriter.WriteInt32((int)timeSpanScale, dest);
                }
                ProtoWriter.EndSubItem(token, dest);
                break;
            }

            case WireType.Fixed64:
                ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
                break;

            default:
                throw new ProtoException("Unexpected wire-type: " + dest.WireType);
            }
        }
Exemplo n.º 24
0
        public static void WriteInt64(long value, ProtoWriter writer)
        {
            byte[] numArray;
            int    num;

            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            ProtoBuf.WireType wireType = writer.wireType;
            switch (wireType)
            {
            case ProtoBuf.WireType.Variant:
            {
                if (value >= (long)0)
                {
                    ProtoWriter.WriteUInt64Variant((ulong)value, writer);
                    writer.wireType = ProtoBuf.WireType.None;
                    return;
                }
                ProtoWriter.DemandSpace(10, writer);
                numArray          = writer.ioBuffer;
                num               = writer.ioIndex;
                numArray[num]     = (byte)(value | (long)128);
                numArray[num + 1] = (byte)((int)(value >> 7) | 128);
                numArray[num + 2] = (byte)((int)(value >> 14) | 128);
                numArray[num + 3] = (byte)((int)(value >> 21) | 128);
                numArray[num + 4] = (byte)((int)(value >> 28) | 128);
                numArray[num + 5] = (byte)((int)(value >> 35) | 128);
                numArray[num + 6] = (byte)((int)(value >> 42) | 128);
                numArray[num + 7] = (byte)((int)(value >> 49) | 128);
                numArray[num + 8] = (byte)((int)(value >> 56) | 128);
                numArray[num + 9] = 1;
                ProtoWriter.IncrementedAndReset(10, writer);
                return;
            }

            case ProtoBuf.WireType.Fixed64:
            {
                ProtoWriter.DemandSpace(8, writer);
                numArray          = writer.ioBuffer;
                num               = writer.ioIndex;
                numArray[num]     = (byte)value;
                numArray[num + 1] = (byte)(value >> 8);
                numArray[num + 2] = (byte)(value >> 16);
                numArray[num + 3] = (byte)(value >> 24);
                numArray[num + 4] = (byte)(value >> 32);
                numArray[num + 5] = (byte)(value >> 40);
                numArray[num + 6] = (byte)(value >> 48);
                numArray[num + 7] = (byte)(value >> 56);
                ProtoWriter.IncrementedAndReset(8, writer);
                return;
            }

            default:
            {
                if (wireType == ProtoBuf.WireType.Fixed32)
                {
                    break;
                }
                else
                {
                    if (wireType != ProtoBuf.WireType.SignedVariant)
                    {
                        throw ProtoWriter.CreateException(writer);
                    }
                    ProtoWriter.WriteUInt64Variant(ProtoWriter.Zig(value), writer);
                    writer.wireType = ProtoBuf.WireType.None;
                    return;
                }
            }
            }
            ProtoWriter.WriteInt32(checked ((int)value), writer);
        }
Exemplo n.º 25
0
 /// <summary>
 /// Writes a signed 8-bit integer to the stream; supported wire-types: Variant, Fixed32, Fixed64, SignedVariant
 /// </summary>
 public static void WriteSByte(sbyte value, ProtoWriter writer)
 {
     ProtoWriter.WriteInt32(value, writer);
 }
Exemplo n.º 26
0
        private static void WriteTimeSpanImpl(TimeSpan timeSpan, ProtoWriter dest, DateTimeKind kind)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            long value;

            switch (dest.WireType)
            {
            case WireType.String:
            case WireType.StartGroup:
                TimeSpanScale scale;
                value = timeSpan.Ticks;
                if (timeSpan == TimeSpan.MaxValue)
                {
                    value = 1;
                    scale = TimeSpanScale.MinMax;
                }
                else if (timeSpan == TimeSpan.MinValue)
                {
                    value = -1;
                    scale = TimeSpanScale.MinMax;
                }
                else if (value % TimeSpan.TicksPerDay == 0)
                {
                    scale  = TimeSpanScale.Days;
                    value /= TimeSpan.TicksPerDay;
                }
                else if (value % TimeSpan.TicksPerHour == 0)
                {
                    scale  = TimeSpanScale.Hours;
                    value /= TimeSpan.TicksPerHour;
                }
                else if (value % TimeSpan.TicksPerMinute == 0)
                {
                    scale  = TimeSpanScale.Minutes;
                    value /= TimeSpan.TicksPerMinute;
                }
                else if (value % TimeSpan.TicksPerSecond == 0)
                {
                    scale  = TimeSpanScale.Seconds;
                    value /= TimeSpan.TicksPerSecond;
                }
                else if (value % TimeSpan.TicksPerMillisecond == 0)
                {
                    scale  = TimeSpanScale.Milliseconds;
                    value /= TimeSpan.TicksPerMillisecond;
                }
                else
                {
                    scale = TimeSpanScale.Ticks;
                }

                SubItemToken token = ProtoWriter.StartSubItem(null, dest);

                if (value != 0)
                {
                    ProtoWriter.WriteFieldHeader(FieldTimeSpanValue, WireType.SignedVariant, dest);
                    ProtoWriter.WriteInt64(value, dest);
                }
                if (scale != TimeSpanScale.Days)
                {
                    ProtoWriter.WriteFieldHeader(FieldTimeSpanScale, WireType.Variant, dest);
                    ProtoWriter.WriteInt32((int)scale, dest);
                }
                if (kind != DateTimeKind.Unspecified)
                {
                    ProtoWriter.WriteFieldHeader(FieldTimeSpanKind, WireType.Variant, dest);
                    ProtoWriter.WriteInt32((int)kind, dest);
                }
                ProtoWriter.EndSubItem(token, dest);
                break;

            case WireType.Fixed64:
                ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
                break;

            default:
                throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString());
            }
        }
Exemplo n.º 27
0
        public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            WireType wireType = dest.WireType;

            if (wireType != WireType.Fixed64)
            {
                if ((uint)(wireType - 2) <= 1u)
                {
                    long          num = timeSpan.Ticks;
                    TimeSpanScale timeSpanScale;
                    if (timeSpan == TimeSpan.MaxValue)
                    {
                        num           = 1L;
                        timeSpanScale = TimeSpanScale.MinMax;
                    }
                    else if (timeSpan == TimeSpan.MinValue)
                    {
                        num           = -1L;
                        timeSpanScale = TimeSpanScale.MinMax;
                    }
                    else if (num % 864000000000L == 0L)
                    {
                        timeSpanScale = TimeSpanScale.Days;
                        num          /= 864000000000L;
                    }
                    else if (num % 36000000000L == 0L)
                    {
                        timeSpanScale = TimeSpanScale.Hours;
                        num          /= 36000000000L;
                    }
                    else if (num % 600000000 == 0L)
                    {
                        timeSpanScale = TimeSpanScale.Minutes;
                        num          /= 600000000;
                    }
                    else if (num % 10000000 == 0L)
                    {
                        timeSpanScale = TimeSpanScale.Seconds;
                        num          /= 10000000;
                    }
                    else if (num % 10000 == 0L)
                    {
                        timeSpanScale = TimeSpanScale.Milliseconds;
                        num          /= 10000;
                    }
                    else
                    {
                        timeSpanScale = TimeSpanScale.Ticks;
                    }
                    SubItemToken token = ProtoWriter.StartSubItem(null, dest);
                    if (num != 0L)
                    {
                        ProtoWriter.WriteFieldHeader(1, WireType.SignedVariant, dest);
                        ProtoWriter.WriteInt64(num, dest);
                    }
                    if (timeSpanScale != 0)
                    {
                        ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest);
                        ProtoWriter.WriteInt32((int)timeSpanScale, dest);
                    }
                    ProtoWriter.EndSubItem(token, dest);
                    return;
                }
                wireType = dest.WireType;
                throw new ProtoException("Unexpected wire-type: " + wireType.ToString());
            }
            ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
        }