Lifecycle
This document describes the complete lifecycle of a multiplayer game server managed by PlayFab Multiplayer Servers (MPS) and integrated with the Game Server SDK (GSDK).
High-Level Lifecycle Phases
Section titled “High-Level Lifecycle Phases”The game server process progresses through five major lifecycle phases, coordinated in real-time with the local VM Agent via GSDK heartbeats.
stateDiagram-v2
[*] --> Startup
Startup --> Initializing
Initializing --> StandingBy : Call Ready-for-Players
StandingBy --> Active : Player Allocation Assigned
Active --> Terminating : Termination Signal received
Terminating --> Terminated : Process Exit
Terminated --> [*]
Process Flow
Section titled “Process Flow”Startup & Bootstrapping
Section titled “Startup & Bootstrapping”When the PlayFab Multiplayer Servers control plane provisions or scales up a game server instance, it spins up the container or launches the executable process on the host VM.
- Environment Variable Injection: The OS injects critical parameters into the process space, including the config file pointer (
GSDK_CONFIG_FILE), title ID (PF_TITLE_ID), build ID (PF_BUILD_ID), and region (PF_REGION). - SDK Parsing: The game engine starts and initializes the GSDK. The SDK reads the local JSON configuration file to resolve the VM Agent endpoint address and its unique session host instance ID.
- Local Logging: The GSDK creates a timestamped output file in the specified log folder and prepares the file stream for writing logs.
Game Initialization
Section titled “Game Initialization”The game server transitions to the Initializing state.
- Asset Loading: The game engine compiles shaders, loads map geometry, initializes physics grids, and configures internal networking sockets.
- SDK Handshake: The GSDK sends an initial HTTP POST containing SDK metadata (its language flavor and compiler version) to the VM Agent.
- Loop Startup: The GSDK starts its background heartbeat thread, sending periodic PATCH requests to the VM Agent to signal that it is currently alive and initializing.
- Ready Signal: Once the game engine finishes loading all required startup resources, it invokes the Ready-for-Players synchronizer.
Standing By (Idle)
Section titled “Standing By (Idle)”The game server transitions to the StandingBy state and waits for players.
- State Promotion: The GSDK updates its state to
StandingByand immediately triggers an early out-of-band heartbeat to report this state change to the VM Agent. - Execution Pause: The GSDK blocks the main thread of the game engine using an OS-level synchronization lock.
- Idle Monitoring: The game engine remains idle. The background heartbeat thread continues to ping the VM Agent every second with a
StandingBystate, confirming the server is ready to accept a match allocation.
Allocation & Active Play
Section titled “Allocation & Active Play”When players request a game session (typically via PlayFab Matchmaking), the control plane allocates the standing-by server.
- Allocation Command: The VM Agent returns an
Activeoperation command in its next heartbeat response. The payload includes a session configuration block containing the allocated session ID, session cookie, custom metadata, and a list of authorized player IDs. - Unblocking: The GSDK updates its internal configuration map with the allocation metadata and transitions the state to
Active. It signals the synchronization primitive, unblocking the game server’s main thread. - Socket Bind: The game engine wakes up, parses the port mapping and assigned players, binds to the server listening port, and opens socket connections to begin receiving clients.
- Player Tracking: As players connect and disconnect, the game engine registers these updates via the GSDK. The GSDK reports the active player IDs to the VM Agent in subsequent heartbeat payloads.
Graceful Termination
Section titled “Graceful Termination”When the match is completed, or if the host VM needs to be scaled down or rebooted, the server is shut down.
- Termination Command: The VM Agent returns a
Terminateoperation command in a heartbeat response. - Callback Invocation: The GSDK transitions the internal state to
Terminatingand executes the developer’s registered shutdown callback. - Cleanup: The game engine stops accepting new connections, saves outstanding player progress or match metrics, notifies connected clients, and gracefully disconnects them.
- Process Exit: The game engine terminates its process cleanly. The GSDK logs the final state change, and the VM Agent cleans up the container/process.
Real-Time Interaction Sequence
Section titled “Real-Time Interaction Sequence”The following sequence diagram outlines the chronological interaction between the Game Server Engine, the internal GSDK, and the Local VM Agent during a standard lifecycle.
sequenceDiagram
autonumber
participant Engine as Game Server Engine
participant GSDK as Game Server SDK
participant Agent as Local VM Agent
Note over Engine, Agent: Startup
Engine->>GSDK: Initialize SDK (reads GSDK_CONFIG_FILE)
GSDK->>Agent: POST /v1/metrics/{id}/gsdkinfo (Handshake)
Agent-->>GSDK: 200 OK
GSDK->>GSDK: Start background heartbeat thread
Note over Engine, Agent: Initialization & Standing By
loop Every 1 Second
GSDK->>Agent: PATCH /v1/sessionHosts/{id} (State: Initializing)
Agent-->>GSDK: 200 OK (Operation: Continue)
end
Engine->>Engine: Load assets, prepare sockets
Engine->>GSDK: Invoke Ready-for-Players Sync
GSDK->>GSDK: Set State: StandingBy
GSDK->>Agent: PATCH (State: StandingBy - Out-of-band early wakeup)
Agent-->>GSDK: 200 OK (Operation: Continue)
GSDK->>Engine: Block Engine thread (Wait Event)
Note over Engine, Agent: Allocation
Agent->>Agent: PlayFab Allocates Server Instance
loop Every 1 Second (While Blocked)
GSDK->>Agent: PATCH (State: StandingBy)
Agent-->>GSDK: 200 OK (Operation: Active + Session Metadata)
end
GSDK->>GSDK: Parse Session Config & Initial Players
GSDK->>GSDK: Set State: Active
GSDK->>Engine: Signal Event (Unblock Engine Thread)
Engine-->>GSDK: Unblocked & Allocated
Engine->>Engine: Bind listening port & open sockets
Note over Engine, Agent: Active Gameplay
loop Every 1 Second
Engine->>GSDK: Register Connected Player IDs
GSDK->>Agent: PATCH (State: Active + Current Players)
Agent-->>GSDK: 200 OK (Operation: Continue)
end
Note over Engine, Agent: Termination
Agent-->>GSDK: PATCH Response (Operation: Terminate)
GSDK->>GSDK: Set State: Terminating
GSDK->>Engine: Execute Registered Shutdown Callback
Engine->>Engine: Notify clients, save game state, close sockets
Engine->>Engine: Exit Process (0)