Skip to content

ryu_ldn::ldn::LdnSessionHandler

LDN Session Handler.

Manages LDN session state and processes incoming packets from the server. Maintains network info, node list, and provides callbacks for state changes.Thread SafetyNOT thread-safe. All methods should be called from the same thread.classryu__ldn_1_1ldn_1_1LdnSessionHandler_1

Type: LdnSessionState

Current session state.

Type: bool

Whether we are the host.

Type: int8_t

Our node ID (-1 if not assigned)

Type: uint8_t

Last ping ID from server.

Type: protocol::SessionId

Assigned session ID.

Type: protocol::MacAddress

Assigned MAC address.

Type: protocol::NetworkInfo

Current network info.

Type: protocol::AcceptPolicy

Current accept policy.

Type: SessionStateCallback

Type: NetworkUpdatedCallback

Type: ScanResultCallback

Type: ScanCompletedCallback

Type: DisconnectedCallback

Type: ErrorCallback

Type: RejectedCallback

Type: AcceptPolicyChangedCallback

void LdnSessionHandler()

Default constructor.

Construct a new.Creates handler in None state.Initializes all state to defaults:

void ~LdnSessionHandler()

Destructor.

void LdnSessionHandler(const&)

Parameters:

  • param (const&)
& operator=(const&)

Parameters:

  • param (const&)

Returns: &

void set_state_callback(SessionStateCallback callback)

Set callback for state changes.

The callback is invoked whenever the session state changes. Useful for updating UI or triggering application logic.callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (SessionStateCallback)
void set_network_updated_callback(NetworkUpdatedCallback callback)

Set callback for network info updates.

Called when:callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (NetworkUpdatedCallback)
void set_scan_result_callback(ScanResultCallback callback)

Set callback for scan results.

Called once for each network found during scan operation. Application should collect these until scan_completed is called.callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (ScanResultCallback)
void set_scan_completed_callback(ScanCompletedCallback callback)

Set callback for scan completion.

Called when ScanReplyEnd is received, indicating no more scan results will be sent for the current scan operation.callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (ScanCompletedCallback)
void set_disconnected_callback(DisconnectedCallback callback)

Set callback for disconnection events.

Called when:callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (DisconnectedCallback)
void set_error_callback(ErrorCallback callback)

Set callback for errors.

Set callback for error events.Called when NetworkError packet is received from server. Application should check the error code and handle appropriately.callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (ErrorCallback)
void set_rejected_callback(RejectedCallback callback)

Set callback for rejection events.

Called when a player is rejected/kicked from the session.callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (RejectedCallback)
void set_accept_policy_changed_callback(AcceptPolicyChangedCallback callback)

Set callback for accept policy changes.

Called when accept policy is confirmed changed.callbackFunction pointer or nullptr to disableparam

Parameters:

  • callback (AcceptPolicyChangedCallback)
void handle_initialize(const protocol::LdnHeader & header, const protocol::InitializeMessage & msg)

Handle Initialize response from server.

Called when server responds to our Initialize with assigned ID/MAC. Transitions to Initialized state.The server sends Initialize in response to our Initialize request. This response contains:After receiving this, we are ready to scan, create, or join networks.headerPacket header (unused but kept for consistency)msgInitialize message containing assigned ID and MACparam

Parameters:

  • header (const protocol::LdnHeader &)
  • msg (const protocol::InitializeMessage &)
void handle_connected(const protocol::LdnHeader & header, const protocol::NetworkInfo & info)

Handle Connected packet (join success)

Called when successfully joined a network. Transitions to Station state.Server sends Connected when we successfully join a network. The packet contains full NetworkInfo with:headerPacket header (unused)infoComplete network informationparam

Parameters:

  • header (const protocol::LdnHeader &)
  • info (const protocol::NetworkInfo &)
void handle_sync_network(const protocol::LdnHeader & header, const protocol::NetworkInfo & info)

Handle SyncNetwork packet.

Called when network state changes (player join/leave, etc.). Updates stored network info and notifies callback.Server broadcasts SyncNetwork to all clients when network state changes:This packet is also sent when we successfully create an access point, confirming we are now the host.headerPacket header (unused)infoUpdated network informationparam

Parameters:

  • header (const protocol::LdnHeader &)
  • info (const protocol::NetworkInfo &)
void handle_scan_reply(const protocol::LdnHeader & header, const protocol::NetworkInfo & info)

Handle ScanReply packet.

Called for each network found during scan.Server sends one ScanReply for each network that matches the scan filter. Multiple ScanReply packets may be received before ScanReplyEnd.The application should collect these results until scan_completed is called.headerPacket header (unused)infoNetwork information for one found networkparam

Parameters:

  • header (const protocol::LdnHeader &)
  • info (const protocol::NetworkInfo &)
void handle_scan_reply_end(const protocol::LdnHeader & header)

Handle ScanReplyEnd packet.

Called when scan is complete.Server sends this after all ScanReply packets to indicate the scan operation is complete. No more networks will be reported for this scan.headerPacket header (unused)param

Parameters:

  • header (const protocol::LdnHeader &)
