-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListener.cs
154 lines (144 loc) · 4.35 KB
/
Listener.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
namespace SimpleHttpListener
{
using System;
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
public class Listener : IDisposable
{
public static int FindFreePort(int min, bool random)
{
BitArray free = new BitArray(UInt16.MaxValue);
for (var i = min; i < UInt16.MaxValue; i++)
{
free.Set(i, true);
}
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
foreach (var i in properties.GetActiveTcpConnections())
{
free.Set(i.LocalEndPoint.Port, false);
}
foreach (var i in properties.GetActiveTcpListeners())
{
free.Set(i.Port, false);
}
foreach (var i in properties.GetActiveUdpListeners())
{
free.Set(i.Port, false);
}
if (random)
{
RandomNumberGenerator prng = RandomNumberGenerator.Create();
for (var i = 0; i < UInt16.MaxValue; i++)
{
byte[] b = new byte[2];
prng.GetNonZeroBytes(b);
int port = BitConverter.ToUInt16(b, 0);
if (port >= min && port < UInt16.MaxValue && free[port])
{
return port;
}
}
}
else
{
for (var i = min; i < UInt16.MaxValue; i++)
{
if (free[i])
{
return i;
}
}
}
throw new ArgumentException();
}
private readonly AtomicBool started = new AtomicBool();
private readonly AtomicBool closed = new AtomicBool();
private HttpListener _listener;
public Listener(Uri prefix, bool anyHost)
{
Prefix = prefix;
_listener = new HttpListener();
string s = Prefix.ToString();
if(!s.EndsWith("/"))
{
s += "/";
}
if(anyHost) {
UriBuilder u = new UriBuilder(s);
u.Host = "*";
_listener.Prefixes.Add(u.ToString());
} else {
_listener.Prefixes.Add(s);
}
}
public Uri Prefix { get; private set; }
internal Request ReadRequest(CancellationToken cancellationToken)
{
if(_listener == null)
{
return null;
}
if (closed)
{
return null;
}
if(!started.GetAndSet(true))
{
_listener.Start();
}
try
{
Task<HttpListenerContext> t = _listener.GetContextAsync();
t.Wait(cancellationToken);
if (t.IsCompleted && t.Result != null)
{
return new Request(this, t.Result);
}
}
catch (OperationCanceledException)
{
// ignore
}
catch (HttpListenerException)
{
// ignore
}
return null;
}
internal void Close()
{
if(!closed.GetAndSet(true))
{
_listener.Abort();
_listener.Close();
_listener = null;
}
}
public override string ToString()
{
return Prefix.ToString();
}
#region IDisposable Support
private bool disposedValue = false;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
Close();
}
disposedValue = true;
}
}
void IDisposable.Dispose()
{
Dispose(true);
}
#endregion
}
}