Back to Site
Loading...
Searching...
No Matches
librats::IceManager Class Reference

ICE-lite Manager for NAT traversal. More...

#include <ice.h>

Public Member Functions

 IceManager ()
 
 IceManager (const IceConfig &config)
 
 ~IceManager ()
 
 IceManager (const IceManager &)=delete
 
IceManageroperator= (const IceManager &)=delete
 
void set_config (const IceConfig &config)
 Set ICE configuration.
 
const IceConfigconfig () const
 Get current configuration.
 
void add_stun_server (const std::string &host, uint16_t port=STUN_DEFAULT_PORT)
 Add a STUN server.
 
void add_turn_server (const std::string &host, uint16_t port, const std::string &username, const std::string &password)
 Add a TURN server with credentials.
 
void clear_ice_servers ()
 Clear all ICE servers.
 
bool gather_candidates ()
 Start gathering ICE candidates.
 
std::vector< IceCandidateget_local_candidates () const
 Get local candidates.
 
IceGatheringState get_gathering_state () const
 Get gathering state.
 
bool is_gathering_complete () const
 Check if gathering is complete.
 
void add_remote_candidate (const IceCandidate &candidate)
 Add a remote candidate.
 
void add_remote_candidates_from_sdp (const std::vector< std::string > &sdp_lines)
 Add remote candidates from SDP.
 
std::vector< IceCandidateget_remote_candidates () const
 Get remote candidates.
 
void end_of_remote_candidates ()
 Signal end of remote candidates (for trickle ICE)
 
void start_checks ()
 Start connectivity checks.
 
void stop_checks ()
 Stop connectivity checks.
 
std::vector< IceCandidatePairget_candidate_pairs () const
 Get candidate pairs.
 
std::optional< IceCandidatePairget_selected_pair () const
 Get the selected (best) candidate pair.
 
IceConnectionState get_connection_state () const
 Get connection state.
 
bool is_connected () const
 Check if connected.
 
std::optional< std::pair< std::string, uint16_t > > get_public_address () const
 Get our public address (from server-reflexive candidate)
 
socket_t get_socket () const
 Get the UDP socket used for ICE Can be used for data transfer after ICE completes.
 
uint16_t get_local_port () const
 Get local port.
 
void set_on_candidates_gathered (IceCandidatesCallback callback)
 
void set_on_new_candidate (IceNewCandidateCallback callback)
 
void set_on_gathering_state_changed (IceGatheringStateCallback callback)
 
void set_on_connection_state_changed (IceConnectionStateCallback callback)
 
void set_on_selected_pair (IceSelectedPairCallback callback)
 
void close ()
 Close ICE manager and release resources.
 
void restart ()
 Restart ICE (gather new candidates and start checks)
 

Detailed Description

ICE-lite Manager for NAT traversal.

Provides:

  • Candidate gathering from local interfaces, STUN, and TURN
  • Connectivity checks via STUN binding requests
  • Candidate pair prioritization and selection

Example usage:

config.add_stun_server("stun.l.google.com", 19302);
ice.set_on_candidates_gathered([](const std::vector<IceCandidate>& candidates) {
// Send candidates to remote peer via signaling
for (const auto& c : candidates) {
std::cout << c.to_sdp_attribute() << std::endl;
}
});
ice.set_on_connection_state_changed([](IceConnectionState state) {
std::cout << "ICE connected!" << std::endl;
}
});
// Start gathering
ice.gather_candidates();
// Add remote candidates from signaling
ice.add_remote_candidate(remote_candidate);
// Wait for connection
while (ice.get_connection_state() != IceConnectionState::Connected) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// Get selected pair for data transfer
auto pair = ice.get_selected_pair();
ICE-lite Manager for NAT traversal.
Definition ice.h:321
const IceConfig & config() const
Get current configuration.
Definition ice.h:343
IceConnectionState
ICE connection state.
Definition ice.h:73
@ Connected
At least one valid pair found.
ICE configuration.
Definition ice.h:226
void add_stun_server(const std::string &host, uint16_t port=STUN_DEFAULT_PORT)
Add a STUN server.
Definition ice.h:241

Definition at line 321 of file ice.h.

Constructor & Destructor Documentation

◆ IceManager() [1/3]

librats::IceManager::IceManager ( )

◆ IceManager() [2/3]

librats::IceManager::IceManager ( const IceConfig config)
explicit

◆ ~IceManager()

librats::IceManager::~IceManager ( )

◆ IceManager() [3/3]

librats::IceManager::IceManager ( const IceManager )
delete

