Added callback to start and accept_and_run

This commit is contained in:
eidheim 2019-12-29 10:35:14 +01:00
commit d156326598

View file

@ -129,7 +129,7 @@ namespace SimpleWeb {
/// Send the content of the response stream to client. The callback is called when the send has completed. /// Send the content of the response stream to client. The callback is called when the send has completed.
/// ///
/// Use this function if you need to recursively send parts of a longer message, or when using server-sent events. /// Use this function if you need to recursively send parts of a longer message, or when using server-sent events.
void send(const std::function<void(const error_code &)> &callback = nullptr) noexcept { void send(std::function<void(const error_code &)> callback = nullptr) noexcept {
session->connection->set_timeout(timeout_content); session->connection->set_timeout(timeout_content);
std::shared_ptr<asio::streambuf> streambuf = std::move(this->streambuf); std::shared_ptr<asio::streambuf> streambuf = std::move(this->streambuf);
@ -137,7 +137,7 @@ namespace SimpleWeb {
rdbuf(this->streambuf.get()); rdbuf(this->streambuf.get());
LockGuard lock(send_queue_mutex); LockGuard lock(send_queue_mutex);
send_queue.emplace_back(streambuf, callback); send_queue.emplace_back(std::move(streambuf), std::move(callback));
if(send_queue.size() == 1) if(send_queue.size() == 1)
send_from_queue(); send_from_queue();
} }
@ -432,7 +432,8 @@ namespace SimpleWeb {
/// If you know the server port in advance, use start() instead. /// If you know the server port in advance, use start() instead.
/// Accept requests, and if io_service was not set before calling bind(), run the internal io_service instead. /// Accept requests, and if io_service was not set before calling bind(), run the internal io_service instead.
/// Call after bind(). /// Call after bind().
void accept_and_run() { /// The callback parameter is called after the server is accepting connections.
void accept_and_run(std::function<void()> callback = nullptr) {
acceptor->listen(); acceptor->listen();
accept(); accept();
@ -440,6 +441,9 @@ namespace SimpleWeb {
if(io_service->stopped()) if(io_service->stopped())
restart(*io_service); restart(*io_service);
if(callback)
io_service->post(std::move(callback));
// If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling // If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling
threads.clear(); threads.clear();
for(std::size_t c = 1; c < config.thread_pool_size; c++) { for(std::size_t c = 1; c < config.thread_pool_size; c++) {
@ -456,12 +460,15 @@ namespace SimpleWeb {
for(auto &t : threads) for(auto &t : threads)
t.join(); t.join();
} }
else if(callback)
io_service->post(std::move(callback));
} }
/// Start the server by calling bind() and accept_and_run() /// Start the server by calling bind() and accept_and_run().
void start() { /// The callback parameter is called after the server is accepting connections.
void start(std::function<void()> callback = nullptr) {
bind(); bind();
accept_and_run(); accept_and_run(std::move(callback));
} }
/// Stop accepting new requests, and close current connections. /// Stop accepting new requests, and close current connections.