/// <summary> /// Writes a field-header, indicating the format of the next data we plan to write. /// </summary> public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer, ref State state) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (writer.WireType != WireType.None) { throw new InvalidOperationException("Cannot write a " + wireType.ToString() + " header until the " + writer.WireType.ToString() + " data has been written"); } if (fieldNumber < 0) { throw new ArgumentOutOfRangeException(nameof(fieldNumber)); } #if DEBUG switch (wireType) { // validate requested header-type case WireType.Fixed32: case WireType.Fixed64: case WireType.String: case WireType.StartGroup: case WireType.SignedVariant: case WireType.Variant: break; // fine case WireType.None: case WireType.EndGroup: default: throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), nameof(wireType)); } #endif writer._needFlush = true; if (writer.packedFieldNumber == 0) { writer.fieldNumber = fieldNumber; writer.WireType = wireType; WriteHeaderCore(fieldNumber, wireType, writer, ref state); } else if (writer.packedFieldNumber == fieldNumber) { // we'll set things up, but note we *don't* actually write the header here switch (wireType) { case WireType.Fixed32: case WireType.Fixed64: case WireType.Variant: case WireType.SignedVariant: break; // fine default: throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString()); } writer.fieldNumber = fieldNumber; writer.WireType = wireType; } else { throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString()); } }
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("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; WriteHeaderCore(fieldNumber, wireType, writer); return; } if (writer.packedFieldNumber == fieldNumber) { if ((uint)wireType > 1u && wireType != WireType.Fixed32 && wireType != WireType.SignedVariant) { throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString()); } writer.fieldNumber = fieldNumber; writer.wireType = wireType; return; } throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString()); }
/// <summary> /// Used for packed encoding; writes the length prefix using fixed sizes rather than using /// buffering. Only valid for fixed-32 and fixed-64 encoding. /// </summary> public static void WritePackedPrefix(int elementCount, WireType wireType, ProtoWriter writer) { if (writer.WireType != WireType.String) { throw new InvalidOperationException("Invalid wire-type: " + writer.WireType); } if (elementCount < 0) { throw new ArgumentOutOfRangeException(elementCount.ToString()); //throw new ArgumentOutOfRangeException(nameof(elementCount)); } ulong bytes; switch (wireType) { // use long in case very large arrays are enabled case WireType.Fixed32: bytes = ((ulong)elementCount) << 2; break; // x4 case WireType.Fixed64: bytes = ((ulong)elementCount) << 3; break; // x8 default: throw new ArgumentOutOfRangeException(wireType.ToString(), "Invalid wire-type: " + wireType); //throw new ArgumentOutOfRangeException(nameof(wireType), "Invalid wire-type: " + wireType); } WriteUInt64Variant(bytes, writer); writer.wireType = WireType.None; }
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; } }
public void WriteFieldHeader(WireType _wireType) { if (mWireType != WireType.None) { throw new InvalidOperationException("Cannot write a " + _wireType.ToString() + " header until the " + mWireType.ToString() + " data has been written"); } mWireType = _wireType; WriteHeaderCore(mFieldNumber, _wireType); }
internal static void SkipData(SerializationContext context, int fieldTag, WireType wireType) { switch (wireType) { case WireType.Variant: context.ReadRawVariant(); break; case WireType.Fixed32: context.ReadBlock(4); break; case WireType.Fixed64: context.ReadBlock(8); break; case WireType.String: int len = context.DecodeInt32(); context.WriteTo(Stream.Null, len); break; case WireType.EndGroup: throw new ProtoException("End-group not expected at this location"); case WireType.StartGroup: context.StartGroup(fieldTag); // will be ended internally Serializer <UnknownType> .Build(); UnknownType ut = UnknownType.Default; Serializer <UnknownType> .Deserialize <UnknownType>(ref ut, context); break; default: throw new ProtoException("Unknown wire-type " + wireType.ToString()); } }
public static Wire ToInstance(this WireType wire) { return((Wire)Activator.CreateInstance( Type.GetType(string.Concat("Core.Wires.", wire.ToString())), false)); }
/// <summary> /// Writes a field-header, indicating the format of the next data we plan to write. /// </summary> 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("Cannot write a " + wireType.ToString() + " header until the " + writer.wireType.ToString() + " data has been written"); if(fieldNumber < 0) throw new ArgumentOutOfRangeException("fieldNumber"); #if DEBUG switch (wireType) { // validate requested header-type case WireType.Fixed32: case WireType.Fixed64: case WireType.String: case WireType.StartGroup: case WireType.SignedVariant: case WireType.Variant: break; // fine case WireType.None: case WireType.EndGroup: default: throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), "wireType"); } #endif if (writer.packedFieldNumber == 0) { writer.fieldNumber = fieldNumber; writer.wireType = wireType; WriteHeaderCore(fieldNumber, wireType, writer); } else if (writer.packedFieldNumber == fieldNumber) { // we'll set things up, but note we *don't* actually write the header here switch (wireType) { case WireType.Fixed32: case WireType.Fixed64: case WireType.Variant: case WireType.SignedVariant: break; // fine default: throw new InvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString()); } writer.fieldNumber = fieldNumber; writer.wireType = wireType; } else { throw new InvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString()); } }
static void WritePackedField(ProtoWriter writer, int fieldNumber, WireType wireType) { if (writer.packedFieldNumber == fieldNumber) { // we'll set things up, but note we *don't* actually write the header here switch (wireType) { case WireType.Fixed32: case WireType.Fixed64: case WireType.Varint: case WireType.SignedVarint: break; // fine default: ThrowHelper.ThrowInvalidOperationException("Wire-type cannot be encoded as packed: " + wireType.ToString()); break; } writer.fieldNumber = fieldNumber; writer.WireType = wireType; } else { ThrowHelper.ThrowInvalidOperationException("Field mismatch during packed encoding; expected " + writer.packedFieldNumber.ToString() + " but received " + fieldNumber.ToString()); } }
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; } }
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); }
private static long ReadTimeSpanTicks(ProtoReader source) { WireType wireType = source.WireType; if (wireType != WireType.Fixed64) { if ((uint)(wireType - 2) <= 1u) { SubItemToken token = ProtoReader.StartSubItem(source); TimeSpanScale timeSpanScale = TimeSpanScale.Days; long num = 0L; int num2; while ((num2 = source.ReadFieldHeader()) > 0) { switch (num2) { case 2: timeSpanScale = (TimeSpanScale)source.ReadInt32(); break; case 1: source.Assert(WireType.SignedVariant); num = source.ReadInt64(); break; default: source.SkipField(); break; } } ProtoReader.EndSubItem(token, source); switch (timeSpanScale) { case TimeSpanScale.Days: return(num * 864000000000L); case TimeSpanScale.Hours: return(num * 36000000000L); case TimeSpanScale.Minutes: return(num * 600000000); case TimeSpanScale.Seconds: return(num * 10000000); case TimeSpanScale.Milliseconds: return(num * 10000); case TimeSpanScale.Ticks: return(num); case TimeSpanScale.MinMax: switch (num) { case 1L: return(9223372036854775807L); case -1L: return(-9223372036854775808L); default: throw new ProtoException("Unknown min/max value: " + num.ToString()); } default: throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString()); } } wireType = source.WireType; throw new ProtoException("Unexpected wire-type: " + wireType.ToString()); } return(source.ReadInt64()); }
/////////////////////////////////////////////////////////////////////////// /// <summary> /// 生成异常 /// </summary> Exception createException(WireType wireType) { return(new ProtoException("Invalid serialization operation with wire-type " + wireType.ToString() + " at position " + _position.ToString())); }