mirror of
https://github.com/DarkflameUniverse/DarkflameServer
synced 2024-08-30 18:43:58 +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);
|
||||
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
|
||||
|
||||
int softCap = 8;
|
||||
@ -238,7 +247,7 @@ void InstanceManager::RedirectPendingRequests(Instance* instance) {
|
||||
for (const auto& request : instance->GetPendingAffirmations()) {
|
||||
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);
|
||||
|
||||
@ -295,6 +304,15 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
|
||||
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;
|
||||
|
||||
uint32_t port = GetFreePort();
|
||||
|
@ -128,6 +128,7 @@ public:
|
||||
|
||||
Instance* CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID cloneID, const std::string& password);
|
||||
Instance* FindPrivateInstance(const std::string& password);
|
||||
void SetIsShuttingDown(bool value) { this->m_IsShuttingDown = value; };
|
||||
|
||||
private:
|
||||
dLogger* mLogger;
|
||||
@ -136,6 +137,11 @@ private:
|
||||
unsigned short m_LastPort;
|
||||
LWOINSTANCEID m_LastInstanceID;
|
||||
|
||||
/**
|
||||
* Whether or not the master server is currently shutting down.
|
||||
*/
|
||||
bool m_IsShuttingDown = false;
|
||||
|
||||
//Private functions:
|
||||
bool IsInstanceFull(Instance* instance, bool isFriendTransfer);
|
||||
int GetSoftCap(LWOMAPID mapID);
|
||||
|
@ -63,6 +63,8 @@ SystemAddress authServerMasterPeerSysAddr;
|
||||
SystemAddress chatServerMasterPeerSysAddr;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
constexpr uint32_t masterFramerate = mediumFramerate;
|
||||
constexpr uint32_t masterFrameDelta = mediumFrameDelta;
|
||||
Diagnostics::SetProcessName("Master");
|
||||
Diagnostics::SetProcessFileName(argv[0]);
|
||||
Diagnostics::Initialize();
|
||||
@ -287,6 +289,10 @@ int main(int argc, char** argv) {
|
||||
|
||||
auto t = std::chrono::high_resolution_clock::now();
|
||||
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 framesSinceLastSQLPing = 0;
|
||||
int framesSinceKillUniverseCommand = 0;
|
||||
@ -303,14 +309,14 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
//Push our log every 15s:
|
||||
if (framesSinceLastFlush >= 900) {
|
||||
if (framesSinceLastFlush >= logFlushTime) {
|
||||
Game::logger->Flush();
|
||||
framesSinceLastFlush = 0;
|
||||
} else
|
||||
framesSinceLastFlush++;
|
||||
|
||||
//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:
|
||||
std::string masterIP;
|
||||
int masterPort;
|
||||
@ -330,7 +336,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
//10m shutdown for universe kill command
|
||||
if (Game::shouldShutdown) {
|
||||
if (framesSinceKillUniverseCommand >= 40000) {
|
||||
if (framesSinceKillUniverseCommand >= shutdownUniverseTime) {
|
||||
//Break main loop and exit
|
||||
break;
|
||||
} else
|
||||
@ -354,7 +360,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
instance->SetAffirmationTimeout(affirmTimeout);
|
||||
|
||||
if (affirmTimeout == 1000) {
|
||||
if (affirmTimeout == instanceReadyTimeout) {
|
||||
instance->Shutdown();
|
||||
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);
|
||||
}
|
||||
return FinalizeShutdown(EXIT_SUCCESS);
|
||||
@ -424,7 +430,6 @@ void HandlePacket(Packet* packet) {
|
||||
if (instance) {
|
||||
LWOZONEID zoneID = instance->GetZoneID(); //Get the zoneID so we can recreate a server
|
||||
Game::im->RemoveInstance(instance); //Delete the old
|
||||
//Game::im->GetInstance(zoneID.GetMapID(), false, 0); //Create the new
|
||||
}
|
||||
|
||||
if (packet->systemAddress == chatServerMasterPeerSysAddr) {
|
||||
@ -465,14 +470,17 @@ void HandlePacket(Packet* packet) {
|
||||
inStream.Read(mythranShift);
|
||||
inStream.Read(zoneID);
|
||||
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);
|
||||
|
||||
for (auto* instance : Game::im->GetInstances()) {
|
||||
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 });
|
||||
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;
|
||||
inStream.Read(zoneID);
|
||||
|
||||
Game::logger->Log("MasterServer", "Prepping zone %i", zoneID);
|
||||
Game::im->GetInstance(zoneID, false, 0);
|
||||
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::im->GetInstance(zoneID, false, 0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -770,8 +782,8 @@ void HandlePacket(Packet* packet) {
|
||||
default:
|
||||
Game::logger->Log("MasterServer", "Unknown master packet ID from server: %i", packet->data[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StartChatServer() {
|
||||
if (Game::shouldShutdown) {
|
||||
@ -788,9 +800,9 @@ void StartChatServer() {
|
||||
auto result = system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
|
||||
} else {
|
||||
auto result = system(((BinaryPathFinder::GetBinaryDir() / "ChatServer").string() + "&").c_str());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void StartAuthServer() {
|
||||
if (Game::shouldShutdown) {
|
||||
@ -806,7 +818,7 @@ void StartAuthServer() {
|
||||
auto result = system(("sudo " + (BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
|
||||
} else {
|
||||
auto result = system(((BinaryPathFinder::GetBinaryDir() / "AuthServer").string() + "&").c_str());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -815,6 +827,11 @@ void ShutdownSequence(int signal) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Game::im) {
|
||||
FinalizeShutdown(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
Game::im->SetIsShuttingDown(true);
|
||||
shutdownSequenceStarted = true;
|
||||
Game::shouldShutdown = true;
|
||||
|
||||
@ -826,40 +843,38 @@ void ShutdownSequence(int signal) {
|
||||
}
|
||||
|
||||
auto* objIdManager = ObjectIDManager::TryInstance();
|
||||
if (objIdManager != nullptr) {
|
||||
if (objIdManager) {
|
||||
objIdManager->SaveToDatabase();
|
||||
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.
|
||||
for (auto instance : Game::im->GetInstances()) {
|
||||
for (auto* instance : Game::im->GetInstances()) {
|
||||
if (!instance->GetIsReady()) {
|
||||
Game::im->RemoveInstance(instance);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto instance : Game::im->GetInstances()) {
|
||||
for (auto* instance : Game::im->GetInstances()) {
|
||||
instance->SetIsShuttingDown(true);
|
||||
}
|
||||
|
||||
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) {
|
||||
auto packet = Game::server->Receive();
|
||||
packet = Game::server->Receive();
|
||||
if (packet) {
|
||||
HandlePacket(packet);
|
||||
Game::server->DeallocatePacket(packet);
|
||||
packet = nullptr;
|
||||
}
|
||||
|
||||
auto done = true;
|
||||
allInstancesShutdown = true;
|
||||
|
||||
for (auto* instance : Game::im->GetInstances()) {
|
||||
if (instance == nullptr) {
|
||||
@ -867,21 +882,21 @@ void ShutdownSequence(int signal) {
|
||||
}
|
||||
|
||||
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!");
|
||||
break;
|
||||
}
|
||||
|
||||
t += std::chrono::milliseconds(highFrameDelta);
|
||||
t += std::chrono::milliseconds(mediumFrameDelta);
|
||||
std::this_thread::sleep_until(t);
|
||||
|
||||
ticks++;
|
||||
framesSinceShutdownStart++;
|
||||
|
||||
if (ticks == 600 * 6) {
|
||||
if (framesSinceShutdownStart == maxShutdownTime) {
|
||||
Game::logger->Log("MasterServer", "Finished shutting down by timeout!");
|
||||
break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user