@@ -743,6 +743,7 @@ class Server {
743743 bool listen (const std::string &host, int port, int socket_flags = 0 );
744744
745745 bool is_running () const ;
746+ void wait_until_ready () const ;
746747 void stop ();
747748
748749 std::function<TaskQueue *(void )> new_task_queue;
@@ -752,7 +753,7 @@ class Server {
752753 bool &connection_closed,
753754 const std::function<void (Request &)> &setup_request);
754755
755- std::atomic<socket_t > svr_sock_;
756+ std::atomic<socket_t > svr_sock_{INVALID_SOCKET} ;
756757 size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
757758 time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND;
758759 time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
@@ -816,7 +817,8 @@ class Server {
816817 };
817818 std::vector<MountPointEntry> base_dirs_;
818819
819- std::atomic<bool > is_running_;
820+ std::atomic<bool > is_running_{false };
821+ std::atomic<bool > done_{false };
820822 std::map<std::string, std::string> file_extension_and_mimetype_map_;
821823 Handler file_request_handler_;
822824 Handlers get_handlers_;
@@ -5120,8 +5122,7 @@ inline const std::string &BufferStream::get_buffer() const { return buffer; }
51205122// HTTP server implementation
51215123inline Server::Server ()
51225124 : new_task_queue (
5123- [] { return new ThreadPool (CPPHTTPLIB_THREAD_POOL_COUNT); }),
5124- svr_sock_ (INVALID_SOCKET), is_running_ (false ) {
5125+ [] { return new ThreadPool (CPPHTTPLIB_THREAD_POOL_COUNT); }) {
51255126#ifndef _WIN32
51265127 signal (SIGPIPE, SIG_IGN);
51275128#endif
@@ -5334,15 +5335,25 @@ inline int Server::bind_to_any_port(const std::string &host, int socket_flags) {
53345335 return bind_internal (host, 0 , socket_flags);
53355336}
53365337
5337- inline bool Server::listen_after_bind () { return listen_internal (); }
5338+ inline bool Server::listen_after_bind () {
5339+ auto se = detail::scope_exit ([&]() { done_ = true ; });
5340+ return listen_internal ();
5341+ }
53385342
53395343inline bool Server::listen (const std::string &host, int port,
53405344 int socket_flags) {
5345+ auto se = detail::scope_exit ([&]() { done_ = true ; });
53415346 return bind_to_port (host, port, socket_flags) && listen_internal ();
53425347}
53435348
53445349inline bool Server::is_running () const { return is_running_; }
53455350
5351+ inline void Server::wait_until_ready () const {
5352+ while (!is_running () && !done_) {
5353+ std::this_thread::sleep_for (std::chrono::milliseconds{1 });
5354+ }
5355+ }
5356+
53465357inline void Server::stop () {
53475358 if (is_running_) {
53485359 assert (svr_sock_ != INVALID_SOCKET);
@@ -5733,6 +5744,7 @@ inline int Server::bind_internal(const std::string &host, int port,
57335744inline bool Server::listen_internal () {
57345745 auto ret = true ;
57355746 is_running_ = true ;
5747+ auto se = detail::scope_exit ([&]() { is_running_ = false ; });
57365748
57375749 {
57385750 std::unique_ptr<TaskQueue> task_queue (new_task_queue ());
@@ -5804,7 +5816,6 @@ inline bool Server::listen_internal() {
58045816 task_queue->shutdown ();
58055817 }
58065818
5807- is_running_ = false ;
58085819 return ret;
58095820}
58105821
0 commit comments