Merge branch 'main' into Log-Line-Cleaning

This commit is contained in:
cooltrain7
2021-12-28 18:46:44 +00:00
65 changed files with 1026 additions and 534 deletions

View File

@@ -43,7 +43,8 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L
maxPlayers = GetHardCap(mapID);
}
instance = new Instance(mExternalIP, ++m_LastPort, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers);
uint32_t port = GetFreePort();
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, softCap, maxPlayers);
//Start the actual process:
#ifdef _WIN32
@@ -59,7 +60,7 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L
cmd.append(std::to_string(mapID));
cmd.append(" -port ");
cmd.append(std::to_string(m_LastPort));
cmd.append(std::to_string(port));
cmd.append(" -instance ");
cmd.append(std::to_string(m_LastInstanceID));
cmd.append(" -maxclients ");
@@ -74,8 +75,6 @@ Instance * InstanceManager::GetInstance(LWOMAPID mapID, bool isFriendTransfer, L
system(cmd.c_str());
m_LastPort++; //Increment it again because the next port is for World<->Server comm.
m_LastPort++; //Increment it again because the next port is for World<->Chat comm.
m_Instances.push_back(instance);
if (instance) {
@@ -97,6 +96,25 @@ bool InstanceManager::IsPortInUse(uint32_t port) {
return false;
}
uint32_t InstanceManager::GetFreePort() {
uint32_t port = m_LastPort;
std::vector<uint32_t> usedPorts;
for (Instance* i : m_Instances) {
usedPorts.push_back(i->GetPort());
}
std::sort(usedPorts.begin(), usedPorts.end());
int portIdx = 0;
while (portIdx < usedPorts.size() && port == usedPorts[portIdx]) {
//increment by 3 since each instance uses 3 ports (instance, world-server, world-chat)
port += 3;
portIdx++;
}
return port;
}
void InstanceManager::AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID) {
Instance* inst = FindInstance(mapID, instanceID);
if (inst) {
@@ -291,7 +309,8 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
int maxPlayers = 999;
instance = new Instance(mExternalIP, ++m_LastPort, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password);
uint32_t port = GetFreePort();
instance = new Instance(mExternalIP, port, mapID, ++m_LastInstanceID, cloneID, maxPlayers, maxPlayers, true, password);
//Start the actual process:
std::string cmd = "start ./WorldServer.exe -zone ";
@@ -302,7 +321,7 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
cmd.append(std::to_string(mapID));
cmd.append(" -port ");
cmd.append(std::to_string(m_LastPort));
cmd.append(std::to_string(port));
cmd.append(" -instance ");
cmd.append(std::to_string(m_LastInstanceID));
cmd.append(" -maxclients ");
@@ -317,8 +336,6 @@ Instance* InstanceManager::CreatePrivateInstance(LWOMAPID mapID, LWOCLONEID clon
system(cmd.c_str());
m_LastPort++; //Increment it again because the next port is for World<->Server comm.
m_LastPort++; //Increment it again because the next port is for World<->Chat comm.
m_Instances.push_back(instance);
if (instance) return instance;

View File

@@ -102,6 +102,7 @@ public:
Instance* GetInstance(LWOMAPID mapID, bool isFriendTransfer, LWOCLONEID cloneID); //Creates an instance if none found
bool IsPortInUse(uint32_t port);
uint32_t GetFreePort();
void AddPlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID);
void RemovePlayer(SystemAddress systemAddr, LWOMAPID mapID, LWOINSTANCEID instanceID);

View File

@@ -47,9 +47,12 @@ namespace Game {
bool shutdownSequenceStarted = false;
void ShutdownSequence();
dLogger* SetupLogger();
void StartAuthServer();
void StartChatServer();
void HandlePacket(Packet* packet);
std::map<uint32_t, std::string> activeSessions;
bool shouldShutdown = false;
SystemAddress chatServerMasterPeerSysAddr;
int main(int argc, char** argv) {
Diagnostics::SetProcessName("Master");
@@ -72,6 +75,7 @@ int main(int argc, char** argv) {
dConfig config("masterconfig.ini");
Game::config = &config;
Game::logger->SetLogToConsole(bool(std::stoi(config.GetValue("log_to_console"))));
Game::logger->SetLogDebugStatements(config.GetValue("log_debug_statements") == "1");
//Connect to CDClient
try {
@@ -181,35 +185,12 @@ int main(int argc, char** argv) {
//Depending on the config, start up servers:
if (config.GetValue("prestart_servers") != "" && config.GetValue("prestart_servers") == "1") {
#ifdef __APPLE__
//macOS doesn't need sudo to run on ports < 1024
system("./ChatServer&");
#elif _WIN32
system("start ./ChatServer.exe");
#else
if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) {
system("sudo ./ChatServer&");
}
else {
system("./ChatServer&");
}
#endif
StartChatServer();
Game::im->GetInstance(0, false, 0)->SetIsReady(true);
Game::im->GetInstance(1000, false, 0)->SetIsReady(true);
#ifdef __APPLE__
system("./AuthServer&");
#elif _WIN32
system("start ./AuthServer.exe");
#else
if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) {
system("sudo ./AuthServer&");
}
else {
system("./AuthServer&");
}
#endif
StartAuthServer();
}
auto t = std::chrono::high_resolution_clock::now();
@@ -320,11 +301,13 @@ dLogger* SetupLogger() {
std::string logPath =
"./logs/MasterServer_" + std::to_string(time(nullptr)) + ".log";
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif
return new dLogger(logPath, logToConsole);
return new dLogger(logPath, logToConsole, logDebugStatements);
}
void HandlePacket(Packet* packet) {
@@ -338,6 +321,10 @@ void HandlePacket(Packet* packet) {
if (instance) {
Game::im->RemoveInstance(instance); //Delete the old
}
if (packet->systemAddress == chatServerMasterPeerSysAddr && !shouldShutdown) {
StartChatServer();
}
}
if (packet->data[0] == ID_CONNECTION_LOST) {
@@ -350,6 +337,10 @@ void HandlePacket(Packet* packet) {
Game::im->RemoveInstance(instance); //Delete the old
//Game::im->GetInstance(zoneID.GetMapID(), false, 0); //Create the new
}
if (packet->systemAddress == chatServerMasterPeerSysAddr && !shouldShutdown) {
StartChatServer();
}
}
if (packet->data[1] == MASTER) {
@@ -438,6 +429,14 @@ void HandlePacket(Packet* packet) {
}
}
if (theirServerType == ServerType::Chat) {
SystemAddress copy;
copy.binaryAddress = packet->systemAddress.binaryAddress;
copy.port = packet->systemAddress.port;
chatServerMasterPeerSysAddr = copy;
}
Game::logger->Log("MasterServer", "Received server info, instance: %i port: %i\n", theirInstanceID, theirPort);
break;
@@ -657,6 +656,37 @@ void HandlePacket(Packet* packet) {
}
}
void StartChatServer() {
#ifdef __APPLE__
//macOS doesn't need sudo to run on ports < 1024
system("./ChatServer&");
#elif _WIN32
system("start ./ChatServer.exe");
#else
if (std::atoi(Game::config->GetValue("use_sudo_chat").c_str())) {
system("sudo ./ChatServer&");
}
else {
system("./ChatServer&");
}
#endif
}
void StartAuthServer() {
#ifdef __APPLE__
system("./AuthServer&");
#elif _WIN32
system("start ./AuthServer.exe");
#else
if (std::atoi(Game::config->GetValue("use_sudo_auth").c_str())) {
system("sudo ./AuthServer&");
}
else {
system("./AuthServer&");
}
#endif
}
void ShutdownSequence() {
if (shutdownSequenceStarted) {
return;