librats 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/native-src/CMakeLists.txt +15 -2
  2. package/native-src/src/bt_client.cpp +18 -5
  3. package/native-src/src/bt_client.h +3 -1
  4. package/native-src/src/bt_network.cpp +297 -184
  5. package/native-src/src/bt_network.h +25 -5
  6. package/native-src/src/bt_peer_connection.cpp +11 -0
  7. package/native-src/src/bt_peer_connection.h +6 -0
  8. package/native-src/src/bt_torrent.cpp +17 -6
  9. package/native-src/src/bt_torrent.h +1 -0
  10. package/native-src/src/bt_types.h +287 -20
  11. package/native-src/src/dht.cpp +26 -8
  12. package/native-src/src/dht.h +1 -1
  13. package/native-src/src/ice.cpp +1 -1
  14. package/native-src/src/io_poller.cpp +910 -0
  15. package/native-src/src/io_poller.h +138 -0
  16. package/native-src/src/krpc.cpp +8 -1
  17. package/native-src/src/krpc.h +3 -2
  18. package/native-src/src/librats.cpp +824 -1213
  19. package/native-src/src/librats.h +167 -54
  20. package/native-src/src/librats_bittorrent.cpp +4 -7
  21. package/native-src/src/librats_c.cpp +13 -1
  22. package/native-src/src/librats_c.h +2 -0
  23. package/native-src/src/librats_discovery.cpp +309 -0
  24. package/native-src/src/librats_encryption.cpp +130 -283
  25. package/native-src/src/librats_file_transfer.cpp +2 -6
  26. package/native-src/src/librats_gossipsub.cpp +1 -5
  27. package/native-src/src/librats_ice.cpp +1 -1
  28. package/native-src/src/librats_log_macros.h +36 -0
  29. package/native-src/src/librats_logging.cpp +10 -7
  30. package/native-src/src/librats_mdns.cpp +1 -11
  31. package/native-src/src/librats_persistence.cpp +1 -11
  32. package/native-src/src/librats_reconnection.cpp +2 -13
  33. package/native-src/src/librats_statistic.cpp +97 -0
  34. package/native-src/src/logger.h +40 -4
  35. package/native-src/src/network_utils.cpp +175 -373
  36. package/native-src/src/network_utils.h +2 -113
  37. package/native-src/src/socket.cpp +485 -883
  38. package/native-src/src/socket.h +36 -130
  39. package/native-src/src/stun.cpp +3 -5
  40. package/native-src/src/tracker.cpp +3 -2
  41. package/native-src/src/turn.cpp +4 -6
  42. package/package.json +1 -1
