Fixes #280 : added start_stop_mutex that is used in Server::bind() and Server::stop()

This commit is contained in:
eidheim 2019-08-26 14:13:43 +02:00
commit 253b18f02b

View file

@ -386,6 +386,8 @@ namespace SimpleWeb {
/// Returns assigned port. If io_service is not set, an internal io_service is created instead.
/// Call before accept_and_run().
unsigned short bind() {
std::lock_guard<std::mutex> lock(start_stop_mutex);
asio::ip::tcp::endpoint endpoint;
if(config.address.size() > 0)
endpoint = asio::ip::tcp::endpoint(make_address(config.address), config.port);
@ -452,6 +454,8 @@ namespace SimpleWeb {
/// Stop accepting new requests, and close current connections.
void stop() noexcept {
std::lock_guard<std::mutex> lock(start_stop_mutex);
if(acceptor) {
error_code ec;
acceptor->close(ec);
@ -474,6 +478,8 @@ namespace SimpleWeb {
}
protected:
std::mutex start_stop_mutex;
bool internal_io_service = false;
std::unique_ptr<asio::ip::tcp::acceptor> acceptor;