Server - jimdroberts/FishMMO GitHub Wiki
Main Server class for FishMMO. Handles configuration, database context initialization, network setup, and server lifecycle management. Provides core infrastructure for launching, configuring, and running different server types (Login, World, Scene) in the FishMMO architecture.
-
public NpgsqlDbContextFactory NpgsqlDbContextFactory { get; private set; }
Factory for creating PostgreSQL database contexts.
-
public RedisDbContextFactory RedisDbContextFactory { get; private set; }
Factory for creating Redis database contexts.
-
public NetworkManager NetworkManager { get; private set; }
Reference to the FishNet NetworkManager in the scene.
-
public string RemoteAddress { get; private set; }
The external IP address of the server, as detected at startup.
-
public string AddressOverride
Optional override for the server bind address.
-
public ushort PortOverride
Optional override for the server bind port.
-
public string Address { get; private set; }
The bind address used by the server transport.
-
public ushort Port { get; private set; }
The bind port used by the server transport.
-
public Action OnLoginServerInitialized
Invoked when the LoginServer is initialized.
-
public Action OnWorldServerInitialized
Invoked when the WorldServer is initialized.
-
public Action OnSceneServerInitialized
Invoked when the SceneServer is initialized.
-
public ServerWindowTitleUpdater ServerWindowTitleUpdater { get; private set; }
Reference to the window title updater for the server process.
-
void Start()
Unity Start callback. Initializes server type, configuration, and network setup.
-
private void OnFinalizeSetup(string remoteAddress)
Finalizes server setup after external IP address is fetched. Loads configuration, initializes database contexts, network manager, and server behaviours. remoteAddress: string - The detected external IP address.
-
public static void Quit()
Quits the server application. Exits play mode in editor, or closes the application in build.
-
private ServerType GetServerType()
Determines the server type based on the current scene name. Returns: ServerType - The detected ServerType.
-
private T GetOrCreateComponent() where T : UnityEngine.Component
Gets a component of type T, creating and adding it if it does not exist. Returns: T - The component instance.
-
private void ServerManager_OnServerConnectionState(ServerConnectionStateArgs obj)
Handles server connection state changes and logs transport details. obj: ServerConnectionStateArgs - Connection state arguments.
-
IEnumerator OnAwaitingConnectionReady()
Coroutine that waits for the server connection to be ready before proceeding. Returns: IEnumerator for coroutine.
-
public static void Broadcast(NetworkConnection conn, T broadcast, bool requireAuthentication = true, Channel channel = Channel.Reliable) where T : struct, IBroadcast
Broadcasts a message to a network connection. conn: NetworkConnection - The network connection. broadcast: T - The broadcast message. requireAuthentication: bool - Whether authentication is required. channel: Channel - The channel to use for broadcasting.
-
public void RegisterBroadcast(Action<NetworkConnection, T, Channel> handler, bool requireAuthentication = true) where T : struct, IBroadcast
Registers a broadcast handler for a specific broadcast type. handler: Action<NetworkConnection, T, Channel> - The handler to register. requireAuthentication: bool - Whether authentication is required.
-
public void UnregisterBroadcast(Action<NetworkConnection, T, Channel> handler, bool requireAuthentication = true) where T : struct, IBroadcast
Unregisters a broadcast handler for a specific broadcast type. handler: Action<NetworkConnection, T, Channel> - The handler to unregister. requireAuthentication: bool - Whether authentication is required.
-
private bool LoadTransportServerDetails()
Loads transport server details from the configuration file and applies them to the transport. Returns: bool - True if details were loaded and applied successfully, false otherwise.
-
public bool TryGetServerIPv4AddressFromTransport(out ServerAddress address)
Attempts to get the server's IPv4 address and port from the transport. address: out ServerAddress - Output server address struct. Returns: bool - True if successful, false otherwise.
-
public bool TryGetServerIPv6AddressFromTransport(out ServerAddress address)
Attempts to get the server's IPv6 address and port from the transport. address: out ServerAddress - Output server address struct. Returns: bool - True if successful, false otherwise.
-
public bool TryGetServerIPAddress(out ServerAddress address)
Attempts to get the server's IP address and port, using overrides if set, otherwise using transport and remote address. address: out ServerAddress - Output server address struct. Returns: bool - True if successful, false otherwise.
- Requires FishNet networking, FishMMO server infrastructure, and a configured database connection.
- Attach the Server component to a GameObject in the server scene. Ensure NetworkManager and database factories are present.
- Configure server settings in GlobalSettings or via AddressOverride/PortOverride as needed.
- The system initializes itself on startup, setting up network, database, and server behaviours automatically.
// Example 1: Setting up the Server
// Attach Server to a GameObject and ensure dependencies are set up.
void Start()
{
// Server will initialize automatically if attached and enabled.
}
// Example 2: Broadcasting a message to a connection
Server.Broadcast(conn, new MyBroadcastMessage());
// Example 3: Registering a broadcast handler
server.RegisterBroadcast<MyBroadcastMessage>(OnMyBroadcastReceived);
- Ensure all required dependencies (NetworkManager, database factories) are present before starting the server.
- Use AddressOverride and PortOverride for custom network configurations as needed.
- Register and unregister broadcast handlers to manage network events cleanly.
- Monitor server logs for startup, connection, and error messages to ensure healthy operation.
- Clean up all event subscriptions and resources on shutdown to prevent resource leaks.