/// <summary>Helper: put the buffer into a MemoryStream and create a new instance to deserializing into</summary> internal static LocalFeatures Deserialize(byte[] buffer) { LocalFeatures instance = new LocalFeatures(); using (var ms = new MemoryStream(buffer)) Deserialize(ms, instance); return instance; }
/// <summary>Helper: Serialize into a MemoryStream and return its byte array</summary> internal static byte[] SerializeToBytes(LocalFeatures instance) { using (var ms = new MemoryStream()) { Serialize(ms, instance); return ms.ToArray(); } }
/// <summary>Helper: Serialize with a varint length prefix</summary> internal static void SerializeLengthDelimited(Stream stream, LocalFeatures instance) { var data = SerializeToBytes(instance); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, (uint)data.Length); stream.Write(data, 0, data.Length); }
/// <summary>Serialize the instance into the stream</summary> internal static void Serialize(Stream stream, LocalFeatures instance) { instance.BeforeSerialize(); BinaryWriter bw = new BinaryWriter(stream); // Key for field: 1, Varint stream.WriteByte(8); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.Uptime.Ticks); // Key for field: 2, Varint stream.WriteByte(16); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.DueDate.Ticks); // Key for field: 3, Fixed64 stream.WriteByte(25); bw.Write(instance.Amount); if (instance.Denial != null) { // Key for field: 4, LengthDelimited stream.WriteByte(34); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Denial)); } if (instance.Secret != null) { // Key for field: 5, LengthDelimited stream.WriteByte(42); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Secret)); } if (instance.Internal != null) { // Key for field: 6, LengthDelimited stream.WriteByte(50); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Internal)); } if (instance.PR != null) { // Key for field: 7, LengthDelimited stream.WriteByte(58); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.PR)); } if (instance.TestingReadOnly != null) { // Key for field: 8, LengthDelimited stream.WriteByte(66); using (var ms8 = new MemoryStream()) { Mine.MyMessageV1.Serialize(ms8, instance.TestingReadOnly); // Length delimited byte array uint ms8Length = (uint)ms8.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms8Length); stream.Write(ms8.GetBuffer(), 0, (int)ms8Length); } } if (instance.MyInterface == null) throw new ArgumentNullException("MyInterface", "Required by proto specification."); // Key for field: 9, LengthDelimited stream.WriteByte(74); using (var ms9 = new MemoryStream()) { LocalFeatureTest.InterfaceTestSerializer.Serialize(ms9, instance.MyInterface); // Length delimited byte array uint ms9Length = (uint)ms9.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms9Length); stream.Write(ms9.GetBuffer(), 0, (int)ms9Length); } // Key for field: 10, LengthDelimited stream.WriteByte(82); using (var ms10 = new MemoryStream()) { LocalFeatureTest.StructTest.Serialize(ms10, instance.MyStruct); // Length delimited byte array uint ms10Length = (uint)ms10.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms10Length); stream.Write(ms10.GetBuffer(), 0, (int)ms10Length); } // Key for field: 11, LengthDelimited stream.WriteByte(90); using (var ms11 = new MemoryStream()) { TestB.ExternalStructSerializer.Serialize(ms11, instance.MyExtStruct); // Length delimited byte array uint ms11Length = (uint)ms11.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms11Length); stream.Write(ms11.GetBuffer(), 0, (int)ms11Length); } if (instance.MyExtClass != null) { // Key for field: 12, LengthDelimited stream.WriteByte(98); using (var ms12 = new MemoryStream()) { TestB.ExternalClassSerializer.Serialize(ms12, instance.MyExtClass); // Length delimited byte array uint ms12Length = (uint)ms12.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, ms12Length); stream.Write(ms12.GetBuffer(), 0, (int)ms12Length); } } // Key for field: 13, Varint stream.WriteByte(104); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.MyEnum); }
/// <summary>Helper: create a new instance to deserializing into</summary> internal static LocalFeatures DeserializeLengthDelimited(Stream stream) { LocalFeatures instance = new LocalFeatures(); DeserializeLengthDelimited(stream, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> internal static LocalFeatures DeserializeLength(Stream stream, int length) { LocalFeatures instance = new LocalFeatures(); DeserializeLength(stream, length, instance); return instance; }
/// <summary>Helper: create a new instance to deserializing into</summary> internal static LocalFeatures Deserialize(Stream stream) { LocalFeatures instance = new LocalFeatures(); Deserialize(stream, instance); return instance; }
/// <summary>Serialize the instance into the stream</summary> internal static void Serialize(Stream stream, LocalFeatures instance) { instance.BeforeSerialize(); var bw = new BinaryWriter(stream); var msField = global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Pop(); // Key for field: 1, Varint stream.WriteByte(8); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.Uptime.Ticks); // Key for field: 2, Varint stream.WriteByte(16); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)(instance.DueDate.Kind == DateTimeKind.Utc ? instance.DueDate : instance.DueDate.ToUniversalTime()).Ticks); // Key for field: 3, Fixed64 stream.WriteByte(25); bw.Write(instance.Amount); if (instance.Denial != null) { // Key for field: 4, LengthDelimited stream.WriteByte(34); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Denial)); } if (instance.Secret != null) { // Key for field: 5, LengthDelimited stream.WriteByte(42); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Secret)); } if (instance.Internal != null) { // Key for field: 6, LengthDelimited stream.WriteByte(50); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.Internal)); } if (instance.PR != null) { // Key for field: 7, LengthDelimited stream.WriteByte(58); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(instance.PR)); } if (instance.TestingReadOnly != null) { // Key for field: 8, LengthDelimited stream.WriteByte(66); msField.SetLength(0); Mine.MyMessageV1.Serialize(msField, instance.TestingReadOnly); // Length delimited byte array uint length8 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length8); msField.WriteTo(stream); } if (instance.MyInterface == null) throw new global::SilentOrbit.ProtocolBuffers.ProtocolBufferException("MyInterface is required by the proto specification."); // Key for field: 9, LengthDelimited stream.WriteByte(74); msField.SetLength(0); LocalFeatureTest.InterfaceTestSerializer.Serialize(msField, instance.MyInterface); // Length delimited byte array uint length9 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length9); msField.WriteTo(stream); // Key for field: 10, LengthDelimited stream.WriteByte(82); msField.SetLength(0); LocalFeatureTest.StructTest.Serialize(msField, instance.MyStruct); // Length delimited byte array uint length10 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length10); msField.WriteTo(stream); // Key for field: 11, LengthDelimited stream.WriteByte(90); msField.SetLength(0); TestB.ExternalStructSerializer.Serialize(msField, instance.MyExtStruct); // Length delimited byte array uint length11 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length11); msField.WriteTo(stream); if (instance.MyExtClass != null) { // Key for field: 12, LengthDelimited stream.WriteByte(98); msField.SetLength(0); TestB.ExternalClassSerializer.Serialize(msField, instance.MyExtClass); // Length delimited byte array uint length12 = (uint)msField.Length; global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt32(stream, length12); msField.WriteTo(stream); } // Key for field: 13, Varint stream.WriteByte(104); global::SilentOrbit.ProtocolBuffers.ProtocolParser.WriteUInt64(stream,(ulong)instance.MyEnum); global::SilentOrbit.ProtocolBuffers.ProtocolParser.Stack.Push(msField); }
/// <summary> /// Test wire format of the person example against protobuf-net - another c# protocol buffers library /// </summary> public static void Run() { LocalFeatures local = new LocalFeatures("139pt2m7"); local.Uptime = TimeSpan.FromHours(37.8); local.DueDate = DateTime.Now.AddMinutes(1); local.Internal = "assembly"; local.PR = "Hi"; local.Amount = Math.E; local.Deny("they exist"); local.MyInterface = new MyImplementeInterface(); local.MyEnum = LocalFeatureTest.TopEnum.Last; MemoryStream ms1 = new MemoryStream(); LocalFeatures.Serialize(ms1, local); MemoryStream ms2 = new MemoryStream(ms1.ToArray()); LocalFeatures l2 = new LocalFeatures("Secret"); //Since this property is an interface AND required we must set it before calling Deserialize l2.MyInterface = new MyImplementeInterface(); LocalFeatures.Deserialize(ms2, l2); //Test in Equals to have access to all fields Test("Local Features", local.Equals(l2)); //Test preservation of unknown fields byte[] streamBuffer; byte[] streamBufferV2Orig; byte[] streamBufferV1Mod; MyMessageV2 v2original = MyMessageV2.TestInstance(); //Write using (MemoryStream ms = new MemoryStream()) { MyMessageV2.Serialize(ms, v2original); streamBuffer = ms.ToArray(); streamBufferV2Orig = streamBuffer; } //Read V1, modify and write back MyMessageV1 v1 = MyMessageV1.Deserialize(new MemoryStream(streamBuffer)); v1.FieldA = 42; using (MemoryStream ms = new MemoryStream()) { MyMessageV1.Serialize(ms, v1); streamBuffer = ms.ToArray(); streamBufferV1Mod = streamBuffer; } //Compare stream buffers //Test ( // "Stream buffer length", // streamBufferV2Orig.Length == streamBufferV1Mod.Length //); for (int n = 0; n < streamBufferV2Orig.Length; n++) { if (n == 1) { continue; //expected difference for FieldA } if (streamBufferV2Orig [n] != streamBufferV1Mod [n]) { throw new InvalidDataException("Stream buffers do not match at byte " + n); } } //Read V2 and test MyMessageV2 v2test = MyMessageV2.Deserialize(new MemoryStream(streamBuffer)); //Test FieldA Test("Modified in v1", v2test.FieldA == v1.FieldA); //Restore and test entire v2 v2test.FieldA = v2original.FieldA; Test("MyMessageV2 WriteRead", v2original.Equals(v2test)); }