Vixen
02b45cc4
csharp
public sealed class NetworkSession

A session: who is in it, what tick it is, and the handshake that decides both.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

This is the layer between the transport, which knows about connections and bytes, and everything above, which wants to know about players and ticks. It owns exactly three things — the handshake, the clock, and the player list — and hands every payload it does not understand to an ISessionMessageHandler.

Nothing is dispatched before the handshake finishes. A payload from a connection that has not been accepted is dropped, not queued and not delivered late. Everything above this layer may therefore assume that a payload it is handed came from a peer that agreed on the protocol version and the content hash, and was let in by the authenticator.

A player is not a connection. When a connection drops, the player it carried stays in the list with IsConnected false for the length of ReconnectWindow, holding their id and their slot. A client that comes back with the token it was issued resumes as the same player. That is the whole of reconnect support, and it is here rather than bolted on later because retrofitting it means changing what every layer above means by "player".

Host mode is not a special case. StartHost starts both halves of one transport, and the host's own client half does the same handshake through the loopback that a remote client does over a socket. There is no offline path to rot.

Single-threaded, like everything else that runs in the frame: Update is where all of it happens.

Fields and properties (11)

  • public SessionOptions Options

    How the session behaves.

  • public ITransport Transport

    The transport underneath.

  • public TickManager Clock

    The clock. On a client, the one being kept in step with the server's.

  • public Tick Tick

    The tick the session is on.

  • public SessionTopology Topology

    What role this session is playing.

  • public SessionState State

    Where the session is in its life.

  • public bool IsServer

    Whether this session is the authority.

  • public bool IsClient

    Whether this session has a player in the game.

  • public NetworkPlayer? LocalPlayer

    Our own player, on anything that is not a dedicated server.

  • public IReadOnlyList<NetworkPlayer> Players

    Everybody in the session, connected or inside their reconnect window.

  • public ReadOnlySpan<byte> ReconnectToken

    The token this client was issued. Keep a copy of it: presenting it to PresentReconnectToken before connecting again is what makes the server give you back the same PlayerId rather than a new one.

Events (6)

  • public Action<NetworkPlayer>? PlayerJoined

    A player joined for the first time.

  • public Action<NetworkPlayer>? PlayerConnectionChanged

    A player dropped into their reconnect window, or came back out of it.

  • public Action<NetworkPlayer, PlayerLeaveReason>? PlayerLeft

    A player left for good.

  • public Action<NetworkPlayer>? Connected

    This client was let in.

  • public Action<SessionRejectReason, string>? Rejected

    This client was refused, with the reason the server gave.

  • public Action<DisconnectReason>? Disconnected

    This client's connection ended.

Methods (14)

  • public NetworkSession(ITransport transport, SessionOptions? options = null, ISessionAuthenticator? authenticator = null, bool ownsTransport = false)

    Creates a session over a transport.

  • public void StartServer()

    Starts listening, without playing.

  • public void StartClient()

    Connects to a server.

  • public void PresentReconnectToken(ReadOnlySpan<byte> token)

    Presents a token issued by an earlier session, so the server gives back the same player rather than a new one. Call it before connecting.

  • public void StartHost()

    Listens and plays: one process, both halves, a loopback between them.

  • public void StartOffline()

    Single player. Mechanically StartHost; the difference is what the game means by it, and that the transport it was given is one nobody else can reach.

  • public void Stop()

    Stops everything and empties the session.

  • public int Update(TimeSpan elapsed, ISessionMessageHandler? messages = null)

    Runs the session for a frame: polls the transport, finishes handshakes, measures round trips, retires players whose reconnect window closed, and advances the clock.

  • public bool SendToServer(ReadOnlySpan<byte> payload, Channel channel)

    Sends to the server, from a client.

  • public bool SendToPlayer(PlayerId player, ReadOnlySpan<byte> payload, Channel channel)

    Sends to one player, from a server.

  • public int SendToAll(ReadOnlySpan<byte> payload, Channel channel)

    Sends to every connected player, from a server.

  • public bool Kick(PlayerId player, string reason = "")

    Removes a player and closes their connection. No reconnect window: a kick is final.

  • public bool TryGetPlayer(PlayerId id, out NetworkPlayer? player)

    Finds a player.

  • public void Dispose()

    Stops the session, and the transport if it owns it.

Used by (21)

  • GameClientMultiplayer
  • GameServerMultiplayer
  • LocalMatchMultiplayer
  • NetworkMatchMultiplayer
  • PeerVoiceChat
  • ProgramVoiceChat
  • RelayVoiceChat
  • HandshakeTargetVixen.Net.Fuzz
  • NetworkMetricsVixen.Net
  • NetworkMetricsTestsVixen.Net.Tests
  • RpcOverSessionTestsVixen.Net.Tests
  • SessionClientTargetVixen.Net.Fuzz
  • SessionHarnessVixen.Net.Tests
  • SessionRpcTransportVixen.Net
  • SessionTestsVixen.Net.Tests
  • RealmVixen.Live.Realm
  • RealmVixen.Live.Realm.Tests
  • RealmClusterTestsVixen.Live.Realm.Cluster.Tests
  • RealmFixtureVixen.Live.Realm.Tests
  • RealmHostVixen.Live.Realm
  • RealmHostTestsVixen.Live.Realm.Tests