-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcHTTPServer_V2.cpp
More file actions
305 lines (260 loc) · 5.75 KB
/
cHTTPServer_V2.cpp
File metadata and controls
305 lines (260 loc) · 5.75 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "cHTTPServer_V2.h"
#include <algorithm>
#include <utility>
cHTTPServer_V2::cHTTPServer_V2(
std::string name,
std::uint16_t port,
std::size_t ioThreadCount)
: cBaseWorker_V2(std::move(name)),
m_ioThreadCount((ioThreadCount == 0U) ? 1U : ioThreadCount)
{
m_server.config.port = port;
}
cHTTPServer_V2::~cHTTPServer_V2() noexcept
{
(void)stopThread();
}
cHTTPServer_V2::server_type &cHTTPServer_V2::server() noexcept
{
return m_server;
}
const cHTTPServer_V2::server_type &cHTTPServer_V2::server() const noexcept
{
return m_server;
}
std::shared_ptr<SimpleWeb::io_context> cHTTPServer_V2::ioContext() const noexcept
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_io;
}
void cHTTPServer_V2::setIOThreadCount(std::size_t ioThreadCount) noexcept
{
if (isRunning())
{
return;
}
std::lock_guard<std::mutex> lock(m_mutex);
m_ioThreadCount = (ioThreadCount == 0U) ? 1U : ioThreadCount;
}
std::size_t cHTTPServer_V2::ioThreadCount() const noexcept
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_ioThreadCount;
}
std::uint16_t cHTTPServer_V2::configuredPort() const noexcept
{
return m_server.config.port;
}
std::string cHTTPServer_V2::bindAddress() const
{
return m_server.config.address;
}
std::uint16_t cHTTPServer_V2::listeningPort() const noexcept
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_listeningPort;
}
std::string cHTTPServer_V2::lastError() const
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_lastError;
}
bool cHTTPServer_V2::startThread(duration_type waitForStartTimeout)
{
resetStartState();
if (!cBaseWorker_V2::startThread(duration_type::zero()))
{
return false;
}
return waitUntilListening(waitForStartTimeout);
}
bool cHTTPServer_V2::startThread(std::uint16_t waitForStartTimeoutMilliSec)
{
return startThread(duration_type{waitForStartTimeoutMilliSec});
}
bool cHTTPServer_V2::preRun()
{
try
{
auto io = std::make_shared<SimpleWeb::io_context>();
{
std::lock_guard<std::mutex> lock(m_mutex);
m_io = io;
m_userOnError = m_server.on_error;
m_server.io_service = io;
m_server.config.thread_pool_size = 1U;
m_server.on_error =
[this](request_ptr request, const SimpleWeb::error_code &error)
{
updateHeartbeat();
setLastError(error.message());
const auto userHandler = m_userOnError;
if (userHandler)
{
userHandler(std::move(request), error);
}
};
}
return true;
}
catch (const std::exception &error)
{
resolveStartIfPending(false, 0U, error.what());
}
catch (...)
{
resolveStartIfPending(false, 0U, "Failed to initialize HTTP server");
}
return false;
}
void cHTTPServer_V2::run()
{
try
{
m_server.start([this](unsigned short port)
{
updateHeartbeat();
resolveStartIfPending(true, port, {}); });
processWork();
resolveStartIfPending(
false,
0U,
stopRequested() ? "HTTP server stopped" : "HTTP server exited unexpectedly");
}
catch (const std::exception &error)
{
resolveStartIfPending(false, 0U, error.what());
throw;
}
catch (...)
{
resolveStartIfPending(false, 0U, "Unhandled HTTP server exception");
throw;
}
{
std::lock_guard<std::mutex> lock(m_mutex);
m_server.on_error = m_userOnError;
m_server.io_service.reset();
m_userOnError = {};
m_io.reset();
}
}
void cHTTPServer_V2::stopTriggered()
{
try
{
m_server.stop();
}
catch (...)
{
}
auto io = ioContext();
if (io)
{
io->stop();
}
resolveStartIfPending(false, 0U, "HTTP server stop requested");
}
void cHTTPServer_V2::processWork()
{
auto io = ioContext();
if (!io)
{
return;
}
const auto runIo = [this, io]() noexcept
{
try
{
io->run();
}
catch (const std::exception &error)
{
setLastError(error.what());
io->stop();
requestStop();
}
catch (...)
{
setLastError("Unhandled io_context exception");
io->stop();
requestStop();
}
};
{
std::lock_guard<std::mutex> lock(m_mutex);
m_ioThreads.clear();
for (std::size_t index = 1U; index < m_ioThreadCount; ++index)
{
m_ioThreads.emplace_back(runIo);
}
}
runIo();
std::vector<std::thread> threadsToJoin;
{
std::lock_guard<std::mutex> lock(m_mutex);
threadsToJoin.swap(m_ioThreads);
}
for (auto &thread : threadsToJoin)
{
if (thread.joinable())
{
thread.join();
}
}
}
void cHTTPServer_V2::resetStartState()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_listeningPort = 0U;
m_startResolved = false;
m_startSucceeded = false;
m_lastError.clear();
}
void cHTTPServer_V2::resolveStartIfPending(
bool success,
std::uint16_t port,
std::string errorMessage)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_startResolved)
{
if (!errorMessage.empty())
{
m_lastError = std::move(errorMessage);
}
return;
}
m_startResolved = true;
m_startSucceeded = success;
m_listeningPort = success ? port : 0U;
m_lastError = std::move(errorMessage);
}
m_startCondition.notify_all();
}
void cHTTPServer_V2::setLastError(std::string errorMessage)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_lastError = std::move(errorMessage);
}
bool cHTTPServer_V2::waitUntilListening(duration_type waitForStartTimeout)
{
if (waitForStartTimeout == duration_type::zero())
{
return true;
}
std::unique_lock<std::mutex> lock(m_mutex);
const bool completed = m_startCondition.wait_for(
lock,
waitForStartTimeout,
[this]()
{
return m_startResolved;
});
if (!completed)
{
return false;
}
return m_startSucceeded;
}