From b658bbc9fdc21ea166f0d06ed98c26e4b8c187ed Mon Sep 17 00:00:00 2001 From: Yoshi Askharoun Date: Thu, 25 May 2023 01:02:40 -0500 Subject: [PATCH] Convert objects that otherwise can't be serialized, to strings --- .../Iris/Debug/FallbackSerializer.cs | 30 +++++++++++++++++++ UIX/Microsoft/Iris/Debug/ZmqDebuggerClient.cs | 7 +++-- UIX/Microsoft/Iris/Debug/ZmqDebuggerServer.cs | 8 ++++- 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 UIX/Microsoft/Iris/Debug/FallbackSerializer.cs diff --git a/UIX/Microsoft/Iris/Debug/FallbackSerializer.cs b/UIX/Microsoft/Iris/Debug/FallbackSerializer.cs new file mode 100644 index 0000000..da1a25c --- /dev/null +++ b/UIX/Microsoft/Iris/Debug/FallbackSerializer.cs @@ -0,0 +1,30 @@ +using System; +using System.Runtime.Serialization; + +namespace Microsoft.Iris.Debug; + +internal class FallbackSerializer : ISerializationSurrogate +{ + public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) + { + info.AddValue("ToString", obj?.ToString()); + } + + public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) + { + return info.GetString("ToString"); + } +} + +internal class FallbackSurrogateSelector : SurrogateSelector +{ + public override ISerializationSurrogate GetSurrogate(Type type, StreamingContext context, out ISurrogateSelector selector) + { + selector = this; + + if (type.Attributes.HasFlag(System.Reflection.TypeAttributes.Serializable)) + return null; + + return new FallbackSerializer(); + } +} \ No newline at end of file diff --git a/UIX/Microsoft/Iris/Debug/ZmqDebuggerClient.cs b/UIX/Microsoft/Iris/Debug/ZmqDebuggerClient.cs index c75a462..4417772 100644 --- a/UIX/Microsoft/Iris/Debug/ZmqDebuggerClient.cs +++ b/UIX/Microsoft/Iris/Debug/ZmqDebuggerClient.cs @@ -3,7 +3,7 @@ using NetMQ; using NetMQ.Sockets; using System; using System.IO; -using System.Runtime.Serialization.Formatters.Binary; +using System.Runtime.Serialization; using System.Text; namespace Microsoft.Iris.Debug; @@ -11,6 +11,7 @@ namespace Microsoft.Iris.Debug; public class ZmqDebuggerClient : IDebuggerClient, IDisposable { private readonly SubscriberSocket _subSocket; + private readonly IFormatter _formatter; public event EventHandler InterpreterStep; public event Action DispatcherStep; @@ -21,6 +22,8 @@ public class ZmqDebuggerClient : IDebuggerClient, IDisposable _subSocket.Connect(connectionUri); _subSocket.SubscribeToAnyTopic(); + _formatter = ZmqDebuggerServer.CreateFormatter(); + System.Threading.Thread th = new(MessageRecieveLoop); th.Start(); } @@ -38,7 +41,7 @@ public class ZmqDebuggerClient : IDebuggerClient, IDisposable case DebuggerMessageType.InterpreterOpCode: { using MemoryStream stream = new(bytes); - var entry = (InterpreterEntry)new BinaryFormatter().Deserialize(stream); + var entry = (InterpreterEntry)_formatter.Deserialize(stream); InterpreterStep?.Invoke(this, entry); } break; diff --git a/UIX/Microsoft/Iris/Debug/ZmqDebuggerServer.cs b/UIX/Microsoft/Iris/Debug/ZmqDebuggerServer.cs index 9d28c72..de44975 100644 --- a/UIX/Microsoft/Iris/Debug/ZmqDebuggerServer.cs +++ b/UIX/Microsoft/Iris/Debug/ZmqDebuggerServer.cs @@ -2,6 +2,7 @@ using NetMQ.Sockets; using System; using System.IO; +using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; @@ -13,19 +14,22 @@ internal class ZmqDebuggerServer : IDebuggerServer, IDisposable private readonly PublisherSocket _pubSocket; private readonly byte[][] _messageFrame = new byte[2][]; + private readonly IFormatter _formatter; public ZmqDebuggerServer(string connectionUri) { _pubSocket = new(); _pubSocket.Bind(connectionUri); + _formatter = CreateFormatter(); + Current = this; } public void LogInterpreterOpCode(object context, Data.InterpreterEntry entry) { using MemoryStream entryStream = new(); - new BinaryFormatter().Serialize(entryStream, entry); + _formatter.Serialize(entryStream, entry); SendDebuggerMessage(DebuggerMessageType.InterpreterOpCode, entryStream.ToArray()); } @@ -50,4 +54,6 @@ internal class ZmqDebuggerServer : IDebuggerServer, IDisposable _pubSocket.SendMultipartBytes(_messageFrame); } + + internal static IFormatter CreateFormatter() => new BinaryFormatter(new FallbackSurrogateSelector(), new(StreamingContextStates.Remoting)); }