Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created March 16, 2017 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuecc/04fc56e5224a398dac19de81e1c7be3d to your computer and use it in GitHub Desktop.
Save neuecc/04fc56e5224a398dac19de81e1c7be3d to your computer and use it in GitHub Desktop.
ActorPathFormatter.cs
internal static class ActorPathResolverGetFormatterHelper
{
// If type is concrete type, use type-formatter map
static readonly Dictionary<Type, object> FormatterMap = new Dictionary<Type, object>()
{
{typeof(ActorPath), new ActorPathFormatter<ActorPath>()},
{typeof(ChildActorPath), new ActorPathFormatter<ChildActorPath>()},
{typeof(RootActorPath), new ActorPathFormatter<RootActorPath>()}
};
internal static object GetFormatter(Type t)
{
object formatter;
if (FormatterMap.TryGetValue(t, out formatter))
{
return formatter;
}
// If type can not get, must return null for fallback mecanism.
return null;
}
}
public class ActorPathFormatter<T> : IMessagePackFormatter<T>
where T : ActorPath
{
public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver)
{
if (value == null)
{
return MessagePackBinary.WriteNil(ref bytes, offset);
}
var startOffset = offset;
offset += MessagePackBinary.WriteString(ref bytes, offset, value.ToSerializationFormat());
return offset - startOffset;
}
public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
{
if (MessagePackBinary.IsNil(bytes, offset))
{
readSize = 1;
return null;
}
var path = MessagePackBinary.ReadString(bytes, offset, out readSize);
ActorPath actorPath;
if (ActorPath.TryParse(path, out actorPath))
{
return (T)actorPath;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment