gale.net.server module

This file contains the implementation of the class Server, the listening/authoritative side of a gale.net connection.

Author: Alejandro Mujica (aledrums@gmail.com)

class gale.net.server.Server(port, host='0.0.0.0', max_peers=None, serialize=<function json_serialize>, deserialize=<function json_deserialize>, heartbeat_interval=1.0, timeout=5.0)[source]

Bases: object

The authoritative/listening side of a gale.net game: accepts connections from any number of Clients over a single non-blocking UDP socket, and lets the game send/broadcast messages to them and react to the ones they send back.

Usage example:

server = Server(port=9000) server.on_connect(lambda peer: print(f”{peer.peer_id} joined”)) server.on_message(“input”, lambda peer, payload: …)

# In the game loop: server.update(dt) server.broadcast(“snapshot”, {“positions”: […]})

Parameters:
  • port (int)

  • host (str)

  • max_peers (int | None)

  • serialize (Callable[[str, Dict[str, Any]], bytes])

  • deserialize (Callable[[bytes], Tuple[str, Dict[str, Any]]])

  • heartbeat_interval (float)

  • timeout (float)

on_connect(callback)[source]
Parameters:

callback (Callable[[Peer], None]) – Called with the new Peer every time a Client connects.

Return type:

None

on_disconnect(callback)[source]
Parameters:

callback (Callable[[Peer], None]) – Called with the Peer every time a connected Client disconnects (explicitly or by timing out).

Return type:

None

on_message(message_type, callback)[source]
Parameters:
  • message_type (str) – The message type to react to.

  • callback (Callable[[Peer, Dict[str, Any]], None]) – Called with (peer, payload) every time a message of this type is received from any peer.

Return type:

None

enable_lan_discovery(name, discovery_port=9998)[source]

Start answering LAN discovery requests (see gale.net.discovery) so a Client can find this Server without knowing its address ahead of time. Only meant for servers reachable on a local network: do not call this on a public, internet-facing dedicated server.

Parameters:
  • name (str) – The name shown to clients discovering this server.

  • discovery_port (int) – The UDP port to listen for discovery requests on. The default value is DEFAULT_DISCOVERY_PORT.

Return type:

None

get_peers()[source]
Returns:

The list of currently connected peers.

Return type:

List[Peer]

get_rtt(peer_id)[source]
Parameters:

peer_id (int) – The peer to query.

Returns:

The peer’s smoothed round-trip time in seconds, or None if peer_id is not connected.

Return type:

float | None

send_to(peer_id, message_type, payload, channel=0)[source]
Parameters:
  • peer_id (int) – The peer to send to.

  • message_type (str) – The message type.

  • payload (Dict[str, Any]) – The message data.

  • channel (int) – One of the Channel constants. The default value is Channel.UNRELIABLE.

Return type:

None

broadcast(message_type, payload, channel=0, exclude=None)[source]
Parameters:
  • message_type (str) – The message type.

  • payload (Dict[str, Any]) – The message data.

  • channel (int) – One of the Channel constants. The default value is Channel.UNRELIABLE.

  • exclude (Iterable[int] | None) – Peer ids to skip. The default value is None.

Return type:

None

disconnect(peer_id)[source]
Parameters:

peer_id (int) – The peer to disconnect.

Return type:

None

close()[source]

Close the underlying sockets. The server is no longer usable afterwards.

Return type:

None

update(dt)[source]

Poll the network and drive every peer’s connection state: receive and dispatch messages, retransmit unacked reliable packets, send heartbeats, and detect timed-out peers.

Parameters:

dt (float) – Time elapsed, in seconds, since the last call. Currently unused (timing is wall-clock based), accepted for consistency with the rest of gale.

Return type:

None