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 "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-08-03 06:07:32 +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)
|
|
|
|
{
|
2020-08-01 04:38:20 +01:00
|
|
|
auto [data, clientLock] = client.GetTCPClient()->GetData().borrow();
|
2020-07-31 02:53:40 +01:00
|
|
|
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]);
|
2020-08-03 06:07:32 +01:00
|
|
|
Protocol::ProcessPacket(*this, client, reader, client.GetContext().GetState());
|
2020-08-01 05:53:22 +01:00
|
|
|
offset += reader.Size();
|
|
|
|
}
|
2020-07-31 02:53:40 +01:00
|
|
|
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
|
|
|
|
2020-08-01 04:38:20 +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-08-03 06:07:32 +01:00
|
|
|
|
|
|
|
using namespace Protocol;
|
|
|
|
|
|
|
|
void DedicatedServer::HandleLegacyPing(MinecraftClient& client)
|
|
|
|
{
|
|
|
|
Log_Info("Got legacy server list ping.");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void DedicatedServer::HandlePacket(MinecraftClient& client, const Handholding::ServerboundHandshake& handshake)
|
|
|
|
{
|
|
|
|
Log_Info("Client Intention Packet: version=%d, serverIp=%s, port=%u, intention=%d\n",
|
|
|
|
handshake.protocolVersion,
|
|
|
|
handshake.serverIP.c_str(),
|
|
|
|
handshake.port,
|
|
|
|
handshake.intention
|
|
|
|
);
|
|
|
|
|
|
|
|
client.GetContext().SetState(handshake.intention);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void DedicatedServer::HandlePacket(MinecraftClient& client, const Status::ServerboundRequest& request)
|
|
|
|
{
|
|
|
|
Log_Info("Client sent STATUS_PING_REQUEST");
|
|
|
|
|
|
|
|
Status::ClientboundResponse message =
|
|
|
|
{
|
|
|
|
.jsonResponse = m_status.GetServerStatusJSON()
|
|
|
|
};
|
|
|
|
|
|
|
|
client.SendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void DedicatedServer::HandlePacket(MinecraftClient& client, const Status::ServerboundPing& ping)
|
|
|
|
{
|
|
|
|
Log_Info("Client sent STATUS_PING: %llu", ping.timestamp);
|
|
|
|
|
|
|
|
Status::ClientboundPong message =
|
|
|
|
{
|
|
|
|
.timestamp = ping.timestamp
|
|
|
|
};
|
|
|
|
|
|
|
|
client.SendMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void DedicatedServer::HandlePacket(MinecraftClient& client, const Login::ServerboundStart& start)
|
|
|
|
{
|
|
|
|
Login::ClientboundSuccess message =
|
|
|
|
{
|
|
|
|
.uuid = { 4658857991808325907ull, 7518717155607718277ull },
|
|
|
|
.username = start.username
|
|
|
|
};
|
|
|
|
|
|
|
|
client.SendMessage(message);
|
|
|
|
}
|
2020-07-24 10:17:36 +01:00
|
|
|
}
|