private static T RoundTrip <T>(IProtoBufSerializer serializer, T req) where T : IpcMessage { var sw = Stopwatch.StartNew(); var stream = new MemoryStream(); serializer.Serialize(stream, req); sw.Stop(); Trace.WriteLine(string.Format("Serialized message of type {0} into {1:n0} bytes in {2} msec.", req.Data.GetType().FullName, stream.Length, sw.ElapsedMilliseconds)); stream.Position = 0; sw.Restart(); var result = serializer.Deserialize(stream); sw.Stop(); Trace.WriteLine(string.Format("Deserialized message of type {0} from {1:n0} bytes in {2} msec.", req.Data.GetType().FullName, stream.Length, sw.ElapsedMilliseconds)); return((T)result); }
private T ReadMessage <T>() where T : IpcMessage, new() { lock (_readerLock) { try { return((T)_serializer.Deserialize(_stream)); } catch (Exception e) { if (IsSocketClosedException(e)) { Logger.LogInfo("Socket connection was closed -- assuming normal termination."); } else { Logger.LogError(e, "Error reading string from NetworkStream"); } return(null); } } }