Skip to content

ryu_ldn::network::ConnectionStateMachine

Connection state machine.

Manages the connection lifecycle with well-defined states and transitions. Thread-safe when using the provided atomic operations.State diagram:

Type: ConnectionState

Type: StateChangeCallback

Type: uint32_t

void ConnectionStateMachine()

Constructor - starts in Disconnected state.

Constructor initializes state machine in Disconnected state.The state machine starts in the Disconnected state with no callback registered and zero retry count.

ConnectionState get_state()

Get current connection state.

Current statereturn

Returns: ConnectionState

bool is_connected()

Check if currently connected (Connected, Handshaking, or Ready)

Check if currently in a connected state.A connection is considered “connected” if TCP is established, regardless of whether handshake is complete.true if in Connected, Handshaking, or Ready statereturn

Returns: bool

bool is_ready()

Check if fully ready (handshake complete)

true if in Ready statereturn

Returns: bool

bool is_transitioning()

Check if in a transitional state (Connecting, Handshaking, etc.)

Check if in a transitional (non-stable) state.Transitional states are intermediate states where the connection is neither fully established nor fully disconnected.true if in a transitional statereturn

Returns: bool

TransitionResult process_event(ConnectionEvent event)

Process an event and perform state transition.

Process an event and perform state transition if valid.This is the main entry point for driving the state machine. Events are validated against the current state, and if a valid transition exists, it is executed.eventEvent to processparamTransitionResult indicating success or failure reasonreturn

Parameters:

  • event (ConnectionEvent)

Returns: TransitionResult

void set_state_change_callback(StateChangeCallback callback)

Set callback for state changes.

Set callback for state change notifications.The callback will be invoked after each successful state transition with the old state, new state, and triggering event.callbackFunction pointer to callback, or nullptr to disableparam

Parameters:

  • callback (StateChangeCallback)
uint32_t get_retry_count()

Get retry count since last successful connection.

Number of retry attemptsreturn

Returns: uint32_t

void reset_retry_count()

Reset retry count (call on successful connection)

void force_state(ConnectionState state)

Force state (use with caution, bypasses transition validation)

Force state machine into a specific state.This method bypasses all transition validation and directly sets the state. Use with caution - primarily for testing or error recovery scenarios.Does not invoke the state change callbackwarningDoes not update retry countwarningstateState to forceparam

Parameters:

  • state (ConnectionState)
const char * state_to_string(ConnectionState state)

Convert state to string for logging.

Convert ConnectionState enum to human-readable string.stateState to convertparamstateState to convertparamHuman-readable state namereturnNull-terminated string representationreturn

Parameters:

  • state (ConnectionState)

Returns: const char *

const char * event_to_string(ConnectionEvent event)

Convert event to string for logging.

Convert ConnectionEvent enum to human-readable string.eventEvent to convertparameventEvent to convertparamHuman-readable event namereturnNull-terminated string representationreturn

Parameters:

  • event (ConnectionEvent)

Returns: const char *

bool is_valid_transition(ConnectionState from, ConnectionEvent event, & to)

Check if transition is valid.

Validate state transition and determine target state.This method implements the state transition table. Each state has a defined set of valid events that can trigger transitions to specific target states.fromCurrent stateeventEvent being processedtoTarget state if transition is validparamtrue if the transition is valid, false otherwisereturn

Parameters:

  • from (ConnectionState)
  • event (ConnectionEvent)
  • to (&)

Returns: bool

void transition_to(ConnectionState new_state, ConnectionEvent event)

Perform the state transition.

Execute state transition and notify callback.This method performs the actual state change, updates the retry counter as appropriate, and invokes the state change callback if one is registered.Retry count is incremented when transitioning to Retrying or Connecting from Backoff/Retrying states. It is reset to zero when reaching the Ready state.new_stateTarget state to transition toeventEvent that triggered this transitionparam

Parameters:

  • new_state (ConnectionState)
  • event (ConnectionEvent)