Convert objects that otherwise can't be serialized, to strings

This commit is contained in:
Yoshi Askharoun
2023-05-25 01:03:47 -05:00
parent e10a663714
commit b658bbc9fd
3 changed files with 42 additions and 3 deletions
@@ -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();
}
}
@@ -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<InterpreterEntry> InterpreterStep;
public event Action<string> 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;
@@ -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));
}