librats 0.7.2 → 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.
@@ -5,12 +5,14 @@
5
5
  * @brief Async network layer for BitTorrent peer connections
6
6
  *
7
7
  * Provides efficient multiplexed I/O for managing multiple peer connections
8
- * with non-blocking sockets. Buffers are owned by BtPeerConnection (single
9
- * source of truth) - this class only handles socket I/O.
8
+ * using platform-optimal I/O polling (epoll/kqueue/WSAPoll).
9
+ * Buffers are owned by BtPeerConnection (single source of truth) -
10
+ * this class only handles socket I/O.
10
11
  */
11
12
 
12
13
  #include "bt_types.h"
13
14
  #include "bt_peer_connection.h"
15
+ #include "io_poller.h"
14
16
  #include "socket.h"
15
17
 
16
18
  #include <vector>
@@ -37,7 +39,7 @@ struct BtNetworkConfig {
37
39
  size_t max_connections; ///< Maximum total connections
38
40
  bool enable_incoming; ///< Accept incoming connections
39
41
  int connect_timeout_ms; ///< Timeout for outgoing connections (ms)
40
- int select_timeout_ms; ///< Timeout for select() call (ms)
42
+ int poll_timeout_ms; ///< Timeout for I/O poll (ms)
41
43
  size_t send_buffer_high_water; ///< High water mark for send buffer
42
44
  PeerID peer_id; ///< Our peer ID (for incoming connections)
43
45
 
@@ -46,7 +48,7 @@ struct BtNetworkConfig {
46
48
  , max_connections(200)
47
49
  , enable_incoming(true)
48
50
  , connect_timeout_ms(30000)
49
- , select_timeout_ms(15)
51
+ , poll_timeout_ms(15)
50
52
  , send_buffer_high_water(1024 * 1024) // 1MB
51
53
  , peer_id{} {}
52
54
  };
@@ -150,10 +152,16 @@ struct DisconnectedEvent {
150
152
  /**
151
153
  * @brief Manages all BitTorrent peer connections
152
154
  *
153
- * Provides async I/O via select() multiplexing:
155
+ * Provides async I/O via platform-optimal multiplexing:
156
+ * - Linux: epoll (O(1) per event)
157
+ * - macOS: kqueue (O(1) per event)
158
+ * - Windows: WSAPoll (no FD_SETSIZE limit)
159
+ *
160
+ * Features:
154
161
  * - Listens for incoming connections
155
162
  * - Manages outgoing connection queue
156
163
  * - Handles non-blocking reads/writes
164
+ * - TCP_NODELAY for low-latency messaging
157
165
  * - Invokes callbacks for events
158
166
  *
159
167
  * Thread-safety: The manager runs its own I/O thread.
@@ -328,6 +336,9 @@ private:
328
336
  /// Main I/O loop (runs in separate thread)
329
337
  void io_loop();
330
338
 
339
+ /// Synchronize poller state with current connections (under mutex)
340
+ void sync_poller();
341
+
331
342
  /// Process the queue of pending connections
332
343
  void process_pending_connects();
333
344
 
@@ -360,6 +371,9 @@ private:
360
371
  /// Handle info_hash discovered from incoming connection handshake
361
372
  void on_incoming_info_hash(BtPeerConnection* conn, const BtInfoHash& info_hash);
362
373
 
374
+ /// Set TCP_NODELAY on a socket (disable Nagle's algorithm)
375
+ static bool set_tcp_nodelay(socket_t sock);
376
+
363
377
  //=========================================================================
364
378
  // Data Members
365
379
  //=========================================================================
@@ -373,6 +387,12 @@ private:
373
387
 
374
388
  mutable std::mutex mutex_;
375
389
 
390
+ /// Platform-optimal I/O poller (epoll/kqueue/WSAPoll)
391
+ std::unique_ptr<IOPoller> poller_;
392
+
393
+ /// Tracks current poller event mask per socket (to avoid redundant modify calls)
394
+ std::unordered_map<socket_t, uint32_t> poller_state_;
395
+
376
396
  /// Active connections by socket
377
397
  std::unordered_map<socket_t, SocketContext> connections_;
378
398