public static WriteFieldHeader ( int fieldNumber, WireType wireType, |
||
fieldNumber | int | |
wireType | WireType | |
writer | ||
return | void |
/// <summary> /// Writes a TimeSpan to a protobuf stream /// </summary> public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter 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()); } }
/// <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 }
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())); }
public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest) { if (dest == null) { throw new ArgumentNullException("dest"); } switch (dest.WireType) { case WireType.Fixed64: ProtoWriter.WriteInt64(timeSpan.get_Ticks(), dest); break; case WireType.String: case WireType.StartGroup: { long num = timeSpan.get_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 % 600000000L == 0L) { timeSpanScale = TimeSpanScale.Minutes; num /= 600000000L; } else if (num % 10000000L == 0L) { timeSpanScale = TimeSpanScale.Seconds; num /= 10000000L; } else if (num % 10000L == 0L) { timeSpanScale = TimeSpanScale.Milliseconds; num /= 10000L; } 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 != TimeSpanScale.Days) { ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest); ProtoWriter.WriteInt32((int)timeSpanScale, dest); } ProtoWriter.EndSubItem(token, dest); break; } default: throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString()); } }