Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Connection

Introduction

Our transport layer only allows us to send/receive raw packets to a remote address. But we want to be able to create a stateful ‘connection’ where we know that two peers are connected.

To establish that connection, that needs to be some machinery that runs on top of the transport layer and takes care of:

  • sending handshake packets to authenticate the connection
  • sending keep-alive packets to check that the connection is still open
  • storing the list of connected remote peers
  • etc.

Connection logic lives in components you add to the link entity, next to the IO component.

Multiple implementations are provided:

  • Netcode (NetcodeClient / NetcodeServer)
  • Steam (SteamClientIo / SteamEndpoint; add Server for the authoritative role)
  • Local (CrossbeamIo, in-memory, for tests and host-server mode)

Netcode

This implementation is based on the netcode.io standard created by Glenn Fiedler (of GafferOnGames fame). It describes a protocol to establish a secure connection between two peers, provided that there is an unordered unreliable transport layer to exchange packets.

For my purpose I am using this Rust implementation of the standard.

You use the Netcode connection by adding the NetcodeClient or NetcodeServer component, coupled with any of the available IO components (UdpIo, WebTransportClientIo, etc.)

To connect to a game server, the client needs to send a ConnectToken to the game server to start the connection process.

There are several ways to obtain a ConnectToken:

  • the client can request a ConnectToken via a secure (e.g. HTTPS) connection from a backend server. The server must use the same protocol_id and private_key as the game servers. The backend server could be a dedicated webserver; or the game server itself, if it has a way to establish secure connection.
  • when testing, it can be convenient for the client to create its own ConnectToken manually. You can use Authentication::Manual for those cases.

Currently lightyear does not provide any functionality to let a game server send a ConnectToken securely to a client. You will have to handle this logic yourself.

Steam

This implementation is based on the Steamworks SDK.

Local

Local connections are used when running in host-server mode: the server and the client are running in the same bevy App. No packets are actually sent over the network since the client and server share the same World.