From 3ba786f5851fe7b0b86ff680e3b3cba72a96fd5a Mon Sep 17 00:00:00 2001 From: Thomas Karlsson Date: Thu, 19 Apr 2018 15:36:38 +0200 Subject: [PATCH] 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 --- server_http.hpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server_http.hpp b/server_http.hpp index 60cd760..c5389e9 100644 --- a/server_http.hpp +++ b/server_http.hpp @@ -332,7 +332,7 @@ namespace SimpleWeb { /// If you have your own asio::io_service, store its pointer here before running start(). std::shared_ptr io_service; - virtual void start() { + virtual unsigned short bindAndPrepare() { if(!io_service) { io_service = std::make_shared(); internal_io_service = true; @@ -356,6 +356,10 @@ namespace SimpleWeb { accept(); + return acceptor->local_endpoint().port(); + } + + virtual void runServer() { if(internal_io_service) { // If thread_pool_size>1, start m_io_service.run() in (thread_pool_size-1) threads for thread-pooling threads.clear(); @@ -375,6 +379,11 @@ namespace SimpleWeb { } } + virtual void start() { + bindAndPrepare(); + runServer(); + } + /// Stop accepting new requests, and close current connections. void stop() noexcept { if(acceptor) {