ryu_ldn::network::ConnectionStateMachine
ryu_ldn::network::ConnectionStateMachine
Section titled “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:
Members
Section titled “Members”m_state
Section titled “m_state”Type: ConnectionState
m_callback
Section titled “m_callback”Type: StateChangeCallback
m_retry_count
Section titled “m_retry_count”Type: uint32_t
Methods
Section titled “Methods”ConnectionStateMachine
Section titled “ConnectionStateMachine”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.
get_state
Section titled “get_state”ConnectionState get_state()Get current connection state.
Current statereturn
Returns: ConnectionState
is_connected
Section titled “is_connected”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
is_ready
Section titled “is_ready”bool is_ready()Check if fully ready (handshake complete)
true if in Ready statereturn
Returns: bool
is_transitioning
Section titled “is_transitioning”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
process_event
Section titled “process_event”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
set_state_change_callback
Section titled “set_state_change_callback”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)
get_retry_count
Section titled “get_retry_count”uint32_t get_retry_count()Get retry count since last successful connection.
Number of retry attemptsreturn
Returns: uint32_t
reset_retry_count
Section titled “reset_retry_count”void reset_retry_count()Reset retry count (call on successful connection)
force_state
Section titled “force_state”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)
Methods
Section titled “Methods”state_to_string
Section titled “state_to_string”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 *
event_to_string
Section titled “event_to_string”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 *
Methods
Section titled “Methods”is_valid_transition
Section titled “is_valid_transition”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
transition_to
Section titled “transition_to”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)