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/socket.h
DELETED
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <string>
|
|
4
|
-
#include <functional>
|
|
5
|
-
#include <vector>
|
|
6
|
-
#include <cstdint>
|
|
7
|
-
|
|
8
|
-
#ifdef _WIN32
|
|
9
|
-
#include <winsock2.h>
|
|
10
|
-
#include <ws2tcpip.h>
|
|
11
|
-
#ifdef _MSC_VER
|
|
12
|
-
#pragma comment(lib, "ws2_32.lib")
|
|
13
|
-
#endif
|
|
14
|
-
typedef SOCKET socket_t;
|
|
15
|
-
#define INVALID_SOCKET_VALUE INVALID_SOCKET
|
|
16
|
-
#define SOCKET_ERROR_VALUE SOCKET_ERROR
|
|
17
|
-
#else
|
|
18
|
-
#include <sys/socket.h>
|
|
19
|
-
#include <arpa/inet.h>
|
|
20
|
-
#include <netinet/in.h>
|
|
21
|
-
#include <unistd.h>
|
|
22
|
-
typedef int socket_t;
|
|
23
|
-
#define INVALID_SOCKET_VALUE -1
|
|
24
|
-
#define SOCKET_ERROR_VALUE -1
|
|
25
|
-
#define closesocket close
|
|
26
|
-
#endif
|
|
27
|
-
|
|
28
|
-
namespace librats {
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Address family for socket creation
|
|
32
|
-
*/
|
|
33
|
-
enum class AddressFamily {
|
|
34
|
-
IPv4, // IPv4 only
|
|
35
|
-
IPv6, // IPv6 only
|
|
36
|
-
DualStack // IPv6 socket with IPv4 support (default)
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* UDP peer information
|
|
41
|
-
*/
|
|
42
|
-
struct Peer {
|
|
43
|
-
std::string ip;
|
|
44
|
-
uint16_t port;
|
|
45
|
-
|
|
46
|
-
Peer() : port(0) {}
|
|
47
|
-
Peer(const std::string& ip, uint16_t port) : ip(ip), port(port) {}
|
|
48
|
-
|
|
49
|
-
bool operator==(const Peer& other) const {
|
|
50
|
-
return ip == other.ip && port == other.port;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
bool operator!=(const Peer& other) const {
|
|
54
|
-
return !(*this == other);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// Socket Library Initialization
|
|
59
|
-
/**
|
|
60
|
-
* Initialize the socket library
|
|
61
|
-
* @return true if successful, false otherwise
|
|
62
|
-
*/
|
|
63
|
-
bool init_socket_library();
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Cleanup the socket library
|
|
67
|
-
*/
|
|
68
|
-
void cleanup_socket_library();
|
|
69
|
-
|
|
70
|
-
// TCP Socket Functions
|
|
71
|
-
/**
|
|
72
|
-
* Create a TCP client socket and connect to a server using dual stack (IPv6 with IPv4 fallback)
|
|
73
|
-
* @param host The hostname or IP address to connect to
|
|
74
|
-
* @param port The port number to connect to
|
|
75
|
-
* @param timeout_ms Connection timeout in milliseconds (0 for blocking)
|
|
76
|
-
* @return Socket handle, or INVALID_SOCKET_VALUE on error
|
|
77
|
-
*/
|
|
78
|
-
socket_t create_tcp_client(const std::string& host, int port, int timeout_ms = 0);
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Create a TCP server socket and bind to a port
|
|
82
|
-
* @param port The port number to bind to
|
|
83
|
-
* @param backlog The maximum number of pending connections
|
|
84
|
-
* @param bind_address The interface IP address to bind to (empty for all interfaces)
|
|
85
|
-
* @param af Address family (DualStack by default)
|
|
86
|
-
* @return Socket handle, or INVALID_SOCKET_VALUE on error
|
|
87
|
-
*/
|
|
88
|
-
socket_t create_tcp_server(int port, int backlog = 5, const std::string& bind_address = "",
|
|
89
|
-
AddressFamily af = AddressFamily::DualStack);
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Accept a client connection on a server socket
|
|
93
|
-
* @param server_socket The server socket handle
|
|
94
|
-
* @return Client socket handle, or INVALID_SOCKET_VALUE on error
|
|
95
|
-
*/
|
|
96
|
-
socket_t accept_client(socket_t server_socket);
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Get the peer address (IP:port) from a connected socket
|
|
100
|
-
* @param socket The connected socket handle
|
|
101
|
-
* @return Peer address string in format "IP:port", or empty string on error
|
|
102
|
-
*/
|
|
103
|
-
std::string get_peer_address(socket_t socket);
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Send data through a TCP socket
|
|
107
|
-
* @param socket The socket handle
|
|
108
|
-
* @param data The binary data to send
|
|
109
|
-
* @return Number of bytes sent, or -1 on error
|
|
110
|
-
*/
|
|
111
|
-
int send_tcp_data(socket_t socket, const std::vector<uint8_t>& data);
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Receive data from a TCP socket
|
|
115
|
-
* @param socket The socket handle
|
|
116
|
-
* @param buffer_size Maximum number of bytes to receive
|
|
117
|
-
* @return Received binary data, empty vector on error
|
|
118
|
-
*/
|
|
119
|
-
std::vector<uint8_t> receive_tcp_data(socket_t socket, size_t buffer_size = 1024);
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Send a length-prefixed framed message through a TCP socket
|
|
123
|
-
* @param socket The socket handle
|
|
124
|
-
* @param message The binary message to send
|
|
125
|
-
* @return Total bytes sent (including length prefix), or -1 on error
|
|
126
|
-
*/
|
|
127
|
-
int send_tcp_message(socket_t socket, const std::vector<uint8_t>& message);
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Receive a complete length-prefixed framed message from a TCP socket
|
|
131
|
-
* @param socket The socket handle
|
|
132
|
-
* @return Complete binary message, empty vector on error or connection close
|
|
133
|
-
*/
|
|
134
|
-
std::vector<uint8_t> receive_tcp_message(socket_t socket);
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Send string data through a TCP socket (converts to binary)
|
|
138
|
-
* @param socket The socket handle
|
|
139
|
-
* @param data The string data to send
|
|
140
|
-
* @return Number of bytes sent, or -1 on error
|
|
141
|
-
*/
|
|
142
|
-
int send_tcp_string(socket_t socket, const std::string& data);
|
|
143
|
-
|
|
144
|
-
// UDP Socket Functions
|
|
145
|
-
/**
|
|
146
|
-
* Create a UDP socket and bind to a port
|
|
147
|
-
* @param port The port to bind to (0 for any available port)
|
|
148
|
-
* @param bind_address The interface IP address to bind to (empty for all interfaces)
|
|
149
|
-
* @param af Address family (DualStack by default)
|
|
150
|
-
* @return UDP socket handle, or INVALID_SOCKET_VALUE on error
|
|
151
|
-
*/
|
|
152
|
-
socket_t create_udp_socket(int port = 0, const std::string& bind_address = "",
|
|
153
|
-
AddressFamily af = AddressFamily::DualStack);
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Send UDP data to a destination host and port
|
|
157
|
-
* @param socket The UDP socket handle
|
|
158
|
-
* @param data The data to send
|
|
159
|
-
* @param host The destination hostname or IP address
|
|
160
|
-
* @param port The destination port
|
|
161
|
-
* @param af Address family matching the socket (DualStack by default)
|
|
162
|
-
* @return Number of bytes sent, or -1 on error
|
|
163
|
-
*/
|
|
164
|
-
int send_udp_data(socket_t socket, const std::vector<uint8_t>& data, const std::string& host, int port,
|
|
165
|
-
AddressFamily af = AddressFamily::DualStack);
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Receive UDP data with optional timeout
|
|
169
|
-
* @param socket The UDP socket handle
|
|
170
|
-
* @param buffer_size Maximum number of bytes to receive
|
|
171
|
-
* @param sender_peer Output parameter for the sender's peer info
|
|
172
|
-
* @param timeout_ms Timeout in milliseconds (-1 for blocking, 0 for non-blocking, >0 for timeout)
|
|
173
|
-
* @param interrupt_fd Optional second socket to watch; when it becomes readable the
|
|
174
|
-
* call returns immediately with an empty vector (used to wake a
|
|
175
|
-
* blocking receive on shutdown). INVALID_SOCKET_VALUE disables it.
|
|
176
|
-
* @return Received data, empty vector on timeout, error or interrupt
|
|
177
|
-
*/
|
|
178
|
-
std::vector<uint8_t> receive_udp_data(socket_t socket, size_t buffer_size, Peer& sender_peer,
|
|
179
|
-
int timeout_ms = -1,
|
|
180
|
-
socket_t interrupt_fd = INVALID_SOCKET_VALUE);
|
|
181
|
-
|
|
182
|
-
// Common Socket Functions
|
|
183
|
-
/**
|
|
184
|
-
* Close a socket
|
|
185
|
-
* @param socket The socket handle to close
|
|
186
|
-
* @param force If true, send RST instead of FIN (avoids TIME_WAIT)
|
|
187
|
-
*/
|
|
188
|
-
void close_socket(socket_t socket, bool force = false);
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Check if a socket is valid
|
|
192
|
-
* @param socket The socket handle to check
|
|
193
|
-
* @return true if valid, false otherwise
|
|
194
|
-
*/
|
|
195
|
-
bool is_valid_socket(socket_t socket);
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* Set socket to non-blocking mode
|
|
199
|
-
* @param socket The socket handle
|
|
200
|
-
* @return true if successful, false otherwise
|
|
201
|
-
*/
|
|
202
|
-
bool set_socket_nonblocking(socket_t socket);
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Set socket to blocking mode
|
|
206
|
-
* @param socket The socket handle
|
|
207
|
-
* @return true if successful, false otherwise
|
|
208
|
-
*/
|
|
209
|
-
bool set_socket_blocking(socket_t socket);
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Connect to a socket address with timeout
|
|
213
|
-
* @param socket The socket handle (should be non-blocking)
|
|
214
|
-
* @param addr The socket address structure
|
|
215
|
-
* @param addr_len Length of the address structure
|
|
216
|
-
* @param timeout_ms Connection timeout in milliseconds
|
|
217
|
-
* @return true if connected successfully, false on timeout or error
|
|
218
|
-
*/
|
|
219
|
-
bool connect_with_timeout(socket_t socket, struct sockaddr* addr, socklen_t addr_len, int timeout_ms);
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Get the port that a socket is bound to
|
|
223
|
-
* @param socket The socket handle
|
|
224
|
-
* @return The bound port, or 0 on error
|
|
225
|
-
*/
|
|
226
|
-
int get_bound_port(socket_t socket);
|
|
227
|
-
|
|
228
|
-
} // namespace librats
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
#include "threadmanager.h"
|
|
2
|
-
|
|
3
|
-
// ThreadManager module logging macros
|
|
4
|
-
#define LOG_THREAD_DEBUG(message) LOG_DEBUG("thread", message)
|
|
5
|
-
#define LOG_THREAD_INFO(message) LOG_INFO("thread", message)
|
|
6
|
-
#define LOG_THREAD_WARN(message) LOG_WARN("thread", message)
|
|
7
|
-
#define LOG_THREAD_ERROR(message) LOG_ERROR("thread", message)
|
|
8
|
-
|
|
9
|
-
namespace librats {
|
|
10
|
-
|
|
11
|
-
ThreadManager::ThreadManager() {
|
|
12
|
-
LOG_THREAD_DEBUG("ThreadManager initialized");
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
ThreadManager::~ThreadManager() {
|
|
16
|
-
// Ensure all threads are properly cleaned up
|
|
17
|
-
join_all_active_threads();
|
|
18
|
-
LOG_THREAD_DEBUG("ThreadManager destroyed");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
void ThreadManager::add_managed_thread(std::thread&& t, const std::string& name) {
|
|
22
|
-
// Check if shutdown has been requested - don't add new threads during shutdown
|
|
23
|
-
if (shutdown_requested_.load()) {
|
|
24
|
-
LOG_THREAD_WARN("Ignoring thread detach during shutdown: " << name);
|
|
25
|
-
// Join the thread so it can finish on its own during shutdown
|
|
26
|
-
if (t.joinable()) {
|
|
27
|
-
t.join();
|
|
28
|
-
}
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
std::lock_guard<std::mutex> lock(active_threads_mutex_);
|
|
33
|
-
|
|
34
|
-
// Double-check after acquiring lock
|
|
35
|
-
if (shutdown_requested_.load()) {
|
|
36
|
-
LOG_THREAD_WARN("Ignoring thread detach during shutdown (double-check): " << name);
|
|
37
|
-
// Join the thread so it can finish on its own during shutdown
|
|
38
|
-
if (t.joinable()) {
|
|
39
|
-
t.join();
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
active_threads_.emplace_back(std::move(t));
|
|
45
|
-
LOG_THREAD_DEBUG("Added managed thread: " << name << " (total: " << active_threads_.size() << ")");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
void ThreadManager::cleanup_finished_threads() {
|
|
49
|
-
// we wait until all threads are finished thus mean cleanup is done
|
|
50
|
-
join_all_active_threads();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
void ThreadManager::shutdown_all_threads() {
|
|
54
|
-
LOG_THREAD_INFO("Initiating shutdown of all background threads");
|
|
55
|
-
|
|
56
|
-
// Set shutdown flag first
|
|
57
|
-
shutdown_requested_.store(true);
|
|
58
|
-
|
|
59
|
-
// Notify all waiting threads to wake up immediately
|
|
60
|
-
notify_shutdown();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
void ThreadManager::join_all_active_threads() {
|
|
64
|
-
std::vector<std::thread> threads_to_join;
|
|
65
|
-
|
|
66
|
-
// Move threads out of the container while holding the lock
|
|
67
|
-
{
|
|
68
|
-
std::lock_guard<std::mutex> lock(active_threads_mutex_);
|
|
69
|
-
LOG_THREAD_INFO("Waiting for " << active_threads_.size() << " managed threads to finish");
|
|
70
|
-
|
|
71
|
-
if (active_threads_.empty()) {
|
|
72
|
-
LOG_THREAD_INFO("No active threads to join");
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Move all threads to local vector to avoid holding lock during join
|
|
77
|
-
threads_to_join = std::move(active_threads_);
|
|
78
|
-
active_threads_.clear();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Join threads without holding the mutex
|
|
82
|
-
for (auto& t : threads_to_join) {
|
|
83
|
-
if (t.joinable()) {
|
|
84
|
-
try {
|
|
85
|
-
t.join();
|
|
86
|
-
} catch (const std::exception& e) {
|
|
87
|
-
LOG_THREAD_ERROR("Exception while joining thread: " << e.what());
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
LOG_THREAD_INFO("All managed threads have been cleaned up");
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
size_t ThreadManager::get_active_thread_count() const {
|
|
96
|
-
std::lock_guard<std::mutex> lock(active_threads_mutex_);
|
|
97
|
-
return active_threads_.size();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
void ThreadManager::notify_shutdown() {
|
|
101
|
-
shutdown_cv_.notify_all();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
} // namespace librats
|
|
105
|
-
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
#ifndef THREADMANAGER_H
|
|
2
|
-
#define THREADMANAGER_H
|
|
3
|
-
|
|
4
|
-
#include <thread>
|
|
5
|
-
#include <vector>
|
|
6
|
-
#include <mutex>
|
|
7
|
-
#include <condition_variable>
|
|
8
|
-
#include <atomic>
|
|
9
|
-
#include "logger.h"
|
|
10
|
-
|
|
11
|
-
namespace librats {
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* ThreadManager - Manages background threads with graceful shutdown
|
|
15
|
-
*/
|
|
16
|
-
class ThreadManager {
|
|
17
|
-
public:
|
|
18
|
-
ThreadManager();
|
|
19
|
-
virtual ~ThreadManager();
|
|
20
|
-
|
|
21
|
-
// Add a new managed thread with optional name for debugging
|
|
22
|
-
void add_managed_thread(std::thread&& t, const std::string& name = "unnamed");
|
|
23
|
-
|
|
24
|
-
// Clean up finished threads (non-blocking)
|
|
25
|
-
void cleanup_finished_threads();
|
|
26
|
-
|
|
27
|
-
// Signal shutdown to all threads
|
|
28
|
-
void shutdown_all_threads();
|
|
29
|
-
|
|
30
|
-
// Wait for all active threads to finish (blocking)
|
|
31
|
-
void join_all_active_threads();
|
|
32
|
-
|
|
33
|
-
// Get count of active threads
|
|
34
|
-
size_t get_active_thread_count() const;
|
|
35
|
-
|
|
36
|
-
protected:
|
|
37
|
-
// Notify waiting threads of shutdown
|
|
38
|
-
void notify_shutdown();
|
|
39
|
-
|
|
40
|
-
// Condition variable for shutdown coordination
|
|
41
|
-
std::condition_variable shutdown_cv_;
|
|
42
|
-
std::mutex shutdown_mutex_;
|
|
43
|
-
|
|
44
|
-
private:
|
|
45
|
-
mutable std::mutex active_threads_mutex_;
|
|
46
|
-
std::vector<std::thread> active_threads_;
|
|
47
|
-
std::atomic<bool> shutdown_requested_{false};
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
} // namespace librats
|
|
51
|
-
|
|
52
|
-
#endif // THREADMANAGER_H
|
|
53
|
-
|