Skip to content

Commit f52c8a0

Browse files
committed
Cleanup of start and its related functions. Also removed unused lambda captures.
1 parent 3ba786f commit f52c8a0

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

client_http.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ namespace SimpleWeb {
501501
auto content_length = stoull(header_it->second);
502502
if(content_length > num_additional_bytes) {
503503
session->connection->set_timeout();
504-
asio::async_read(*session->connection->socket, session->response->streambuf, asio::transfer_exactly(content_length - num_additional_bytes), [this, session](const error_code &ec, std::size_t /*bytes_transferred*/) {
504+
asio::async_read(*session->connection->socket, session->response->streambuf, asio::transfer_exactly(content_length - num_additional_bytes), [session](const error_code &ec, std::size_t /*bytes_transferred*/) {
505505
session->connection->cancel_timeout();
506506
auto lock = session->connection->handler_runner->continue_lock();
507507
if(!lock)
@@ -526,7 +526,7 @@ namespace SimpleWeb {
526526
}
527527
else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) {
528528
session->connection->set_timeout();
529-
asio::async_read(*session->connection->socket, session->response->streambuf, [this, session](const error_code &ec, std::size_t /*bytes_transferred*/) {
529+
asio::async_read(*session->connection->socket, session->response->streambuf, [session](const error_code &ec, std::size_t /*bytes_transferred*/) {
530530
session->connection->cancel_timeout();
531531
auto lock = session->connection->handler_runner->continue_lock();
532532
if(!lock)

server_http.hpp

+26-16
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ namespace SimpleWeb {
286286
Config(unsigned short port) noexcept : port(port) {}
287287

288288
public:
289-
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS.
289+
/// Port number to use. Defaults to 80 for HTTP and 443 for HTTPS. Set to 0 get an assigned port.
290290
unsigned short port;
291291
/// If io_service is not set, number of threads that the server will use when start() is called.
292292
/// Defaults to 1 thread.
@@ -332,35 +332,43 @@ namespace SimpleWeb {
332332
/// If you have your own asio::io_service, store its pointer here before running start().
333333
std::shared_ptr<asio::io_service> io_service;
334334

335-
virtual unsigned short bindAndPrepare() {
336-
if(!io_service) {
337-
io_service = std::make_shared<asio::io_service>();
338-
internal_io_service = true;
339-
}
340-
341-
if(io_service->stopped())
342-
io_service->reset();
343-
335+
/// If you know the server port in advance, use start() instead.
336+
/// Returns assigned port. If io_service is not set, an internal io_service is created instead.
337+
/// Call before accept_and_run().
338+
unsigned short bind() {
344339
asio::ip::tcp::endpoint endpoint;
345340
if(config.address.size() > 0)
346341
endpoint = asio::ip::tcp::endpoint(asio::ip::address::from_string(config.address), config.port);
347342
else
348343
endpoint = asio::ip::tcp::endpoint(asio::ip::tcp::v4(), config.port);
349344

345+
if(!io_service) {
346+
io_service = std::make_shared<asio::io_service>();
347+
internal_io_service = true;
348+
}
349+
350350
if(!acceptor)
351351
acceptor = std::unique_ptr<asio::ip::tcp::acceptor>(new asio::ip::tcp::acceptor(*io_service));
352352
acceptor->open(endpoint.protocol());
353353
acceptor->set_option(asio::socket_base::reuse_address(config.reuse_address));
354354
acceptor->bind(endpoint);
355-
acceptor->listen();
356355

357-
accept();
356+
after_bind();
358357

359358
return acceptor->local_endpoint().port();
360359
}
361360

362-
virtual void runServer() {
361+
/// If you know the server port in advance, use start() instead.
362+
/// Accept requests, and if io_service was not set before calling bind(), run the internal io_service instead.
363+
/// Call after bind().
364+
void accept_and_run() {
365+
acceptor->listen();
366+
accept();
367+
363368
if(internal_io_service) {
369+
if(io_service->stopped())
370+
io_service->reset();
371+
364372
// If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
365373
threads.clear();
366374
for(std::size_t c = 1; c < config.thread_pool_size; c++) {
@@ -379,9 +387,10 @@ namespace SimpleWeb {
379387
}
380388
}
381389

382-
virtual void start() {
383-
bindAndPrepare();
384-
runServer();
390+
/// Start the server by calling bind() and accept_and_run()
391+
void start() {
392+
bind();
393+
accept_and_run();
385394
}
386395

387396
/// Stop accepting new requests, and close current connections.
@@ -420,6 +429,7 @@ namespace SimpleWeb {
420429

421430
ServerBase(unsigned short port) noexcept : config(port), connections(new std::unordered_set<Connection *>()), connections_mutex(new std::mutex()), handler_runner(new ScopeRunner()) {}
422431

432+
virtual void after_bind() {}
423433
virtual void accept() = 0;
424434

425435
template <typename... Args>

server_https.hpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace SimpleWeb {
1717

1818
template <>
1919
class Server<HTTPS> : public ServerBase<HTTPS> {
20-
std::string session_id_context;
2120
bool set_session_id_context = false;
2221

2322
public:
@@ -33,20 +32,19 @@ namespace SimpleWeb {
3332
}
3433
}
3534

36-
void start() override {
35+
protected:
36+
asio::ssl::context context;
37+
38+
void after_bind() override {
3739
if(set_session_id_context) {
3840
// Creating session_id_context from address:port but reversed due to small SSL_MAX_SSL_SESSION_ID_LENGTH
39-
session_id_context = std::to_string(config.port) + ':';
41+
auto session_id_context = std::to_string(acceptor->local_endpoint().port()) + ':';
4042
session_id_context.append(config.address.rbegin(), config.address.rend());
4143
SSL_CTX_set_session_id_context(context.native_handle(), reinterpret_cast<const unsigned char *>(session_id_context.data()),
4244
std::min<std::size_t>(session_id_context.size(), SSL_MAX_SSL_SESSION_ID_LENGTH));
4345
}
44-
ServerBase::start();
4546
}
4647

47-
protected:
48-
asio::ssl::context context;
49-
5048
void accept() override {
5149
auto connection = create_connection(*io_service, context);
5250

0 commit comments

Comments
 (0)