Skip to content

Commit

Permalink
Modified ghosts to exist in memory cache only, avoid writing them to …
Browse files Browse the repository at this point in the history
…database, they can get costly.
  • Loading branch information
TLeonardUK committed Mar 18, 2024
1 parent ea239bd commit 0fe30e8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ MessageHandleResult DS2_GhostManager::OnMessageRecieved(GameClient* Client, cons

MessageHandleResult DS2_GhostManager::Handle_RequestCreateGhostData(GameClient* Client, const Frpg2ReliableUdpMessage& Message)
{
const RuntimeConfig& Config = ServerInstance->GetConfig();
ServerDatabase& Database = ServerInstance->GetDatabase();
PlayerState& Player = Client->GetPlayerState();

Expand All @@ -89,7 +90,28 @@ MessageHandleResult DS2_GhostManager::Handle_RequestCreateGhostData(GameClient*
std::vector<uint8_t> Data;
Data.assign(Request->data().data(), Request->data().data() + Request->data().size());

if (std::shared_ptr<Ghost> ActiveGhost = Database.CreateGhost((uint32_t)Request->online_area_id(), (uint64_t)Request->cell_id(), Player.GetPlayerId(), Player.GetSteamId(), Data))
std::shared_ptr<Ghost> ActiveGhost = nullptr;
if (Config.GhostMemoryCacheOnly)
{
ActiveGhost = std::make_shared<Ghost>();
ActiveGhost->GhostId = (uint32_t)NextMemoryCacheGhostId--;
ActiveGhost->OnlineAreaId = (uint32_t)Request->online_area_id();
ActiveGhost->CellId = (uint64_t)Request->cell_id();
ActiveGhost->PlayerId = Player.GetPlayerId();
ActiveGhost->PlayerSteamId = Player.GetSteamId();
ActiveGhost->Data = Data;
}
else
{
ActiveGhost = Database.CreateGhost(
(uint32_t)Request->online_area_id(),
(uint64_t)Request->cell_id(),
Player.GetPlayerId(),
Player.GetSteamId(),
Data);
}

if (ActiveGhost)
{
LiveCache.Add(ActiveGhost->CellId, ActiveGhost->GhostId, ActiveGhost);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ class DS2_GhostManager

OnlineAreaPool<uint64_t, Ghost> LiveCache;

uint32_t NextMemoryCacheGhostId = std::numeric_limits<uint32_t>::max();

};
1 change: 1 addition & 0 deletions Source/Server/Config/RuntimeConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ bool RuntimeConfig::Serialize(nlohmann::json& Json, bool Loading)
SERIALIZE_VAR(GhostMaxLivePoolEntriesPerArea);
SERIALIZE_VAR(GhostPrimeCountPerArea);
SERIALIZE_VAR(GhostPrimeCountPerArea);
SERIALIZE_VAR(GhostMemoryCacheOnly);
SERIALIZE_VAR(GhostMaxDatabaseEntries);
SERIALIZE_VAR(QuickMatchWinXp);
SERIALIZE_VAR(QuickMatchLoseXp);
Expand Down
6 changes: 5 additions & 1 deletion Source/Server/Config/RuntimeConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ class RuntimeConfig
// re-enter their messages.
int GhostPrimeCountPerArea = 50;

// If set to true ghosts will only be stored in the memory-cache, and not persistently
// on disk. This can reduce database size and query costs as they can be quite spammily created.
bool GhostMemoryCacheOnly = true;

// This should be for all intents and purposes infinite. But you can limit
// it if you so wish. Bare in mind that players may see signs that no longer
// exist on the server.
Expand Down Expand Up @@ -305,7 +309,7 @@ class RuntimeConfig

// How frequently (in seconds) the clients should send PlayerStatus updates. Increase this to
// reduce network bandwidth. Client clamps this to a minimum of 5.
float PlayerStatusUploadInterval = 5.0f;
float PlayerStatusUploadInterval = 15.0f;

// How much delay (in seconds) should be placed on RequestUpdatePlayerCharacter calls. Clamped to 60->50000
float PlayerCharacterUpdateSendDelay = 600.0f;
Expand Down
9 changes: 7 additions & 2 deletions Source/Server/Server/AuthService/AuthClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,22 @@ bool AuthClient::Poll()

if constexpr (BuildConfig::AUTH_ENABLED)
{
double Start = GetHighResolutionSeconds();

int AuthResult = SteamGameServer()->BeginAuthSession(Ticket.data(), (int)Ticket.size(), SteamIdStruct);
SteamGameServer()->EndAuthSession(SteamIdStruct);

double Elapsed = GetHighResolutionSeconds() - Start;

if (AuthResult != k_EBeginAuthSessionResultOK)
{
WarningS(GetName().c_str(), "Disconnecting client as steam ticket authentication failed with error %i.", AuthResult);
return true;
}
else
{
VerboseS(GetName().c_str(), "Client steam ticket authenticated successfully.");
LogS(GetName().c_str(), "Client steam ticket authenticated successfully in %.2f seconds.");
}
SteamGameServer()->EndAuthSession(SteamIdStruct);
}

// If user IP is on a private network, we can assume they are on our LAN
Expand Down
5 changes: 3 additions & 2 deletions Source/Server/Server/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void ServerManager::RunUntilQuit()
PlayerCount += Server->GetService<GameService>()->GetClients().size();
}

WriteLog(true, ConsoleColor::Grey, "", "Log", "%zi players | %zi servers | %.2f ms update | connections auth %.2f login %.2f game %.2f p/s | tcp in %.2f out %.2f kb/s | udp in %.2f out %.2f kb/s ",
WriteLog(true, ConsoleColor::Grey, "", "Log", "%zi players | %zi servers | %.2f ms update | connections auth %.2f login %.2f game %.2f p/s | tcp in %.2f out %.2f kb/s | udp in %.2f out %.2f kb/s | database queries %.2f p/s ",
PlayerCount,
ServerInstances.size(),
Debug::AllServerUpdateTime.GetAverage(),
Expand All @@ -141,7 +141,8 @@ void ServerManager::RunUntilQuit()
(Debug::TcpBytesRecieved.GetAverageRate()) / 1024.0f,
(Debug::TcpBytesSent.GetAverageRate()) / 1024.0f,
(Debug::UdpBytesRecieved.GetAverageRate()) / 1024.0f,
(Debug::UdpBytesSent.GetAverageRate()) / 1024.0f
(Debug::UdpBytesSent.GetAverageRate()) / 1024.0f,
Debug::DatabaseQueries.GetAverageRate()
);

LastStatsPrint = GetSeconds();
Expand Down
4 changes: 2 additions & 2 deletions Source/Shared/Core/Utils/DebugObjects.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ TIMER(LoginService_PollTime, "Login Service (Poll Time)")
TIMER(DatabaseQueryTime, "Database Query Time")
TIMER(AntiCheatTime, "Anti-Cheat Time")

COUNTER(DatabaseQueries, "Database Queries")

COUNTER(AuthConnections, "Auth Connections")
COUNTER(LoginConnections, "Login Connections")
COUNTER(GameConnections, "Game Connections")
Expand All @@ -28,5 +30,3 @@ COUNTER(UdpBytesSent, "UDP Bytes Sent")
COUNTER(RequestsRecieved, "Requests Recieved")
COUNTER(ResponsesSent, "Responses Sent")
COUNTER(PushMessagesSent, "Push Messages Sent")

COUNTER(DatabaseQueries, "Database Queries")

0 comments on commit 0fe30e8

Please sign in to comment.