Class SessionStateMachine

Namespace
Unity.Multiplayer.Core

The engine-free life cycle of one session. The NGO adapter drives it from NetworkManager callbacks; game code reads State and listens to StateChanged.

public sealed class SessionStateMachine
Inheritance
SessionStateMachine
Inherited Members

Remarks

Host / Server: Idle → Starting → Running → Stopping → Idle. Client: Idle → Connecting → Connected → Disconnected; a Disconnected client may Start(Topology) again or Reset() to Idle.

Requests from game code (Start(Topology)) throw on an illegal transition, because they are programming errors. Notifications from the transport (NotifyStarted(), NotifyConnected(), NotifyStopped(), NotifyDisconnected(Disconnection)) and Stop() are tolerant: a late or duplicate one is ignored and returns false, since transports report events after the session has already ended.

Not thread-safe: drive it from one thread (Unity's main thread).

Properties

IsActive

True from Start until the session has ended (any state but Idle and Disconnected).

public bool IsActive { get; }

Property Value

bool

IsLive

True when the session is fully up: Running (Host/Server) or Connected (Client).

public bool IsLive { get; }

Property Value

bool

LastDisconnect

Why the last session ended; None while one is live or none has ended.

public Disconnection LastDisconnect { get; }

Property Value

Disconnection

State

The current state.

public SessionState State { get; }

Property Value

SessionState

Topology

The topology of the current or last session; null before the first Start(Topology).

public Topology? Topology { get; }

Property Value

Topology?

Methods

IsAllowed(SessionState, SessionState)

Whether the machine ever moves directly from from to to.

public static bool IsAllowed(SessionState from, SessionState to)

Parameters

from SessionState
to SessionState

Returns

bool

NotifyConnected()

The client was approved: Connecting → Connected. Ignored (returns false) in any other state.

public bool NotifyConnected()

Returns

bool

NotifyDisconnected(DisconnectReason, string)

The session ended without a local request. Client: Connecting or Connected → Disconnected. Host/Server: Starting or Running → Idle (a failed start or a lost transport). While Stopping it completes the stop with the local reason. Ignored (returns false) when nothing is active.

public bool NotifyDisconnected(DisconnectReason reason, string message = null)

Parameters

reason DisconnectReason
message string

Returns

bool

Exceptions

ArgumentException

reason is None.

NotifyDisconnected(Disconnection)

The session ended without a local request; the same as NotifyDisconnected(DisconnectReason, string) but keeping the whole record (for example a server's Rejection).

public bool NotifyDisconnected(Disconnection disconnection)

Parameters

disconnection Disconnection

Returns

bool

Exceptions

ArgumentException

The reason is None or undefined.

NotifyStarted()

The server is up: Starting → Running. Ignored (returns false) in any other state.

public bool NotifyStarted()

Returns

bool

NotifyStopped()

The server finished shutting down: Stopping → Idle. Ignored (returns false) in any other state.

public bool NotifyStopped()

Returns

bool

Reset()

Acknowledges a client disconnect: Disconnected → Idle, keeping LastDisconnect. Returns false in any other state.

public bool Reset()

Returns

bool

Start(Topology)

Begins a session: Host and Server go to Starting, Client goes to Connecting. Allowed only from Idle or Disconnected.

public void Start(Topology topology)

Parameters

topology Topology

Exceptions

InvalidOperationException

A session is already active.

ArgumentOutOfRangeException

topology is not a defined value.

Stop()

Leaves or stops by local request. Host/Server: Starting or Running → Stopping (finish with NotifyStopped()). Client: Connecting or Connected → Disconnected with LocalLeave. Returns false when there is nothing to stop.

public bool Stop()

Returns

bool

Events

StateChanged

Raised after every state change, once the new state is visible.

public event Action<SessionStateChange> StateChanged

Event Type

Action<SessionStateChange>