diff options
Diffstat (limited to 'zenhttp/httpasio.cpp')
| -rw-r--r-- | zenhttp/httpasio.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/zenhttp/httpasio.cpp b/zenhttp/httpasio.cpp index 801bb51ac..f2d48200e 100644 --- a/zenhttp/httpasio.cpp +++ b/zenhttp/httpasio.cpp @@ -64,7 +64,7 @@ public: HttpAsioServerImpl(); ~HttpAsioServerImpl(); - void Start(uint16_t Port, int ThreadCount); + int Start(uint16_t Port, int ThreadCount); void Stop(); void RegisterService(const char* UrlPath, HttpService& Service); HttpService* RouteRequest(std::string_view Url); @@ -934,7 +934,12 @@ struct HttpAcceptor m_Acceptor.set_option(asio::ip::v6_only(false)); m_Acceptor.set_option(asio::socket_base::reuse_address(true)); m_Acceptor.set_option(asio::ip::tcp::no_delay(true)); - m_Acceptor.bind(asio::ip::tcp::endpoint(asio::ip::address_v6::any(), Port)); + asio::error_code BindErrorCode; + m_Acceptor.bind(asio::ip::tcp::endpoint(asio::ip::address_v6::any(), Port), BindErrorCode); + if (BindErrorCode == asio::error::access_denied) + { + m_Acceptor.bind(asio::ip::tcp::endpoint(asio::ip::address_v6::any(), 0)); + } m_Acceptor.listen(); } @@ -980,6 +985,8 @@ struct HttpAcceptor }); } + int GetAcceptPort() { return m_Acceptor.local_endpoint().port(); } + private: HttpAsioServerImpl& m_Server; asio::io_service& m_IoService; @@ -1119,7 +1126,7 @@ HttpAsioServerImpl::~HttpAsioServerImpl() { } -void +int HttpAsioServerImpl::Start(uint16_t Port, int ThreadCount) { ZEN_ASSERT(ThreadCount > 0); @@ -1142,6 +1149,8 @@ HttpAsioServerImpl::Start(uint16_t Port, int ThreadCount) } }); } + + return m_Acceptor->GetAcceptPort(); } void @@ -1212,12 +1221,11 @@ HttpAsioServer::RegisterService(HttpService& Service) m_Impl->RegisterService(Service.BaseUri(), Service); } -void +int HttpAsioServer::Initialize(int BasePort) { - m_BasePort = BasePort; - - m_Impl->Start(gsl::narrow<uint16_t>(m_BasePort), Max(std::thread::hardware_concurrency(), 8u)); + m_BasePort = m_Impl->Start(gsl::narrow<uint16_t>(BasePort), Max(std::thread::hardware_concurrency(), 8u)); + return m_BasePort; } void |