/
MessageContainerSerializer.cs
112 lines (96 loc) · 4.35 KB
/
MessageContainerSerializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//-----------------------------------------------------------------------
// <copyright file="MessageContainerSerializer.cs" company="Akka.NET Project">
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Linq;
using System.Runtime.Serialization;
using Akka.Actor;
using Akka.Remote.Serialization.Proto.Msg;
using Akka.Serialization;
using Akka.Util;
using Google.Protobuf;
namespace Akka.Remote.Serialization
{
/// <summary>
/// This is a special <see cref="Serializer"/> that serializes and deserializes <see cref="ActorSelectionMessage"/> only.
/// </summary>
public class MessageContainerSerializer : Serializer
{
private readonly WrappedPayloadSupport _payloadSupport;
/// <summary>
/// Initializes a new instance of the <see cref="MessageContainerSerializer"/> class.
/// </summary>
/// <param name="system">The actor system to associate with this serializer. </param>
public MessageContainerSerializer(ExtendedActorSystem system) : base(system)
{
_payloadSupport = new WrappedPayloadSupport(system);
}
/// <inheritdoc />
public override bool IncludeManifest => false;
/// <inheritdoc />
public override byte[] ToBinary(object obj)
{
if (obj is ActorSelectionMessage sel)
{
var envelope = new Proto.Msg.SelectionEnvelope();
envelope.Payload = _payloadSupport.PayloadToProto(sel.Message);
foreach (var element in sel.Elements)
{
Proto.Msg.Selection selection = null;
if (element is SelectChildName m1)
{
selection = BuildPattern(m1.Name, Proto.Msg.Selection.Types.PatternType.ChildName);
}
else if (element is SelectChildPattern m)
{
selection = BuildPattern(m.PatternStr, Proto.Msg.Selection.Types.PatternType.ChildPattern);
}
else if (element is SelectParent)
{
selection = BuildPattern(null, Proto.Msg.Selection.Types.PatternType.Parent);
}
envelope.Pattern.Add(selection);
}
return envelope.ToByteArray();
}
throw new ArgumentException($"Cannot serialize object of type [{obj.GetType().TypeQualifiedName()}]");
}
/// <inheritdoc />
public override object FromBinary(byte[] bytes, Type type)
{
var selectionEnvelope = Proto.Msg.SelectionEnvelope.Parser.ParseFrom(bytes);
var elements = new SelectionPathElement[selectionEnvelope.Pattern.Count];
for (var i = 0; i < selectionEnvelope.Pattern.Count; i++)
{
var p = selectionEnvelope.Pattern[i];
if (p.Type == Proto.Msg.Selection.Types.PatternType.ChildName)
elements[i] = new SelectChildName(p.Matcher);
if (p.Type == Proto.Msg.Selection.Types.PatternType.ChildPattern)
elements[i] = new SelectChildPattern(p.Matcher);
if (p.Type == Proto.Msg.Selection.Types.PatternType.Parent)
elements[i] = new SelectParent();
}
object message;
try
{
message = _payloadSupport.PayloadFrom(selectionEnvelope.Payload);
}
catch (Exception ex)
{
throw new SerializationException(
$"Failed to deserialize payload object when deserializing {nameof(ActorSelectionMessage)} addressed to [{string.Join(",", elements.Select(e => e.ToString()))}]", ex);
}
return new ActorSelectionMessage(message, elements);
}
private Proto.Msg.Selection BuildPattern(string matcher, Proto.Msg.Selection.Types.PatternType tpe)
{
var selection = new Proto.Msg.Selection { Type = tpe };
if (matcher != null)
selection.Matcher = matcher;
return selection;
}
}
}