Skip to main content

ServerHandler

A class in Root.Township, Alta.Networking.Servers.

The running server's handler: owns the socket and join handler, runs bootup and per-player connection steps, spawns players and saves them when they leave. Server only; get it from ServerHandler.Current, which in the main menu is a dummy (IsDummyLocalServer).

Constructors​

MemberWhat it is
ServerHandler(ISocket socket, IServerAccess serverAccess, bool isRunningLocally)Creates a ServerHandler from socket, serverAccess and isRunningLocally.
1 private and internal constructor
MemberWhat it is
private ServerHandler()Creates a ServerHandler from socket, serverAccess and isRunningLocally.

Fields​

12 private and internal fields
MemberWhat it is
private Queue<ServerHandler.BootupStep> bootupStepsQueue of bootup steps still to run. Null once bootup has finished, so late steps are refused.
private Queue<ServerHandler.ConnectionStep> connectionStepsSteps every joining connection runs after bootup, added with AddConnectionStep.
private static ServerHandler currentBacking field for Current.
private ServerHeartbeat heartbeatHeartbeat sender, only created with /heartbeat.
private bool isRunningLocallyTrue for a locally hosted server; makes Initialize use fixed local ports and look up the public IP.
private static Logger loggerNLog logger for this class.
private Dictionary<Connection, PlatformTarget> platformTargetsPlatform of each connection, stored on join and used when spawning the player.
private ServerPlayerConnectionHandlerOld playerJoinHandlerHandles connection approval and raises the joined and left callbacks.
private Dictionary<Connection, PlayerMode> playerModesPlayer mode of each connection, stored on join and used when spawning the player.
private ISocket socketThe server socket that accepts connections.
private string statusCurrent bootup status text sent to waiting clients.
private WebServerThread webServerThe server's web server thread, started in Initialize and disposed when the server stops.

Properties​

MemberWhat it is
ServerConfiguration Config { get; private set; }An allow or block list from Alta's hosting. Nothing sets it in this build, so it is null.
int Connections { get; }Open connections on the socket, including ones still joining.
static ServerHandler Current { get; private set; }The running handler, or null. Setting it raises Initialized.
static int CurrentServerId { get; }-1 in a local test, else the joined server's identifier. Meant for clients.
bool IsBottingUp { get; private set; }True while bootup steps run. (Spelling is Alta's.)
bool IsDummyLocalServer { get; }True for the stand-in handler the main menu makes so settings have a home.
bool IsRunning { get; private set; }True from the end of startup until stopping. Also keeps the API ping loop going.
bool IsUsingConfig { get; }Whether Config is set. Always false in this build.
int PlayerLimit { get; }The settings' player limit. On a simple (Quest) server anything outside 1 to 8 becomes 8.
ServerSaveUtility SaveUtility { get; private set; }Save folders and player save loading for this server.
IServerAccess ServerApiAccess { get; private set; }The API access object for this server.
SettingsFileAccess<ServerSettings> ServerConfig { get; private set; }The game's server settings, from ServerConfiguration in the server folder (or by server id with /db_settings).
GameServerInfo ServerInfo { get; }The server's info from ServerApiAccess.
ServerLockCollection ServerLocks { get; }Join locks; while any are held, joins are refused.

Events​

MemberWhat it is
event Action BootupFinishedRaised when every bootup step is done.
static event Action<ServerHandler> InitializedRaised whenever Current changes, including to null. Static.
event Action<Player> PlayerJoinedRaised once a player has been spawned with their save loaded.
event Action<Player> PlayerLeftRaised after a leaving player has been saved and destroyed.
1 private and internal event
MemberWhat it is
private event Action BootupProgressedPrivate event raised each time the bootup status message changes; waiting connections use it to get status updates.

Methods​

MemberWhat it is
void AddBootupStep(ServerHandler.BootupStep step)Queues an async step to run before the server opens. Logs an error if bootup has already finished.
void AddConnectionStep(ServerHandler.ConnectionStep step)Queues an async step every joining player goes through before they're let in. Logs an error if players are already on.
async Task Bootup()Runs the queued bootup steps in order, then raises BootupFinished.
static void StartDummyLocalServer()Creates the dummy handler used outside a real server.
async Task StopAsync(ShutdownReason reason)Stops the server: saves the world (unless wiping), closes the socket and deregisters. Clears Current unless wiping.
static void StopDummyLocalServer()Clears Current if it is the dummy handler.
async Task StopForMigration(ShutdownReason reason)Stops pinging and deregisters from the API without closing the socket.
void StopPinging()Stops the API ping loop by clearing IsRunning.
void WipeCache()Deletes the server's cache folder.
async Task WipeServer(bool isSettingOffline)Stops pinging and wipes the server's save, optionally setting it offline.
protected async Task OnServerStopped(ShutdownReason reason)Shuts the web server and heartbeat, raises the server ended event and deregisters from the API.
14 private and internal methods
MemberWhat it is
private async void CheckForNoResponseFromConnectionAsync(Connection connection)Disconnects a connection that hasn't sent 'ready to spawn' within 2 minutes, unless it disconnects first.
private void ConnectionCreated(Connection connection)Runs for each new socket connection: hands it to the join handler and registers the core message handlers (ping, diagnostics, entity, cache and pause).
private async Task HandleConnectionsChange(Connection connection)Sends the list of connected users to the server API as the online players; logs failures.
private async Task Initialize(Action<string> progress)First bootup step: starts the server with the API (ports and public IP when local), the web server, optional heartbeat and wipe, then server pings.
private async void PingServer()Pings the server API every 15 seconds while running; after 10 failures in a row (or MAX_PING_FAIL_COUNT) it shuts the server down.
private void RemovePlayer(IPlayer player)Saves a leaving player (when the scene has save files), destroys their objects, raises PlayerLeft and forgets their mode and platform.
private void SendPlayerOwnership(Player player)Marks the new player entity as local to its owner.
private void SetBootupMessage(string message)Logs and stores the bootup status, then raises BootupProgressed.
private async void SpawnPlayerToClient(Connection connection, Stream stream)Handler for 'ready to spawn': waits for the scene, loads the save, spawns and initializes the player, syncs entities and raises PlayerJoined.
private void SyncStatus(Connection connection, Stream stream)Writes the current bootup status string into a ServerStatus message.
private async void UserJoined(Connection connection, PlayerMode playerMode, PlatformTarget platformTarget)Called when a connection is approved: stores its player mode and platform, waits for 'ready to spawn' and updates the online list.
private async void UserLeft(Connection connection)Called when a connection drops: removes its player on the next frame and updates the online list.
private async Task<bool> UserPrerequisites(Connection connection)Holds a joining connection until bootup finishes (sending status updates), then runs every connection step. Returns false if a step throws.
private async void WipeServerAsync()Runs a wipe once the scene has started, when the server was launched with /wipe.

Used in​

Members of other types that take, return or hold a ServerHandler: