/
Peer.cs
139 lines (127 loc) · 4.59 KB
/
Peer.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
using System;
using System.Diagnostics.Contracts;
using System.Net;
using System.Runtime.Serialization;
using Libplanet.Crypto;
using Libplanet.Serialization;
namespace Libplanet.Net
{
/// <summary>
/// A representation of peer node.
/// </summary>
/// <seealso cref="Swarm{T}"/>
[Serializable]
[Equals]
public class Peer : ISerializable
{
/// <summary>
/// Initializes a new instance of the <see cref="Peer"/> class
/// with omitting <see cref="AppProtocolVersion"/>.
/// </summary>
/// <param name="publicKey">A <see cref="PublicKey"/> of the
/// <see cref="Peer"/>.</param>
/// <param name="endPoint">A <see cref="DnsEndPoint"/> consisting of the
/// host and port of the <see cref="Peer"/>.</param>
public Peer(
PublicKey publicKey,
DnsEndPoint endPoint)
: this(publicKey, endPoint, default(int?), null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Peer"/> class.
/// </summary>
/// <param name="publicKey">A <see cref="PublicKey"/> of the
/// <see cref="Peer"/>.</param>
/// <param name="endPoint">A <see cref="DnsEndPoint"/> consisting of the
/// host and port of the <see cref="Peer"/>.</param>
/// <param name="appProtocolVersion">An application protocol version
/// that the <see cref="Peer"/> is using.</param>
public Peer(
PublicKey publicKey,
DnsEndPoint endPoint,
int? appProtocolVersion)
: this(publicKey, endPoint, appProtocolVersion, null)
{
}
internal Peer(
PublicKey publicKey,
DnsEndPoint endPoint,
int? appProtocolVersion,
IPAddress publicIPAddress)
{
PublicKey = publicKey ??
throw new ArgumentNullException(nameof(publicKey));
EndPoint = endPoint ??
throw new ArgumentNullException(nameof(endPoint));
AppProtocolVersion = appProtocolVersion;
PublicIPAddress = publicIPAddress;
}
protected Peer(SerializationInfo info, StreamingContext context)
{
PublicKey = new PublicKey(info.GetValue<byte[]>("public_key"));
EndPoint = new DnsEndPoint(
info.GetString("end_point_host"),
info.GetInt32("end_point_port"));
AppProtocolVersion =
info.GetValueOrDefault<int?>("app_protocol_version", null);
string addressStr = info.GetString("public_ip_address");
if (addressStr != null)
{
PublicIPAddress = IPAddress.Parse(addressStr);
}
}
/// <summary>
/// The corresponding <see cref="Libplanet.Crypto.PublicKey"/> of
/// this peer.
/// </summary>
[Pure]
public PublicKey PublicKey { get; }
/// <summary>
/// The corresponding <see cref="DnsEndPoint"/> of this peer.
/// </summary>
[Pure]
public DnsEndPoint EndPoint { get; }
/// <summary>
/// The corresponding application protocol version of this peer.
/// </summary>
/// <seealso cref="Swarm{T}.DifferentVersionPeerEncountered"/>
[IgnoreDuringEquals]
[Pure]
public int? AppProtocolVersion { get; }
/// <summary>The peer's address which is derived from
/// its <see cref="PublicKey"/>.
/// </summary>
/// <seealso cref="PublicKey"/>
[IgnoreDuringEquals]
[Pure]
public Address Address => new Address(PublicKey);
[Pure]
internal IPAddress PublicIPAddress { get; }
/// <inheritdoc/>
public void GetObjectData(
SerializationInfo info,
StreamingContext context
)
{
info.AddValue("public_key", PublicKey.Format(true));
info.AddValue("end_point_host", EndPoint.Host);
info.AddValue("end_point_port", EndPoint.Port);
info.AddValue("app_protocol_version", AppProtocolVersion);
info.AddValue("public_ip_address", PublicIPAddress?.ToString());
}
/// <inheritdoc/>
public override string ToString()
{
return $"{Address}.{EndPoint}.{AppProtocolVersion}";
}
internal Peer WithAppProtocolVersion(int appProtocolVersion)
{
return new Peer(
PublicKey,
EndPoint,
appProtocolVersion,
PublicIPAddress);
}
}
}