RatsClient - Core peer-to-peer networking client. More...
#include <librats.h>
Public Types | |
| using | ConnectionCallback = std::function< void(socket_t, const std::string &)> |
| using | BinaryDataCallback = std::function< void(socket_t, const std::string &, const std::vector< uint8_t > &)> |
| using | StringDataCallback = std::function< void(socket_t, const std::string &, const std::string &)> |
| using | JsonDataCallback = std::function< void(socket_t, const std::string &, const nlohmann::json &)> |
| using | DisconnectCallback = std::function< void(socket_t, const std::string &)> |
| using | MessageCallback = std::function< void(const std::string &, const nlohmann::json &)> |
| using | SendCallback = std::function< void(bool, const std::string &)> |
| using | NetworkChangeCallback = std::function< void(const std::vector< std::string > &local_addresses)> |
| using | TorrentCreationProgressCallback = std::function< void(uint32_t current_piece, uint32_t total_pieces)> |
| Torrent creation progress callback type Called during piece hashing to report progress. | |
| using | SpiderAnnounceCallback = std::function< void(const std::string &info_hash, const std::string &peer_address)> |
| Spider announce callback type Called when a peer announces they have a torrent (announce_peer request received) | |
Public Member Functions | |
| RatsClient (int listen_port, int max_peers=10, const std::string &bind_address="") | |
| Constructor. | |
| ~RatsClient () | |
| Destructor. | |
| bool | start () |
| Start the RatsClient and begin listening for connections. | |
| void | stop () |
| Stop the RatsClient and close all connections. | |
| void | shutdown_all_threads () |
| Shutdown all background threads. | |
| bool | is_running () const |
| Check if the client is currently running. | |
| int | get_listen_port () const |
| std::string | get_bind_address () const |
| Get the bind address being used. | |
| bool | connect_to_peer (const std::string &host, int port) |
| Connect to a peer via direct TCP connection. | |
| void | disconnect_peer (socket_t socket) |
| Disconnect from a specific peer. | |
| void | disconnect_peer_by_id (const std::string &peer_id) |
| Disconnect from a peer by peer_id (preferred) | |
| bool | send_binary_to_peer (socket_t socket, const std::vector< uint8_t > &data, MessageDataType message_type=MessageDataType::BINARY) |
| Send binary data to a specific peer (primary method) | |
| bool | send_string_to_peer (socket_t socket, const std::string &data) |
| Send string data to a specific peer. | |
| bool | send_json_to_peer (socket_t socket, const nlohmann::json &data) |
| Send JSON data to a specific peer. | |
| bool | send_binary_to_peer_id (const std::string &peer_id, const std::vector< uint8_t > &data, MessageDataType message_type=MessageDataType::BINARY) |
| Send binary data to a peer by peer_id (preferred) | |
| bool | send_string_to_peer_id (const std::string &peer_id, const std::string &data) |
| Send string data to a peer by peer_id (preferred) | |
| bool | send_json_to_peer_id (const std::string &peer_id, const nlohmann::json &data) |
| Send JSON data to a peer by peer_id (preferred) | |
| int | broadcast_binary_to_peers (const std::vector< uint8_t > &data, MessageDataType message_type=MessageDataType::BINARY) |
| Broadcast binary data to all connected peers (primary method) | |
| int | broadcast_string_to_peers (const std::string &data) |
| Broadcast string data to all connected peers. | |
| int | broadcast_json_to_peers (const nlohmann::json &data) |
| Broadcast JSON data to all connected peers. | |
| int | get_peer_count () const |
| Get the number of currently connected peers. | |
| std::string | get_peer_id (socket_t socket) const |
| Get peer_id for a peer by socket (preferred) | |
| socket_t | get_peer_socket_by_id (const std::string &peer_id) const |
| Get socket for a peer by peer_id (preferred) | |
| std::string | get_our_peer_id () const |
| Get our own peer ID. | |
| std::vector< RatsPeer > | get_all_peers () const |
| Get all connected peers. | |
| std::vector< RatsPeer > | get_validated_peers () const |
| Get all peers that have completed handshake. | |
| std::optional< RatsPeer > | get_peer_by_id (const std::string &peer_id) const |
| Get peer information by peer ID. | |
| std::optional< RatsPeer > | get_peer_by_socket (socket_t socket) const |
| Get peer information by socket. | |
| int | get_max_peers () const |
| Get maximum number of peers. | |
| void | set_max_peers (int max_peers) |
| Set maximum number of peers. | |
| bool | is_peer_limit_reached () const |
| Check if peer limit has been reached. | |
| void | set_reconnect_enabled (bool enabled) |
| Enable or disable automatic reconnection to disconnected peers. | |
| bool | is_reconnect_enabled () const |
| Check if automatic reconnection is enabled. | |
| void | set_reconnect_config (const ReconnectConfig &config) |
| Set reconnection configuration. | |
| ReconnectConfig | get_reconnect_config () const |
| Get current reconnection configuration. | |
| size_t | get_reconnect_queue_size () const |
| Get the number of peers pending reconnection. | |
| void | clear_reconnect_queue () |
| Clear all pending reconnection attempts. | |
| std::vector< ReconnectInfo > | get_reconnect_queue () const |
| Get information about peers pending reconnection. | |
| void | set_connection_callback (ConnectionCallback callback) |
| Set connection callback (called when a new peer connects) | |
| void | set_binary_data_callback (BinaryDataCallback callback) |
| Set binary data callback (called when binary data is received) | |
| void | set_string_data_callback (StringDataCallback callback) |
| Set string data callback (called when string data is received) | |
| void | set_json_data_callback (JsonDataCallback callback) |
| Set JSON data callback (called when JSON data is received) | |
| void | set_disconnect_callback (DisconnectCallback callback) |
| Set disconnect callback (called when a peer disconnects) | |
| bool | start_dht_discovery (int dht_port=6881) |
| Start DHT discovery on specified port. | |
| void | stop_dht_discovery () |
| Stop DHT discovery. | |
| bool | find_peers_by_hash (const std::string &content_hash, std::function< void(const std::vector< std::string > &)> callback) |
| Find peers by content hash using DHT. | |
| bool | announce_for_hash (const std::string &content_hash, uint16_t port=0, std::function< void(const std::vector< std::string > &)> callback=nullptr) |
| Announce our presence for a content hash with optional peer discovery callback If callback is provided, peers discovered during DHT traversal will be returned through it. | |
| bool | is_dht_running () const |
| Check if DHT is currently running. | |
| size_t | get_dht_routing_table_size () const |
| Get the size of the DHT routing table. | |
| bool | start_mdns_discovery (const std::string &service_instance_name="", const std::map< std::string, std::string > &txt_records={}) |
| Start mDNS service discovery and announcement. | |
| void | stop_mdns_discovery () |
| Stop mDNS discovery. | |
| bool | is_mdns_running () const |
| Check if mDNS is currently running. | |
| void | set_mdns_callback (std::function< void(const std::string &, int, const std::string &)> callback) |
| Set mDNS service discovery callback. | |
| std::vector< MdnsService > | get_mdns_services () const |
| Get recently discovered mDNS services. | |
| bool | query_mdns_services () |
| Manually query for mDNS services. | |
| void | start_automatic_peer_discovery () |
| Start automatic peer discovery. | |
| void | stop_automatic_peer_discovery () |
| Stop automatic peer discovery. | |
| bool | is_automatic_discovery_running () const |
| Check if automatic discovery is running. | |
| std::chrono::seconds | calculate_discovery_interval () const |
| Calculate discovery interval based on current peer count Uses graduated scaling: more aggressive when fewer peers, less aggressive when nearly full. | |
| std::string | get_discovery_hash () const |
| Get the discovery hash for current protocol configuration. | |
| void | add_ignored_address (const std::string &ip_address) |
| Add an IP address to the ignore list (for blocking connections to self) | |
| void | set_protocol_name (const std::string &protocol_name) |
| Set custom protocol name for handshakes and DHT discovery. | |
| void | set_protocol_version (const std::string &protocol_version) |
| Set custom protocol version for handshakes. | |
| std::string | get_protocol_name () const |
| Get current protocol name. | |
| std::string | get_protocol_version () const |
| Get current protocol version. | |
| void | on (const std::string &message_type, MessageCallback callback) |
| Register a persistent message handler. | |
| void | once (const std::string &message_type, MessageCallback callback) |
| Register a one-time message handler. | |
| void | off (const std::string &message_type) |
| Remove all handlers for a message type. | |
| void | send (const std::string &message_type, const nlohmann::json &data, SendCallback callback=nullptr) |
| Send a message to all peers. | |
| void | send (const std::string &peer_id, const std::string &message_type, const nlohmann::json &data, SendCallback callback=nullptr) |
| Send a message to a specific peer. | |
| bool | initialize_encryption (bool enable) |
| Initialize encryption system. | |
| void | set_encryption_enabled (bool enabled) |
| Set encryption enabled/disabled. | |
| bool | is_encryption_enabled () const |
| Check if encryption is enabled. | |
| bool | is_peer_encrypted (const std::string &peer_id) const |
| Check if a peer connection is encrypted. | |
| bool | set_noise_static_keypair (const uint8_t private_key[32]) |
| Set a custom static keypair for Noise Protocol If not set, a new keypair is generated automatically. | |
| std::vector< uint8_t > | get_noise_static_public_key () const |
| Get our Noise Protocol static public key. | |
| std::vector< uint8_t > | get_peer_noise_public_key (const std::string &peer_id) const |
| Get the remote peer's Noise static public key. | |
| std::vector< uint8_t > | get_peer_handshake_hash (const std::string &peer_id) const |
| Get the handshake hash for a peer connection (for channel binding) | |
| bool | load_configuration () |
| Load configuration from files. | |
| bool | save_configuration () |
| Save configuration to files. | |
| bool | set_data_directory (const std::string &directory_path) |
| Set directory where data files will be stored. | |
| std::string | get_data_directory () const |
| Get current data directory path. | |
| int | load_and_reconnect_peers () |
| Load saved peers and attempt to reconnect. | |
| bool | load_historical_peers () |
| Load historical peers from a file. | |
| bool | save_historical_peers () |
| Save current peers to a historical file. | |
| void | clear_historical_peers () |
| Clear all historical peers. | |
| std::vector< RatsPeer > | get_historical_peers () const |
| Get all historical peers. | |
| nlohmann::json | get_connection_statistics () const |
| Get connection statistics. | |
| GossipSub & | get_gossipsub () |
| Get GossipSub instance for publish-subscribe messaging. | |
| bool | is_gossipsub_available () const |
| Check if GossipSub is available. | |
| bool | subscribe_to_topic (const std::string &topic) |
| Subscribe to a GossipSub topic. | |
| bool | unsubscribe_from_topic (const std::string &topic) |
| Unsubscribe from a GossipSub topic. | |
| bool | is_subscribed_to_topic (const std::string &topic) const |
| Check if subscribed to a GossipSub topic. | |
| std::vector< std::string > | get_subscribed_topics () const |
| Get list of subscribed GossipSub topics. | |
| bool | publish_to_topic (const std::string &topic, const std::string &message) |
| Publish a message to a GossipSub topic. | |
| bool | publish_json_to_topic (const std::string &topic, const nlohmann::json &message) |
| Publish a JSON message to a GossipSub topic. | |
| void | on_topic_message (const std::string &topic, std::function< void(const std::string &, const std::string &, const std::string &)> callback) |
| Set a message handler for a GossipSub topic using unified event API pattern. | |
| void | on_topic_json_message (const std::string &topic, std::function< void(const std::string &, const std::string &, const nlohmann::json &)> callback) |
| Set a JSON message handler for a GossipSub topic using unified event API pattern. | |
| void | on_topic_peer_joined (const std::string &topic, std::function< void(const std::string &, const std::string &)> callback) |
| Set a peer joined handler for a GossipSub topic using unified event API pattern. | |
| void | on_topic_peer_left (const std::string &topic, std::function< void(const std::string &, const std::string &)> callback) |
| Set a peer left handler for a GossipSub topic using unified event API pattern | |
| void | set_topic_message_validator (const std::string &topic, std::function< ValidationResult(const std::string &, const std::string &, const std::string &)> validator) |
| Set a message validator for a GossipSub topic. | |
| void | off_topic (const std::string &topic) |
| Remove all event handlers for a GossipSub topic. | |
| std::vector< std::string > | get_topic_peers (const std::string &topic) const |
| Get peers subscribed to a GossipSub topic. | |
| std::vector< std::string > | get_topic_mesh_peers (const std::string &topic) const |
| Get mesh peers for a GossipSub topic. | |
| nlohmann::json | get_gossipsub_statistics () const |
| Get GossipSub statistics. | |
| bool | is_gossipsub_running () const |
| Check if GossipSub is running. | |
| void | set_console_logging_enabled (bool enabled) |
| Enable or disable console logging When disabled, log messages will not be printed to stdout/stderr File logging (if enabled) will still work. | |
| bool | is_console_logging_enabled () const |
| Check if console logging is currently enabled. | |
| void | set_logging_enabled (bool enabled) |
| Enable or disable file logging When enabled, logs will be written to "rats.log" by default. | |
| bool | is_logging_enabled () const |
| Check if file logging is currently enabled. | |
| void | set_log_file_path (const std::string &file_path) |
| Set the log file path. | |
| std::string | get_log_file_path () const |
| Get the current log file path. | |
| void | set_log_level (LogLevel level) |
| Set the minimum log level. | |
| void | set_log_level (const std::string &level_str) |
| Set the minimum log level using string. | |
| LogLevel | get_log_level () const |
| Get the current log level. | |
| void | set_log_colors_enabled (bool enabled) |
| Enable or disable colored log output. | |
| bool | is_log_colors_enabled () const |
| Check if colored log output is enabled. | |
| void | set_log_timestamps_enabled (bool enabled) |
| Enable or disable timestamps in log output. | |
| bool | is_log_timestamps_enabled () const |
| Check if timestamps are enabled in log output. | |
| void | set_log_rotation_size (size_t max_size_bytes) |
| Set log file rotation size. | |
| void | set_log_retention_count (int count) |
| Set the number of log files to retain during rotation. | |
| void | set_log_rotate_on_startup (bool enabled) |
| Enable or disable log rotation on application startup When enabled, the existing log file will be rotated when logging starts, so each application run gets a fresh log file. | |
| bool | is_log_rotate_on_startup_enabled () const |
| Check if log rotation on startup is enabled. | |
| void | clear_log_file () |
| Clear/reset the current log file. | |
| FileTransferManager & | get_file_transfer_manager () |
| Get the file transfer manager instance. | |
| bool | is_file_transfer_available () const |
| Check whether the file transfer manager is initialized. | |
| std::string | send_file (const std::string &peer_id, const std::string &file_path, const std::string &remote_filename="") |
| Send a file to a peer. | |
| std::string | send_directory (const std::string &peer_id, const std::string &directory_path, const std::string &remote_name="") |
| Send a directory tree (recursively) to a peer. | |
| bool | accept_file_transfer (const std::string &transfer_id, const std::string &local_path) |
| Accept an incoming transfer. | |
| bool | reject_file_transfer (const std::string &transfer_id, const std::string &reason="") |
| Reject an incoming transfer. | |
| bool | pause_file_transfer (const std::string &transfer_id) |
| Pause an active transfer (either direction). | |
| bool | resume_file_transfer (const std::string &transfer_id) |
| Resume a paused transfer. | |
| bool | cancel_file_transfer (const std::string &transfer_id) |
| Cancel an active or paused transfer. | |
| std::shared_ptr< FileTransferProgress > | get_file_transfer_progress (const std::string &transfer_id) const |
| Get a progress snapshot, or nullptr if the transfer is unknown. | |
| std::vector< std::shared_ptr< FileTransferProgress > > | get_active_file_transfers () const |
| Get progress snapshots for all non-finished transfers. | |
| nlohmann::json | get_file_transfer_statistics () const |
| Get aggregate file transfer statistics as JSON. | |
| void | set_file_transfer_config (const FileTransferConfig &config) |
| Replace the file transfer configuration. | |
| FileTransferConfig | get_file_transfer_config () const |
| Get the current file transfer configuration. | |
| void | on_file_transfer_progress (TransferProgressCallback callback) |
| Set the progress callback (fires for both directions). | |
| void | on_file_transfer_completed (TransferCompletedCallback callback) |
| Set the completion callback (fires once per transfer). | |
| void | on_file_transfer_request (TransferOfferCallback callback) |
| Set the incoming-offer callback. | |
| IceManager & | get_ice_manager () |
| Get the ICE manager instance. | |
| bool | is_ice_available () const |
| Check if ICE is available. | |
| void | add_stun_server (const std::string &host, uint16_t port=STUN_DEFAULT_PORT) |
| Add a STUN server for NAT traversal. | |
| void | add_turn_server (const std::string &host, uint16_t port, const std::string &username, const std::string &password) |
| Add a TURN server for relay-based NAT traversal. | |
| void | clear_ice_servers () |
| Clear all ICE (STUN/TURN) servers. | |
| bool | gather_ice_candidates () |
| Start gathering ICE candidates This discovers our public address and generates connection candidates. | |
| std::vector< IceCandidate > | get_ice_candidates () const |
| Get our local ICE candidates Call after gathering is complete. | |
| bool | is_ice_gathering_complete () const |
| Check if ICE candidate gathering is complete. | |
| std::optional< std::pair< std::string, uint16_t > > | get_public_address () const |
| Get our public IP address (discovered via STUN) | |
| std::optional< StunMappedAddress > | discover_public_address (const std::string &server="stun.l.google.com", uint16_t port=19302, int timeout_ms=5000) |
| Perform a simple STUN binding request to discover public address This is a convenience method that doesn't require full ICE setup. | |
| void | add_remote_ice_candidate (const IceCandidate &candidate) |
| Add a remote ICE candidate (received from peer via signaling) | |
| void | add_remote_ice_candidates_from_sdp (const std::vector< std::string > &sdp_lines) |
| Add remote ICE candidates from SDP attribute lines. | |
| void | end_of_remote_ice_candidates () |
| Signal end of remote candidates (trickle ICE complete) | |
| void | start_ice_checks () |
| Start ICE connectivity checks. | |
| IceConnectionState | get_ice_connection_state () const |
| Get current ICE connection state. | |
| IceGatheringState | get_ice_gathering_state () const |
| Get ICE gathering state. | |
| bool | is_ice_connected () const |
| Check if ICE is connected. | |
| std::optional< IceCandidatePair > | get_ice_selected_pair () const |
| Get the selected ICE candidate pair. | |
| void | on_ice_candidates_gathered (IceCandidatesCallback callback) |
| Set callback for ICE candidates gathered. | |
| void | on_ice_new_candidate (IceNewCandidateCallback callback) |
| Set callback for new ICE candidate (trickle ICE) | |
| void | on_ice_gathering_state_changed (IceGatheringStateCallback callback) |
| Set callback for ICE gathering state changes. | |
| void | on_ice_connection_state_changed (IceConnectionStateCallback callback) |
| Set callback for ICE connection state changes. | |
| void | on_ice_selected_pair (IceSelectedPairCallback callback) |
| Set callback for ICE selected pair. | |
| void | set_ice_config (const IceConfig &config) |
| Set ICE configuration. | |
| IceConfig | get_ice_config () const |
| Get current ICE configuration. | |
| void | close_ice () |
| Close ICE manager and release resources. | |
| void | restart_ice () |
| Restart ICE (re-gather candidates and restart checks) | |
| void | set_port_mapping_enabled (bool enabled) |
| Enable or disable automatic port forwarding. | |
| bool | is_port_mapping_enabled () const |
| Whether automatic port forwarding is currently enabled. | |
| void | set_port_mapping_config (const PortMappingConfig &config) |
| Replace the full port mapping configuration (takes effect on next start). | |
| PortMappingConfig | get_port_mapping_config () const |
| Get the current port mapping configuration. | |
| void | add_port_mapping (PortMapProtocol protocol, uint16_t port) |
| Request an additional port mapping beyond the automatic listen-port mapping (e.g. | |
| std::optional< std::pair< std::string, uint16_t > > | get_mapped_public_address () const |
| Get the public (external) address discovered by the port mapping backends. | |
| void | on_port_mapping (PortMapCallback callback) |
| Register a callback fired whenever a port mapping is established, refreshed, removed or fails (invoked from a backend worker thread). | |
| void | set_network_change_detection_enabled (bool enabled) |
| Enable or disable automatic reaction to host network changes. | |
| bool | is_network_change_detection_enabled () const |
| void | on_network_changed (NetworkChangeCallback callback) |
| Register a callback fired (debounced) whenever the set of local interface addresses changes. | |
| StorageManager & | get_storage_manager () |
| Get the storage manager instance. | |
| bool | is_storage_available () const |
| Check if storage is available. | |
| bool | storage_put (const std::string &key, const std::string &value) |
| Store a string value. | |
| bool | storage_put (const std::string &key, int64_t value) |
| Store a 64-bit integer value. | |
| bool | storage_put (const std::string &key, double value) |
| Store a double-precision floating point value. | |
| bool | storage_put (const std::string &key, const std::vector< uint8_t > &value) |
| Store binary data. | |
| bool | storage_put_json (const std::string &key, const nlohmann::json &value) |
| Store a JSON document. | |
| std::optional< std::string > | storage_get_string (const std::string &key) const |
| Get a string value. | |
| std::optional< int64_t > | storage_get_int (const std::string &key) const |
| Get a 64-bit integer value. | |
| std::optional< double > | storage_get_double (const std::string &key) const |
| Get a double-precision floating point value. | |
| std::optional< std::vector< uint8_t > > | storage_get_binary (const std::string &key) const |
| Get binary data. | |
| std::optional< nlohmann::json > | storage_get_json (const std::string &key) const |
| Get a JSON document. | |
| bool | storage_delete (const std::string &key) |
| Delete a key from storage. | |
| bool | storage_has (const std::string &key) const |
| Check if a key exists in storage. | |
| std::vector< std::string > | storage_keys () const |
| Get all keys in storage. | |
| std::vector< std::string > | storage_keys_with_prefix (const std::string &prefix) const |
| Get keys matching a prefix. | |
| size_t | storage_size () const |
| Get the number of entries in storage. | |
| bool | storage_request_sync () |
| Request storage sync from connected peers. | |
| bool | is_storage_synced () const |
| Check if storage is synchronized. | |
| nlohmann::json | get_storage_statistics () const |
| Get storage statistics. | |
| void | set_storage_config (const StorageConfig &config) |
| Set storage configuration. | |
| const StorageConfig & | get_storage_config () const |
| Get current storage configuration. | |
| void | on_storage_change (StorageChangeCallback callback) |
| Set storage change callback. | |
| void | on_storage_sync_complete (StorageSyncCompleteCallback callback) |
| Set storage sync complete callback. | |
| bool | enable_bittorrent (int listen_port=6881) |
| Enable BitTorrent functionality. | |
| void | set_resume_data_path (const std::string &path) |
| Set the directory for storing resume data files Resume data allows torrents to resume from where they left off. | |
| void | disable_bittorrent () |
| Disable BitTorrent functionality. | |
| bool | is_bittorrent_enabled () const |
| Check if BitTorrent is enabled. | |
| std::shared_ptr< TorrentDownload > | add_torrent (const std::string &torrent_file, const std::string &download_path) |
| Add a torrent from a file. | |
| std::shared_ptr< TorrentDownload > | add_torrent (const TorrentInfo &torrent_info, const std::string &download_path) |
| Add a torrent from TorrentInfo. | |
| std::shared_ptr< TorrentDownload > | add_torrent_by_hash (const InfoHash &info_hash, const std::string &download_path) |
| Add a torrent by info hash (magnet link style - uses DHT to find peers) | |
| std::shared_ptr< TorrentDownload > | add_torrent_by_hash (const std::string &info_hash_hex, const std::string &download_path) |
| Add a torrent by info hash hex string (magnet link style - uses DHT to find peers) | |
| bool | remove_torrent (const InfoHash &info_hash) |
| Remove a torrent by info hash. | |
| std::shared_ptr< TorrentDownload > | get_torrent (const InfoHash &info_hash) |
| Get a torrent by info hash. | |
| std::vector< std::shared_ptr< TorrentDownload > > | get_all_torrents () |
| Get all active torrents. | |
| size_t | get_active_torrents_count () const |
| Get the number of active torrents. | |
| std::pair< uint64_t, uint64_t > | get_bittorrent_stats () const |
| Get BitTorrent statistics (downloaded and uploaded bytes) | |
| void | get_torrent_metadata (const InfoHash &info_hash, std::function< void(const TorrentInfo &, bool, const std::string &)> callback) |
| Get torrent metadata without downloading (requires DHT to be running) | |
| void | get_torrent_metadata (const std::string &info_hash_hex, std::function< void(const TorrentInfo &, bool, const std::string &)> callback) |
| Get torrent metadata without downloading by hex string (requires DHT to be running) | |
| void | get_torrent_metadata_from_peer (const InfoHash &info_hash, const std::string &peer_ip, uint16_t peer_port, std::function< void(const TorrentInfo &, bool, const std::string &)> callback) |
| Get torrent metadata directly from a specific peer (fast path - no DHT search needed) This is more efficient when you already know a peer that has the torrent (e.g., from announce_peer) | |
| void | get_torrent_metadata_from_peer (const std::string &info_hash_hex, const std::string &peer_ip, uint16_t peer_port, std::function< void(const TorrentInfo &, bool, const std::string &)> callback) |
| Get torrent metadata directly from a specific peer by hex string (fast path) | |
| std::optional< TorrentInfo > | create_torrent_from_path (const std::string &path, const std::vector< std::string > &trackers={}, const std::string &comment="", TorrentCreationProgressCallback progress_callback=nullptr) |
| Create a torrent from a file or directory and return TorrentInfo This is a synchronous operation that reads all files to compute piece hashes. | |
| std::vector< uint8_t > | create_torrent_data (const std::string &path, const std::vector< std::string > &trackers={}, const std::string &comment="", TorrentCreationProgressCallback progress_callback=nullptr) |
| Create a torrent from a file or directory and return raw torrent data. | |
| bool | create_torrent_file (const std::string &path, const std::string &output_file, const std::vector< std::string > &trackers={}, const std::string &comment="", TorrentCreationProgressCallback progress_callback=nullptr) |
| Create a torrent and save it to a file. | |
| std::shared_ptr< TorrentDownload > | create_and_seed_torrent (const std::string &path, const std::vector< std::string > &trackers={}, const std::string &comment="", TorrentCreationProgressCallback progress_callback=nullptr) |
| Create a torrent, add it to the BitTorrent client, and start seeding This combines torrent creation with immediately starting to seed it. | |
| void | set_spider_mode (bool enable) |
| Enable spider mode on DHT In spider mode: | |
| bool | is_spider_mode () const |
| Check if spider mode is enabled. | |
| void | set_spider_announce_callback (SpiderAnnounceCallback callback) |
| Set callback for announce_peer requests (spider mode) Called when other peers announce they have a torrent. | |
| void | set_spider_ignore (bool ignore) |
| Set spider ignore mode - when true, incoming requests are not processed Useful for rate limiting in spider mode. | |
| bool | is_spider_ignoring () const |
| Check if spider ignore mode is enabled. | |
| void | spider_walk () |
| Trigger a single spider walk iteration Sends find_node to a random node from the spider pool Call this periodically at desired frequency to discover new nodes. | |
| size_t | get_spider_pool_size () const |
| Get the size of the spider node pool. | |
| size_t | get_spider_visited_count () const |
| Get the number of visited nodes in spider mode. | |
| void | clear_spider_state () |
| Clear spider state (pool and visited nodes) Useful for resetting the spider walk. | |
Public Member Functions inherited from librats::ThreadManager | |
| ThreadManager () | |
| virtual | ~ThreadManager () |
| void | add_managed_thread (std::thread &&t, const std::string &name="unnamed") |
| void | cleanup_finished_threads () |
| void | shutdown_all_threads () |
| void | join_all_active_threads () |
| size_t | get_active_thread_count () const |
Static Public Member Functions | |
| static std::string | get_rats_peer_discovery_hash () |
| Get the well-known RATS peer discovery hash. | |
Additional Inherited Members | |
Protected Member Functions inherited from librats::ThreadManager | |
| void | notify_shutdown () |
Protected Attributes inherited from librats::ThreadManager | |
| std::condition_variable | shutdown_cv_ |
| std::mutex | shutdown_mutex_ |
RatsClient - Core peer-to-peer networking client.
| using librats::RatsClient::BinaryDataCallback = std::function<void(socket_t, const std::string&, const std::vector<uint8_t>&)> |
| using librats::RatsClient::ConnectionCallback = std::function<void(socket_t, const std::string&)> |
| using librats::RatsClient::DisconnectCallback = std::function<void(socket_t, const std::string&)> |
| using librats::RatsClient::JsonDataCallback = std::function<void(socket_t, const std::string&, const nlohmann::json&)> |
| using librats::RatsClient::MessageCallback = std::function<void(const std::string&, const nlohmann::json&)> |
| using librats::RatsClient::NetworkChangeCallback = std::function<void(const std::vector<std::string>& local_addresses)> |
| using librats::RatsClient::SendCallback = std::function<void(bool, const std::string&)> |
| using librats::RatsClient::SpiderAnnounceCallback = std::function<void(const std::string& info_hash, const std::string& peer_address)> |
| using librats::RatsClient::StringDataCallback = std::function<void(socket_t, const std::string&, const std::string&)> |
| using librats::RatsClient::TorrentCreationProgressCallback = std::function<void(uint32_t current_piece, uint32_t total_pieces)> |
| librats::RatsClient::RatsClient | ( | int | listen_port, |
| int | max_peers = 10, |
||
| const std::string & | bind_address = "" |
||
| ) |
Constructor.
| listen_port | Port to listen on for incoming connections |
| max_peers | Maximum number of concurrent peers (default: 10) |
| bind_address | Interface IP address to bind to (empty for all interfaces) |
| librats::RatsClient::~RatsClient | ( | ) |
Destructor.
| bool librats::RatsClient::accept_file_transfer | ( | const std::string & | transfer_id, |
| const std::string & | local_path | ||
| ) |
Accept an incoming transfer.
For a file, local_path is the destination file path; for a directory, it is the destination directory.
| void librats::RatsClient::add_ignored_address | ( | const std::string & | ip_address | ) |
Add an IP address to the ignore list (for blocking connections to self)
| ip_address | IP address to ignore |
| void librats::RatsClient::add_port_mapping | ( | PortMapProtocol | protocol, |
| uint16_t | port | ||
| ) |
Request an additional port mapping beyond the automatic listen-port mapping (e.g.
a DHT UDP port). Has effect only while port mapping is enabled.
| void librats::RatsClient::add_remote_ice_candidate | ( | const IceCandidate & | candidate | ) |
Add a remote ICE candidate (received from peer via signaling)
| candidate | Remote candidate to add |
| void librats::RatsClient::add_remote_ice_candidates_from_sdp | ( | const std::vector< std::string > & | sdp_lines | ) |
Add remote ICE candidates from SDP attribute lines.
| sdp_lines | Vector of SDP candidate lines |
| void librats::RatsClient::add_stun_server | ( | const std::string & | host, |
| uint16_t | port = STUN_DEFAULT_PORT |
||
| ) |
Add a STUN server for NAT traversal.
| host | STUN server hostname or IP |
| port | STUN server port (default: 3478) |
| std::shared_ptr< TorrentDownload > librats::RatsClient::add_torrent | ( | const std::string & | torrent_file, |
| const std::string & | download_path | ||
| ) |
Add a torrent from a file.
| torrent_file | Path to the .torrent file |
| download_path | Directory where files will be downloaded |
| std::shared_ptr< TorrentDownload > librats::RatsClient::add_torrent | ( | const TorrentInfo & | torrent_info, |
| const std::string & | download_path | ||
| ) |
Add a torrent from TorrentInfo.
| torrent_info | TorrentInfo object with torrent metadata |
| download_path | Directory where files will be downloaded |
| std::shared_ptr< TorrentDownload > librats::RatsClient::add_torrent_by_hash | ( | const InfoHash & | info_hash, |
| const std::string & | download_path | ||
| ) |
Add a torrent by info hash (magnet link style - uses DHT to find peers)
| info_hash | Info hash of the torrent |
| download_path | Directory where files will be downloaded |
| std::shared_ptr< TorrentDownload > librats::RatsClient::add_torrent_by_hash | ( | const std::string & | info_hash_hex, |
| const std::string & | download_path | ||
| ) |
Add a torrent by info hash hex string (magnet link style - uses DHT to find peers)
| info_hash_hex | Info hash as 40-character hex string |
| download_path | Directory where files will be downloaded |
| void librats::RatsClient::add_turn_server | ( | const std::string & | host, |
| uint16_t | port, | ||
| const std::string & | username, | ||
| const std::string & | password | ||
| ) |
Add a TURN server for relay-based NAT traversal.
| host | TURN server hostname or IP |
| port | TURN server port (default: 3478) |
| username | TURN username |
| password | TURN password |
| bool librats::RatsClient::announce_for_hash | ( | const std::string & | content_hash, |
| uint16_t | port = 0, |
||
| std::function< void(const std::vector< std::string > &)> | callback = nullptr |
||
| ) |
Announce our presence for a content hash with optional peer discovery callback If callback is provided, peers discovered during DHT traversal will be returned through it.
| content_hash | Hash to announce for (40-character hex string) |
| port | Port to announce (default: our listen port) |
| callback | Optional function to call with discovered peers during traversal |
| int librats::RatsClient::broadcast_binary_to_peers | ( | const std::vector< uint8_t > & | data, |
| MessageDataType | message_type = MessageDataType::BINARY |
||
| ) |
Broadcast binary data to all connected peers (primary method)
| data | Binary data to broadcast |
| message_type | Type of message data (BINARY, STRING, JSON) |
| int librats::RatsClient::broadcast_json_to_peers | ( | const nlohmann::json & | data | ) |
Broadcast JSON data to all connected peers.
| data | JSON data to broadcast |
| int librats::RatsClient::broadcast_string_to_peers | ( | const std::string & | data | ) |
Broadcast string data to all connected peers.
| data | String data to broadcast |
| std::chrono::seconds librats::RatsClient::calculate_discovery_interval | ( | ) | const |
Calculate discovery interval based on current peer count Uses graduated scaling: more aggressive when fewer peers, less aggressive when nearly full.
| bool librats::RatsClient::cancel_file_transfer | ( | const std::string & | transfer_id | ) |
Cancel an active or paused transfer.
| void librats::RatsClient::clear_historical_peers | ( | ) |
Clear all historical peers.
| void librats::RatsClient::clear_ice_servers | ( | ) |
Clear all ICE (STUN/TURN) servers.
| void librats::RatsClient::clear_log_file | ( | ) |
Clear/reset the current log file.
| void librats::RatsClient::clear_reconnect_queue | ( | ) |
Clear all pending reconnection attempts.
| void librats::RatsClient::clear_spider_state | ( | ) |
Clear spider state (pool and visited nodes) Useful for resetting the spider walk.
| void librats::RatsClient::close_ice | ( | ) |
Close ICE manager and release resources.
| bool librats::RatsClient::connect_to_peer | ( | const std::string & | host, |
| int | port | ||
| ) |
Connect to a peer via direct TCP connection.
| host | Target host/IP address |
| port | Target port |
| std::shared_ptr< TorrentDownload > librats::RatsClient::create_and_seed_torrent | ( | const std::string & | path, |
| const std::vector< std::string > & | trackers = {}, |
||
| const std::string & | comment = "", |
||
| TorrentCreationProgressCallback | progress_callback = nullptr |
||
| ) |
Create a torrent, add it to the BitTorrent client, and start seeding This combines torrent creation with immediately starting to seed it.
| path | Path to file or directory |
| trackers | Optional list of tracker URLs |
| comment | Optional comment |
| progress_callback | Optional callback for progress updates |
| std::vector< uint8_t > librats::RatsClient::create_torrent_data | ( | const std::string & | path, |
| const std::vector< std::string > & | trackers = {}, |
||
| const std::string & | comment = "", |
||
| TorrentCreationProgressCallback | progress_callback = nullptr |
||
| ) |
Create a torrent from a file or directory and return raw torrent data.
| path | Path to file or directory |
| trackers | Optional list of tracker URLs |
| comment | Optional comment |
| progress_callback | Optional callback for progress updates |
| bool librats::RatsClient::create_torrent_file | ( | const std::string & | path, |
| const std::string & | output_file, | ||
| const std::vector< std::string > & | trackers = {}, |
||
| const std::string & | comment = "", |
||
| TorrentCreationProgressCallback | progress_callback = nullptr |
||
| ) |
Create a torrent and save it to a file.
| path | Path to file or directory |
| output_file | Path to save the .torrent file |
| trackers | Optional list of tracker URLs |
| comment | Optional comment |
| progress_callback | Optional callback for progress updates |
| std::optional< TorrentInfo > librats::RatsClient::create_torrent_from_path | ( | const std::string & | path, |
| const std::vector< std::string > & | trackers = {}, |
||
| const std::string & | comment = "", |
||
| TorrentCreationProgressCallback | progress_callback = nullptr |
||
| ) |
Create a torrent from a file or directory and return TorrentInfo This is a synchronous operation that reads all files to compute piece hashes.
| path | Path to file or directory to create torrent from |
| trackers | Optional list of tracker URLs |
| comment | Optional comment |
| progress_callback | Optional callback for progress updates |
| void librats::RatsClient::disable_bittorrent | ( | ) |
Disable BitTorrent functionality.
| void librats::RatsClient::disconnect_peer | ( | socket_t | socket | ) |
Disconnect from a specific peer.
| socket | Peer socket to disconnect |
| void librats::RatsClient::disconnect_peer_by_id | ( | const std::string & | peer_id | ) |
Disconnect from a peer by peer_id (preferred)
| peer_id | Peer ID to disconnect |
| std::optional< StunMappedAddress > librats::RatsClient::discover_public_address | ( | const std::string & | server = "stun.l.google.com", |
| uint16_t | port = 19302, |
||
| int | timeout_ms = 5000 |
||
| ) |
Perform a simple STUN binding request to discover public address This is a convenience method that doesn't require full ICE setup.
| server | STUN server hostname |
| port | STUN server port (default: 3478) |
| timeout_ms | Timeout in milliseconds (default: 5000) |
| bool librats::RatsClient::enable_bittorrent | ( | int | listen_port = 6881 | ) |
Enable BitTorrent functionality.
| listen_port | Port to listen for BitTorrent connections (default: 6881) |
| void librats::RatsClient::end_of_remote_ice_candidates | ( | ) |
Signal end of remote candidates (trickle ICE complete)
| bool librats::RatsClient::find_peers_by_hash | ( | const std::string & | content_hash, |
| std::function< void(const std::vector< std::string > &)> | callback | ||
| ) |
Find peers by content hash using DHT.
| content_hash | Hash to search for (40-character hex string) |
| callback | Function to call with discovered peers |
| bool librats::RatsClient::gather_ice_candidates | ( | ) |
Start gathering ICE candidates This discovers our public address and generates connection candidates.
| std::vector< std::shared_ptr< FileTransferProgress > > librats::RatsClient::get_active_file_transfers | ( | ) | const |
Get progress snapshots for all non-finished transfers.
| size_t librats::RatsClient::get_active_torrents_count | ( | ) | const |
Get the number of active torrents.
| std::vector< RatsPeer > librats::RatsClient::get_all_peers | ( | ) | const |
Get all connected peers.
| std::vector< std::shared_ptr< TorrentDownload > > librats::RatsClient::get_all_torrents | ( | ) |
Get all active torrents.
| std::string librats::RatsClient::get_bind_address | ( | ) | const |
Get the bind address being used.
| std::pair< uint64_t, uint64_t > librats::RatsClient::get_bittorrent_stats | ( | ) | const |
Get BitTorrent statistics (downloaded and uploaded bytes)
| nlohmann::json librats::RatsClient::get_connection_statistics | ( | ) | const |
Get connection statistics.
| std::string librats::RatsClient::get_data_directory | ( | ) | const |
Get current data directory path.
| size_t librats::RatsClient::get_dht_routing_table_size | ( | ) | const |
Get the size of the DHT routing table.
| std::string librats::RatsClient::get_discovery_hash | ( | ) | const |
Get the discovery hash for current protocol configuration.
| FileTransferConfig librats::RatsClient::get_file_transfer_config | ( | ) | const |
Get the current file transfer configuration.
| FileTransferManager & librats::RatsClient::get_file_transfer_manager | ( | ) |
Get the file transfer manager instance.
| std::shared_ptr< FileTransferProgress > librats::RatsClient::get_file_transfer_progress | ( | const std::string & | transfer_id | ) | const |
Get a progress snapshot, or nullptr if the transfer is unknown.
| nlohmann::json librats::RatsClient::get_file_transfer_statistics | ( | ) | const |
Get aggregate file transfer statistics as JSON.
| GossipSub & librats::RatsClient::get_gossipsub | ( | ) |
| nlohmann::json librats::RatsClient::get_gossipsub_statistics | ( | ) | const |
| std::vector< RatsPeer > librats::RatsClient::get_historical_peers | ( | ) | const |
Get all historical peers.
| std::vector< IceCandidate > librats::RatsClient::get_ice_candidates | ( | ) | const |
Get our local ICE candidates Call after gathering is complete.
| IceConfig librats::RatsClient::get_ice_config | ( | ) | const |
Get current ICE configuration.
| IceConnectionState librats::RatsClient::get_ice_connection_state | ( | ) | const |
Get current ICE connection state.
| IceGatheringState librats::RatsClient::get_ice_gathering_state | ( | ) | const |
Get ICE gathering state.
| IceManager & librats::RatsClient::get_ice_manager | ( | ) |
Get the ICE manager instance.
| std::optional< IceCandidatePair > librats::RatsClient::get_ice_selected_pair | ( | ) | const |
Get the selected ICE candidate pair.
| int librats::RatsClient::get_listen_port | ( | ) | const |
| std::string librats::RatsClient::get_log_file_path | ( | ) | const |
Get the current log file path.
| LogLevel librats::RatsClient::get_log_level | ( | ) | const |
Get the current log level.
| std::optional< std::pair< std::string, uint16_t > > librats::RatsClient::get_mapped_public_address | ( | ) | const |
Get the public (external) address discovered by the port mapping backends.
| int librats::RatsClient::get_max_peers | ( | ) | const |
Get maximum number of peers.
| std::vector< MdnsService > librats::RatsClient::get_mdns_services | ( | ) | const |
Get recently discovered mDNS services.
| std::vector< uint8_t > librats::RatsClient::get_noise_static_public_key | ( | ) | const |
Get our Noise Protocol static public key.
| std::string librats::RatsClient::get_our_peer_id | ( | ) | const |
Get our own peer ID.
| std::optional< RatsPeer > librats::RatsClient::get_peer_by_id | ( | const std::string & | peer_id | ) | const |
Get peer information by peer ID.
| peer_id | The peer ID to look up |
Get peer information by socket.
| socket | The socket handle to look up |
| int librats::RatsClient::get_peer_count | ( | ) | const |
Get the number of currently connected peers.
| std::vector< uint8_t > librats::RatsClient::get_peer_handshake_hash | ( | const std::string & | peer_id | ) | const |
Get the handshake hash for a peer connection (for channel binding)
| peer_id | Peer ID to query |
| std::string librats::RatsClient::get_peer_id | ( | socket_t | socket | ) | const |
| std::vector< uint8_t > librats::RatsClient::get_peer_noise_public_key | ( | const std::string & | peer_id | ) | const |
Get the remote peer's Noise static public key.
| peer_id | Peer ID to query |
| socket_t librats::RatsClient::get_peer_socket_by_id | ( | const std::string & | peer_id | ) | const |
| PortMappingConfig librats::RatsClient::get_port_mapping_config | ( | ) | const |
Get the current port mapping configuration.
| std::string librats::RatsClient::get_protocol_name | ( | ) | const |
Get current protocol name.
| std::string librats::RatsClient::get_protocol_version | ( | ) | const |
Get current protocol version.
| std::optional< std::pair< std::string, uint16_t > > librats::RatsClient::get_public_address | ( | ) | const |
Get our public IP address (discovered via STUN)
|
static |
Get the well-known RATS peer discovery hash.
| ReconnectConfig librats::RatsClient::get_reconnect_config | ( | ) | const |
Get current reconnection configuration.
| std::vector< ReconnectInfo > librats::RatsClient::get_reconnect_queue | ( | ) | const |
Get information about peers pending reconnection.
| size_t librats::RatsClient::get_reconnect_queue_size | ( | ) | const |
Get the number of peers pending reconnection.
| size_t librats::RatsClient::get_spider_pool_size | ( | ) | const |
Get the size of the spider node pool.
| size_t librats::RatsClient::get_spider_visited_count | ( | ) | const |
Get the number of visited nodes in spider mode.
| const StorageConfig & librats::RatsClient::get_storage_config | ( | ) | const |
Get current storage configuration.
| StorageManager & librats::RatsClient::get_storage_manager | ( | ) |
Get the storage manager instance.
| nlohmann::json librats::RatsClient::get_storage_statistics | ( | ) | const |
Get storage statistics.
| std::vector< std::string > librats::RatsClient::get_subscribed_topics | ( | ) | const |
Get list of subscribed GossipSub topics.
| std::vector< std::string > librats::RatsClient::get_topic_mesh_peers | ( | const std::string & | topic | ) | const |
Get mesh peers for a GossipSub topic.
| topic | Topic name |
| std::vector< std::string > librats::RatsClient::get_topic_peers | ( | const std::string & | topic | ) | const |
| std::shared_ptr< TorrentDownload > librats::RatsClient::get_torrent | ( | const InfoHash & | info_hash | ) |
Get a torrent by info hash.
| info_hash | Info hash of the torrent |
| void librats::RatsClient::get_torrent_metadata | ( | const InfoHash & | info_hash, |
| std::function< void(const TorrentInfo &, bool, const std::string &)> | callback | ||
| ) |
Get torrent metadata without downloading (requires DHT to be running)
| info_hash | Info hash of the torrent |
| callback | Function called when metadata is retrieved (torrent_info, success, error_message) |
| void librats::RatsClient::get_torrent_metadata | ( | const std::string & | info_hash_hex, |
| std::function< void(const TorrentInfo &, bool, const std::string &)> | callback | ||
| ) |
Get torrent metadata without downloading by hex string (requires DHT to be running)
| info_hash_hex | Info hash as 40-character hex string |
| callback | Function called when metadata is retrieved (torrent_info, success, error_message) |
| void librats::RatsClient::get_torrent_metadata_from_peer | ( | const InfoHash & | info_hash, |
| const std::string & | peer_ip, | ||
| uint16_t | peer_port, | ||
| std::function< void(const TorrentInfo &, bool, const std::string &)> | callback | ||
| ) |
Get torrent metadata directly from a specific peer (fast path - no DHT search needed) This is more efficient when you already know a peer that has the torrent (e.g., from announce_peer)
| info_hash | Info hash of the torrent |
| peer_ip | IP address of the peer |
| peer_port | Port of the peer |
| callback | Function called when metadata is retrieved (torrent_info, success, error_message) |
| void librats::RatsClient::get_torrent_metadata_from_peer | ( | const std::string & | info_hash_hex, |
| const std::string & | peer_ip, | ||
| uint16_t | peer_port, | ||
| std::function< void(const TorrentInfo &, bool, const std::string &)> | callback | ||
| ) |
Get torrent metadata directly from a specific peer by hex string (fast path)
| info_hash_hex | Info hash as 40-character hex string |
| peer_ip | IP address of the peer |
| peer_port | Port of the peer |
| callback | Function called when metadata is retrieved (torrent_info, success, error_message) |
| std::vector< RatsPeer > librats::RatsClient::get_validated_peers | ( | ) | const |
Get all peers that have completed handshake.
| bool librats::RatsClient::initialize_encryption | ( | bool | enable | ) |
Initialize encryption system.
| enable | Whether to enable encryption |
| bool librats::RatsClient::is_automatic_discovery_running | ( | ) | const |
Check if automatic discovery is running.
| bool librats::RatsClient::is_bittorrent_enabled | ( | ) | const |
Check if BitTorrent is enabled.
| bool librats::RatsClient::is_console_logging_enabled | ( | ) | const |
Check if console logging is currently enabled.
| bool librats::RatsClient::is_dht_running | ( | ) | const |
Check if DHT is currently running.
| bool librats::RatsClient::is_encryption_enabled | ( | ) | const |
Check if encryption is enabled.
| bool librats::RatsClient::is_file_transfer_available | ( | ) | const |
Check whether the file transfer manager is initialized.
| bool librats::RatsClient::is_gossipsub_available | ( | ) | const |
| bool librats::RatsClient::is_gossipsub_running | ( | ) | const |
| bool librats::RatsClient::is_ice_available | ( | ) | const |
Check if ICE is available.
| bool librats::RatsClient::is_ice_connected | ( | ) | const |
Check if ICE is connected.
| bool librats::RatsClient::is_ice_gathering_complete | ( | ) | const |
Check if ICE candidate gathering is complete.
| bool librats::RatsClient::is_log_colors_enabled | ( | ) | const |
Check if colored log output is enabled.
| bool librats::RatsClient::is_log_rotate_on_startup_enabled | ( | ) | const |
Check if log rotation on startup is enabled.
| bool librats::RatsClient::is_log_timestamps_enabled | ( | ) | const |
Check if timestamps are enabled in log output.
| bool librats::RatsClient::is_logging_enabled | ( | ) | const |
Check if file logging is currently enabled.
| bool librats::RatsClient::is_mdns_running | ( | ) | const |
Check if mDNS is currently running.
| bool librats::RatsClient::is_network_change_detection_enabled | ( | ) | const |
| bool librats::RatsClient::is_peer_encrypted | ( | const std::string & | peer_id | ) | const |
Check if a peer connection is encrypted.
| peer_id | Peer ID to check |
| bool librats::RatsClient::is_peer_limit_reached | ( | ) | const |
Check if peer limit has been reached.
| bool librats::RatsClient::is_port_mapping_enabled | ( | ) | const |
Whether automatic port forwarding is currently enabled.
| bool librats::RatsClient::is_reconnect_enabled | ( | ) | const |
Check if automatic reconnection is enabled.
| bool librats::RatsClient::is_running | ( | ) | const |
Check if the client is currently running.
| bool librats::RatsClient::is_spider_ignoring | ( | ) | const |
Check if spider ignore mode is enabled.
| bool librats::RatsClient::is_spider_mode | ( | ) | const |
Check if spider mode is enabled.
| bool librats::RatsClient::is_storage_available | ( | ) | const |
Check if storage is available.
| bool librats::RatsClient::is_storage_synced | ( | ) | const |
Check if storage is synchronized.
| bool librats::RatsClient::is_subscribed_to_topic | ( | const std::string & | topic | ) | const |
Check if subscribed to a GossipSub topic.
| topic | Topic name to check |
| int librats::RatsClient::load_and_reconnect_peers | ( | ) |
Load saved peers and attempt to reconnect.
| bool librats::RatsClient::load_configuration | ( | ) |
Load configuration from files.
| bool librats::RatsClient::load_historical_peers | ( | ) |
Load historical peers from a file.
| void librats::RatsClient::off | ( | const std::string & | message_type | ) |
Remove all handlers for a message type.
| message_type | Type of message to stop handling |
| void librats::RatsClient::off_topic | ( | const std::string & | topic | ) |
Remove all event handlers for a GossipSub topic.
| topic | Topic name |
| void librats::RatsClient::on | ( | const std::string & | message_type, |
| MessageCallback | callback | ||
| ) |
Register a persistent message handler.
| message_type | Type of message to handle |
| callback | Function to call when message is received |
| void librats::RatsClient::on_file_transfer_completed | ( | TransferCompletedCallback | callback | ) |
Set the completion callback (fires once per transfer).
| void librats::RatsClient::on_file_transfer_progress | ( | TransferProgressCallback | callback | ) |
Set the progress callback (fires for both directions).
| void librats::RatsClient::on_file_transfer_request | ( | TransferOfferCallback | callback | ) |
Set the incoming-offer callback.
The handler should call accept_file_transfer()/reject_file_transfer(). Without it, offers are auto-rejected.
| void librats::RatsClient::on_ice_candidates_gathered | ( | IceCandidatesCallback | callback | ) |
Set callback for ICE candidates gathered.
| callback | Function called with all candidates when gathering completes |
| void librats::RatsClient::on_ice_connection_state_changed | ( | IceConnectionStateCallback | callback | ) |
Set callback for ICE connection state changes.
| callback | Function called when connection state changes |
| void librats::RatsClient::on_ice_gathering_state_changed | ( | IceGatheringStateCallback | callback | ) |
Set callback for ICE gathering state changes.
| callback | Function called when gathering state changes |
| void librats::RatsClient::on_ice_new_candidate | ( | IceNewCandidateCallback | callback | ) |
Set callback for new ICE candidate (trickle ICE)
| callback | Function called when each new candidate is discovered |
| void librats::RatsClient::on_ice_selected_pair | ( | IceSelectedPairCallback | callback | ) |
Set callback for ICE selected pair.
| callback | Function called when a candidate pair is selected |
| void librats::RatsClient::on_network_changed | ( | NetworkChangeCallback | callback | ) |
Register a callback fired (debounced) whenever the set of local interface addresses changes.
The argument is the new full list of local addresses. Invoked from the monitor's worker thread, so keep the handler quick. This is in addition to — not a replacement for — the built-in recovery (port re-mapping, STUN re-discovery, DHT re-announce).
| void librats::RatsClient::on_port_mapping | ( | PortMapCallback | callback | ) |
Register a callback fired whenever a port mapping is established, refreshed, removed or fails (invoked from a backend worker thread).
| void librats::RatsClient::on_storage_change | ( | StorageChangeCallback | callback | ) |
Set storage change callback.
| callback | Function to call when storage changes |
| void librats::RatsClient::on_storage_sync_complete | ( | StorageSyncCompleteCallback | callback | ) |
Set storage sync complete callback.
| callback | Function to call when sync completes |
| void librats::RatsClient::on_topic_json_message | ( | const std::string & | topic, |
| std::function< void(const std::string &, const std::string &, const nlohmann::json &)> | callback | ||
| ) |
Set a JSON message handler for a GossipSub topic using unified event API pattern.
| topic | Topic name |
| callback | Function to call when JSON messages are received (peer_id, topic, json_message) |
| void librats::RatsClient::on_topic_message | ( | const std::string & | topic, |
| std::function< void(const std::string &, const std::string &, const std::string &)> | callback | ||
| ) |
Set a message handler for a GossipSub topic using unified event API pattern.
| topic | Topic name |
| callback | Function to call when messages are received (peer_id, topic, message_content) |
| void librats::RatsClient::on_topic_peer_joined | ( | const std::string & | topic, |
| std::function< void(const std::string &, const std::string &)> | callback | ||
| ) |
Set a peer joined handler for a GossipSub topic using unified event API pattern.
| topic | Topic name |
| callback | Function to call when peers join the topic |
| void librats::RatsClient::on_topic_peer_left | ( | const std::string & | topic, |
| std::function< void(const std::string &, const std::string &)> | callback | ||
| ) |
Set a peer left handler for a GossipSub topic using unified event API pattern
| topic | Topic name |
| callback | Function to call when peers leave the topic |
| void librats::RatsClient::once | ( | const std::string & | message_type, |
| MessageCallback | callback | ||
| ) |
Register a one-time message handler.
| message_type | Type of message to handle |
| callback | Function to call when message is received (once only) |
| bool librats::RatsClient::pause_file_transfer | ( | const std::string & | transfer_id | ) |
Pause an active transfer (either direction).
| bool librats::RatsClient::publish_json_to_topic | ( | const std::string & | topic, |
| const nlohmann::json & | message | ||
| ) |
Publish a JSON message to a GossipSub topic.
| topic | Topic to publish to |
| message | JSON message content |
| bool librats::RatsClient::publish_to_topic | ( | const std::string & | topic, |
| const std::string & | message | ||
| ) |
Publish a message to a GossipSub topic.
| topic | Topic to publish to |
| message | Message content |
| bool librats::RatsClient::query_mdns_services | ( | ) |
Manually query for mDNS services.
| bool librats::RatsClient::reject_file_transfer | ( | const std::string & | transfer_id, |
| const std::string & | reason = "" |
||
| ) |
Reject an incoming transfer.
| bool librats::RatsClient::remove_torrent | ( | const InfoHash & | info_hash | ) |
Remove a torrent by info hash.
| info_hash | Info hash of the torrent to remove |
| void librats::RatsClient::restart_ice | ( | ) |
Restart ICE (re-gather candidates and restart checks)
| bool librats::RatsClient::resume_file_transfer | ( | const std::string & | transfer_id | ) |
Resume a paused transfer.
| bool librats::RatsClient::save_configuration | ( | ) |
Save configuration to files.
| bool librats::RatsClient::save_historical_peers | ( | ) |
Save current peers to a historical file.
| void librats::RatsClient::send | ( | const std::string & | message_type, |
| const nlohmann::json & | data, | ||
| SendCallback | callback = nullptr |
||
| ) |
Send a message to all peers.
| message_type | Type of message |
| data | Message data |
| callback | Optional callback for send result |
| void librats::RatsClient::send | ( | const std::string & | peer_id, |
| const std::string & | message_type, | ||
| const nlohmann::json & | data, | ||
| SendCallback | callback = nullptr |
||
| ) |
Send a message to a specific peer.
| peer_id | Target peer ID |
| message_type | Type of message |
| data | Message data |
| callback | Optional callback for send result |
| bool librats::RatsClient::send_binary_to_peer | ( | socket_t | socket, |
| const std::vector< uint8_t > & | data, | ||
| MessageDataType | message_type = MessageDataType::BINARY |
||
| ) |
Send binary data to a specific peer (primary method)
| socket | Target peer socket |
| data | Binary data to send |
| message_type | Type of message data (BINARY, STRING, JSON) |
| bool librats::RatsClient::send_binary_to_peer_id | ( | const std::string & | peer_id, |
| const std::vector< uint8_t > & | data, | ||
| MessageDataType | message_type = MessageDataType::BINARY |
||
| ) |
Send binary data to a peer by peer_id (preferred)
| peer_id | Target peer ID |
| data | Binary data to send |
| message_type | Type of message data (BINARY, STRING, JSON) |
| std::string librats::RatsClient::send_directory | ( | const std::string & | peer_id, |
| const std::string & | directory_path, | ||
| const std::string & | remote_name = "" |
||
| ) |
Send a directory tree (recursively) to a peer.
| peer_id | Target peer ID |
| directory_path | Local directory to send |
| remote_name | Optional name to present to the peer |
| std::string librats::RatsClient::send_file | ( | const std::string & | peer_id, |
| const std::string & | file_path, | ||
| const std::string & | remote_filename = "" |
||
| ) |
Send a file to a peer.
| peer_id | Target peer ID |
| file_path | Local file to send |
| remote_filename | Optional name to present to the peer |
| bool librats::RatsClient::send_json_to_peer | ( | socket_t | socket, |
| const nlohmann::json & | data | ||
| ) |
Send JSON data to a specific peer.
| socket | Target peer socket |
| data | JSON data to send |
| bool librats::RatsClient::send_json_to_peer_id | ( | const std::string & | peer_id, |
| const nlohmann::json & | data | ||
| ) |
Send JSON data to a peer by peer_id (preferred)
| peer_id | Target peer ID |
| data | JSON data to send |
| bool librats::RatsClient::send_string_to_peer | ( | socket_t | socket, |
| const std::string & | data | ||
| ) |
Send string data to a specific peer.
| socket | Target peer socket |
| data | String data to send |
| bool librats::RatsClient::send_string_to_peer_id | ( | const std::string & | peer_id, |
| const std::string & | data | ||
| ) |
Send string data to a peer by peer_id (preferred)
| peer_id | Target peer ID |
| data | String data to send |
| void librats::RatsClient::set_binary_data_callback | ( | BinaryDataCallback | callback | ) |
Set binary data callback (called when binary data is received)
| callback | Function to call when binary data is received |
| void librats::RatsClient::set_connection_callback | ( | ConnectionCallback | callback | ) |
Set connection callback (called when a new peer connects)
| callback | Function to call on new connections |
| void librats::RatsClient::set_console_logging_enabled | ( | bool | enabled | ) |
Enable or disable console logging When disabled, log messages will not be printed to stdout/stderr File logging (if enabled) will still work.
| enabled | Whether to enable console logging (default: true) |
| bool librats::RatsClient::set_data_directory | ( | const std::string & | directory_path | ) |
Set directory where data files will be stored.
| directory_path | Path to directory (default: current folder) |
| void librats::RatsClient::set_disconnect_callback | ( | DisconnectCallback | callback | ) |
Set disconnect callback (called when a peer disconnects)
| callback | Function to call on disconnections |
| void librats::RatsClient::set_encryption_enabled | ( | bool | enabled | ) |
Set encryption enabled/disabled.
| enabled | Whether encryption should be enabled |
| void librats::RatsClient::set_file_transfer_config | ( | const FileTransferConfig & | config | ) |
Replace the file transfer configuration.
| void librats::RatsClient::set_ice_config | ( | const IceConfig & | config | ) |
Set ICE configuration.
| config | ICE configuration settings |
| void librats::RatsClient::set_json_data_callback | ( | JsonDataCallback | callback | ) |
Set JSON data callback (called when JSON data is received)
| callback | Function to call when JSON data is received |
| void librats::RatsClient::set_log_colors_enabled | ( | bool | enabled | ) |
Enable or disable colored log output.
| enabled | Whether to enable colored output |
| void librats::RatsClient::set_log_file_path | ( | const std::string & | file_path | ) |
Set the log file path.
| file_path | Path to the log file (default: "rats.log") |
| void librats::RatsClient::set_log_level | ( | const std::string & | level_str | ) |
Set the minimum log level using string.
| level_str | Log level as string ("DEBUG", "INFO", "WARN", "ERROR") |
| void librats::RatsClient::set_log_level | ( | LogLevel | level | ) |
Set the minimum log level.
| level | Minimum log level (DEBUG=0, INFO=1, WARN=2, ERROR=3) |
| void librats::RatsClient::set_log_retention_count | ( | int | count | ) |
Set the number of log files to retain during rotation.
| count | Number of old log files to keep (default: 5) |
| void librats::RatsClient::set_log_rotate_on_startup | ( | bool | enabled | ) |
Enable or disable log rotation on application startup When enabled, the existing log file will be rotated when logging starts, so each application run gets a fresh log file.
| enabled | Whether to rotate logs on startup (default: false) |
| void librats::RatsClient::set_log_rotation_size | ( | size_t | max_size_bytes | ) |
Set log file rotation size.
| max_size_bytes | Maximum size in bytes before log rotation (default: 10MB) |
| void librats::RatsClient::set_log_timestamps_enabled | ( | bool | enabled | ) |
Enable or disable timestamps in log output.
| enabled | Whether to enable timestamps |
| void librats::RatsClient::set_logging_enabled | ( | bool | enabled | ) |
Enable or disable file logging When enabled, logs will be written to "rats.log" by default.
| enabled | Whether to enable file logging |
| void librats::RatsClient::set_max_peers | ( | int | max_peers | ) |
Set maximum number of peers.
| max_peers | New maximum peer count |
| void librats::RatsClient::set_mdns_callback | ( | std::function< void(const std::string &, int, const std::string &)> | callback | ) |
Set mDNS service discovery callback.
| callback | Function to call when services are discovered |
| void librats::RatsClient::set_network_change_detection_enabled | ( | bool | enabled | ) |
Enable or disable automatic reaction to host network changes.
Enabled by default. Can be called before or after start(): toggling while running starts/stops the monitor immediately. Not persisted.
| bool librats::RatsClient::set_noise_static_keypair | ( | const uint8_t | private_key[32] | ) |
Set a custom static keypair for Noise Protocol If not set, a new keypair is generated automatically.
| private_key | 32-byte private key |
| void librats::RatsClient::set_port_mapping_config | ( | const PortMappingConfig & | config | ) |
Replace the full port mapping configuration (takes effect on next start).
| void librats::RatsClient::set_port_mapping_enabled | ( | bool | enabled | ) |
Enable or disable automatic port forwarding.
If toggled while running, the port mapping backends are started or stopped immediately. The setting is persisted to config.json.
| void librats::RatsClient::set_protocol_name | ( | const std::string & | protocol_name | ) |
Set custom protocol name for handshakes and DHT discovery.
| protocol_name | Custom protocol name (default: "rats") |
| void librats::RatsClient::set_protocol_version | ( | const std::string & | protocol_version | ) |
Set custom protocol version for handshakes.
| protocol_version | Custom protocol version (default: "1.0") |
| void librats::RatsClient::set_reconnect_config | ( | const ReconnectConfig & | config | ) |
Set reconnection configuration.
| config | Reconnection configuration settings |
| void librats::RatsClient::set_reconnect_enabled | ( | bool | enabled | ) |
Enable or disable automatic reconnection to disconnected peers.
| enabled | Whether auto-reconnection should be enabled |
| void librats::RatsClient::set_resume_data_path | ( | const std::string & | path | ) |
Set the directory for storing resume data files Resume data allows torrents to resume from where they left off.
Should be called after enable_bittorrent() and before adding torrents.
| path | Directory path for resume data (e.g., app data folder) |
| void librats::RatsClient::set_spider_announce_callback | ( | SpiderAnnounceCallback | callback | ) |
Set callback for announce_peer requests (spider mode) Called when other peers announce they have a torrent.
| callback | The callback to invoke |
| void librats::RatsClient::set_spider_ignore | ( | bool | ignore | ) |
Set spider ignore mode - when true, incoming requests are not processed Useful for rate limiting in spider mode.
| ignore | true to ignore incoming requests, false to process them |
| void librats::RatsClient::set_spider_mode | ( | bool | enable | ) |
Enable spider mode on DHT In spider mode:
| enable | true to enable spider mode, false to disable |
| void librats::RatsClient::set_storage_config | ( | const StorageConfig & | config | ) |
Set storage configuration.
| config | Storage configuration settings |
| void librats::RatsClient::set_string_data_callback | ( | StringDataCallback | callback | ) |
Set string data callback (called when string data is received)
| callback | Function to call when string data is received |
| void librats::RatsClient::set_topic_message_validator | ( | const std::string & | topic, |
| std::function< ValidationResult(const std::string &, const std::string &, const std::string &)> | validator | ||
| ) |
Set a message validator for a GossipSub topic.
| topic | Topic name (empty for global validator) |
| validator | Validation function returning ACCEPT, REJECT, or IGNORE_MSG |
| void librats::RatsClient::shutdown_all_threads | ( | ) |
Shutdown all background threads.
| void librats::RatsClient::spider_walk | ( | ) |
Trigger a single spider walk iteration Sends find_node to a random node from the spider pool Call this periodically at desired frequency to discover new nodes.
| bool librats::RatsClient::start | ( | ) |
Start the RatsClient and begin listening for connections.
| void librats::RatsClient::start_automatic_peer_discovery | ( | ) |
Start automatic peer discovery.
| bool librats::RatsClient::start_dht_discovery | ( | int | dht_port = 6881 | ) |
Start DHT discovery on specified port.
| dht_port | Port for DHT communication (default: 6881) |
| void librats::RatsClient::start_ice_checks | ( | ) |
Start ICE connectivity checks.
| bool librats::RatsClient::start_mdns_discovery | ( | const std::string & | service_instance_name = "", |
| const std::map< std::string, std::string > & | txt_records = {} |
||
| ) |
Start mDNS service discovery and announcement.
| service_instance_name | Service instance name (optional) |
| txt_records | Additional TXT records for service announcement |
| void librats::RatsClient::stop | ( | ) |
Stop the RatsClient and close all connections.
| void librats::RatsClient::stop_automatic_peer_discovery | ( | ) |
Stop automatic peer discovery.
| void librats::RatsClient::stop_dht_discovery | ( | ) |
Stop DHT discovery.
| void librats::RatsClient::stop_mdns_discovery | ( | ) |
Stop mDNS discovery.
| bool librats::RatsClient::storage_delete | ( | const std::string & | key | ) |
Delete a key from storage.
| key | Key to delete |
| std::optional< std::vector< uint8_t > > librats::RatsClient::storage_get_binary | ( | const std::string & | key | ) | const |
Get binary data.
| key | Key to retrieve |
| std::optional< double > librats::RatsClient::storage_get_double | ( | const std::string & | key | ) | const |
Get a double-precision floating point value.
| key | Key to retrieve |
| std::optional< int64_t > librats::RatsClient::storage_get_int | ( | const std::string & | key | ) | const |
Get a 64-bit integer value.
| key | Key to retrieve |
| std::optional< nlohmann::json > librats::RatsClient::storage_get_json | ( | const std::string & | key | ) | const |
Get a JSON document.
| key | Key to retrieve |
| std::optional< std::string > librats::RatsClient::storage_get_string | ( | const std::string & | key | ) | const |
Get a string value.
| key | Key to retrieve |
| bool librats::RatsClient::storage_has | ( | const std::string & | key | ) | const |
Check if a key exists in storage.
| key | Key to check |
| std::vector< std::string > librats::RatsClient::storage_keys | ( | ) | const |
Get all keys in storage.
| std::vector< std::string > librats::RatsClient::storage_keys_with_prefix | ( | const std::string & | prefix | ) | const |
Get keys matching a prefix.
| prefix | Prefix to match |
| bool librats::RatsClient::storage_put | ( | const std::string & | key, |
| const std::string & | value | ||
| ) |
Store a string value.
| key | Key to store under |
| value | String value to store |
| bool librats::RatsClient::storage_put | ( | const std::string & | key, |
| const std::vector< uint8_t > & | value | ||
| ) |
Store binary data.
| key | Key to store under |
| value | Binary data to store |
| bool librats::RatsClient::storage_put | ( | const std::string & | key, |
| double | value | ||
| ) |
Store a double-precision floating point value.
| key | Key to store under |
| value | Double value to store |
| bool librats::RatsClient::storage_put | ( | const std::string & | key, |
| int64_t | value | ||
| ) |
Store a 64-bit integer value.
| key | Key to store under |
| value | Integer value to store |
| bool librats::RatsClient::storage_put_json | ( | const std::string & | key, |
| const nlohmann::json & | value | ||
| ) |
Store a JSON document.
| key | Key to store under |
| value | JSON value to store |
| bool librats::RatsClient::storage_request_sync | ( | ) |
Request storage sync from connected peers.
| size_t librats::RatsClient::storage_size | ( | ) | const |
Get the number of entries in storage.
| bool librats::RatsClient::subscribe_to_topic | ( | const std::string & | topic | ) |
Subscribe to a GossipSub topic.
| topic | Topic name to subscribe to |
| bool librats::RatsClient::unsubscribe_from_topic | ( | const std::string & | topic | ) |
Unsubscribe from a GossipSub topic.
| topic | Topic name to unsubscribe from |