@@ -932,6 +932,7 @@ class Server {
932932 bool is_running () const ;
933933 void wait_until_ready () const ;
934934 void stop ();
935+ void decommission ();
935936
936937 std::function<TaskQueue *(void )> new_task_queue;
937938
@@ -1006,7 +1007,7 @@ class Server {
10061007 virtual bool process_and_close_socket (socket_t sock);
10071008
10081009 std::atomic<bool > is_running_{false };
1009- std::atomic<bool > done_ {false };
1010+ std::atomic<bool > is_decommisioned {false };
10101011
10111012 struct MountPointEntry {
10121013 std::string mount_point;
@@ -6111,27 +6112,27 @@ inline Server &Server::set_payload_max_length(size_t length) {
61116112
61126113inline bool Server::bind_to_port (const std::string &host, int port,
61136114 int socket_flags) {
6114- return bind_internal (host, port, socket_flags) >= 0 ;
6115+ auto ret = bind_internal (host, port, socket_flags);
6116+ if (ret == -1 ) { is_decommisioned = true ; }
6117+ return ret >= 0 ;
61156118}
61166119inline int Server::bind_to_any_port (const std::string &host, int socket_flags) {
6117- return bind_internal (host, 0 , socket_flags);
6120+ auto ret = bind_internal (host, 0 , socket_flags);
6121+ if (ret == -1 ) { is_decommisioned = true ; }
6122+ return ret;
61186123}
61196124
6120- inline bool Server::listen_after_bind () {
6121- auto se = detail::scope_exit ([&]() { done_ = true ; });
6122- return listen_internal ();
6123- }
6125+ inline bool Server::listen_after_bind () { return listen_internal (); }
61246126
61256127inline bool Server::listen (const std::string &host, int port,
61266128 int socket_flags) {
6127- auto se = detail::scope_exit ([&]() { done_ = true ; });
61286129 return bind_to_port (host, port, socket_flags) && listen_internal ();
61296130}
61306131
61316132inline bool Server::is_running () const { return is_running_; }
61326133
61336134inline void Server::wait_until_ready () const {
6134- while (!is_running () && !done_ ) {
6135+ while (!is_running_ && !is_decommisioned ) {
61356136 std::this_thread::sleep_for (std::chrono::milliseconds{1 });
61366137 }
61376138}
@@ -6143,8 +6144,11 @@ inline void Server::stop() {
61436144 detail::shutdown_socket (sock);
61446145 detail::close_socket (sock);
61456146 }
6147+ is_decommisioned = false ;
61466148}
61476149
6150+ inline void Server::decommission () { is_decommisioned = true ; }
6151+
61486152inline bool Server::parse_request_line (const char *s, Request &req) const {
61496153 auto len = strlen (s);
61506154 if (len < 2 || s[len - 2 ] != ' \r ' || s[len - 1 ] != ' \n ' ) { return false ; }
@@ -6499,6 +6503,8 @@ Server::create_server_socket(const std::string &host, int port,
64996503
65006504inline int Server::bind_internal (const std::string &host, int port,
65016505 int socket_flags) {
6506+ if (is_decommisioned) { return -1 ; }
6507+
65026508 if (!is_valid ()) { return -1 ; }
65036509
65046510 svr_sock_ = create_server_socket (host, port, socket_flags, socket_options_);
@@ -6524,6 +6530,8 @@ inline int Server::bind_internal(const std::string &host, int port,
65246530}
65256531
65266532inline bool Server::listen_internal () {
6533+ if (is_decommisioned) { return false ; }
6534+
65276535 auto ret = true ;
65286536 is_running_ = true ;
65296537 auto se = detail::scope_exit ([&]() { is_running_ = false ; });
@@ -6613,6 +6621,7 @@ inline bool Server::listen_internal() {
66136621 task_queue->shutdown ();
66146622 }
66156623
6624+ is_decommisioned = !ret;
66166625 return ret;
66176626}
66186627
0 commit comments