Beispiel #1
0
        /// <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);
            }
        }
Beispiel #2
0
        static void WriteDouble(double value, ProtoWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            switch (writer.wireType)
            {
            case WireType.Fixed32:
                float f = (float)value;
                if (Helpers.IsInfinity(f) &&
                    !Helpers.IsInfinity(value))
                {
                    throw new OverflowException();
                }
                ProtoWriter.WriteSingle(f, writer);
                return;

            case WireType.Fixed64:
#if FEAT_SAFE
                ProtoWriter.WriteInt64(BitConverter.ToInt64(BitConverter.GetBytes(value), 0), writer);
#else
                ProtoWriter.WriteInt64(*(long *)&value, writer);
#endif
                return;

            default:
                throw CreateException(writer);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Writes a TimeSpan to a protobuf stream
        /// </summary>
        public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
        {
            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);
                }
                ProtoWriter.EndSubItem(token, dest);
                break;

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

            default:
                throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString());
            }
        }