librats 1.0.2 → 2.2.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.
- package/README.md +145 -331
- package/binding.gyp +16 -3
- package/lib/index.d.ts +288 -696
- package/lib/index.js +407 -44
- package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
- package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
- package/native-src/CMakeLists.txt +404 -179
- package/native-src/LICENSE +1 -1
- package/native-src/src/librats/bindings/rats.cpp +762 -0
- package/native-src/src/librats/bindings/rats.h +380 -0
- package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
- package/native-src/src/librats/bittorrent/bencode.h +176 -0
- package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
- package/native-src/src/librats/bittorrent/bitfield.h +76 -0
- package/native-src/src/librats/bittorrent/byte_io.h +58 -0
- package/native-src/src/librats/bittorrent/choker.cpp +25 -0
- package/native-src/src/librats/bittorrent/choker.h +46 -0
- package/native-src/src/librats/bittorrent/client.cpp +413 -0
- package/native-src/src/librats/bittorrent/client.h +227 -0
- package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
- package/native-src/src/librats/bittorrent/disk_io.h +150 -0
- package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
- package/native-src/src/librats/bittorrent/extensions.h +94 -0
- package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
- package/native-src/src/librats/bittorrent/file_storage.h +79 -0
- package/native-src/src/librats/bittorrent/log.h +42 -0
- package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
- package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
- package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
- package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
- package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
- package/native-src/src/librats/bittorrent/peer_list.h +75 -0
- package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
- package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
- package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
- package/native-src/src/librats/bittorrent/reactor.h +89 -0
- package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
- package/native-src/src/librats/bittorrent/resume_data.h +41 -0
- package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
- package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
- package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
- package/native-src/src/librats/bittorrent/torrent.h +260 -0
- package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
- package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
- package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
- package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
- package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
- package/native-src/src/librats/bittorrent/tracker.h +108 -0
- package/native-src/src/librats/bittorrent/types.cpp +206 -0
- package/native-src/src/librats/bittorrent/types.h +86 -0
- package/native-src/src/librats/core/address.cpp +35 -0
- package/native-src/src/librats/core/address.h +78 -0
- package/native-src/src/librats/core/bytes.h +69 -0
- package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
- package/native-src/src/librats/core/chained_send_buffer.h +183 -0
- package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
- package/native-src/src/librats/core/endpoint_parse.h +31 -0
- package/native-src/src/librats/core/event_bus.h +70 -0
- package/native-src/src/librats/core/host_endpoint.h +56 -0
- package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
- package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
- package/native-src/src/librats/core/ip_address.cpp +120 -0
- package/native-src/src/librats/core/ip_address.h +109 -0
- package/native-src/src/librats/core/mpsc_queue.h +47 -0
- package/native-src/src/librats/core/notifier.h +74 -0
- package/native-src/src/librats/core/receive_buffer.cpp +219 -0
- package/native-src/src/librats/core/receive_buffer.h +171 -0
- package/native-src/src/librats/core/service_registry.h +58 -0
- package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
- package/native-src/src/librats/core/socket.h +496 -0
- package/native-src/src/librats/core/timer_queue.h +105 -0
- package/native-src/src/librats/core/types.cpp +43 -0
- package/native-src/src/librats/core/types.h +103 -0
- package/native-src/src/librats/core/wakeup_pipe.h +83 -0
- package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
- package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
- package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
- package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
- package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
- package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
- package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
- package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
- package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
- package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
- package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
- package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
- package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
- package/native-src/src/librats/crypto/hkdf.c +266 -0
- package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
- package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
- package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
- package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
- package/native-src/src/librats/crypto/poly1305.h +37 -0
- package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
- package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
- package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
- package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
- package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
- package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
- package/native-src/src/librats/dht/announce.cpp +37 -0
- package/native-src/src/librats/dht/announce.h +41 -0
- package/native-src/src/librats/dht/bep42.cpp +109 -0
- package/native-src/src/librats/dht/bep42.h +48 -0
- package/native-src/src/librats/dht/dht.cpp +501 -0
- package/native-src/src/librats/dht/dht.h +119 -0
- package/native-src/src/librats/dht/dht_runner.cpp +103 -0
- package/native-src/src/librats/dht/dht_runner.h +71 -0
- package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
- package/native-src/src/librats/dht/dos_blocker.h +47 -0
- package/native-src/src/librats/dht/find_peers.cpp +52 -0
- package/native-src/src/librats/dht/find_peers.h +73 -0
- package/native-src/src/librats/dht/id.h +167 -0
- package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
- package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
- package/native-src/src/librats/dht/log.h +38 -0
- package/native-src/src/librats/dht/node.cpp +473 -0
- package/native-src/src/librats/dht/node.h +164 -0
- package/native-src/src/librats/dht/node_entry.h +81 -0
- package/native-src/src/librats/dht/observer.h +72 -0
- package/native-src/src/librats/dht/persistence.cpp +90 -0
- package/native-src/src/librats/dht/persistence.h +32 -0
- package/native-src/src/librats/dht/routing_table.cpp +559 -0
- package/native-src/src/librats/dht/routing_table.h +185 -0
- package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
- package/native-src/src/librats/dht/rpc_manager.h +77 -0
- package/native-src/src/librats/dht/storage.cpp +92 -0
- package/native-src/src/librats/dht/storage.h +74 -0
- package/native-src/src/librats/dht/transport.h +27 -0
- package/native-src/src/librats/dht/traversal.cpp +326 -0
- package/native-src/src/librats/dht/traversal.h +120 -0
- package/native-src/src/librats/dht/udp_transport.cpp +49 -0
- package/native-src/src/librats/dht/udp_transport.h +51 -0
- package/native-src/src/librats/mdns/log.h +22 -0
- package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
- package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
- package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
- package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
- package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
- package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
- package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
- package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
- package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
- package/native-src/src/librats/node/circuit_service.h +84 -0
- package/native-src/src/librats/node/config.h +110 -0
- package/native-src/src/librats/node/dial_service.h +54 -0
- package/native-src/src/librats/node/dialer.cpp +264 -0
- package/native-src/src/librats/node/dialer.h +188 -0
- package/native-src/src/librats/node/host_events.h +26 -0
- package/native-src/src/librats/node/identify.cpp +130 -0
- package/native-src/src/librats/node/identify.h +71 -0
- package/native-src/src/librats/node/nat_status.cpp +103 -0
- package/native-src/src/librats/node/nat_status.h +118 -0
- package/native-src/src/librats/node/node.cpp +865 -0
- package/native-src/src/librats/node/node.h +344 -0
- package/native-src/src/librats/node/node_context.h +33 -0
- package/native-src/src/librats/node/peer_network.h +91 -0
- package/native-src/src/librats/peer/peer.h +49 -0
- package/native-src/src/librats/peer/peer_book.cpp +181 -0
- package/native-src/src/librats/peer/peer_book.h +88 -0
- package/native-src/src/librats/peer/peer_id.cpp +72 -0
- package/native-src/src/librats/peer/peer_id.h +62 -0
- package/native-src/src/librats/peer/peer_info.h +37 -0
- package/native-src/src/librats/peer/peer_table.cpp +170 -0
- package/native-src/src/librats/peer/peer_table.h +148 -0
- package/native-src/src/librats/security/handshaker.h +66 -0
- package/native-src/src/librats/security/identity.h +43 -0
- package/native-src/src/librats/security/noise_security.cpp +122 -0
- package/native-src/src/librats/security/noise_security.h +37 -0
- package/native-src/src/librats/security/plaintext_security.h +106 -0
- package/native-src/src/librats/security/session.h +37 -0
- package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
- package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
- package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
- package/native-src/src/librats/subsystems/bittorrent.h +136 -0
- package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
- package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
- package/native-src/src/librats/subsystems/dht_service.h +36 -0
- package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
- package/native-src/src/librats/subsystems/file_transfer.h +367 -0
- package/native-src/src/librats/subsystems/hole_punch.cpp +605 -0
- package/native-src/src/librats/subsystems/hole_punch.h +290 -0
- package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
- package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
- package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
- package/native-src/src/librats/subsystems/message_json.cpp +112 -0
- package/native-src/src/librats/subsystems/message_json.h +88 -0
- package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
- package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
- package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
- package/native-src/src/librats/subsystems/ping_service.h +66 -0
- package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
- package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
- package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
- package/native-src/src/librats/subsystems/pubsub.h +175 -0
- package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
- package/native-src/src/librats/subsystems/reconnection.h +126 -0
- package/native-src/src/librats/subsystems/relay.cpp +1142 -0
- package/native-src/src/librats/subsystems/relay.h +211 -0
- package/native-src/src/librats/subsystems/relay_service.h +46 -0
- package/native-src/src/librats/transport/connection.cpp +343 -0
- package/native-src/src/librats/transport/connection.h +283 -0
- package/native-src/src/librats/transport/link.h +96 -0
- package/native-src/src/librats/transport/reactor.cpp +588 -0
- package/native-src/src/librats/transport/reactor.h +262 -0
- package/native-src/src/librats/transport/reactor_pool.h +81 -0
- package/native-src/src/librats/transport/relay_link.cpp +208 -0
- package/native-src/src/librats/transport/relay_link.h +303 -0
- package/native-src/src/librats/transport/tcp_link.cpp +49 -0
- package/native-src/src/librats/transport/tcp_link.h +43 -0
- package/native-src/src/librats/transport/udp_mux.cpp +617 -0
- package/native-src/src/librats/transport/udp_mux.h +363 -0
- package/native-src/src/librats/transport/udp_packet.cpp +121 -0
- package/native-src/src/librats/transport/udp_packet.h +190 -0
- package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
- package/native-src/src/librats/transport/udp_stream.h +614 -0
- package/native-src/src/librats/util/features.h.in +51 -0
- package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
- package/native-src/src/librats/util/fs.h +136 -0
- package/native-src/src/librats/util/json.cpp +1002 -0
- package/native-src/src/librats/util/json.h +444 -0
- package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
- package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
- package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
- package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
- package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
- package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
- package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
- package/native-src/src/librats/util/rats_export.h +69 -0
- package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
- package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
- package/native-src/src/librats/wire/frame.cpp +76 -0
- package/native-src/src/librats/wire/frame.h +111 -0
- package/native-src/src/librats/wire/message_router.cpp +45 -0
- package/native-src/src/librats/wire/message_router.h +47 -0
- package/package.json +5 -4
- package/scripts/build-librats.js +1 -0
- package/scripts/postinstall.js +3 -3
- package/scripts/prepare-package.js +4 -4
- package/scripts/verify-installation.js +63 -105
- package/src/librats_node.cpp +1067 -1323
- package/native-src/src/bencode.cpp +0 -485
- package/native-src/src/bencode.h +0 -145
- package/native-src/src/bittorrent.cpp +0 -14
- package/native-src/src/bittorrent.h +0 -74
- package/native-src/src/bt_bitfield.cpp +0 -372
- package/native-src/src/bt_bitfield.h +0 -316
- package/native-src/src/bt_choker.cpp +0 -228
- package/native-src/src/bt_choker.h +0 -147
- package/native-src/src/bt_client.cpp +0 -1047
- package/native-src/src/bt_client.h +0 -445
- package/native-src/src/bt_create_torrent.cpp +0 -677
- package/native-src/src/bt_create_torrent.h +0 -473
- package/native-src/src/bt_extension.cpp +0 -469
- package/native-src/src/bt_extension.h +0 -309
- package/native-src/src/bt_file_storage.cpp +0 -261
- package/native-src/src/bt_file_storage.h +0 -298
- package/native-src/src/bt_handshake.cpp +0 -134
- package/native-src/src/bt_handshake.h +0 -157
- package/native-src/src/bt_messages.cpp +0 -364
- package/native-src/src/bt_messages.h +0 -324
- package/native-src/src/bt_network.cpp +0 -1007
- package/native-src/src/bt_network.h +0 -417
- package/native-src/src/bt_peer_connection.cpp +0 -742
- package/native-src/src/bt_peer_connection.h +0 -592
- package/native-src/src/bt_piece_picker.cpp +0 -786
- package/native-src/src/bt_piece_picker.h +0 -473
- package/native-src/src/bt_resume_data.cpp +0 -410
- package/native-src/src/bt_resume_data.h +0 -249
- package/native-src/src/bt_torrent.cpp +0 -2120
- package/native-src/src/bt_torrent.h +0 -641
- package/native-src/src/bt_torrent_info.cpp +0 -659
- package/native-src/src/bt_torrent_info.h +0 -418
- package/native-src/src/bt_types.h +0 -621
- package/native-src/src/chained_send_buffer.cpp +0 -75
- package/native-src/src/chained_send_buffer.h +0 -137
- package/native-src/src/crypto/hkdf.c +0 -266
- package/native-src/src/crypto/poly1305.h +0 -36
- package/native-src/src/dht.cpp +0 -3311
- package/native-src/src/dht.h +0 -717
- package/native-src/src/disk_io.cpp +0 -632
- package/native-src/src/disk_io.h +0 -315
- package/native-src/src/file_transfer.cpp +0 -1415
- package/native-src/src/file_transfer.h +0 -286
- package/native-src/src/fs.h +0 -108
- package/native-src/src/gossipsub.cpp +0 -1139
- package/native-src/src/gossipsub.h +0 -403
- package/native-src/src/ice.cpp +0 -893
- package/native-src/src/ice.h +0 -559
- package/native-src/src/json.hpp +0 -25526
- package/native-src/src/librats.cpp +0 -2378
- package/native-src/src/librats.h +0 -2324
- package/native-src/src/librats_bittorrent.cpp +0 -601
- package/native-src/src/librats_c.cpp +0 -1557
- package/native-src/src/librats_c.h +0 -323
- package/native-src/src/librats_discovery.cpp +0 -402
- package/native-src/src/librats_encryption.cpp +0 -275
- package/native-src/src/librats_file_transfer.cpp +0 -144
- package/native-src/src/librats_gossipsub.cpp +0 -289
- package/native-src/src/librats_ice.cpp +0 -213
- package/native-src/src/librats_log_macros.h +0 -36
- package/native-src/src/librats_logging.cpp +0 -173
- package/native-src/src/librats_mdns.cpp +0 -166
- package/native-src/src/librats_persistence.cpp +0 -796
- package/native-src/src/librats_portmap.cpp +0 -419
- package/native-src/src/librats_reconnection.cpp +0 -218
- package/native-src/src/librats_statistic.cpp +0 -105
- package/native-src/src/librats_storage.cpp +0 -189
- package/native-src/src/rats_export.h +0 -17
- package/native-src/src/receive_buffer.cpp +0 -82
- package/native-src/src/receive_buffer.h +0 -127
- package/native-src/src/socket.h +0 -228
- package/native-src/src/threadmanager.cpp +0 -105
- package/native-src/src/threadmanager.h +0 -53
- package/native-src/src/tracker.cpp +0 -1264
- package/native-src/src/tracker.h +0 -319
- package/native-src/src/turn.cpp +0 -762
- package/native-src/src/turn.h +0 -460
- package/native-src/src/wakeup_pipe.h +0 -60
- /package/native-src/src/{os.h → librats/util/os.h} +0 -0
package/native-src/src/disk_io.h
DELETED
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <string>
|
|
4
|
-
#include <vector>
|
|
5
|
-
#include <queue>
|
|
6
|
-
#include <deque>
|
|
7
|
-
#include <functional>
|
|
8
|
-
#include <mutex>
|
|
9
|
-
#include <thread>
|
|
10
|
-
#include <atomic>
|
|
11
|
-
#include <condition_variable>
|
|
12
|
-
#include <memory>
|
|
13
|
-
|
|
14
|
-
namespace librats {
|
|
15
|
-
|
|
16
|
-
// Forward declaration
|
|
17
|
-
class DiskIOThreadPool;
|
|
18
|
-
|
|
19
|
-
//=============================================================================
|
|
20
|
-
// Disk Job Types
|
|
21
|
-
//=============================================================================
|
|
22
|
-
|
|
23
|
-
enum class DiskJobType {
|
|
24
|
-
WRITE_BLOCK, // Write a block to disk
|
|
25
|
-
READ_PIECE, // Read an entire piece from disk
|
|
26
|
-
HASH_PIECE, // Read piece from disk and compute hash
|
|
27
|
-
FLUSH // Ensure all pending writes are complete
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// Job priority (lower = higher priority)
|
|
31
|
-
enum class DiskJobPriority {
|
|
32
|
-
HIGH = 0, // Critical operations (e.g., flush, urgent reads)
|
|
33
|
-
NORMAL = 1, // Standard operations
|
|
34
|
-
LOW = 2 // Background operations (e.g., hash verification)
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// Completion callback types
|
|
38
|
-
using WriteCompleteCallback = std::function<void(bool success)>;
|
|
39
|
-
using ReadCompleteCallback = std::function<void(bool success, const std::vector<uint8_t>& data)>;
|
|
40
|
-
using HashCompleteCallback = std::function<void(bool success, const std::string& hash)>;
|
|
41
|
-
using FlushCompleteCallback = std::function<void(bool success)>;
|
|
42
|
-
|
|
43
|
-
//=============================================================================
|
|
44
|
-
// File Mapping Structure (for multi-file torrents)
|
|
45
|
-
//=============================================================================
|
|
46
|
-
|
|
47
|
-
struct FileMappingInfo {
|
|
48
|
-
std::string path; // File path relative to download directory
|
|
49
|
-
uint64_t length; // Total file length
|
|
50
|
-
uint64_t torrent_offset; // Offset within the torrent's data stream
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
//=============================================================================
|
|
54
|
-
// Disk Job Structure
|
|
55
|
-
//=============================================================================
|
|
56
|
-
|
|
57
|
-
struct DiskJob {
|
|
58
|
-
DiskJobType type;
|
|
59
|
-
DiskJobPriority priority;
|
|
60
|
-
|
|
61
|
-
// Job identification
|
|
62
|
-
uint32_t piece_index;
|
|
63
|
-
uint32_t block_index;
|
|
64
|
-
uint32_t offset; // Offset within piece
|
|
65
|
-
|
|
66
|
-
// File mapping info
|
|
67
|
-
std::string download_path;
|
|
68
|
-
std::vector<FileMappingInfo> file_mappings; // Full file mapping info including lengths
|
|
69
|
-
|
|
70
|
-
// Data for write operations
|
|
71
|
-
std::vector<uint8_t> data;
|
|
72
|
-
|
|
73
|
-
// Piece info for read/hash operations
|
|
74
|
-
uint32_t piece_length;
|
|
75
|
-
uint64_t piece_offset_in_torrent; // Absolute offset in torrent (uint64 to support large torrents >4GB)
|
|
76
|
-
|
|
77
|
-
// Callbacks (only one is set based on job type)
|
|
78
|
-
WriteCompleteCallback write_callback;
|
|
79
|
-
ReadCompleteCallback read_callback;
|
|
80
|
-
HashCompleteCallback hash_callback;
|
|
81
|
-
FlushCompleteCallback flush_callback;
|
|
82
|
-
|
|
83
|
-
DiskJob() : type(DiskJobType::WRITE_BLOCK), priority(DiskJobPriority::NORMAL),
|
|
84
|
-
piece_index(0), block_index(0),
|
|
85
|
-
offset(0), piece_length(0), piece_offset_in_torrent(0) {}
|
|
86
|
-
|
|
87
|
-
// For priority queue ordering
|
|
88
|
-
bool operator<(const DiskJob& other) const {
|
|
89
|
-
return static_cast<int>(priority) > static_cast<int>(other.priority);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
//=============================================================================
|
|
94
|
-
// DiskIOThreadPool Configuration
|
|
95
|
-
//=============================================================================
|
|
96
|
-
|
|
97
|
-
struct DiskIOConfig {
|
|
98
|
-
int num_write_threads = 1; // Number of threads for write operations
|
|
99
|
-
int num_read_threads = 2; // Number of threads for read/hash operations
|
|
100
|
-
int max_pending_jobs = 1000; // Maximum pending jobs before blocking
|
|
101
|
-
bool enable_coalescing = true; // Coalesce adjacent write operations
|
|
102
|
-
|
|
103
|
-
// Watermarks for backpressure (as percentage of max_pending_jobs)
|
|
104
|
-
// When pending jobs exceed high watermark, we signal backpressure
|
|
105
|
-
// When pending jobs fall below low watermark, we allow new writes again
|
|
106
|
-
int high_watermark_percent = 80; // Start backpressure at 80%
|
|
107
|
-
int low_watermark_percent = 50; // Resume at 50%
|
|
108
|
-
|
|
109
|
-
DiskIOConfig() = default;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
//=============================================================================
|
|
113
|
-
// DiskIOThreadPool - Multi-threaded disk I/O handler
|
|
114
|
-
//=============================================================================
|
|
115
|
-
|
|
116
|
-
class DiskIOThreadPool {
|
|
117
|
-
public:
|
|
118
|
-
explicit DiskIOThreadPool(const DiskIOConfig& config = DiskIOConfig());
|
|
119
|
-
~DiskIOThreadPool();
|
|
120
|
-
|
|
121
|
-
// Start/stop the thread pool
|
|
122
|
-
bool start();
|
|
123
|
-
void stop();
|
|
124
|
-
bool is_running() const { return running_.load(); }
|
|
125
|
-
|
|
126
|
-
// Configuration
|
|
127
|
-
void set_num_write_threads(int count);
|
|
128
|
-
void set_num_read_threads(int count);
|
|
129
|
-
int get_num_write_threads() const { return num_write_threads_.load(); }
|
|
130
|
-
int get_num_read_threads() const { return num_read_threads_.load(); }
|
|
131
|
-
|
|
132
|
-
// Queue a write operation (writes block immediately to disk)
|
|
133
|
-
void async_write_block(
|
|
134
|
-
const std::string& download_path,
|
|
135
|
-
const std::vector<FileMappingInfo>& files,
|
|
136
|
-
uint32_t piece_index,
|
|
137
|
-
uint32_t piece_length_standard, // Standard piece length from torrent
|
|
138
|
-
uint32_t block_offset, // Offset within piece
|
|
139
|
-
const std::vector<uint8_t>& data,
|
|
140
|
-
WriteCompleteCallback callback,
|
|
141
|
-
DiskJobPriority priority = DiskJobPriority::NORMAL
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
// Queue a piece read operation (reads entire piece from disk)
|
|
145
|
-
void async_read_piece(
|
|
146
|
-
const std::string& download_path,
|
|
147
|
-
const std::vector<FileMappingInfo>& files,
|
|
148
|
-
uint32_t piece_index,
|
|
149
|
-
uint32_t piece_length_standard,
|
|
150
|
-
uint32_t actual_piece_length, // May be smaller for last piece
|
|
151
|
-
ReadCompleteCallback callback,
|
|
152
|
-
DiskJobPriority priority = DiskJobPriority::NORMAL
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
// Queue a piece hash operation (reads piece and computes SHA1)
|
|
156
|
-
void async_hash_piece(
|
|
157
|
-
const std::string& download_path,
|
|
158
|
-
const std::vector<FileMappingInfo>& files,
|
|
159
|
-
uint32_t piece_index,
|
|
160
|
-
uint32_t piece_length_standard,
|
|
161
|
-
uint32_t actual_piece_length,
|
|
162
|
-
HashCompleteCallback callback,
|
|
163
|
-
DiskJobPriority priority = DiskJobPriority::LOW
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
// Queue a flush operation (ensures all pending writes are complete)
|
|
167
|
-
void async_flush(FlushCompleteCallback callback);
|
|
168
|
-
|
|
169
|
-
// Statistics
|
|
170
|
-
size_t get_pending_write_jobs() const;
|
|
171
|
-
size_t get_pending_read_jobs() const;
|
|
172
|
-
size_t get_total_pending_jobs() const;
|
|
173
|
-
uint64_t get_total_bytes_written() const { return total_bytes_written_.load(); }
|
|
174
|
-
uint64_t get_total_bytes_read() const { return total_bytes_read_.load(); }
|
|
175
|
-
uint64_t get_jobs_completed() const { return jobs_completed_.load(); }
|
|
176
|
-
|
|
177
|
-
// Backpressure control
|
|
178
|
-
// Returns true if disk I/O can accept more write operations
|
|
179
|
-
// Uses watermark-based hysteresis to avoid thrashing
|
|
180
|
-
bool can_accept_write() const;
|
|
181
|
-
private:
|
|
182
|
-
DiskIOConfig config_;
|
|
183
|
-
std::atomic<bool> running_;
|
|
184
|
-
|
|
185
|
-
// Thread counts (can be adjusted at runtime)
|
|
186
|
-
std::atomic<int> num_write_threads_;
|
|
187
|
-
std::atomic<int> num_read_threads_;
|
|
188
|
-
|
|
189
|
-
// Worker threads
|
|
190
|
-
std::vector<std::thread> write_threads_;
|
|
191
|
-
std::vector<std::thread> read_threads_;
|
|
192
|
-
|
|
193
|
-
// Separate job queues for writes and reads (with priority)
|
|
194
|
-
std::priority_queue<DiskJob> write_queue_;
|
|
195
|
-
std::priority_queue<DiskJob> read_queue_;
|
|
196
|
-
|
|
197
|
-
mutable std::mutex write_mutex_;
|
|
198
|
-
mutable std::mutex read_mutex_;
|
|
199
|
-
std::condition_variable write_cv_;
|
|
200
|
-
std::condition_variable read_cv_;
|
|
201
|
-
|
|
202
|
-
// Statistics
|
|
203
|
-
std::atomic<uint64_t> total_bytes_written_;
|
|
204
|
-
std::atomic<uint64_t> total_bytes_read_;
|
|
205
|
-
std::atomic<uint64_t> jobs_completed_;
|
|
206
|
-
|
|
207
|
-
// Backpressure state (mutable for hysteresis in const methods)
|
|
208
|
-
mutable std::atomic<bool> under_pressure_;
|
|
209
|
-
|
|
210
|
-
// Worker thread functions
|
|
211
|
-
void write_worker_loop();
|
|
212
|
-
void read_worker_loop();
|
|
213
|
-
|
|
214
|
-
// Execute individual job types
|
|
215
|
-
void execute_write_block(const DiskJob& job);
|
|
216
|
-
void execute_read_piece(const DiskJob& job);
|
|
217
|
-
void execute_hash_piece(const DiskJob& job);
|
|
218
|
-
void execute_flush(const DiskJob& job);
|
|
219
|
-
|
|
220
|
-
// Helper: Map torrent offset to file(s)
|
|
221
|
-
// Returns list of (file_path, file_offset, length) tuples for the given range
|
|
222
|
-
static std::vector<std::tuple<std::string, uint64_t, size_t>> map_to_files(
|
|
223
|
-
const std::string& download_path,
|
|
224
|
-
const std::vector<FileMappingInfo>& files,
|
|
225
|
-
uint64_t torrent_offset,
|
|
226
|
-
size_t length
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
// Helper: Start/stop worker threads
|
|
230
|
-
void start_write_threads(int count);
|
|
231
|
-
void start_read_threads(int count);
|
|
232
|
-
void stop_write_threads();
|
|
233
|
-
void stop_read_threads();
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
//=============================================================================
|
|
237
|
-
// Legacy compatibility - DiskIOThread using thread pool internally
|
|
238
|
-
//=============================================================================
|
|
239
|
-
|
|
240
|
-
class DiskIOThread {
|
|
241
|
-
public:
|
|
242
|
-
DiskIOThread();
|
|
243
|
-
~DiskIOThread();
|
|
244
|
-
|
|
245
|
-
// Start/stop the disk I/O thread
|
|
246
|
-
bool start();
|
|
247
|
-
void stop();
|
|
248
|
-
bool is_running() const;
|
|
249
|
-
|
|
250
|
-
// Queue a write operation (writes block immediately to disk)
|
|
251
|
-
void async_write_block(
|
|
252
|
-
const std::string& download_path,
|
|
253
|
-
const std::vector<FileMappingInfo>& files,
|
|
254
|
-
uint32_t piece_index,
|
|
255
|
-
uint32_t piece_length_standard,
|
|
256
|
-
uint32_t block_offset,
|
|
257
|
-
const std::vector<uint8_t>& data,
|
|
258
|
-
WriteCompleteCallback callback
|
|
259
|
-
);
|
|
260
|
-
|
|
261
|
-
// Queue a piece read operation (reads entire piece from disk)
|
|
262
|
-
void async_read_piece(
|
|
263
|
-
const std::string& download_path,
|
|
264
|
-
const std::vector<FileMappingInfo>& files,
|
|
265
|
-
uint32_t piece_index,
|
|
266
|
-
uint32_t piece_length_standard,
|
|
267
|
-
uint32_t actual_piece_length,
|
|
268
|
-
ReadCompleteCallback callback
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
// Queue a piece hash operation (reads piece and computes SHA1)
|
|
272
|
-
void async_hash_piece(
|
|
273
|
-
const std::string& download_path,
|
|
274
|
-
const std::vector<FileMappingInfo>& files,
|
|
275
|
-
uint32_t piece_index,
|
|
276
|
-
uint32_t piece_length_standard,
|
|
277
|
-
uint32_t actual_piece_length,
|
|
278
|
-
HashCompleteCallback callback
|
|
279
|
-
);
|
|
280
|
-
|
|
281
|
-
// Queue a flush operation (ensures all pending writes are complete)
|
|
282
|
-
void async_flush(FlushCompleteCallback callback);
|
|
283
|
-
|
|
284
|
-
// Statistics
|
|
285
|
-
size_t get_pending_jobs() const;
|
|
286
|
-
uint64_t get_total_bytes_written() const;
|
|
287
|
-
uint64_t get_total_bytes_read() const;
|
|
288
|
-
|
|
289
|
-
private:
|
|
290
|
-
std::unique_ptr<DiskIOThreadPool> pool_;
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
//=============================================================================
|
|
294
|
-
// Global DiskIO instance (singleton pattern)
|
|
295
|
-
//=============================================================================
|
|
296
|
-
|
|
297
|
-
class DiskIO {
|
|
298
|
-
public:
|
|
299
|
-
static DiskIOThreadPool& instance();
|
|
300
|
-
|
|
301
|
-
// Configure the singleton (must be called before first instance() call)
|
|
302
|
-
static void configure(const DiskIOConfig& config);
|
|
303
|
-
|
|
304
|
-
// Non-copyable
|
|
305
|
-
DiskIO(const DiskIO&) = delete;
|
|
306
|
-
DiskIO& operator=(const DiskIO&) = delete;
|
|
307
|
-
|
|
308
|
-
private:
|
|
309
|
-
DiskIO() = default;
|
|
310
|
-
static std::unique_ptr<DiskIOThreadPool> instance_;
|
|
311
|
-
static std::once_flag init_flag_;
|
|
312
|
-
static DiskIOConfig config_;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
} // namespace librats
|