2020-07-24 10:17:36 +01:00
|
|
|
#include "DedicatedServer.h"
|
2020-07-25 04:44:00 +01:00
|
|
|
#include "config/ServerProperties.h"
|
2020-07-24 10:17:36 +01:00
|
|
|
|
2020-07-31 02:53:40 +01:00
|
|
|
#include "Protocol.h"
|
|
|
|
#include "PacketReader.h"
|
|
|
|
|
2020-07-24 10:17:36 +01:00
|
|
|
namespace Feather
|
|
|
|
{
|
2020-07-25 04:44:00 +01:00
|
|
|
DedicatedServer::DedicatedServer(ServerProperties* properties) :
|
|
|
|
m_properties(properties),
|
2020-07-29 01:46:31 +01:00
|
|
|
m_listener(properties->serverPort.GetValue(), this),
|
2020-07-25 04:44:00 +01:00
|
|
|
m_status()
|
2020-07-24 10:17:36 +01:00
|
|
|
{
|
2020-07-25 04:44:00 +01:00
|
|
|
m_status.descriptionText = properties->motd.GetValue();
|
|
|
|
m_status.maxPlayers = properties->maxPlayers.GetValue();
|
|
|
|
|
2020-07-25 03:01:16 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2020-07-31 02:53:40 +01:00
|
|
|
auto [clients, clientsLock] = m_clients.borrow();
|
|
|
|
for (auto& client : clients)
|
|
|
|
{
|
|
|
|
Protocol protocol;
|
|
|
|
auto [data, clientLock] = client.GetTCPClient().GetData().borrow();
|
|
|
|
if (data.empty())
|
|
|
|
continue;
|
|
|
|
auto reader = PacketReader(data.data());
|
|
|
|
protocol.HandlePacket(client, reader);
|
|
|
|
data.clear();
|
|
|
|
}
|
2020-07-25 03:01:16 +01:00
|
|
|
}
|
2020-07-24 10:17:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DedicatedServer::~DedicatedServer()
|
|
|
|
{
|
|
|
|
}
|
2020-07-29 01:46:31 +01:00
|
|
|
|
|
|
|
void DedicatedServer::OnClientConnect(Network::TCPClient&& client)
|
|
|
|
{
|
|
|
|
auto [clients, lock] = m_clients.borrow();
|
|
|
|
clients.emplace_back(std::move(client));
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:34:10 +01:00
|
|
|
void DedicatedServer::OnClientDisconnect(const Network::TCPClient& client)
|
2020-07-29 01:46:31 +01:00
|
|
|
{
|
|
|
|
auto [clients, lock] = m_clients.borrow();
|
2020-07-31 02:53:40 +01:00
|
|
|
clients.remove_if([&](MinecraftClient& other) { return &other.GetTCPClient() == &client; });
|
2020-07-29 01:46:31 +01:00
|
|
|
}
|
2020-07-24 10:17:36 +01:00
|
|
|
}
|