-
Notifications
You must be signed in to change notification settings - Fork 141
/
Peer.cs
87 lines (78 loc) · 2.62 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
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
{
public Peer(
PublicKey publicKey,
int appProtocolVersion)
: this(publicKey, appProtocolVersion, null)
{
}
internal Peer(
PublicKey publicKey,
int appProtocolVersion,
IPAddress publicIPAddress)
{
PublicKey = publicKey ??
throw new ArgumentNullException(nameof(publicKey));
AppProtocolVersion = appProtocolVersion;
PublicIPAddress = publicIPAddress;
}
protected Peer(SerializationInfo info, StreamingContext context)
{
PublicKey = new PublicKey(info.GetValue<byte[]>("public_key"));
AppProtocolVersion = info.GetInt32("app_protocol_version");
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 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]
public IPAddress PublicIPAddress { get; }
/// <inheritdoc/>
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("public_key", PublicKey.Format(true));
info.AddValue("app_protocol_version", AppProtocolVersion);
info.AddValue("public_ip_address", PublicIPAddress?.ToString());
}
/// <inheritdoc/>
public override string ToString()
{
return $"{nameof(Peer)} {{ {nameof(Address)} = {Address} }}";
}
}
}