Exemplo n.º 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);
            }
        }
Exemplo n.º 2
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.º 3
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.º 4
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.º 5
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
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            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
        }
Exemplo n.º 6
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());
            }
        }