Skip to content

Commit

Permalink
Initial work on TCP transport layer:
Browse files Browse the repository at this point in the history
* Optionally compiled additional TCP transport layer.
* Config to enable it.
* Tested and functional with lcdr's tcpudp dll, udp being disabled in the dll due to port issues.
* Removed unused RakNet replica manager and id manager. We've got our own replica manager since pre-open-source.
* Utilizes async boost behavior.

Todo:
* Figure out how to do ping calculations.
* Fix crashes on universe shutdown.
* Test TLS on a VPS.
* Remove unnecessary logging.
* Test with lots of clients.
* Finish "master" to "manager" naming refactor.
  • Loading branch information
Wincent01 committed Oct 13, 2024
1 parent 8d54db7 commit 4004534
Show file tree
Hide file tree
Showing 28 changed files with 2,321 additions and 366 deletions.
4 changes: 3 additions & 1 deletion dCommon/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class EntityManager;
class dZoneManager;
class PlayerContainer;

#include "TransportPeerInterface.h"

namespace Game {
using signal_t = volatile std::sig_atomic_t;
extern Logger* logger;
Expand All @@ -24,7 +26,7 @@ namespace Game {
extern dChatFilter* chatFilter;
extern dConfig* config;
extern std::mt19937 randomEngine;
extern RakPeerInterface* chatServer;
extern TransportPeerInterface* chatServer;
extern AssetManager* assetManager;
extern SystemAddress chatSysAddr;
extern signal_t lastSignal;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef __EMASTERMESSAGETYPE__H__
#define __EMASTERMESSAGETYPE__H__
#ifndef __EMANAGERMESSAGETYPE__H__
#define __EMANAGERMESSAGETYPE__H__

#include <cstdint>

enum class eMasterMessageType : uint32_t {
enum class eManagerMessageType : uint32_t {
REQUEST_PERSISTENT_ID = 1,
REQUEST_PERSISTENT_ID_RESPONSE,
REQUEST_ZONE_TRANSFER,
Expand Down Expand Up @@ -33,4 +33,4 @@ enum class eMasterMessageType : uint32_t {
NEW_SESSION_ALERT
};

#endif //!__EMASTERMESSAGETYPE__H__
#endif //!__EMANAGERMESSAGETYPE__H__
4 changes: 2 additions & 2 deletions dGame/dComponents/RocketLaunchpadControlComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "BitStreamUtils.h"
#include "eObjectWorldState.h"
#include "eConnectionType.h"
#include "eMasterMessageType.h"
#include "eManagerMessageType.h"

RocketLaunchpadControlComponent::RocketLaunchpadControlComponent(Entity* parent, int rocketId) : Component(parent) {
auto query = CDClientDatabase::CreatePreppedStmt(
Expand Down Expand Up @@ -137,7 +137,7 @@ LWOCLONEID RocketLaunchpadControlComponent::GetSelectedCloneId(LWOOBJID player)

void RocketLaunchpadControlComponent::TellMasterToPrepZone(int zoneID) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::PREP_ZONE);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::PREP_ZONE);
bitStream.Write(zoneID);
Game::server->SendToMaster(bitStream);
}
Expand Down
3 changes: 2 additions & 1 deletion dGame/dUtilities/ObjectIDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Database.h"
#include "Logger.h"
#include "Game.h"
#include "dServer.h"

//! The persistent ID request
struct PersistentIDRequest {
Expand All @@ -25,7 +26,7 @@ namespace {
void ObjectIDManager::RequestPersistentID(const std::function<void(uint32_t)> callback) {
const auto& request = Requests.emplace_back(++CurrentRequestID, callback);

MasterPackets::SendPersistentIDRequest(Game::server, request.requestID);
MasterPackets::SendPersistentIDRequest(Game::server->GetTransportLayerPtr(), request.requestID);
}

//! Handles a persistent ID response
Expand Down
17 changes: 13 additions & 4 deletions dGame/dUtilities/SlashCommands/DEVGMCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@

// Enums
#include "eGameMasterLevel.h"
#include "eMasterMessageType.h"
#include "eManagerMessageType.h"
#include "eInventoryType.h"
#include "ePlayerFlag.h"
#include <RakNetTransportLayer.h>


namespace DEVGMCommands {
Expand Down Expand Up @@ -503,7 +504,7 @@ namespace DEVGMCommands {
void ShutdownUniverse(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
//Tell the master server that we're going to be shutting down whole "universe":
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SHUTDOWN_UNIVERSE);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::SHUTDOWN_UNIVERSE);
Game::server->SendToMaster(bitStream);
ChatPackets::SendSystemMessage(sysAddr, u"Sent universe shutdown notification to master.");

Expand Down Expand Up @@ -1247,8 +1248,16 @@ namespace DEVGMCommands {

scriptedActivityComponent->ReloadConfig();
}
Game::server->UpdateMaximumMtuSize();
Game::server->UpdateBandwidthLimit();

if (Game::server->GetTransportType() == TransportType::RakNet) {
const auto& transport = Game::server->GetTransportLayer();

auto* raknetTransport = static_cast<RakNetTransportLayer*>(transport.get());

raknetTransport->UpdateMaximumMtuSize();
raknetTransport->UpdateBandwidthLimit();
}

ChatPackets::SendSystemMessage(sysAddr, u"Successfully reloaded config for world!");
}

Expand Down
10 changes: 5 additions & 5 deletions dMasterServer/InstanceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "MasterPackets.h"
#include "BitStreamUtils.h"
#include "eConnectionType.h"
#include "eMasterMessageType.h"
#include "eManagerMessageType.h"

#include "Start.h"

Expand Down Expand Up @@ -157,7 +157,7 @@ void InstanceManager::ReadyInstance(Instance* instance) {
LOG("Responding to pending request %llu -> %i (%i)", request, zoneId.GetMapID(), zoneId.GetCloneID());

MasterPackets::SendZoneTransferResponse(
Game::server,
Game::server->GetTransportLayerPtr(),
request.sysAddr,
request.id,
request.mythranShift,
Expand All @@ -177,7 +177,7 @@ void InstanceManager::RequestAffirmation(Instance* instance, const PendingInstan

CBITSTREAM;

BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::AFFIRM_TRANSFER_REQUEST);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::AFFIRM_TRANSFER_REQUEST);

bitStream.Write(request.id);

Expand All @@ -200,7 +200,7 @@ void InstanceManager::AffirmTransfer(Instance* instance, const uint64_t transfer
const auto& zoneId = instance->GetZoneID();

MasterPackets::SendZoneTransferResponse(
Game::server,
Game::server->GetTransportLayerPtr(),
request.sysAddr,
request.id,
request.mythranShift,
Expand Down Expand Up @@ -359,7 +359,7 @@ bool Instance::GetShutdownComplete() const {
void Instance::Shutdown() {
CBITSTREAM;

BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SHUTDOWN);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::SHUTDOWN);

Game::server->Send(bitStream, this->m_SysAddr, false);

Expand Down
54 changes: 31 additions & 23 deletions dMasterServer/MasterServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "AssetManager.h"
#include "BinaryPathFinder.h"
#include "eConnectionType.h"
#include "eMasterMessageType.h"
#include "eManagerMessageType.h"

//RakNet includes:
#include "RakNetDefines.h"
Expand Down Expand Up @@ -295,7 +295,7 @@ int main(int argc, char** argv) {
const auto externalIPString = Game::config->GetValue("external_ip");
if (!externalIPString.empty()) ourIP = externalIPString;

Game::server = new dServer(ourIP, ourPort, 0, maxClients, true, false, Game::logger, "", 0, ServerType::Master, Game::config, &Game::lastSignal);
Game::server = new dServer(ourIP, ourPort, 0, maxClients, true, false, Game::logger, "", 0, ServerType::Manager, Game::config, &Game::lastSignal);

std::string master_server_ip = "localhost";
const auto masterServerIPString = Game::config->GetValue("master_ip");
Expand Down Expand Up @@ -473,20 +473,20 @@ void HandlePacket(Packet* packet) {
if (packet->length < 4) return;

if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::MASTER) {
switch (static_cast<eMasterMessageType>(packet->data[3])) {
case eMasterMessageType::REQUEST_PERSISTENT_ID: {
switch (static_cast<eManagerMessageType>(packet->data[3])) {
case eManagerMessageType::REQUEST_PERSISTENT_ID: {
LOG("A persistent ID req");
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);
uint64_t requestID = 0;
inStream.Read(requestID);

uint32_t objID = PersistentIDManager::GeneratePersistentID();
MasterPackets::SendPersistentIDResponse(Game::server, packet->systemAddress, requestID, objID);
MasterPackets::SendPersistentIDResponse(Game::server->GetTransportLayerPtr(), packet->systemAddress, requestID, objID);
break;
}

case eMasterMessageType::REQUEST_ZONE_TRANSFER: {
case eManagerMessageType::REQUEST_ZONE_TRANSFER: {
LOG("Received zone transfer req");
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);
Expand Down Expand Up @@ -522,7 +522,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::SERVER_INFO: {
case eManagerMessageType::SERVER_INFO: {
//MasterPackets::HandleServerInfo(packet);

//This is here because otherwise we'd have to include IM in
Expand All @@ -543,6 +543,8 @@ void HandlePacket(Packet* packet) {
inStream.Read(theirServerType);
inStream.Read(theirIP);

LOG("Received server info, %i %i %i %i %s", theirPort, theirZoneID, theirInstanceID, theirServerType, theirIP.string.c_str());

if (theirServerType == ServerType::World) {
if (!Game::im->IsPortInUse(theirPort)) {
Instance* in = new Instance(theirIP.string, theirPort, theirZoneID, theirInstanceID, 0, 12, 12);
Expand All @@ -551,12 +553,18 @@ void HandlePacket(Packet* packet) {
copy.binaryAddress = packet->systemAddress.binaryAddress;
copy.port = packet->systemAddress.port;

LOG("Adding instance %i %i on %s:%i (%s:%i)", theirZoneID, theirInstanceID, packet->systemAddress.ToString(false), packet->systemAddress.port, theirIP.string.c_str(), theirPort);

in->SetSysAddr(copy);
Game::im->AddInstance(in);
} else {
auto instance = Game::im->FindInstance(
auto* instance = Game::im->FindInstance(
theirZoneID, static_cast<uint16_t>(theirInstanceID));
if (instance) {
LOG("Setting address of instance %i %i from %s:%i to %s:%i (%s:%i)",
theirZoneID, theirInstanceID, instance->GetIP().c_str(), instance->GetPort(), packet->systemAddress.ToString(false), packet->systemAddress.port, theirIP.string.c_str(), theirPort
);

instance->SetSysAddr(packet->systemAddress);
}
}
Expand All @@ -583,7 +591,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::SET_SESSION_KEY: {
case eManagerMessageType::SET_SESSION_KEY: {
CINSTREAM_SKIP_HEADER;
uint32_t sessionKey = 0;
inStream.Read(sessionKey);
Expand All @@ -595,7 +603,7 @@ void HandlePacket(Packet* packet) {
activeSessions.erase(it.first);

CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::NEW_SESSION_ALERT);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::NEW_SESSION_ALERT);
bitStream.Write(sessionKey);
bitStream.Write(username);
SEND_PACKET_BROADCAST;
Expand All @@ -609,15 +617,15 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::REQUEST_SESSION_KEY: {
case eManagerMessageType::REQUEST_SESSION_KEY: {
CINSTREAM_SKIP_HEADER;
LUWString username;
inStream.Read(username);
LOG("Requesting session key for %s", username.GetAsString().c_str());
for (auto key : activeSessions) {
if (key.second == username.GetAsString()) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SESSION_KEY_RESPONSE);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::SESSION_KEY_RESPONSE);
bitStream.Write(key.first);
bitStream.Write(username);
Game::server->Send(bitStream, packet->systemAddress, false);
Expand All @@ -627,7 +635,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::PLAYER_ADDED: {
case eManagerMessageType::PLAYER_ADDED: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -647,7 +655,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::PLAYER_REMOVED: {
case eManagerMessageType::PLAYER_REMOVED: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -665,7 +673,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::CREATE_PRIVATE_ZONE: {
case eManagerMessageType::CREATE_PRIVATE_ZONE: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -689,7 +697,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::REQUEST_PRIVATE_ZONE: {
case eManagerMessageType::REQUEST_PRIVATE_ZONE: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand Down Expand Up @@ -719,12 +727,12 @@ void HandlePacket(Packet* packet) {

const auto& zone = instance->GetZoneID();

MasterPackets::SendZoneTransferResponse(Game::server, packet->systemAddress, requestID, static_cast<bool>(mythranShift), zone.GetMapID(), instance->GetInstanceID(), zone.GetCloneID(), instance->GetIP(), instance->GetPort());
MasterPackets::SendZoneTransferResponse(Game::server->GetTransportLayerPtr(), packet->systemAddress, requestID, static_cast<bool>(mythranShift), zone.GetMapID(), instance->GetInstanceID(), zone.GetCloneID(), instance->GetIP(), instance->GetPort());

break;
}

case eMasterMessageType::WORLD_READY: {
case eManagerMessageType::WORLD_READY: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -748,7 +756,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::PREP_ZONE: {
case eManagerMessageType::PREP_ZONE: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -764,7 +772,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::AFFIRM_TRANSFER_RESPONSE: {
case eManagerMessageType::AFFIRM_TRANSFER_RESPONSE: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -784,7 +792,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::SHUTDOWN_RESPONSE: {
case eManagerMessageType::SHUTDOWN_RESPONSE: {
RakNet::BitStream inStream(packet->data, packet->length, false);
uint64_t header = inStream.Read(header);

Expand All @@ -799,7 +807,7 @@ void HandlePacket(Packet* packet) {
break;
}

case eMasterMessageType::SHUTDOWN_UNIVERSE: {
case eManagerMessageType::SHUTDOWN_UNIVERSE: {
LOG("Received shutdown universe command, shutting down in 10 minutes.");
Game::universeShutdownRequested = true;
break;
Expand Down Expand Up @@ -829,7 +837,7 @@ int ShutdownSequence(int32_t signal) {

{
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SHUTDOWN);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::SHUTDOWN);
Game::server->Send(bitStream, UNASSIGNED_SYSTEM_ADDRESS, true);
LOG("Triggered master shutdown");
}
Expand Down
6 changes: 3 additions & 3 deletions dNet/AuthPackets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "eLoginResponse.h"
#include "eConnectionType.h"
#include "eServerMessageType.h"
#include "eMasterMessageType.h"
#include "eManagerMessageType.h"
#include "eGameMasterLevel.h"
#include "StringifiedEnum.h"
namespace {
Expand Down Expand Up @@ -60,7 +60,7 @@ void AuthPackets::HandleHandshake(dServer* server, Packet* packet) {

uint16_t port;
inStream.Read(port);
if (port != packet->systemAddress.port) LOG("WARNING: Port written in packet does not match the port the client is connecting over!");
if (port != packet->systemAddress.port) LOG("WARNING: Port written in packet does not match the port the client is connecting over, %i != %i!", port, packet->systemAddress.port);

inStream.IgnoreBytes(33);

Expand Down Expand Up @@ -297,7 +297,7 @@ void AuthPackets::SendLoginResponse(dServer* server, const SystemAddress& sysAdd
//Inform the master server that we've created a session for this user:
if (responseCode == eLoginResponse::SUCCESS) {
CBITSTREAM;
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eMasterMessageType::SET_SESSION_KEY);
BitStreamUtils::WriteHeader(bitStream, eConnectionType::MASTER, eManagerMessageType::SET_SESSION_KEY);
bitStream.Write(sessionKey);
bitStream.Write(LUString(username));
server->SendToMaster(bitStream);
Expand Down
Loading

0 comments on commit 4004534

Please sign in to comment.