/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="serviceName"></param> /// <param name="methodName"></param> /// <param name="outputPerformanceLog"></param> /// <returns></returns> private Marshaller <T> GetMarshaller <T>(string serviceName, string methodName, bool outputPerformanceLog) { Marshaller <T> marshaller = m_Settings.GetMarshallerFactoryOrDefault().GetMarshaller <T>(); if (!outputPerformanceLog) { return(marshaller); } string typeName = typeof(T).Name; return(new Marshaller <T>( delegate(T arg) { try { Stopwatch watch = Stopwatch.StartNew(); byte[] data = marshaller.Serializer(arg); if (m_Settings.PerformanceListener != null) { m_Settings.PerformanceListener.NotifySerialized(serviceName, methodName, typeName, GrpcPerformanceListener.GetMilliseconds(watch), data == null ? 0 : data.Length); } return data; } catch (Exception ex) { GrpcExceptionListener.NotifyCatchSerializerException(serviceName, methodName, typeof(T), ex); throw new GrpcSerializerException(ex.Message, ex, serviceName, methodName, typeName); } } , delegate(byte[] data) { try { Stopwatch watch = Stopwatch.StartNew(); T arg = marshaller.Deserializer(data); if (m_Settings.PerformanceListener != null) { m_Settings.PerformanceListener.NotifyDeserialized(serviceName, methodName, typeName, GrpcPerformanceListener.GetMilliseconds(watch), data == null ? 0 : data.Length); } return arg; } catch (Exception ex) { GrpcExceptionListener.NotifyCatchSerializerException(serviceName, methodName, typeof(T), ex); throw new GrpcSerializerException(ex.Message, ex, serviceName, methodName, typeName); } } )); }
public IObservable <bool> MoveNext() { return(inner.MoveNext().Do(x => { if (x) { this.Current = marshaller.Deserializer(inner.Current); } })); }
public async Task <bool> MoveNext(CancellationToken cancellationToken) { if (await inner.MoveNext(cancellationToken)) { this.Current = marshaller.Deserializer(inner.Current); return(true); } else { return(false); } }
public void SimpleSerializerEmulation() { Action <string, SerializationContext> contextualSerializer = (str, context) => { var bytes = System.Text.Encoding.UTF8.GetBytes(str); context.Complete(bytes); }; Func <DeserializationContext, string> contextualDeserializer = (context) => { return(System.Text.Encoding.UTF8.GetString(context.PayloadAsNewBuffer())); }; var marshaller = new Marshaller <string>(contextualSerializer, contextualDeserializer); Assert.AreSame(contextualSerializer, marshaller.ContextualSerializer); Assert.AreSame(contextualDeserializer, marshaller.ContextualDeserializer); Assert.Throws(typeof(NotImplementedException), () => marshaller.Serializer("abc")); Assert.Throws(typeof(NotImplementedException), () => marshaller.Deserializer(new byte[] { 1, 2, 3 })); }
public void SimpleSerializerEmulation() { Action <string, SerializationContext> contextualSerializer = (str, context) => { var bytes = System.Text.Encoding.UTF8.GetBytes(str); context.Complete(bytes); }; Func <DeserializationContext, string> contextualDeserializer = (context) => { return(System.Text.Encoding.UTF8.GetString(context.PayloadAsNewBuffer())); }; var marshaller = new Marshaller <string>(contextualSerializer, contextualDeserializer); Assert.AreSame(contextualSerializer, marshaller.ContextualSerializer); Assert.AreSame(contextualDeserializer, marshaller.ContextualDeserializer); // test that emulated serializer and deserializer work var origMsg = "abc"; var serialized = marshaller.Serializer(origMsg); Assert.AreEqual(origMsg, marshaller.Deserializer(serialized)); }
async Task <TResponse> Deserialize() { var bytes = await inner.ResponseAsync.ConfigureAwait(false); return(responseMarshaller.Deserializer(bytes)); }
public static TReceived GetData <TReceived>(InternalPacket packet, Marshaller <TReceived> marshaller) { return(marshaller.Deserializer(packet.Payload)); }