forked from kevinkinnett/BitSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeer.cs
191 lines (150 loc) · 5.46 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
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
using BitSharp.Common;
using NLog;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
namespace BitSharp.Network
{
public class Peer : IDisposable
{
public event Action<Peer, Exception> OnDisconnect;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly object objectLock = new object();
private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1);
private bool isDisposed;
private bool startedConnecting = false;
private readonly Socket socket;
private CountMeasure blockMissCountMeasure;
public Peer(IPEndPoint remoteEndPoint, bool isSeed, bool isIncoming)
{
RemoteEndPoint = remoteEndPoint;
IsSeed = isSeed;
IsIncoming = isIncoming;
this.socket = new Socket(remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Receiver = new RemoteReceiver(this, this.socket);
Sender = new RemoteSender(this, this.socket);
this.blockMissCountMeasure = new CountMeasure(TimeSpan.FromMinutes(10));
WireNode();
}
public Peer(Socket socket, bool isSeed, bool isIncoming)
{
this.socket = socket;
this.IsConnected = true;
IsIncoming = isIncoming;
LocalEndPoint = (IPEndPoint)socket.LocalEndPoint;
RemoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;
Receiver = new RemoteReceiver(this, this.socket);
Sender = new RemoteSender(this, this.socket);
this.blockMissCountMeasure = new CountMeasure(TimeSpan.FromMinutes(10));
WireNode();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed && disposing)
{
lock (this.objectLock)
{
if (this.isDisposed)
return;
UnwireNode();
this.Sender.Dispose();
this.socket.Dispose();
this.blockMissCountMeasure.Dispose();
this.semaphore.Dispose();
this.IsConnected = false;
this.isDisposed = true;
}
}
}
public IPEndPoint LocalEndPoint { get; private set; }
public IPEndPoint RemoteEndPoint { get; }
public RemoteReceiver Receiver { get; }
public RemoteSender Sender { get; }
public bool IsConnected { get; private set; }
public bool IsSeed { get; }
public bool IsIncoming { get; }
public int BlockMissCount
{
get
{
lock (this.objectLock)
{
if (!this.isDisposed)
return this.blockMissCountMeasure.GetCount();
else
return 0;
}
}
}
public void AddBlockMiss()
{
lock (this.objectLock)
{
if (!this.isDisposed)
this.blockMissCountMeasure.Tick();
}
}
public async Task ConnectAsync()
{
// take the lock to see if a connect can be started
lock (this.objectLock)
{
if (this.isDisposed)
return;
// don't connect if already connected, or started connecting elsewhere
if (this.IsConnected || this.startedConnecting)
return;
// indicate that connecting will be started
this.startedConnecting = true;
}
// start the connection
try
{
await Task.Factory.FromAsync(this.socket.BeginConnect(this.RemoteEndPoint, null, null), this.socket.EndConnect);
this.LocalEndPoint = (IPEndPoint)this.socket.LocalEndPoint;
this.IsConnected = true;
}
catch (Exception ex)
{
logger.Debug(ex, $"Error on connecting to {RemoteEndPoint}");
Disconnect(ex);
throw;
}
finally
{
// ensure started connecting flag is cleared
this.startedConnecting = false;
}
}
public void Disconnect(Exception ex = null)
{
this.OnDisconnect?.Invoke(this, ex);
this.Dispose();
}
private void WireNode()
{
this.Receiver.OnFailed += HandleFailed;
this.Sender.OnFailed += HandleFailed;
}
private void UnwireNode()
{
this.Receiver.OnFailed -= HandleFailed;
this.Sender.OnFailed -= HandleFailed;
}
private void HandleFailed(Peer peer, Exception ex)
{
if (ex != null)
logger.Debug(ex, $"Remote peer failed: {this.RemoteEndPoint}");
else
logger.Debug($"Remote peer failed: {this.RemoteEndPoint}");
Disconnect(ex);
}
}
}