Splitted the start function into a two in order to access the bound port

This is relevant if binding on a random free port (0) and since start never
returns... we need a split function to safely get it
This commit is contained in:
Thomas Karlsson 2018-04-19 15:36:38 +02:00
commit 3ba786f585

View file

@ -332,7 +332,7 @@ namespace SimpleWeb {
/// If you have your own asio::io_service, store its pointer here before running start(). /// If you have your own asio::io_service, store its pointer here before running start().
std::shared_ptr<asio::io_service> io_service; std::shared_ptr<asio::io_service> io_service;
virtual void start() { virtual unsigned short bindAndPrepare() {
if(!io_service) { if(!io_service) {
io_service = std::make_shared<asio::io_service>(); io_service = std::make_shared<asio::io_service>();
internal_io_service = true; internal_io_service = true;
@ -356,6 +356,10 @@ namespace SimpleWeb {
accept(); accept();
return acceptor->local_endpoint().port();
}
virtual void runServer() {
if(internal_io_service) { if(internal_io_service) {
// 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();
@ -375,6 +379,11 @@ namespace SimpleWeb {
} }
} }
virtual void start() {
bindAndPrepare();
runServer();
}
/// Stop accepting new requests, and close current connections. /// Stop accepting new requests, and close current connections.
void stop() noexcept { void stop() noexcept {
if(acceptor) { if(acceptor) {