FeatherMC/src/DedicatedServer.cpp

60 lines
1.8 KiB
C++
Raw Normal View History

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
#include "PacketReader.h"
2020-08-01 05:53:22 +01:00
#include <Windows.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-08-01 02:36:29 +01:00
m_status(),
m_protocol(*this)
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)
{
auto [clients, clientsLock] = m_clients.borrow();
for (auto& client : clients)
{
auto [data, clientLock] = client.GetTCPClient()->GetData().borrow();
if (data.empty())
continue;
2020-08-01 05:53:22 +01:00
uint32_t offset = 0;
while (offset != data.size())
{
auto reader = PacketReader(&data[offset]);
m_protocol.HandlePacket(client, reader);
offset += reader.Size();
}
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::TCPClientHandle&& client)
2020-07-29 01:46:31 +01:00
{
2020-08-02 00:35:54 +01:00
const auto& address = client->GetAddress();
Log_Info("New connection from %s:%hu", address.ip, address.port);
2020-07-29 01:46:31 +01:00
auto [clients, lock] = m_clients.borrow();
clients.emplace_back(std::move(client));
}
2020-08-01 04:54:31 +01:00
void DedicatedServer::OnClientDisconnect(const Network::TCPClient* client)
2020-07-29 01:46:31 +01:00
{
2020-08-02 00:35:54 +01:00
const auto& address = client->GetAddress();
Log_Info("Disconnected from %s:%hu", address.ip, address.port);
2020-07-29 01:46:31 +01:00
auto [clients, lock] = m_clients.borrow();
2020-08-01 04:54:31 +01:00
clients.remove_if([&](MinecraftClient& other) { return other.GetTCPClient().get() == client; });
2020-07-29 01:46:31 +01:00
}
2020-07-24 10:17:36 +01:00
}