def start_server(self): """ Starts the Dedicated Server process and waits for it to be registered """ self.DedicatedServer.status = "starting" self.DedicatedServer.busy = False oldLobbyIDs = self.DedicatedServer.deregister_all_server() AstroLogging.logPrint("Starting Server process...") if self.launcherConfig.EnableAutoRestart: AstroLogging.logPrint( f"Next restart is at {self.DedicatedServer.nextRestartTime}") time.sleep(5) startTime = time.time() self.DedicatedServer.start() self.DaemonProcess = AstroDaemon.launch( executable=self.isExecutable, consolePID=self.DedicatedServer.process.pid) # Wait for server to finish registering... while not self.DedicatedServer.registered: try: serverData = (AstroAPI.get_server( self.DedicatedServer.ipPortCombo, self.headers)) serverData = serverData['data']['Games'] lobbyIDs = [x['LobbyID'] for x in serverData] if len(set(lobbyIDs) - set(oldLobbyIDs)) == 0: time.sleep(self.launcherConfig.PlayfabAPIFrequency) else: now = time.time() if now - startTime > 15: self.DedicatedServer.registered = True del oldLobbyIDs self.DedicatedServer.LobbyID = serverData[0]['LobbyID'] if self.DedicatedServer.process.poll() is not None: AstroLogging.logPrint( "Server was forcefully closed before registration. Exiting...." ) return False except KeyboardInterrupt: self.DedicatedServer.kill_server("Launcher shutting down") except: AstroLogging.logPrint( "Failed to check server. Probably hit rate limit. Backing off and trying again..." ) self.launcherConfig.PlayfabAPIFrequency += 1 time.sleep(self.launcherConfig.PlayfabAPIFrequency) doneTime = time.time() elapsed = doneTime - startTime AstroLogging.logPrint( f"Server ready with ID {self.DedicatedServer.LobbyID}. Took {round(elapsed,2)} seconds to register." ) self.DedicatedServer.status = "ready" self.DedicatedServer.server_loop()
def deregister_all_server(self): servers_registered = (AstroAPI.get_server( self.ipPortCombo, self.launcher.headers))['data']['Games'] self.registered = False if (len(servers_registered)) > 0: AstroLogging.logPrint( f"Attemping to deregister all ({len(servers_registered)}) servers as {self.ipPortCombo}") # pprint(servers_registered) for reg_srvr in servers_registered: AstroLogging.logPrint(f"Deregistering {reg_srvr['LobbyID']}..") AstroAPI.deregister_server( reg_srvr['LobbyID'], self.launcher.headers) AstroLogging.logPrint("All servers deregistered") time.sleep(1) return [x['LobbyID'] for x in servers_registered] return []
def deregister_all_server(self): servers_registered = (AstroAPI.get_server( self.ipPortCombo, self.launcher.headers))['data']['Games'] self.registered = False if (len(servers_registered)) > 0: AstroLogging.logPrint( f"Attemping to deregister all ({len(servers_registered)}) servers matching self" ) # pprint(servers_registered) for counter, reg_srvr in enumerate(servers_registered): # reg_srvr['LobbyID'] AstroLogging.logPrint( f"Deregistering {counter+1}/{len(servers_registered)}...") drg_status = AstroAPI.deregister_server( reg_srvr['LobbyID'], self.launcher.headers) if 'status' in drg_status and drg_status['status'] == "Error": AstroLogging.logPrint( "Trouble deregistering server. Multiple servers may still be registered.", msgType="warning") AstroLogging.logPrint("All servers deregistered") time.sleep(1) return [x['LobbyID'] for x in servers_registered] return []
def start_server(self, firstLaunch=False): """ Starts the Dedicated Server process and waits for it to be registered """ if firstLaunch: atexit.register(self.DedicatedServer.kill_server, reason="Launcher shutting down via exit", save=True) signal.signal(signal.SIGINT, self.signal_handler) else: self.check_for_update(serverStart=True) self.DedicatedServer = AstroDedicatedServer(self.astroPath, self) self.DedicatedServer.status = "starting" self.DedicatedServer.busy = False gxAuth = None while gxAuth is None: try: gxAuth = AstroAPI.generate_XAUTH( self.DedicatedServer.settings.ServerGuid) except: AstroLogging.logPrint( "Unable to generate XAuth token... Are you connected to the internet?", msgType="warning") time.sleep(5) self.headers['X-Authorization'] = gxAuth oldLobbyIDs = self.DedicatedServer.deregister_all_server() AstroLogging.logPrint("Starting Server process...") if self.launcherConfig.EnableAutoRestart: AstroLogging.logPrint( f"Next restart is at {self.DedicatedServer.nextRestartTime}") # time.sleep(5) startTime = time.time() try: self.DedicatedServer.start() except: AstroLogging.logPrint("Unable to launch AstroServer.exe", "critical") return False reachableProcess = None pcounter = 40 while not reachableProcess: try: reachableProcess = not bool( self.DedicatedServer.process.poll()) pcounter -= 1 time.sleep(0.25) except: pcounter -= 2 time.sleep(0.5) if pcounter <= 0: AstroLogging.logPrint( "Unable to start Server Process after 10 seconds!", "critical") return False AstroLogging.logPrint("Server started! Getting ready....", ovrDWHL=True) try: self.DaemonProcess = AstroDaemon.launch( executable=self.isExecutable, consolePID=self.DedicatedServer.process.pid) except: AstroLogging.logPrint("Unable to start watcher daemon", "warning") return False # Wait for server to finish registering... serverData = None oPFF = self.launcherConfig.PlayfabAPIFrequency while not self.DedicatedServer.registered: AstroLogging.logPrint("Waiting for server to register...", "debug") try: serverData = (AstroAPI.get_server( self.DedicatedServer.ipPortCombo, self.headers)) serverData = serverData['data']['Games'] lobbyIDs = [x['LobbyID'] for x in serverData] if len(set(lobbyIDs) - set(oldLobbyIDs)) == 0: time.sleep(self.launcherConfig.PlayfabAPIFrequency) else: now = time.time() if now - startTime > 15: serverData = serverData[0] self.DedicatedServer.registered = True oldLobbyIDs = None self.DedicatedServer.LobbyID = serverData['LobbyID'] if self.DedicatedServer.process.poll() is not None: AstroLogging.logPrint( "Server was forcefully closed before registration. Exiting...." ) return False except KeyboardInterrupt: self.DedicatedServer.kill_server( "Launcher shutting down via KeyboardInterrupt") except: AstroLogging.logPrint( "Failed to check server. Probably hit rate limit. Backing off and trying again..." ) if self.launcherConfig.PlayfabAPIFrequency < 30: self.launcherConfig.PlayfabAPIFrequency += 1 time.sleep(self.launcherConfig.PlayfabAPIFrequency) self.launcherConfig.PlayfabAPIFrequency = oPFF self.DedicatedServer.serverData = serverData doneTime = time.time() elapsed = doneTime - startTime AstroLogging.logPrint( f"Server ready! Took {round(elapsed,2)} seconds to register.", ovrDWHL=True) # {self.DedicatedServer.LobbyID} self.DedicatedServer.status = "ready" self.DedicatedServer.server_loop()
def server_loop(self): while True: # Ensure RCON is connected try: if not self.AstroRCON or not self.AstroRCON.connected: self.AstroRCON = self.start_RCON() self.quickToggleWhitelist() except: pass while not self.AstroRCON.connected: time.sleep(0.1) ########################### if not self.launcher.launcherConfig.DisableBackupRetention: self.launcher.backup_retention() self.launcher.save_reporting() if self.launcher.launcherConfig.EnableAutoRestart: if (((datetime.datetime.now() - self.lastRestart).total_seconds() > 60) and ((self.nextRestartTime - datetime.datetime.now()).total_seconds() < 0)): AstroLogging.logPrint("Preparing to shutdown the server.") self.lastRestart = datetime.datetime.now() self.nextRestartTime += datetime.timedelta( hours=self.launcher.launcherConfig. AutoRestartEveryHours) self.save_and_shutdown() if self.process.poll() is not None: AstroLogging.logPrint("Server was closed. Restarting..") return self.launcher.start_server() self.getXauth() if self.lastHeartbeat is None or ( datetime.datetime.now() - self.lastHeartbeat).total_seconds() > 30: serverData = [] try: serverData = (AstroAPI.get_server( self.ipPortCombo, self.launcher.headers))['data']['Games'] if len(serverData) > 0: self.serverData = serverData[0] except: pass hbServerName = { "customdata": { "ServerName": self.settings.ServerName, "ServerType": ("AstroLauncherEXE" if self.launcher.isExecutable else "AstroLauncherPy") + f" {self.launcher.version}", "ServerPaks": self.pakList } } hbStatus = AstroAPI.heartbeat_server( self.serverData, self.launcher.headers, {"serverName": json.dumps(hbServerName)}) hbTryCount = 0 while hbStatus['status'] != "OK": if hbTryCount > 3: self.kill_server( reason= "Server was unable to heartbeat, restarting...", save=True, killLauncher=False) time.sleep(5) return self.launcher.start_server() self.getXauth() hbTryCount += 1 try: hbStatus = AstroAPI.heartbeat_server( self.serverData, self.launcher.headers, {"serverName": json.dumps(hbServerName)}) except: AstroLogging.logPrint( f"Failed to heartbeat server on attempt: {hbTryCount}" ) time.sleep(5 * hbTryCount) self.lastHeartbeat = datetime.datetime.now() if self.launcher.webServer is not None: self.setStatus("ready") self.busy = "getSavesInLoop" self.getSaves() self.busy = False self.setStatus("ready") serverStats = self.AstroRCON.DSServerStatistics() if serverStats is not None and 'averageFPS' in serverStats: self.DSServerStats = serverStats if self.launcher.launcherConfig.ShowServerFPSInConsole: FPSJumpRate = (float(self.settings.MaxServerFramerate) / 5) if self.oldServerStats is None or (abs( float(self.DSServerStats['averageFPS']) - float(self.oldServerStats['averageFPS'])) > FPSJumpRate): AstroLogging.logPrint( f"Server FPS: {round(self.DSServerStats['averageFPS'])}" ) self.oldServerStats = self.DSServerStats self.setStatus("ready") playerList = self.AstroRCON.DSListPlayers() if playerList is not None and 'playerInfo' in playerList: self.players = playerList curPlayers = [ x['playerName'] for x in self.players['playerInfo'] if x['inGame'] ] if len(curPlayers) > len(self.onlinePlayers): playerDif = list( set(curPlayers) - set(self.onlinePlayers))[0] self.onlinePlayers = curPlayers if playerDif in self.stripPlayers: self.stripPlayers.remove(playerDif) AstroLogging.logPrint(f"Player joining: {playerDif}") # Add player to INI with Unlisted category if not exists or is Pending pp = list(self.settings.PlayerProperties) difGuid = [ x for x in self.players['playerInfo'] if x['playerName'] == playerDif ][0]["playerGuid"] if len([ x for x in pp if difGuid in x and "PlayerCategory=Pending" not in x ]) == 0: self.AstroRCON.DSSetPlayerCategoryForPlayerName( playerDif, "Unlisted") self.refresh_settings() elif len(curPlayers) < len(self.onlinePlayers): playerDif = list( set(self.onlinePlayers) - set(curPlayers))[0] self.onlinePlayers = curPlayers AstroLogging.logPrint(f"Player left: {playerDif}") self.players['playerInfo'] = [ x for x in playerList['playerInfo'] if x['playerName'] not in self.stripPlayers ] time.sleep(self.launcher.launcherConfig.ServerStatusFrequency)
def start_server(self, firstLaunch=False): """ Starts the Dedicated Server process and waits for it to be registered """ if firstLaunch: atexit.register(self.DedicatedServer.kill_server, reason="Launcher shutting down", save=True) signal.signal(signal.SIGINT, self.signal_handler) else: self.check_for_update(serverStart=True) self.DedicatedServer = AstroDedicatedServer(self.astroPath, self) self.DedicatedServer.status = "starting" self.DedicatedServer.busy = False gxAuth = None while gxAuth is None: try: gxAuth = AstroAPI.generate_XAUTH( self.DedicatedServer.settings.ServerGuid) except: time.sleep(10) self.headers['X-Authorization'] = gxAuth oldLobbyIDs = self.DedicatedServer.deregister_all_server() AstroLogging.logPrint("Starting Server process...") if self.launcherConfig.EnableAutoRestart: AstroLogging.logPrint( f"Next restart is at {self.DedicatedServer.nextRestartTime}") # time.sleep(5) startTime = time.time() self.DedicatedServer.start() self.DaemonProcess = AstroDaemon.launch( executable=self.isExecutable, consolePID=self.DedicatedServer.process.pid) # Wait for server to finish registering... serverData = None while not self.DedicatedServer.registered: try: serverData = (AstroAPI.get_server( self.DedicatedServer.ipPortCombo, self.headers)) serverData = serverData['data']['Games'] lobbyIDs = [x['LobbyID'] for x in serverData] if len(set(lobbyIDs) - set(oldLobbyIDs)) == 0: time.sleep(self.launcherConfig.PlayfabAPIFrequency) else: now = time.time() if now - startTime > 15: serverData = serverData[0] self.DedicatedServer.registered = True oldLobbyIDs = None self.DedicatedServer.LobbyID = serverData['LobbyID'] if self.DedicatedServer.process.poll() is not None: AstroLogging.logPrint( "Server was forcefully closed before registration. Exiting...." ) return False except KeyboardInterrupt: self.DedicatedServer.kill_server("Launcher shutting down") except: AstroLogging.logPrint( "Failed to check server. Probably hit rate limit. Backing off and trying again..." ) self.launcherConfig.PlayfabAPIFrequency += 1 time.sleep(self.launcherConfig.PlayfabAPIFrequency) self.DedicatedServer.serverData = serverData doneTime = time.time() elapsed = doneTime - startTime AstroLogging.logPrint( f"Server ready! Took {round(elapsed,2)} seconds to register." ) # {self.DedicatedServer.LobbyID} self.DedicatedServer.status = "ready" self.DedicatedServer.server_loop()