static void Main() { // Define root node for a tree of strings var root = new Node <string> { data = "root" }; root.left = new Node <string> { data = "root/left" }; root.right = new Node <string> { data = "root/right" }; root.left.left = new Node <string> { data = "root/left/left" }; root.left.left.right = new Node <string> { data = "root/left/left/right" }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output); Marshal.To(writer, root); var tree = Unmarshal <Node <string> > .From(output.Data); Debug.Assert(Comparer.Equal(root, tree)); }
private void DispatchResponse(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData) { IMessage response; if (headers.error_code != (int)ErrorCode.OK) { response = Message.FromError(Unmarshal <Error> .From(payload)); } else { response = Message.FromPayload(Unmarshal.From(payload)); } var receiveContext = new EpoxyReceiveContext(this); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); Error layerError = LayerStackUtils.ProcessOnReceive(parentTransport.LayerStack, MessageType.Response, receiveContext, bondedLayerData); if (layerError != null) { Log.Error("{0}.{1}: Receiving response {2}/{3} failed due to layer error (Code: {4}, Message: {5}).", this, nameof(DispatchResponse), headers.conversation_id, headers.method_name, layerError.error_code, layerError.message); response = Message.FromError(layerError); } if (!responseMap.Complete(headers.conversation_id, response)) { Log.Error("{0}.{1}: Response for unmatched request. Conversation ID: {2}", this, nameof(DispatchResponse), headers.conversation_id); } }
private void DispatchEvent(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData) { if (headers.error_code != (int)ErrorCode.OK) { Log.Error("{0}.{1}: Received event with a non-zero error code. Conversation ID: {2}", this, nameof(DispatchEvent), headers.conversation_id); return; } IMessage request = Message.FromPayload(Unmarshal.From(payload)); var receiveContext = new EpoxyReceiveContext(this); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); Error layerError = LayerStackUtils.ProcessOnReceive(parentTransport.LayerStack, MessageType.Event, receiveContext, bondedLayerData); if (layerError != null) { Log.Error("{0}.{1}: Receiving event {2}/{3} failed due to layer error (Code: {4}, Message: {5}).", this, nameof(DispatchEvent), headers.conversation_id, headers.method_name, layerError.error_code, layerError.message); return; } Task.Run(async() => { await serviceHost.DispatchEvent(headers.method_name, receiveContext, request, connectionMetrics); }); }
static void Subscriber() { var ctx = new ZContext(); var subscriber = new ZSocket(ctx, ZSocketType.SUB); subscriber.Connect("tcp://127.0.0.1:12345"); subscriber.SubscribeAll(); for (;;) { var received = subscriber.ReceiveFrame(); // INCORRECT // var str = received.ReadString(); // var byteArr = Encoding.ASCII.GetBytes(str); var byteArr = received.Read(); var arrSeg = new ArraySegment <byte>(byteArr); // There's an InputBuffer ctor that takes a byte[] directly var input = new InputBuffer(arrSeg); var dst = Unmarshal <Record> .From(input); foreach (var kvp in dst.payload) { Console.WriteLine("{0} {1}", kvp.Key, kvp.Value); } } }
private State?DispatchRequest(EpoxyHeaders headers, EpoxyProtocol.MessageData messageData, ArraySegment <byte> layerData) { Task.Run(async() => { var totalTime = Stopwatch.StartNew(); var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics); var receiveContext = new EpoxyReceiveContext(this, ConnectionMetrics, requestMetrics); ILayerStack layerStack = null; IMessage result; if (messageData.IsError) { logger.Site().Error("{0} Received request with an error message. Only payload messages are allowed. Conversation ID: {1}", this, headers.conversation_id); result = Message.FromError(new Error { error_code = (int)ErrorCode.INVALID_INVOCATION, message = "Received request with an error message" }); } else { IMessage request = Message.FromPayload(Unmarshal.From(messageData.Data)); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); Error layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack); if (layerError == null) { layerError = LayerStackUtils.ProcessOnReceive( layerStack, MessageType.REQUEST, receiveContext, bondedLayerData, logger); } if (layerError == null) { result = await serviceHost.DispatchRequest(headers.service_name, headers.method_name, receiveContext, request); } else { logger.Site().Error("{0} Receiving request {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).", this, headers.conversation_id, headers.service_name, headers.method_name, layerError.error_code, layerError.message); // Set layer error as result of this Bond method call and do not dispatch to method. // Since this error will be returned to client, cleanse out internal server error details, if any. result = Message.FromError(Errors.CleanseInternalServerError(layerError)); } } await SendReplyAsync(headers.conversation_id, result, layerStack, requestMetrics); Metrics.FinishRequestMetrics(requestMetrics, totalTime); metrics.Emit(requestMetrics); }); // no state change needed return(null); }
static void Main() { var obj = new Struct { n = 0x1000, str = "test", items = { 3.14, 0 } }; // Protocols may have different versions with different features. // When serializing/deserializing the same version needs to be used. // // Marshaling can be used to embed the protocol and version in the // payload so the reading side can automatically determine which // protocol and version to use. { // Here, we use Compact Binary v1. var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output, version: 1); Serialize.To(writer, obj); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader <InputBuffer>(input, version: 1); var obj2 = Deserialize <Struct> .From(reader); ThrowIfFalse(Comparer.Equal(obj, obj2)); } { // Here, we use Compact Binary v2. var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output, version: 2); Serialize.To(writer, obj); var input = new InputBuffer(output.Data); var reader = new CompactBinaryReader <InputBuffer>(input, version: 2); var obj2 = Deserialize <Struct> .From(reader); ThrowIfFalse(Comparer.Equal(obj, obj2)); } { // Here, we Marshal to Compact Binary v2. var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output, version: 2); Marshal.To(writer, obj); var input = new InputBuffer(output.Data); // The protocol and version are determined from the payload // itself. var obj2 = Unmarshal <Struct> .From(input); ThrowIfFalse(Comparer.Equal(obj, obj2)); } }
IBonded CreateBondedTestData(string value) { var realLayerData = new Dummy { string_value = value }; var outputBuffer = new OutputBuffer(20); var compactWriter = new CompactBinaryWriter <OutputBuffer>(outputBuffer); Marshal.To <CompactBinaryWriter <OutputBuffer>, Dummy>(compactWriter, realLayerData); return(Unmarshal.From(outputBuffer.Data)); }
private State?DispatchRequest(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData) { if (headers.error_code != (int)ErrorCode.OK) { logger.Site().Error("{0} Received request with a non-zero error code. Conversation ID: {1}", this, headers.conversation_id); protocolError = ProtocolErrorCode.PROTOCOL_VIOLATED; return(State.SendProtocolError); } Task.Run(async() => { var totalTime = Stopwatch.StartNew(); var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics); var receiveContext = new EpoxyReceiveContext(this, ConnectionMetrics, requestMetrics); IMessage request = Message.FromPayload(Unmarshal.From(payload)); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); ILayerStack layerStack; Error layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack); if (layerError == null) { layerError = LayerStackUtils.ProcessOnReceive( layerStack, MessageType.Request, receiveContext, bondedLayerData, logger); } IMessage result; if (layerError == null) { result = await serviceHost.DispatchRequest(headers.method_name, receiveContext, request); } else { logger.Site().Error("{0} Receiving request {1}/{2} failed due to layer error (Code: {3}, Message: {4}).", this, headers.conversation_id, headers.method_name, layerError.error_code, layerError.message); // Set layer error as result of this Bond method call and do not dispatch to method. // Since this error will be returned to client, cleanse out internal server error details, if any. result = Message.FromError(Errors.CleanseInternalServerError(layerError)); } await SendReplyAsync(headers.conversation_id, result, layerStack, requestMetrics); Metrics.FinishRequestMetrics(requestMetrics, totalTime); metrics.Emit(requestMetrics); }); // no state change needed return(null); }
void DispatchEvent(EpoxyHeaders headers, EpoxyProtocol.MessageData messageData, ArraySegment <byte> layerData) { if (messageData.IsError) { logger.Site().Error( "{0} Received event with an error message. Only payload messages are allowed. Conversation ID: {1}", this, headers.conversation_id); return; } Task.Run( async() => { IMessage request = Message.FromPayload(Unmarshal.From(messageData.Data)); var totalTime = Stopwatch.StartNew(); var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics); var receiveContext = new RelayEpoxyReceiveContext(this, ConnectionMetrics, requestMetrics); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); ILayerStack layerStack; Error layerError = parentTransport.GetLayerStack(requestMetrics.request_id, out layerStack); if (layerError == null) { layerError = LayerStackUtils.ProcessOnReceive( layerStack, MessageType.EVENT, receiveContext, bondedLayerData, logger); } if (layerError != null) { logger.Site().Error( "{0}: Receiving event {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).", this, headers.conversation_id, headers.service_name, headers.method_name, layerError.error_code, layerError.message); return; } await serviceHost.DispatchEvent(headers.service_name, headers.method_name, receiveContext, request); Metrics.FinishRequestMetrics(requestMetrics, totalTime); metrics.Emit(requestMetrics); }); }
void DispatchResponse(EpoxyHeaders headers, EpoxyProtocol.MessageData messageData, ArraySegment <byte> layerData) { IMessage response = messageData.IsError ? Message.FromError(Unmarshal <Error> .From(messageData.Data)) : Message.FromPayload(Unmarshal.From(messageData.Data)); TaskCompletionSource <IMessage> tcs = responseMap.TakeTaskCompletionSource(headers.conversation_id); if (tcs == null) { logger.Site().Error( "{0} Response for unmatched request. Conversation ID: {1}", this, headers.conversation_id); return; } Task.Run( () => { var totalTime = Stopwatch.StartNew(); var requestMetrics = Metrics.StartRequestMetrics(ConnectionMetrics); var receiveContext = new RelayEpoxyReceiveContext(this, ConnectionMetrics, requestMetrics); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); ILayerStack layerStack = tcs.Task.AsyncState as ILayerStack; Error layerError = LayerStackUtils.ProcessOnReceive(layerStack, MessageType.RESPONSE, receiveContext, bondedLayerData, logger); if (layerError != null) { logger.Site().Error( "{0} Receiving response {1}/{2}.{3} failed due to layer error (Code: {4}, Message: {5}).", this, headers.conversation_id, headers.service_name, headers.method_name, layerError.error_code, layerError.message); response = Message.FromError(layerError); } tcs.SetResult(response); Metrics.FinishRequestMetrics(requestMetrics, totalTime); metrics.Emit(requestMetrics); }); }
static void Main() { var v1 = new Struct_v1(); // Struct_v1 has a required field foo which by default is set to // 'nothing'. If we try to serialize object v1 w/o initializing // the field to some value Bond will throw an exception. try { Console.WriteLine("Serializing v1... "); Marshal(v1); } catch (Exception ex) { Console.WriteLine(ex); } // Initialize field by assigning a value to it... v1.foo = 10; // ... or for complex fields create a new instance of the // appropriate C# type. v1.baz = new LinkedList <string>(); v1.baz.AddLast("test1"); v1.baz.AddLast("test2"); // We can also set a field to 'nothing' by assigning null to the // field itself. Optional fields that are set to 'nothing' are // omitted when object is serialized. v1.baz = null; ArraySegment <byte> buffer = Marshal(v1); // Deserialize the payload into object of type Struct_v2 Struct_v2 v2 = Unmarshal <Struct_v2> .From(buffer); // Struct_v2 has an optional field bar, which didn't exist in // Struct_v1. It is initialized to 'nothing' by default. By // checking if the field is 'nothing' (== null) after // de-serialization we can detect if it was present in the // payload or not. Debug.Assert(v2.bar == null); Debug.Assert(v2.baz == null); }
private void DispatchResponse(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData) { IMessage response; if (headers.error_code != (int)ErrorCode.OK) { response = Message.FromError(Unmarshal <Error> .From(payload)); } else { response = Message.FromPayload(Unmarshal.From(payload)); } TaskCompletionSource <IMessage> tcs = responseMap.TakeTaskCompletionSource(headers.conversation_id); if (tcs == null) { logger.Site().Error("{0} Response for unmatched request. Conversation ID: {1}", this, headers.conversation_id); return; } Task.Run(() => { var receiveContext = new EpoxyReceiveContext(this); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); ILayerStack layerStack = tcs.Task.AsyncState as ILayerStack; Error layerError = LayerStackUtils.ProcessOnReceive(layerStack, MessageType.Response, receiveContext, bondedLayerData, logger); if (layerError != null) { logger.Site().Error("{0} Receiving response {1}/{2} failed due to layer error (Code: {3}, Message: {4}).", this, headers.conversation_id, headers.method_name, layerError.error_code, layerError.message); response = Message.FromError(layerError); } tcs.SetResult(response); }); }
static void Main() { var example = new Example { num = 42, str = "test", items = { 3.14, 0 } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output); Marshal.To(writer, example); var input = new InputBuffer(output.Data); ExampleView view = Unmarshal <ExampleView> .From(input); Debug.Assert(example.num == view.num); Debug.Assert(example.str.Equals(view.str, StringComparison.Ordinal)); }
private State?DispatchRequest(EpoxyHeaders headers, ArraySegment <byte> payload, ArraySegment <byte> layerData) { if (headers.error_code != (int)ErrorCode.OK) { Log.Error("{0}.{1}: Received request with a non-zero error code. Conversation ID: {2}", this, nameof(DispatchRequest), headers.conversation_id); protocolError = ProtocolErrorCode.PROTOCOL_VIOLATED; return(State.SendProtocolError); } IMessage request = Message.FromPayload(Unmarshal.From(payload)); var receiveContext = new EpoxyReceiveContext(this); IBonded bondedLayerData = (layerData.Array == null) ? null : Unmarshal.From(layerData); Error layerError = LayerStackUtils.ProcessOnReceive(parentTransport.LayerStack, MessageType.Request, receiveContext, bondedLayerData); Task.Run(async() => { IMessage result; if (layerError == null) { result = await serviceHost.DispatchRequest(headers.method_name, receiveContext, request, connectionMetrics); } else { Log.Error("{0}.{1}: Receiving request {2}/{3} failed due to layer error (Code: {4}, Message: {5}).", this, nameof(DispatchRequest), headers.conversation_id, headers.method_name, layerError.error_code, layerError.message); result = Message.FromError(layerError); } await SendReplyAsync(headers.conversation_id, result); }); // no state change needed return(null); }
static void Main() { var src = new Example { Name = "foo", Constants = { 3.14, 6.28 } }; var output = new OutputBuffer(); var writer = new CompactBinaryWriter <OutputBuffer>(output); Marshal.To(writer, src); var input = new InputBuffer(output.Data); // We don't need to specify protocol for unmarshaling, // it is determined from information stored in the payload. var dst = Unmarshal <Example> .From(input); Debug.Assert(Comparer.Equal(src, dst)); }
public static void AllSerializeDeserialize <From, To>(From from, bool noTranscoding = false) where From : class where To : class { RoundtripMemory <From, To> memoryRoundtrip = (serialize, deserialize) => { var data = serialize(from); var to = deserialize(data); Assert.IsTrue(from.IsEqual(to)); }; RoundtripPointer <From, To> pointerRoundtrip = (serialize, deserialize) => { var ptr = RMarshal.AllocHGlobal(UnsafeBufferSize); var data = serialize(from, ptr, UnsafeBufferSize); var to = deserialize(data, UnsafeBufferSize); Assert.IsTrue(from.IsEqual(to)); RMarshal.FreeHGlobal(data); }; RoundtripMemoryPointer <From, To> memoryPointerRoundtrip = (serialize, deserialize) => { var data = serialize(from); var pinned = GCHandle.Alloc(data.Array, GCHandleType.Pinned); var to = deserialize(RMarshal.UnsafeAddrOfPinnedArrayElement(data.Array, data.Offset), data.Count); Assert.IsTrue(from.IsEqual(to)); pinned.Free(); }; RoundtripStream <From, To> streamRoundtrip = (serialize, deserialize) => { var stream = new MemoryStream(); serialize(from, stream); stream.Position = 0; var to = deserialize(stream); Assert.IsTrue(from.IsEqual(to)); }; MarshalStream <From> streamMarshal = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal <To> .From(new InputStream(stream))); }); MarshalStream <From> streamMarshalSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream), Schema <From> .RuntimeSchema).Deserialize <To>()); }); MarshalStream <From> streamMarshalNoSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream)).Deserialize <To>()); }); MarshalMemory <From> memoryMarshal = serialize => memoryRoundtrip(serialize, Unmarshal <To> .From); TranscodeStream <From, To> streamTranscode = (serialize, transcode, deserialize) => streamRoundtrip((obj, stream) => { using (var tmp = new MemoryStream()) { serialize(obj, tmp); tmp.Position = 0; transcode(tmp, stream); } }, deserialize); if (noTranscoding) { streamTranscode = (serialize, transcode, deserialize) => { } } ; // Compact Binary streamRoundtrip(SerializeCB, DeserializeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeUnsafeCB <To>); memoryPointerRoundtrip(SerializeUnsafeCB, DeserializePointerCB <To>); pointerRoundtrip(SerializePointerCB, DeserializePointerCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeUnsafeCB <To>); memoryPointerRoundtrip(SerializeSafeCB, DeserializePointerCB <To>); memoryRoundtrip(SerializeSafeCBNoInlining, DeserializeSafeCB <To>); streamMarshal(MarshalCB); streamMarshal(SerializerMarshalCB); streamMarshalSchema(MarshalCB); streamMarshalNoSchema(MarshalCB); memoryMarshal(MarshalCB); streamTranscode(SerializeCB, TranscodeCBCB, DeserializeCB <To>); streamTranscode(SerializeCB, TranscodeCBFB, DeserializeFB <To>); streamRoundtrip(SerializeCB, stream => { var input = new InputStream(stream); var reader = new CompactBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Fast Binary streamRoundtrip(SerializeFB, DeserializeFB <To>); memoryRoundtrip(SerializeFB, DeserializeSafeFB <To>); memoryRoundtrip(SerializeFB, DeserializeUnsafeFB <To>); memoryPointerRoundtrip(SerializeFB, DeserializePointerFB <To>); streamMarshal(MarshalFB); streamMarshal(SerializerMarshalFB); streamMarshalSchema(MarshalFB); streamMarshalNoSchema(MarshalFB); memoryMarshal(MarshalFB); streamTranscode(SerializeFB, TranscodeFBFB, DeserializeFB <To>); streamTranscode(SerializeFB, TranscodeFBCB, DeserializeCB <To>); streamRoundtrip(SerializeFB, stream => { var input = new InputStream(stream); var reader = new FastBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Simple doesn't support omitting fields if (typeof(From) != typeof(Nothing) && typeof(From) != typeof(GenericsWithNothing)) { streamRoundtrip(SerializeSP, DeserializeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeSafeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeUnsafeSP <From, To>); memoryPointerRoundtrip(SerializeSP, DeserializePointerSP <From, To>); streamRoundtrip(SerializeSP2, DeserializeSP2 <From, To>); memoryRoundtrip(SerializeSP2, DeserializeSafeSP2 <From, To>); memoryRoundtrip(SerializeSP2, DeserializeUnsafeSP2 <From, To>); streamTranscode(SerializeCB, TranscodeCBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeFB, TranscodeFBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPCB <From>, DeserializeCB <To>); streamTranscode(SerializeSP, TranscodeSPFB <From>, DeserializeFB <To>); // Pull parser doesn't supprot bonded<T> if (AnyField <From>(Reflection.IsBonded)) { streamTranscode(SerializeSP, TranscodeSPXml <From>, DeserializeXml <To>); // NewtonSoft JSON doesn't support uint64 if (typeof(From) != typeof(MaxUInt64)) { streamTranscode(SerializeSP, TranscodeSPJson <From>, DeserializeJson <To>); } } streamRoundtrip(SerializeSP, stream => { var input = new InputStream(stream); var reader = new SimpleBinaryReader <InputStream>(input); return(DeserializeUntagged <From, To>(reader)); }); streamMarshalSchema(MarshalSP); } // Pull parser doesn't supprot bonded<T> if (AnyField <From>(Reflection.IsBonded)) { streamRoundtrip(SerializeXml, DeserializeXml <To>); streamTranscode(SerializeCB, TranscodeCBXml <From>, DeserializeXml <To>); streamTranscode(SerializeFB, TranscodeFBXml <From>, DeserializeXml <To>); // NewtonSoft JSON doesn't support uint64 if (typeof(From) != typeof(MaxUInt64)) { streamRoundtrip(SerializeJson, DeserializeJson <To>); streamTranscode(SerializeCB, TranscodeCBJson <From>, DeserializeJson <To>); streamTranscode(SerializeFB, TranscodeFBJson <From>, DeserializeJson <To>); } } } delegate bool TypePredicate(Type field);
static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("Usage:\nBond.CompatibilityTest json|compact|compact2|fast|simple|simple2|schema input_file output_file [json|compact|fast|simple|simple2]"); return; } var fromProtocol = args[0]; var toProtocol = fromProtocol; var inputFile = args[1]; var outputFile = args[2]; if (args.Length == 4) { toProtocol = args[3]; } using (var inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read)) { var input = new InputStream(inputStream); using (var outputStream = new FileStream(outputFile, FileMode.Create)) { var output = new OutputStream(outputStream); if (fromProtocol == "json") { var reader = new SimpleJsonReader(inputStream); var writer = new SimpleJsonWriter(outputStream); var transcoder = new Transcoder <SimpleJsonReader, SimpleJsonWriter>(Schema <Compat> .RuntimeSchema); transcoder.Transcode(reader, writer); writer.Flush(); } else if (fromProtocol == "compact") { var reader = new CompactBinaryReader <InputStream>(input); Write(reader, output, toProtocol); } else if (fromProtocol == "compact2") { var reader = new CompactBinaryReader <InputStream>(input, 2); Write(reader, output, toProtocol); } else if (fromProtocol == "fast") { var reader = new FastBinaryReader <InputStream>(input, 2); Write(reader, output, toProtocol); } else if (fromProtocol == "simple") { var reader = new SimpleBinaryReader <InputStream>(input); Write(reader, output, toProtocol); } else if (fromProtocol == "simple2") { var reader = new SimpleBinaryReader <InputStream>(input, 2); Write(reader, output, toProtocol); } else if (fromProtocol == "schema") { var schema = Unmarshal <SchemaDef> .From(input); if (!Comparer.Equal(schema, Schema <Compat> .RuntimeSchema.SchemaDef)) { Console.WriteLine("SchemaDef is different"); } var writer = new CompactBinaryWriter <OutputStream>(output); Marshal.To(writer, Schema <Compat> .RuntimeSchema.SchemaDef); output.Flush(); } else { Console.WriteLine("Unsupported input protocol {0}", fromProtocol); } } } }
public static void AllSerializeDeserialize <From, To>(From from, bool noTranscoding = false) where From : class where To : class { RoundtripMemory <From, To> memoryRoundtrip = (serialize, deserialize) => { var data = serialize(from); var to = deserialize(data); Assert.IsTrue(from.IsEqual(to)); }; RoundtripStream <From, To> streamRoundtrip = (serialize, deserialize) => { var stream = new MemoryStream(); serialize(from, stream); stream.Position = 0; var to = deserialize(stream); Assert.IsTrue(from.IsEqual(to)); }; MarshalStream <From> streamMarshal = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal <To> .From(new InputStream(stream))); }); MarshalStream <From> streamMarshalSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream), Schema <From> .RuntimeSchema).Deserialize <To>()); }); MarshalStream <From> streamMarshalNoSchema = serialize => streamRoundtrip(serialize, stream => { stream.Position = 0; return(Unmarshal.From(new InputStream(stream)).Deserialize <To>()); }); MarshalMemory <From> memoryMarshal = serialize => memoryRoundtrip(serialize, Unmarshal <To> .From); TranscodeStream <From, To> streamTranscode = (serialize, transcode, deserialize) => streamRoundtrip((obj, stream) => { using (var tmp = new MemoryStream()) { serialize(obj, tmp); tmp.Position = 0; transcode(tmp, stream); } }, deserialize); if (noTranscoding) { streamTranscode = (serialize, transcode, deserialize) => { } } ; // Compact Binary streamRoundtrip(SerializeCB, DeserializeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeUnsafeCB, DeserializeUnsafeCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeSafeCB <To>); memoryRoundtrip(SerializeSafeCB, DeserializeUnsafeCB <To>); streamMarshal(MarshalCB); streamMarshal(SerializerMarshalCB); streamMarshalSchema(MarshalCB); streamMarshalNoSchema(MarshalCB); memoryMarshal(MarshalCB); streamTranscode(SerializeCB, TranscodeCBCB, DeserializeCB <To>); streamTranscode(SerializeCB, TranscodeCBFB, DeserializeFB <To>); streamRoundtrip(SerializeCB, stream => { var input = new InputStream(stream); var reader = new CompactBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Fast Binary streamRoundtrip(SerializeFB, DeserializeFB <To>); memoryRoundtrip(SerializeFB, DeserializeSafeFB <To>); memoryRoundtrip(SerializeFB, DeserializeUnsafeFB <To>); streamMarshal(MarshalFB); streamMarshal(SerializerMarshalFB); streamMarshalSchema(MarshalFB); streamMarshalNoSchema(MarshalFB); memoryMarshal(MarshalFB); streamTranscode(SerializeFB, TranscodeFBFB, DeserializeFB <To>); streamTranscode(SerializeFB, TranscodeFBCB, DeserializeCB <To>); streamRoundtrip(SerializeFB, stream => { var input = new InputStream(stream); var reader = new FastBinaryReader <InputStream>(input); return(DeserializeTagged <To>(reader)); }); // Simple doesn't support omitting fields if (typeof(From) != typeof(Nothing) && typeof(From) != typeof(GenericsWithNothing)) { streamRoundtrip(SerializeSP, DeserializeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeSafeSP <From, To>); memoryRoundtrip(SerializeSP, DeserializeUnsafeSP <From, To>); streamTranscode(SerializeCB, TranscodeCBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeFB, TranscodeFBSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPSP <From>, DeserializeSP <From, To>); streamTranscode(SerializeSP, TranscodeSPCB <From>, DeserializeCB <To>); streamTranscode(SerializeSP, TranscodeSPFB <From>, DeserializeFB <To>); streamTranscode(SerializeSP, TranscodeSPXml <From>, DeserializeXml <To>); streamTranscode(SerializeSP, TranscodeSPJson <From>, DeserializeJson <To>); streamRoundtrip(SerializeSP, stream => { var input = new InputStream(stream); var reader = new SimpleBinaryReader <InputStream>(input); return(DeserializeUntagged <From, To>(reader)); }); streamMarshalSchema(MarshalSP); } streamRoundtrip(SerializeXml, DeserializeXml <To>); streamRoundtrip(SerializeJson, DeserializeJson <To>); streamTranscode(SerializeCB, TranscodeCBXml <From>, DeserializeXml <To>); streamTranscode(SerializeCB, TranscodeCBJson <From>, DeserializeJson <To>); streamTranscode(SerializeFB, TranscodeFBXml <From>, DeserializeXml <To>); streamTranscode(SerializeFB, TranscodeFBJson <From>, DeserializeJson <To>); } }