diff --git a/src/served/net/server.cpp b/src/served/net/server.cpp index a1ebc62..77b235a 100644 --- a/src/served/net/server.cpp +++ b/src/served/net/server.cpp @@ -22,7 +22,6 @@ #include #include -#include #include @@ -71,6 +70,30 @@ server::server( const std::string & address do_accept(); } +server::~server() +{ + _acceptor.close(); + _connection_manager.stop_all(); + + if ( ! _io_service.stopped() ) + { + _io_service.stop(); + } + + if ( _threads.size() > 0 ) { + for ( auto & thread : _threads ) + { + if ( thread.joinable() ) + { + thread.join(); + } + } + + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + _threads.clear(); + } +} + void server::run(int n_threads /* = 1 */, bool block /* = true */) { @@ -82,14 +105,13 @@ server::run(int n_threads /* = 1 */, bool block /* = true */) */ if ( n_threads > 1 ) { - std::vector v_threads; - for ( int i = 0; i < n_threads; i++ ) + for ( int i = 0; i < 1; i++ ) { - v_threads.push_back(std::thread([this](){ - _io_service.run(); - })); + _threads.push_back(std::thread( + std::bind(static_cast( + &boost::asio::io_service::run), std::ref(_io_service)))); } - for ( auto & thread : v_threads ) + for ( auto & thread : _threads ) { if ( block ) { diff --git a/src/served/net/server.hpp b/src/served/net/server.hpp index c83d04b..4028a8f 100644 --- a/src/served/net/server.hpp +++ b/src/served/net/server.hpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -49,6 +50,7 @@ class server int _read_timeout; int _write_timeout; size_t _req_max_bytes; + std::vector _threads; public: server(const server&) = delete; @@ -70,6 +72,11 @@ class server , multiplexer & mux , bool register_signals = true ); + /* + * Destroys the server. + */ + ~server(); + /* * A call that prompts the server into listening for HTTP requests. *