/
LeasedEndpoint.cs
130 lines (119 loc) · 4.31 KB
/
LeasedEndpoint.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Makaretu.Nat
{
/// <summary>
/// A public endpoint that is rented from a NAT.
/// </summary>
/// <remarks>
/// <see cref="NatClient.CreatePublicEndpointAsync"/> is used to create
/// a <see cref="Lease"/>.
/// <para>
/// A background task is created to renew the lease every
/// <see cref="Lease.Lifetime"/> / 2. Use <see cref="Dispose"/> to
/// cancel the task.
/// </para>
/// </remarks>
public class LeasedEndpoint : IPEndPoint, IDisposable
{
/// <summary>
/// Raised when the lease is renewed and the public address and/or port
/// changes.
/// </summary>
public event EventHandler<LeasedEndpointEventArgs> Changed;
CancellationTokenSource renewalCancellation = new CancellationTokenSource();
/// <summary>
/// Create a new instance of the <see cref="LeasedEndpoint"/> class
/// from the specified <see cref="Lease"/>.
/// </summary>
/// <param name="lease">
/// An agreement for a public endpoint.
/// </param>
/// <seealso cref="NatClient.CreatePublicEndpointAsync"/>
public LeasedEndpoint(Lease lease)
: base(lease.PublicAddress, lease.PublicPort)
{
Lease = lease;
var t = new Thread(Renewal)
{
IsBackground = true,
Priority = ThreadPriority.BelowNormal,
Name = "Leased endpoint renewal"
};
t.Start();
}
/// <summary>
/// The lease agreement.
/// </summary>
public Lease Lease { get; private set; }
/// <summary>
/// Stops the renewal process and deletes the public endpoint.
/// </summary>
/// <seealso cref="NatClient.DeletePublicEndpointAsync(Lease)"/>
public void Dispose()
{
if (renewalCancellation != null) {
var cancel = renewalCancellation;
renewalCancellation = null;
cancel.Cancel();
cancel.Dispose();
}
if (Lease != null)
{
var lease = Lease;
Lease = null;
if (lease.Nat != null)
{
lease.Nat.DeletePublicEndpointAsync(lease); // do not wait for task to complete!
}
}
}
async void Renewal()
{
var cancel = renewalCancellation.Token;
while (!cancel.IsCancellationRequested)
{
int nextRenewal = (int)Lease.Lifetime.TotalMilliseconds / 2;
for (; nextRenewal >= 250; nextRenewal /= 2)
{
Console.WriteLine($"renewing lease in {nextRenewal / 1000.0}s on {this}");
try
{
await Task.Delay(nextRenewal, cancel);
var nextLease = await Lease.Nat.RenewPublicEndpointAsync(Lease);
// Check for endpoint change.
if (nextLease.PublicAddress != Lease.PublicAddress ||
nextLease.PublicPort != Lease.PublicPort)
{
this.Address = nextLease.PublicAddress;
this.Port = nextLease.PublicPort;
Changed?.Invoke(this, new LeasedEndpointEventArgs
{
Previous = Lease,
Current = nextLease
});
}
Lease = nextLease;
break;
}
catch (TaskCanceledException)
{
Console.WriteLine($"cancelled lease on {this}");
return;
}
catch (Exception)
{
// keep on trucking
}
}
}
Console.WriteLine($"lease expired on {this}");
Dispose();
}
}
}