Dedicated server shutdown when empty

I’m working on a school project and can launch dedicated servers on a VM (main game world launched manually on one port and dungeon servers spool up on-demand with incrementing port numbers). Players can travel to the new dungeon server and back to the main server with no problem.

My issue is that when the last player leaves the dedicated dungeon server, I want the server to shut down as if the dungeon were completed and is no longer needed. I can either…

  1. Shut down the server after sending all but the last player back to the main server. (sends that final player back to the main menu because its client travel command is not accepted after the shutdown command is given, which is not optimal)
  2. Send all players back to the main dedicated server but can’t shut down the server because no one is left on it to send the command. “FGenericPlatformMisc::RequestExit(true/false);”

Short of creating some sort of web backend, is there a way for that final player to reach back into the dedicated server with no players on it and send a shutdown command?

Players should never control whether a server is running. This would allow easy trolling for any kind of griefer player, faking a “shut down the server” command.

In your server, probably in some common class like GameMode, I would keep a timer that runs once a second, and if it finds no players online, it increments a counter. If the counter hits some number, like 15, it would then just exit the process. Calling ::ExitProcess() on Windows might work just fine, but you could also go through the actual engine shutdown code if you want (like FGenericPlatformMisc::RequestExit(false))

Agreed that a player shouldn’t control the server in production but since this is a school project I was making an exception for it. (Technically, I’m doing a server_implementation call, but point taken)

I’ll give the timer a shot. Thanks!

Update: The timer worked. I was able to keep track of the number of players in the loop. I’m still trying to wrap my head around this stuff and your suggestion really helped. Thanks again.

Your dedicated servers shouldn’t control their lifetime either.
Servers cost you extra money for everything you do with them…

Research about Load Balancing Services, it’s what you should go with:
Load balancing (computing) - Wikipedia

You should look at containerisation and using something like agones which handles that sort of thing for you.

1 Like

Rigged up our servers to auto-shutdown ever 6-8 hours, depending on if players are there or not.
It just calls the following when its time:

GEngine->Exec(nullptr, TEXT("QUIT"));

Optionally, if you create the other server process from the main server, you can terminate it directly.

	if (<all players are back from dedicated server>)
	{
		if (DedicatedServerProcessHandle.IsValid() && FPlatformProcess::IsProcRunning(DedicatedServerProcessHandle))
		{
			FPlatformProcess::TerminateProc(DedicatedServerProcessHandle);
			DedicatedServerProcessHandle.Reset();
		}
	}
2 Likes

Thanks guys! This definitely gives me some options.