Created
March 16, 2017 05:33
-
-
Save neuecc/04fc56e5224a398dac19de81e1c7be3d to your computer and use it in GitHub Desktop.
ActorPathFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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