forked from akkadotnet/akka.net
/
SerializationSupport.cs
233 lines (199 loc) · 7.85 KB
/
SerializationSupport.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//-----------------------------------------------------------------------
// <copyright file="SerializationSupport.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.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using Akka.Actor;
using Akka.DistributedData.Serialization.Proto.Msg;
using Akka.Serialization;
using Google.Protobuf;
using Address = Akka.Actor.Address;
using MemoryStream = System.IO.MemoryStream;
using UniqueAddress = Akka.Cluster.UniqueAddress;
namespace Akka.DistributedData.Serialization
{
/// <summary>
/// INTERNAL API.
///
/// Used to support the DData serializers.
/// </summary>
internal sealed class SerializationSupport
{
public SerializationSupport(ExtendedActorSystem system)
{
System = system;
}
private const int BufferSize = 1024 * 4;
public ExtendedActorSystem System { get; }
private volatile Akka.Serialization.Serialization _ser;
public Akka.Serialization.Serialization Serialization
{
get
{
if (_ser == null)
{
_ser = new Akka.Serialization.Serialization(System);
}
return _ser;
}
}
private volatile string _protocol;
public string AddressProtocol
{
get
{
if (_protocol == null)
_protocol = System.Provider.DefaultAddress.Protocol;
return _protocol;
}
}
private volatile Information _transportInfo;
public Information TransportInfo
{
get
{
if (_transportInfo == null)
{
var address = System.Provider.DefaultAddress;
_transportInfo = new Information(address, System);
}
return _transportInfo;
}
}
public static byte[] Compress(IMessage msg)
{
using (var memStream = new MemoryStream(BufferSize))
{
using (var gzip = new GZipStream(memStream, CompressionMode.Compress))
{
msg.WriteTo(gzip);
}
return memStream.ToArray();
}
}
public static byte[] Decompress(byte[] input)
{
using (var memStream = new MemoryStream())
{
using(var inputStr = new MemoryStream(input))
using (var gzipStream = new GZipStream(inputStr, CompressionMode.Decompress))
{
var buf = new byte[BufferSize];
while (gzipStream.CanRead)
{
var read = gzipStream.Read(buf, 0, BufferSize);
if (read > 0)
{
memStream.Write(buf, 0, read);
}
else
{
break;
}
}
}
return memStream.ToArray();
}
}
public static Proto.Msg.Address AddressToProto(Address address)
{
if (string.IsNullOrEmpty(address.Host) || !address.Port.HasValue)
throw new ArgumentOutOfRangeException(
$"Address [{address}] could not be serialized: host or port missing.");
return new Proto.Msg.Address() { Hostname = address.Host, Port = address.Port.Value };
}
public Address AddressFromProto(Proto.Msg.Address address)
{
return new Address(AddressProtocol, System.Name, address.Hostname, address.Port);
}
public static Proto.Msg.UniqueAddress UniqueAddressToProto(UniqueAddress address)
{
return new Proto.Msg.UniqueAddress() { Address = AddressToProto(address.Address), Uid = address.Uid };
}
public UniqueAddress UniqueAddressFromProto(Proto.Msg.UniqueAddress address)
{
return new UniqueAddress(AddressFromProto(address.Address), (int)address.Uid);
}
public static Proto.Msg.VersionVector VersionVectorToProto(VersionVector versionVector)
{
var b = new Proto.Msg.VersionVector();
using (var enumerator = versionVector.VersionEnumerator)
{
while (enumerator.MoveNext())
{
var (key, value) = enumerator.Current;
b.Entries.Add(new Proto.Msg.VersionVector.Types.Entry()
{
Node = UniqueAddressToProto(key),
Version = value
});
}
}
return b;
}
public VersionVector VersionVectorFromProto(Proto.Msg.VersionVector versionVector)
{
var entries = versionVector.Entries;
if (entries.Count == 0)
return VersionVector.Empty;
if (entries.Count == 1)
return new SingleVersionVector(UniqueAddressFromProto(versionVector.Entries[0].Node),
versionVector.Entries[0].Version);
var versions = entries.ToDictionary(x => UniqueAddressFromProto(x.Node), v => v.Version);
return new MultiVersionVector(versions);
}
public VersionVector VersionVectorFromBinary(byte[] bytes)
{
return VersionVectorFromProto(Proto.Msg.VersionVector.Parser.ParseFrom(bytes));
}
public IActorRef ResolveActorRef(string path)
{
return System.Provider.ResolveActorRef(path);
}
public Proto.Msg.OtherMessage OtherMessageToProto(object msg)
{
Proto.Msg.OtherMessage BuildOther()
{
var m = new OtherMessage();
var msgSerializer = Serialization.FindSerializerFor(msg);
m.SerializerId = msgSerializer.Identifier;
m.EnclosedMessage = ByteString.CopyFrom(msgSerializer.ToBinary(msg));
var ms = Akka.Serialization.Serialization.ManifestFor(msgSerializer, msg);
if (!string.IsNullOrEmpty(ms))
m.MessageManifest = ByteString.CopyFromUtf8(ms);
return m;
}
// Serialize actor references with full address information (defaultAddress).
// When sending remote messages currentTransportInformation is already set,
// but when serializing for digests or DurableStore it must be set here.
var oldInfo = Akka.Serialization.Serialization.CurrentTransportInformation;
try
{
if (oldInfo == null)
Akka.Serialization.Serialization.CurrentTransportInformation =
System.Provider.SerializationInformation;
return BuildOther();
}
finally
{
Akka.Serialization.Serialization.CurrentTransportInformation = oldInfo;
}
}
public object OtherMessageFromBytes(byte[] other)
{
return OtherMessageFromProto(OtherMessage.Parser.ParseFrom(other));
}
public object OtherMessageFromProto(Proto.Msg.OtherMessage other)
{
var manifest = other.MessageManifest != null ? other.MessageManifest.ToStringUtf8() : string.Empty;
return Serialization.Deserialize(other.EnclosedMessage.ToByteArray(), other.SerializerId, manifest);
}
}
}