@@ -129,6 +129,7 @@ set(LIBRARY_SOURCES
129
129
  src/krpc.cpp
130
130
  src/krpc.h
131
131
  src/librats.cpp
132
+ src/librats_discovery.cpp
132
133
  src/librats_reconnection.cpp
133
134
  src/librats_logging.cpp
134
135
  src/librats_encryption.cpp
@@ -136,6 +137,7 @@ set(LIBRARY_SOURCES
136
137
  src/librats_gossipsub.cpp
137
138
  src/librats_mdns.cpp
138
139
  src/librats_persistence.cpp
140
+ src/librats_statistic.cpp
139
141
  src/librats.h
140
142
  src/sha1.cpp
141
143
  src/sha1.h
@@ -157,13 +159,17 @@ set(LIBRARY_SOURCES
157
159
  src/rats_export.h
158
160
  ${PROJECT_BINARY_DIR}/src/version.h
159
161
 
160
- # Buffer utilities for bittorrent and file transfer
162
+ # Buffer utilities for async I/O, file transfer, and bittorrent
161
163
  src/receive_buffer.h
162
164
  src/receive_buffer.cpp
163
165
  src/chained_send_buffer.h
164
166
  src/chained_send_buffer.cpp
165
167
 
166
- # Cypto algorithms including Noise Protocol needed
168
+ # Platform-optimal I/O multiplexing (epoll/kqueue/IOCP)
169
+ src/io_poller.h
170
+ src/io_poller.cpp
171
+
172
+ # Crypto algorithms including Noise Protocol
167
173
  src/crypto/curve25519.c
168
174
  src/crypto/curve25519.h
169
175
  src/crypto/chacha.c
@@ -430,6 +436,8 @@ if(RATS_BUILD_TESTS)
430
436
  tests/test_stun.cpp
431
437
  tests/test_turn.cpp
432
438
  tests/test_ice.cpp
439
+ # I/O poller abstraction (epoll/kqueue/IOCP)
440
+ tests/test_io_poller.cpp
433
441
  # Buffer utilities for file transfer and bittorrent
434
442
  tests/test_receive_buffer.cpp
435
443
  tests/test_chained_send_buffer.cpp
@@ -501,3 +509,8 @@ if(RATS_BUILD_TESTS)
501
509
  RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin
502
510
  )
503
511
  endif()
512
+
513
+ # Examples (basic examples always built; BT examples require RATS_SEARCH_FEATURES)
514
+ if(RATS_BUILD_EXAMPLES)
515
+ add_subdirectory(examples)
516
+ endif()
@@ -326,7 +326,8 @@ Torrent::Ptr BtClient::add_torrent(const TorrentInfo& info,
326
326
  }
327
327
 
328
328
  Torrent::Ptr BtClient::add_magnet(const std::string& magnet_uri,
329
- const std::string& save_path) {
329
+ const std::string& save_path,
330
+ bool skip_dht_search) {
330
331
  auto info = TorrentInfo::from_magnet(magnet_uri);
331
332
  if (!info) {
332
333
  if (on_alert_) {
@@ -345,7 +346,14 @@ Torrent::Ptr BtClient::add_magnet(const std::string& magnet_uri,
345
346
 
346
347
  // Create torrent without full metadata
347
348
  TorrentConfig torrent_config;
348
- torrent_config.save_path = save_path.empty() ? config_.download_path : save_path;
349
+ // For metadata-only mode (skip_dht_search=true with empty save_path), keep save_path empty
350
+ // Otherwise use default download path if not specified
351
+ if (skip_dht_search && save_path.empty()) {
352
+ // Metadata-only mode - keep empty save_path for proper detection
353
+ torrent_config.save_path = "";
354
+ } else {
355
+ torrent_config.save_path = save_path.empty() ? config_.download_path : save_path;
356
+ }
349
357
  torrent_config.resume_data_path = config_.resume_data_path;
350
358
  torrent_config.max_connections = config_.max_connections_per_torrent;
351
359
  torrent_config.max_uploads = config_.max_uploads;
@@ -362,8 +370,8 @@ Torrent::Ptr BtClient::add_magnet(const std::string& magnet_uri,
362
370
  if (running_) {
363
371
  torrent->start();
364
372
 
365
- // Find peers via DHT for metadata
366
- if (dht_running_) {
373
+ // Find peers via DHT for metadata (skip if using direct peer connection)
374
+ if (dht_running_ && !skip_dht_search) {
367
375
  find_peers_dht(info->info_hash());
368
376
  }
369
377
  }
@@ -891,6 +899,9 @@ void BtClient::announce_torrents_to_trackers() {
891
899
  for (auto& [hash, torrent] : torrents_) {
892
900
  if (!torrent->is_active()) continue;
893
901
 
902
+ // Skip metadata-only torrents (empty save_path means only fetching metadata)
903
+ if (torrent->save_path().empty()) continue;
904
+
894
905
  auto tracker_it = tracker_managers_.find(hash);
895
906
  if (tracker_it == tracker_managers_.end()) continue;
896
907
 
@@ -993,12 +1004,14 @@ void BtClient::tick_loop() {
993
1004
  }
994
1005
 
995
1006
  // Periodic DHT announces (every 15 minutes)
1007
+ // Skip metadata-only torrents (empty save_path) to avoid unnecessary network traffic
996
1008
  auto dht_elapsed = std::chrono::duration_cast<std::chrono::minutes>(
997
1009
  now - last_dht_announce_).count();
998
1010
  if (dht_elapsed >= 15 && dht_running_) {
999
1011
  std::lock_guard<std::mutex> lock(mutex_);
1000
1012
  for (auto& [hash, torrent] : torrents_) {
1001
- if (torrent->is_active()) {
1013
+ // Skip metadata-only torrents (empty save_path means only fetching metadata)
1014
+ if (torrent->is_active() && !torrent->save_path().empty()) {
1002
1015
  announce_to_dht(hash);
1003
1016
  }
1004
1017
  }
@@ -170,10 +170,12 @@ public:
170
170
  *
171
171
  * @param magnet_uri Magnet URI
172
172
  * @param save_path Directory to save files
173
+ * @param skip_dht_search If true, don't search DHT for peers (for metadata-only from specific peer)
173
174
  * @return Torrent pointer, or nullptr on failure
174
175
  */
175
176
  Torrent::Ptr add_magnet(const std::string& magnet_uri,
176
- const std::string& save_path = "");
177
+ const std::string& save_path = "",
178
+ bool skip_dht_search = false);
177
179
 
178
180
  /**
179
181
  * @brief Add a torrent with resume data for fast resume