FeatherMC/src/DedicatedServer.cpp

49 lines
1.4 KiB
C++

#include "DedicatedServer.h"
#include "config/ServerProperties.h"
#include "Protocol.h"
#include "PacketReader.h"
namespace Feather
{
DedicatedServer::DedicatedServer(ServerProperties* properties) :
m_properties(properties),
m_listener(properties->serverPort.GetValue(), this),
m_status()
{
m_status.descriptionText = properties->motd.GetValue();
m_status.maxPlayers = properties->maxPlayers.GetValue();
while (1)
{
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();
}
}
}
DedicatedServer::~DedicatedServer()
{
}
void DedicatedServer::OnClientConnect(Network::TCPClient&& client)
{
auto [clients, lock] = m_clients.borrow();
clients.emplace_back(std::move(client));
}
void DedicatedServer::OnClientDisconnect(const Network::TCPClient& client)
{
auto [clients, lock] = m_clients.borrow();
clients.remove_if([&](MinecraftClient& other) { return &other.GetTCPClient() == &client; });
}
}