bool handle_ping(const protocol::LdnHeader & header, const protocol::PingMessage & msg)

Handle Ping packet.

Processes ping from server or response to our ping.Ping packets serve two purposes:headerPacket header (unused)msgPing message with requester and IDparamtrue if we need to echo the ping back (server requested)return

Parameters:

  • header (const protocol::LdnHeader &)
  • msg (const protocol::PingMessage &)

Returns: bool

void handle_disconnect(const protocol::LdnHeader & header, const protocol::DisconnectMessage & msg)

Handle Disconnect packet.

Called when a client disconnects from the session.Disconnect packets indicate a client left the session. The disconnect_ip field identifies who disconnected.If the disconnecting IP matches our IP, we’ve been kicked or the session was closed.headerPacket header (unused)msgDisconnect message with client IPparam

Parameters:

  • header (const protocol::LdnHeader &)
  • msg (const protocol::DisconnectMessage &)
void handle_network_error(const protocol::LdnHeader & header, const protocol::NetworkErrorMessage & msg)

Handle NetworkError packet.

Called when server reports an error.Server sends NetworkError when something goes wrong:The application should check the error code and handle appropriately. Some errors are recoverable, others may require reconnection.headerPacket header (unused)msgError message with codeparam

Parameters:

  • header (const protocol::LdnHeader &)
  • msg (const protocol::NetworkErrorMessage &)
void handle_reject(const protocol::LdnHeader & header, const protocol::RejectRequest & req)

Handle Reject packet.

Called when a player is rejected/kicked from the session. If we are the rejected player, we leave the session.Reject packets are sent when a player is kicked/rejected from the session. This can be initiated by the host or by the server.If the rejected node_id matches our local node ID, we have been kicked and should leave the session.headerPacket header (unused)reqReject request containing node_id and reasonparam

Parameters:

  • header (const protocol::LdnHeader &)
  • req (const protocol::RejectRequest &)
void handle_reject_reply(const protocol::LdnHeader & header)

Handle RejectReply packet.

Called when server confirms a rejection was processed. Usually sent back to the host who initiated the rejection.RejectReply is sent by the server to confirm that a rejection request was processed. This is typically sent back to the host who initiated the rejection.headerPacket header (unused)param

Parameters:

  • header (const protocol::LdnHeader &)
void handle_set_accept_policy(const protocol::LdnHeader & header, const protocol::SetAcceptPolicyRequest & req)

Handle SetAcceptPolicy response.

Called when server confirms accept policy change.SetAcceptPolicy response confirms that the accept policy was changed. This is sent back to the host who changed the policy.headerPacket header (unused)reqAccept policy request containing new policyparam

Parameters:

  • header (const protocol::LdnHeader &)
  • req (const protocol::SetAcceptPolicyRequest &)
LdnSessionState get_state()

Get current session state.

Current statereturn

Returns: LdnSessionState

bool is_in_session()

Check if in an active session.

true if in Station or AccessPoint statereturntrue if currently in a Station or AccessPoint statereturn

Returns: bool

bool is_host()

Check if we are the host.

true if we created the access pointreturn

Returns: bool

uint8_t get_node_count()

Get current node count.

Number of connected playersreturnNumber of connected players in current sessionreturn

Returns: uint8_t

uint8_t get_max_nodes()

Get maximum nodes for current session.

Maximum players allowedreturnMaximum players allowed, or 0 if not in sessionreturn

Returns: uint8_t

int8_t get_local_node_id()

Get our local node ID.

Our node ID (0-7), or -1 if not in sessionreturn

Returns: int8_t

void set_local_node_id(int8_t node_id)

Set our local node ID.

Called when we know our assigned node ID.Called when the server assigns us a node ID or when we determine our node ID from the network info.node_idOur node ID (0-7)param

Parameters:

  • node_id (int8_t)
const protocol::NetworkInfo & get_network_info()

Get current network info.

Reference to stored network info (may be stale if not in session)return

Returns: const protocol::NetworkInfo &

uint8_t get_last_ping_id()

Get last ping ID received from server.

Last ping ID for echo responsereturn

Returns: uint8_t

const protocol::SessionId & get_session_id()

Get assigned session ID.

Session ID assigned by serverreturn

Returns: const protocol::SessionId &

const protocol::MacAddress & get_mac_address()

Get assigned MAC address.

MAC address assigned by serverreturn

Returns: const protocol::MacAddress &

protocol::AcceptPolicy get_accept_policy()

Get current accept policy.

Current accept policy (only valid for host)return

Returns: protocol::AcceptPolicy

void leave_session()

Leave current session.

Transitions back to Initialized state.Clears session-specific state and returns to Initialized state. The application should send a Disconnect packet before calling this.

void reset()

Reset handler to initial state.

Clears all state and returns to None.Clears all state and callbacks. Use this when disconnecting from the server entirely.

void set_state(LdnSessionState new_state)

Set state and invoke callback.

Handles state transition and notifies registered callback.new_stateNew state to transition toparam

Parameters:

  • new_state (LdnSessionState)