Repository: oatpp/oatpp
File: src/oatpp/network/Server.cpp (C++)

Code before:
            OATPP_LOGD("[oatpp::network::server::mainLoop()]", "Error. Server already stopped - closing connection...");
          }
        }
      } else {
        instance->setStatus(STATUS_STOPPING);
      }
    }
  } else {
    while (instance->getStatus() == STATUS_RUNNING) {
      auto connection = instance->m_connectionProvider->get();

      if (connection) {
        if (instance->getStatus() == STATUS_RUNNING) {
          instance->m_connectionHandler->handleConnection(connection, params /* null params */);
        } else {
          OATPP_LOGD("[oatpp::network::server::mainLoop()]", "Error. Server already stopped - closing connection...");
        }
      }
    }
  }

  instance->setStatus(STATUS_DONE);

}

void Server::run(bool startAsNewThread, std::function<bool()> conditional) {
  std::unique_lock<std::mutex> ul(m_mutex);
  switch (getStatus()) {
    case STATUS_STARTING:
      throw std::runtime_error("[oatpp::network::server::run()] Error. Server already starting");
    case STATUS_RUNNING:
      throw std::runtime_error("[oatpp::network::server::run()] Error. Server already started");
  }

  m_threaded = startAsNewThread;
  setStatus(STATUS_CREATED, STATUS_STARTING);

  m_condition = std::move(conditional);

  if (m_threaded) {
    m_thread = std::thread(mainLoop, this);
  } else {
    ul.unlock(); // early unlock
    mainLoop(this);
  }
}

void Server::stop() {
  std::lock_guard<std::mutex> lg(m_mutex);
  switch (getStatus()) {
    case STATUS_CREATED:

Review comment (bug):
I would prefer to get rid of a `startAsNewThread` - it's too confusing to me.
Let `Server::run()` always execute on a current thread.



```cpp
void Server::run(std::function<bool()> stopCondition = nullptr);
```

Code after:
      }
    } else {
      setStatus(STATUS_STOPPING);
    }
  }
  setStatus(STATUS_DONE);
}

void Server::mainLoop(Server *instance) {
  instance->setStatus(STATUS_STARTING, STATUS_RUNNING);
  std::shared_ptr<const std::unordered_map<oatpp::String, oatpp::String>> params;

 while (instance->getStatus() == STATUS_RUNNING) {
    auto connection = instance->m_connectionProvider->get();

    if (connection) {
      if (instance->getStatus() == STATUS_RUNNING) {
        instance->m_connectionHandler->handleConnection(connection, params /* null params */);
      } else {
        OATPP_LOGD("[oatpp::network::server::mainLoop()]", "Error. Server already stopped - closing connection...");
      }
    }
  }


  instance->setStatus(STATUS_DONE);

}

void Server::run(std::function<bool()> conditional) {
  std::unique_lock<std::mutex> ul(m_mutex);
  switch (getStatus()) {
    case STATUS_STARTING:
      throw std::runtime_error("[oatpp::network::server::run()] Error. Server already starting");
    case STATUS_RUNNING:
      throw std::runtime_error("[oatpp::network::server::run()] Error. Server already started");
  }

  m_threaded = false;
  setStatus(STATUS_CREATED, STATUS_STARTING);

  m_condition = std::move(conditional);

  ul.unlock(); // early unlock
  conditionalMainLoop();
}

void Server::run(bool startAsNewThread) {
  std::unique_lock<std::mutex> ul(m_mutex);
  OATPP_LOGW("[oatpp::network::server::run(bool)]", "Using oatpp::network::server::run(bool) is deprecated and will be removed in the next release. Please implement your own threading.")
  switch (getStatus()) {