-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpServiceBase.cs
171 lines (153 loc) · 5.18 KB
/
HttpServiceBase.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
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
#nullable enable
namespace IRWA
{
public abstract class HttpServiceBase
{
public const int DefaultThreadPoolSize = 3;
public const int ContextHandlerTerminationTimeout = 3000;
public const int WebServerShutdownTimeout = 3000;
private readonly int port;
private readonly X509Certificate? httpsCertificate;
private readonly ThreadPool contextHandlers;
private Thread? listenerThread = null;
private CancellationTokenSource? serverCts;
protected HttpServiceBase(int httpPort, ThreadPool? threadPool = null)
{
port = httpPort;
contextHandlers = threadPool
?? new ThreadPool(DefaultThreadPoolSize);
}
protected HttpServiceBase(int httpsPort, X509Certificate httpsCertificate,
ThreadPool? threadPool = null) : this(httpsPort, threadPool)
{
this.httpsCertificate = httpsCertificate;
}
public void Start()
{
Stop();
contextHandlers.Start();
serverCts = new CancellationTokenSource();
listenerThread = new Thread(RunListener);
listenerThread.Start();
}
public void Stop()
{
serverCts?.Cancel();
if (listenerThread != null)
{
if (!listenerThread.Join(WebServerShutdownTimeout))
{
listenerThread.Abort();
}
listenerThread = null;
}
contextHandlers.Stop();
}
protected abstract void HandleClient(HttpListenerContext context);
private void RunListener()
{
if (serverCts == null)
{
return;
}
var token = serverCts.Token;
while (!token.IsCancellationRequested)
{
if (TryStartListener(out var listener))
{
var listenerStoppedEvent = new AutoResetEvent(false);
var contextRetrievalThread = new Thread(() =>
{
GetAndHandleContexts(listener, token);
listenerStoppedEvent.Set();
});
contextRetrievalThread.Start();
WaitHandle.WaitAny(new WaitHandle[]
{
token.WaitHandle, listenerStoppedEvent
});
try
{
listener.Stop();
}
catch
{
// No specific error handling required.
}
if (!contextRetrievalThread.Join(WebServerShutdownTimeout))
{
contextRetrievalThread.Abort();
}
}
}
}
private bool TryStartListener(out HttpListener listener)
{
try
{
listener = new HttpListener(httpsCertificate != null ?
"https" : "http", port);
listener.HttpsCert = httpsCertificate;
listener.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11;
listener.Start();
if (listener.IsListening)
{
return true;
}
else
{
throw new Exception();
}
}
catch
{
listener = null!;
return false;
}
}
private void GetAndHandleContexts(HttpListener listener,
CancellationToken cancellationToken)
{
while (listener.IsListening &&
!cancellationToken.IsCancellationRequested)
{
try
{
var context = listener.GetContext();
if (context != null)
{
contextHandlers.RunAsync(() =>
{
try
{
HandleClient(context);
context.Response.Close();
}
catch
{
// No specific error handling required.
}
finally
{
context.Close();
}
});
}
else
{
break;
}
}
catch
{
continue;
}
}
}
}
}