mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2024-11-10 02:08:20 +00:00
Remove magic numbers
Replace magic numbers with constexpr calculated times. Tested that trying to create a new instance while shutting down doesn't allow a new instance to be created.
This commit is contained in:
parent
4775dbf27f
commit
3c581fffbb
@ -31,6 +31,15 @@ Instance* InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, LW
|
|||||||
Instance* instance = FindInstance(mapID, isFriendTransfer, cloneID);
|
Instance* instance = FindInstance(mapID, isFriendTransfer, cloneID);
|
||||||
if (instance) return instance;
|
if (instance) return instance;
|
||||||
|
|
||||||
|
// If we are shutting down, return a nullptr so a new instance is not created.
|
||||||
|
if (m_IsShuttingDown) {
|
||||||
|
Game::logger->Log("InstanceManager",
|
||||||
|
"Tried to create a new instance map/instance/clone %i/%i/%i, but Master is shutting down.",
|
||||||
|
mapID,
|
||||||
|
m_LastInstanceID + 1,
|
||||||
|
cloneID);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
//TODO: Update this so that the IP is read from a configuration file instead
|
//TODO: Update this so that the IP is read from a configuration file instead
|
||||||
|
|
||||||
int softCap = 8;
|
int softCap = 8;
|
||||||
@ -238,7 +247,7 @@ void InstanceManager::RedirectPendingRequests(Instance* instance) {
|
|||||||
for (const auto& request : instance->GetPendingAffirmations()) {
|
for (const auto& request : instance->GetPendingAffirmations()) {
|
||||||
auto* in = Game::im->GetInstance(zoneId.GetMapID(), false, zoneId.GetCloneID());
|
auto* in = Game::im->GetInstance(zoneId.GetMapID(), false, zoneId.GetCloneID());
|
||||||
|
|
||||||
if (!in->GetIsReady()) // Instance not ready, make a pending request
|
if (in && !in->GetIsReady()) // Instance not ready, make a pending request
|
||||||
{
|
{
|
||||||
in->GetPendingRequests().push_back(request);
|
in->GetPendingRequests().push_back(request);
|
||||||
|
|
||||||
@ -295,6 +304,15 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_IsShuttingDown) {
|
||||||
|
Game::logger->Log("InstanceManager",
|
||||||
|
"Tried to create a new private instance map/instance/clone %i/%i/%i, but Master is shutting down.",
|
||||||
|
mapID,
|
||||||
|
m_LastInstanceID + 1,
|
||||||
|
cloneID);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
int maxPlayers = 999;
|
int maxPlayers = 999;
|
||||||
|
|
||||||
uint32_t port = GetFreePort();
|
uint32_t port = GetFreePort();
|
||||||
|
@ -128,6 +128,7 @@ public:
|
|||||||
|
|
||||||
Instance* CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID cloneID, const std::string& password);
|
Instance* CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID cloneID, const std::string& password);
|
||||||
Instance* FindPrivateInstance(const std::string& password);
|
Instance* FindPrivateInstance(const std::string& password);
|
||||||
|
void SetIsShuttingDown(bool value) { this->m_IsShuttingDown = value; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
dLogger* mLogger;
|
dLogger* mLogger;
|
||||||
@ -136,6 +137,11 @@ private:
|
|||||||
unsigned short m_LastPort;
|
unsigned short m_LastPort;
|
||||||
LWOINSTANCEID m_LastInstanceID;
|
LWOINSTANCEID m_LastInstanceID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether or not the master server is currently shutting down.
|
||||||
|
*/
|
||||||
|
bool m_IsShuttingDown = false;
|
||||||
|
|
||||||
//Private functions:
|
//Private functions:
|
||||||
bool IsInstanceFull(Instance* instance, bool isFriendTransfer);
|
bool IsInstanceFull(Instance* instance, bool isFriendTransfer);
|
||||||
int GetSoftCap(LWOMAPID mapID);
|
int GetSoftCap(LWOMAPID mapID);
|
||||||
|
@ -63,6 +63,8 @@ SystemAddress authServerMasterPeerSysAddr;
|
|||||||
SystemAddress chatServerMasterPeerSysAddr;
|
SystemAddress chatServerMasterPeerSysAddr;
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
constexpr uint32_t masterFramerate = mediumFramerate;
|
||||||
|
constexpr uint32_t masterFrameDelta = mediumFrameDelta;
|
||||||
Diagnostics::SetProcessName("Master");
|
Diagnostics::SetProcessName("Master");
|
||||||
Diagnostics::SetProcessFileName(argv[0]);
|
Diagnostics::SetProcessFileName(argv[0]);
|
||||||
Diagnostics::Initialize();
|
Diagnostics::Initialize();
|
||||||
@ -287,6 +289,10 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
auto t = std::chrono::high_resolution_clock::now();
|
auto t = std::chrono::high_resolution_clock::now();
|
||||||
Packet* packet = nullptr;
|
Packet* packet = nullptr;
|
||||||
|
constexpr uint32_t logFlushTime = 15 * masterFramerate;
|
||||||
|
constexpr uint32_t sqlPingTime = 10 * 60 * masterFramerate;
|
||||||
|
constexpr uint32_t shutdownUniverseTime = 10 * 60 * masterFramerate;
|
||||||
|
constexpr uint32_t instanceReadyTimeout = 30 * masterFramerate;
|
||||||
int framesSinceLastFlush = 0;
|
int framesSinceLastFlush = 0;
|
||||||
int framesSinceLastSQLPing = 0;
|
int framesSinceLastSQLPing = 0;
|
||||||
int framesSinceKillUniverseCommand = 0;
|
int framesSinceKillUniverseCommand = 0;
|
||||||
@ -303,14 +309,14 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Push our log every 15s:
|
//Push our log every 15s:
|
||||||
if (framesSinceLastFlush >= 900) {
|
if (framesSinceLastFlush >= logFlushTime) {
|
||||||
Game::logger->Flush();
|
Game::logger->Flush();
|
||||||
framesSinceLastFlush = 0;
|
framesSinceLastFlush = 0;
|
||||||
} else
|
} else
|
||||||
framesSinceLastFlush++;
|
framesSinceLastFlush++;
|
||||||
|
|
||||||
//Every 10 min we ping our sql server to keep it alive hopefully:
|
//Every 10 min we ping our sql server to keep it alive hopefully:
|
||||||
if (framesSinceLastSQLPing >= 40000) {
|
if (framesSinceLastSQLPing >= sqlPingTime) {
|
||||||
//Find out the master's IP for absolutely no reason:
|
//Find out the master's IP for absolutely no reason:
|
||||||
std::string masterIP;
|
std::string masterIP;
|
||||||
int masterPort;
|
int masterPort;
|
||||||
@ -330,7 +336,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
//10m shutdown for universe kill command
|
//10m shutdown for universe kill command
|
||||||
if (Game::shouldShutdown) {
|
if (Game::shouldShutdown) {
|
||||||
if (framesSinceKillUniverseCommand >= 40000) {
|
if (framesSinceKillUniverseCommand >= shutdownUniverseTime) {
|
||||||
//Break main loop and exit
|
//Break main loop and exit
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
@ -354,7 +360,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
instance->SetAffirmationTimeout(affirmTimeout);
|
instance->SetAffirmationTimeout(affirmTimeout);
|
||||||
|
|
||||||
if (affirmTimeout == 1000) {
|
if (affirmTimeout == instanceReadyTimeout) {
|
||||||
instance->Shutdown();
|
instance->Shutdown();
|
||||||
instance->SetIsShuttingDown(true);
|
instance->SetIsShuttingDown(true);
|
||||||
|
|
||||||
@ -373,7 +379,7 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t += std::chrono::milliseconds(highFrameDelta);
|
t += std::chrono::milliseconds(masterFrameDelta);
|
||||||
std::this_thread::sleep_until(t);
|
std::this_thread::sleep_until(t);
|
||||||
}
|
}
|
||||||
return FinalizeShutdown(EXIT_SUCCESS);
|
return FinalizeShutdown(EXIT_SUCCESS);
|
||||||
@ -424,7 +430,6 @@ void HandlePacket(Packet* packet) {
|
|||||||
if (instance) {
|
if (instance) {
|
||||||
LWOZONEID zoneID = instance->GetZoneID(); //Get the zoneID so we can recreate a server
|
LWOZONEID zoneID = instance->GetZoneID(); //Get the zoneID so we can recreate a server
|
||||||
Game::im->RemoveInstance(instance); //Delete the old
|
Game::im->RemoveInstance(instance); //Delete the old
|
||||||
//Game::im->GetInstance(zoneID.GetMapID(), false, 0); //Create the new
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packet->systemAddress == chatServerMasterPeerSysAddr) {
|
if (packet->systemAddress == chatServerMasterPeerSysAddr) {
|
||||||
@ -465,14 +470,17 @@ void HandlePacket(Packet* packet) {
|
|||||||
inStream.Read(mythranShift);
|
inStream.Read(mythranShift);
|
||||||
inStream.Read(zoneID);
|
inStream.Read(zoneID);
|
||||||
inStream.Read(zoneClone);
|
inStream.Read(zoneClone);
|
||||||
|
if (shutdownSequenceStarted) {
|
||||||
|
Game::logger->Log("MasterServer", "Shutdown sequence has been started. Not creating a new zone.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
Instance* in = Game::im->GetInstance(zoneID, false, zoneClone);
|
Instance* in = Game::im->GetInstance(zoneID, false, zoneClone);
|
||||||
|
|
||||||
for (auto* instance : Game::im->GetInstances()) {
|
for (auto* instance : Game::im->GetInstances()) {
|
||||||
Game::logger->Log("MasterServer", "Instance: %i/%i/%i -> %i", instance->GetMapID(), instance->GetCloneID(), instance->GetInstanceID(), instance == in);
|
Game::logger->Log("MasterServer", "Instance: %i/%i/%i -> %i", instance->GetMapID(), instance->GetCloneID(), instance->GetInstanceID(), instance == in);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in->GetIsReady()) //Instance not ready, make a pending request
|
if (in && !in->GetIsReady()) //Instance not ready, make a pending request
|
||||||
{
|
{
|
||||||
in->GetPendingRequests().push_back({ requestID, static_cast<bool>(mythranShift), packet->systemAddress });
|
in->GetPendingRequests().push_back({ requestID, static_cast<bool>(mythranShift), packet->systemAddress });
|
||||||
Game::logger->Log("MasterServer", "Server not ready, adding pending request %llu %i %i", requestID, zoneID, zoneClone);
|
Game::logger->Log("MasterServer", "Server not ready, adding pending request %llu %i %i", requestID, zoneID, zoneClone);
|
||||||
@ -720,9 +728,13 @@ void HandlePacket(Packet* packet) {
|
|||||||
|
|
||||||
int zoneID;
|
int zoneID;
|
||||||
inStream.Read(zoneID);
|
inStream.Read(zoneID);
|
||||||
|
if (shutdownSequenceStarted) {
|
||||||
|
Game::logger->Log("MasterServer", "Shutdown sequence has been started. Not prepping a new zone.");
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
Game::logger->Log("MasterServer", "Prepping zone %i", zoneID);
|
Game::logger->Log("MasterServer", "Prepping zone %i", zoneID);
|
||||||
Game::im->GetInstance(zoneID, false, 0);
|
Game::im->GetInstance(zoneID, false, 0);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -771,7 +783,7 @@ void HandlePacket(Packet* packet) {
|
|||||||
Game::logger->Log("MasterServer", "Unknown master packet ID from server: %i", packet->data[3]);
|
Game::logger->Log("MasterServer", "Unknown master packet ID from server: %i", packet->data[3]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StartChatServer() {
|
void StartChatServer() {
|
||||||
if (Game::shouldShutdown) {
|
if (Game::shouldShutdown) {
|
||||||
@ -788,9 +800,9 @@ void StartChatServer() {
|
|||||||
auto result = system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
|
auto result = system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
|
||||||
} else {
|
} else {
|
||||||
auto result = system(((BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
|
auto result = system(((BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void StartAuthServer() {
|
void StartAuthServer() {
|
||||||
if (Game::shouldShutdown) {
|
if (Game::shouldShutdown) {
|
||||||
@ -806,7 +818,7 @@ void StartAuthServer() {
|
|||||||
auto result = system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
|
auto result = system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
|
||||||
} else {
|
} else {
|
||||||
auto result = system(((BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
|
auto result = system(((BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -815,6 +827,11 @@ void ShutdownSequence(int signal) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Game::im) {
|
||||||
|
FinalizeShutdown(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
Game::im->SetIsShuttingDown(true);
|
||||||
shutdownSequenceStarted = true;
|
shutdownSequenceStarted = true;
|
||||||
Game::shouldShutdown = true;
|
Game::shouldShutdown = true;
|
||||||
|
|
||||||
@ -826,40 +843,38 @@ void ShutdownSequence(int signal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto* objIdManager = ObjectIDManager::TryInstance();
|
auto* objIdManager = ObjectIDManager::TryInstance();
|
||||||
if (objIdManager != nullptr) {
|
if (objIdManager) {
|
||||||
objIdManager->SaveToDatabase();
|
objIdManager->SaveToDatabase();
|
||||||
Game::logger->Log("MasterServer", "Saved ObjectIDTracker to DB");
|
Game::logger->Log("MasterServer", "Saved ObjectIDTracker to DB");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto t = std::chrono::high_resolution_clock::now();
|
|
||||||
auto ticks = 0;
|
|
||||||
|
|
||||||
if (!Game::im) {
|
|
||||||
FinalizeShutdown(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// A server might not be finished spinning up yet, remove all of those here.
|
// A server might not be finished spinning up yet, remove all of those here.
|
||||||
for (auto instance : Game::im->GetInstances()) {
|
for (auto* instance : Game::im->GetInstances()) {
|
||||||
if (!instance->GetIsReady()) {
|
if (!instance->GetIsReady()) {
|
||||||
Game::im->RemoveInstance(instance);
|
Game::im->RemoveInstance(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto instance : Game::im->GetInstances()) {
|
for (auto* instance : Game::im->GetInstances()) {
|
||||||
instance->SetIsShuttingDown(true);
|
instance->SetIsShuttingDown(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::logger->Log("MasterServer", "Attempting to shutdown instances, max 60 seconds...");
|
Game::logger->Log("MasterServer", "Attempting to shutdown instances, max 60 seconds...");
|
||||||
|
|
||||||
|
auto t = std::chrono::high_resolution_clock::now();
|
||||||
|
uint32_t framesSinceShutdownStart = 0;
|
||||||
|
constexpr uint32_t maxShutdownTime = 60 * mediumFramerate;
|
||||||
|
bool allInstancesShutdown = false;
|
||||||
|
Packet* packet = nullptr;
|
||||||
while (true) {
|
while (true) {
|
||||||
auto packet = Game::server->Receive();
|
packet = Game::server->Receive();
|
||||||
if (packet) {
|
if (packet) {
|
||||||
HandlePacket(packet);
|
HandlePacket(packet);
|
||||||
Game::server->DeallocatePacket(packet);
|
Game::server->DeallocatePacket(packet);
|
||||||
packet = nullptr;
|
packet = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto done = true;
|
allInstancesShutdown = true;
|
||||||
|
|
||||||
for (auto* instance : Game::im->GetInstances()) {
|
for (auto* instance : Game::im->GetInstances()) {
|
||||||
if (instance == nullptr) {
|
if (instance == nullptr) {
|
||||||
@ -867,21 +882,21 @@ void ShutdownSequence(int signal) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!instance->GetShutdownComplete()) {
|
if (!instance->GetShutdownComplete()) {
|
||||||
done = false;
|
allInstancesShutdown = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (done && authServerMasterPeerSysAddr == UNASSIGNED_SYSTEM_ADDRESS && chatServerMasterPeerSysAddr == UNASSIGNED_SYSTEM_ADDRESS) {
|
if (allInstancesShutdown && authServerMasterPeerSysAddr == UNASSIGNED_SYSTEM_ADDRESS && chatServerMasterPeerSysAddr == UNASSIGNED_SYSTEM_ADDRESS) {
|
||||||
Game::logger->Log("MasterServer", "Finished shutting down MasterServer!");
|
Game::logger->Log("MasterServer", "Finished shutting down MasterServer!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
t += std::chrono::milliseconds(highFrameDelta);
|
t += std::chrono::milliseconds(mediumFrameDelta);
|
||||||
std::this_thread::sleep_until(t);
|
std::this_thread::sleep_until(t);
|
||||||
|
|
||||||
ticks++;
|
framesSinceShutdownStart++;
|
||||||
|
|
||||||
if (ticks == 600 * 6) {
|
if (framesSinceShutdownStart == maxShutdownTime) {
|
||||||
Game::logger->Log("MasterServer", "Finished shutting down by timeout!");
|
Game::logger->Log("MasterServer", "Finished shutting down by timeout!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user