Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/server/StarServerRconThread.cpp
blob: a9842eed62a6434c35d05a1a308d86e6eb5e5983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "StarServerRconThread.hpp"
#include "StarLogging.hpp"
#include "StarRoot.hpp"
#include "StarConfiguration.hpp"
#include "StarUniverseServer.hpp"
#include "StarServerRconClient.hpp"
#include "StarIterator.hpp"

namespace Star {

ServerRconThread::ServerRconThread(UniverseServer* universe, HostAddressWithPort const& address)
  : Thread("RconServer"), m_universe(universe), m_rconServer(address), m_stop(true) {
  if (Root::singleton().configuration()->get("rconServerPassword").toString().empty())
    Logger::warn("rconServerPassword is not configured requests will NOT be processed");
}

ServerRconThread::~ServerRconThread() {
  stop();
  join();
}

void ServerRconThread::clearClients(bool all) {
  auto it = makeSMutableMapIterator(m_clients);
  while (it.hasNext()) {
    auto const& pair = it.next();
    auto client = pair.second;
    if (all)
      client->stop();
    else if (!client->isRunning())
      it.remove();
  }
}

void ServerRconThread::start() {
  m_stop = false;
  Thread::start();
}

void ServerRconThread::stop() {
  m_stop = true;
  m_rconServer.stop();
  clearClients(true);
}

void ServerRconThread::run() {
  try {
    auto timeout = Root::singleton().configuration()->get("rconServerTimeout").toInt();
    while (!m_stop) {
      if (auto client = m_rconServer.accept(100)) {
        client->setTimeout(timeout);
        auto rconClient = make_shared<ServerRconClient>(m_universe, client);
        rconClient->start();
        m_clients[client->remoteAddress().address()] = rconClient;
        clearClients();
      }
    }
  } catch (std::exception const& e) {
    Logger::error("ServerRconThread exception caught: {}", e.what());
  }
}

}