60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
#include "DedicatedServer.h"
|
|
#include "config/ServerProperties.h"
|
|
|
|
#include "PacketReader.h"
|
|
#include <Windows.h>
|
|
|
|
namespace Feather
|
|
{
|
|
DedicatedServer::DedicatedServer(ServerProperties* properties) :
|
|
m_properties(properties),
|
|
m_listener(properties->serverPort.GetValue(), this),
|
|
m_status(),
|
|
m_protocol(*this)
|
|
{
|
|
m_status.descriptionText = properties->motd.GetValue();
|
|
m_status.maxPlayers = properties->maxPlayers.GetValue();
|
|
|
|
while (1)
|
|
{
|
|
auto [clients, clientsLock] = m_clients.borrow();
|
|
for (auto& client : clients)
|
|
{
|
|
auto [data, clientLock] = client.GetTCPClient()->GetData().borrow();
|
|
if (data.empty())
|
|
continue;
|
|
uint32_t offset = 0;
|
|
while (offset != data.size())
|
|
{
|
|
auto reader = PacketReader(&data[offset]);
|
|
m_protocol.HandlePacket(client, reader);
|
|
offset += reader.Size();
|
|
}
|
|
data.clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
DedicatedServer::~DedicatedServer()
|
|
{
|
|
}
|
|
|
|
void DedicatedServer::OnClientConnect(Network::TCPClientHandle&& client)
|
|
{
|
|
const auto& address = client->GetAddress();
|
|
Log_Info("New connection from %s:%hu", address.ip, address.port);
|
|
|
|
auto [clients, lock] = m_clients.borrow();
|
|
clients.emplace_back(std::move(client));
|
|
}
|
|
|
|
void DedicatedServer::OnClientDisconnect(const Network::TCPClient* client)
|
|
{
|
|
const auto& address = client->GetAddress();
|
|
Log_Info("Disconnected from %s:%hu", address.ip, address.port);
|
|
|
|
auto [clients, lock] = m_clients.borrow();
|
|
clients.remove_if([&](MinecraftClient& other) { return other.GetTCPClient().get() == client; });
|
|
}
|
|
}
|