Member Function Documentation

◆ add_remote_candidate()

void librats::IceManager::add_remote_candidate ( const IceCandidate candidate)

Add a remote candidate.

Parameters
candidateRemote candidate to add

◆ add_remote_candidates_from_sdp()

void librats::IceManager::add_remote_candidates_from_sdp ( const std::vector< std::string > &  sdp_lines)

Add remote candidates from SDP.

Parameters
sdp_linesSDP attribute lines (a=candidate:...)

◆ add_stun_server()

void librats::IceManager::add_stun_server ( const std::string &  host,
uint16_t  port = STUN_DEFAULT_PORT 
)

Add a STUN server.

◆ add_turn_server()

void librats::IceManager::add_turn_server ( const std::string &  host,
uint16_t  port,
const std::string &  username,
const std::string &  password 
)

Add a TURN server with credentials.

◆ clear_ice_servers()

void librats::IceManager::clear_ice_servers ( )

Clear all ICE servers.

◆ close()

void librats::IceManager::close ( )

Close ICE manager and release resources.

◆ config()

const IceConfig & librats::IceManager::config ( ) const
inline

Get current configuration.

Definition at line 343 of file ice.h.

◆ end_of_remote_candidates()

void librats::IceManager::end_of_remote_candidates ( )

Signal end of remote candidates (for trickle ICE)

◆ gather_candidates()

bool librats::IceManager::gather_candidates ( )

Start gathering ICE candidates.

Returns
true if gathering started successfully

◆ get_candidate_pairs()

std::vector< IceCandidatePair > librats::IceManager::get_candidate_pairs ( ) const

Get candidate pairs.

◆ get_connection_state()

IceConnectionState librats::IceManager::get_connection_state ( ) const
inline

Get connection state.

Definition at line 445 of file ice.h.

◆ get_gathering_state()

IceGatheringState librats::IceManager::get_gathering_state ( ) const
inline

Get gathering state.

Definition at line 379 of file ice.h.

◆ get_local_candidates()

std::vector< IceCandidate > librats::IceManager::get_local_candidates ( ) const

Get local candidates.

◆ get_local_port()

uint16_t librats::IceManager::get_local_port ( ) const
inline

Get local port.

Definition at line 478 of file ice.h.

◆ get_public_address()

std::optional< std::pair< std::string, uint16_t > > librats::IceManager::get_public_address ( ) const

Get our public address (from server-reflexive candidate)

Returns
Public IP and port, or nullopt if not discovered

◆ get_remote_candidates()

std::vector< IceCandidate > librats::IceManager::get_remote_candidates ( ) const

Get remote candidates.

◆ get_selected_pair()

std::optional< IceCandidatePair > librats::IceManager::get_selected_pair ( ) const

Get the selected (best) candidate pair.

◆ get_socket()

socket_t librats::IceManager::get_socket ( ) const
inline

Get the UDP socket used for ICE Can be used for data transfer after ICE completes.

Definition at line 473 of file ice.h.

◆ is_connected()

bool librats::IceManager::is_connected ( ) const
inline

Check if connected.

Definition at line 450 of file ice.h.

References librats::Completed, and librats::Connected.

◆ is_gathering_complete()

bool librats::IceManager::is_gathering_complete ( ) const
inline

Check if gathering is complete.

Definition at line 384 of file ice.h.

References librats::Complete.

◆ operator=()

IceManager & librats::IceManager::operator= ( const IceManager )
delete

◆ restart()

void librats::IceManager::restart ( )

Restart ICE (gather new candidates and start checks)

◆ set_config()

void librats::IceManager::set_config ( const IceConfig config)

Set ICE configuration.

◆ set_on_candidates_gathered()

void librats::IceManager::set_on_candidates_gathered ( IceCandidatesCallback  callback)

◆ set_on_connection_state_changed()

void librats::IceManager::set_on_connection_state_changed ( IceConnectionStateCallback  callback)

◆ set_on_gathering_state_changed()

void librats::IceManager::set_on_gathering_state_changed ( IceGatheringStateCallback  callback)

◆ set_on_new_candidate()

void librats::IceManager::set_on_new_candidate ( IceNewCandidateCallback  callback)

◆ set_on_selected_pair()

void librats::IceManager::set_on_selected_pair ( IceSelectedPairCallback  callback)

◆ start_checks()

void librats::IceManager::start_checks ( )

Start connectivity checks.

◆ stop_checks()

void librats::IceManager::stop_checks ( )

Stop connectivity checks.


The documentation for this class was generated from the following file: