librats 1.0.2 → 2.1.4

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 (313) hide show
  1. package/README.md +137 -332
  2. package/binding.gyp +16 -3
  3. package/lib/index.d.ts +271 -696
  4. package/lib/index.js +383 -44
  5. package/native-src/3rdparty/android/ifaddrs-android.c +1 -0
  6. package/native-src/3rdparty/android/ifaddrs-android.h +1 -0
  7. package/native-src/CMakeLists.txt +396 -179
  8. package/native-src/LICENSE +1 -1
  9. package/native-src/src/librats/bindings/rats.cpp +731 -0
  10. package/native-src/src/librats/bindings/rats.h +352 -0
  11. package/native-src/src/librats/bittorrent/bencode.cpp +437 -0
  12. package/native-src/src/librats/bittorrent/bencode.h +176 -0
  13. package/native-src/src/librats/bittorrent/bitfield.cpp +97 -0
  14. package/native-src/src/librats/bittorrent/bitfield.h +76 -0
  15. package/native-src/src/librats/bittorrent/byte_io.h +58 -0
  16. package/native-src/src/librats/bittorrent/choker.cpp +25 -0
  17. package/native-src/src/librats/bittorrent/choker.h +46 -0
  18. package/native-src/src/librats/bittorrent/client.cpp +413 -0
  19. package/native-src/src/librats/bittorrent/client.h +227 -0
  20. package/native-src/src/librats/bittorrent/disk_io.cpp +209 -0
  21. package/native-src/src/librats/bittorrent/disk_io.h +150 -0
  22. package/native-src/src/librats/bittorrent/extensions.cpp +191 -0
  23. package/native-src/src/librats/bittorrent/extensions.h +94 -0
  24. package/native-src/src/librats/bittorrent/file_storage.cpp +77 -0
  25. package/native-src/src/librats/bittorrent/file_storage.h +79 -0
  26. package/native-src/src/librats/bittorrent/log.h +42 -0
  27. package/native-src/src/librats/bittorrent/magnet_uri.cpp +98 -0
  28. package/native-src/src/librats/bittorrent/magnet_uri.h +35 -0
  29. package/native-src/src/librats/bittorrent/peer_connection.cpp +502 -0
  30. package/native-src/src/librats/bittorrent/peer_connection.h +194 -0
  31. package/native-src/src/librats/bittorrent/peer_list.cpp +68 -0
  32. package/native-src/src/librats/bittorrent/peer_list.h +75 -0
  33. package/native-src/src/librats/bittorrent/piece_picker.cpp +352 -0
  34. package/native-src/src/librats/bittorrent/piece_picker.h +201 -0
  35. package/native-src/src/librats/bittorrent/reactor.cpp +97 -0
  36. package/native-src/src/librats/bittorrent/reactor.h +89 -0
  37. package/native-src/src/librats/bittorrent/resume_data.cpp +72 -0
  38. package/native-src/src/librats/bittorrent/resume_data.h +41 -0
  39. package/native-src/src/librats/bittorrent/store_buffer.cpp +48 -0
  40. package/native-src/src/librats/bittorrent/store_buffer.h +47 -0
  41. package/native-src/src/librats/bittorrent/torrent.cpp +870 -0
  42. package/native-src/src/librats/bittorrent/torrent.h +260 -0
  43. package/native-src/src/librats/bittorrent/torrent_creator.cpp +129 -0
  44. package/native-src/src/librats/bittorrent/torrent_creator.h +58 -0
  45. package/native-src/src/librats/bittorrent/torrent_info.cpp +314 -0
  46. package/native-src/src/librats/bittorrent/torrent_info.h +118 -0
  47. package/native-src/src/librats/bittorrent/tracker.cpp +374 -0
  48. package/native-src/src/librats/bittorrent/tracker.h +108 -0
  49. package/native-src/src/librats/bittorrent/types.cpp +206 -0
  50. package/native-src/src/librats/bittorrent/types.h +86 -0
  51. package/native-src/src/librats/core/address.cpp +35 -0
  52. package/native-src/src/librats/core/address.h +78 -0
  53. package/native-src/src/librats/core/bytes.h +69 -0
  54. package/native-src/src/librats/core/chained_send_buffer.cpp +172 -0
  55. package/native-src/src/librats/core/chained_send_buffer.h +183 -0
  56. package/native-src/src/librats/core/endpoint_parse.cpp +41 -0
  57. package/native-src/src/librats/core/endpoint_parse.h +31 -0
  58. package/native-src/src/librats/core/event_bus.h +70 -0
  59. package/native-src/src/librats/core/host_endpoint.h +56 -0
  60. package/native-src/src/{io_poller.cpp → librats/core/io_poller.cpp} +520 -65
  61. package/native-src/src/{io_poller.h → librats/core/io_poller.h} +12 -6
  62. package/native-src/src/librats/core/ip_address.cpp +120 -0
  63. package/native-src/src/librats/core/ip_address.h +109 -0
  64. package/native-src/src/librats/core/mpsc_queue.h +47 -0
  65. package/native-src/src/librats/core/notifier.h +74 -0
  66. package/native-src/src/librats/core/receive_buffer.cpp +219 -0
  67. package/native-src/src/librats/core/receive_buffer.h +171 -0
  68. package/native-src/src/librats/core/service_registry.h +58 -0
  69. package/native-src/src/{socket.cpp → librats/core/socket.cpp} +625 -118
  70. package/native-src/src/librats/core/socket.h +496 -0
  71. package/native-src/src/librats/core/timer_queue.h +105 -0
  72. package/native-src/src/librats/core/types.cpp +42 -0
  73. package/native-src/src/librats/core/types.h +93 -0
  74. package/native-src/src/librats/core/wakeup_pipe.h +83 -0
  75. package/native-src/src/{crypto → librats/crypto}/blake2_endian.h +21 -23
  76. package/native-src/src/{crypto → librats/crypto}/blake2b.c +34 -33
  77. package/native-src/src/{crypto → librats/crypto}/blake2b.h +7 -6
  78. package/native-src/src/{crypto → librats/crypto}/blake2s.c +55 -54
  79. package/native-src/src/{crypto → librats/crypto}/blake2s.h +13 -12
  80. package/native-src/src/{crypto → librats/crypto}/chacha.c +22 -21
  81. package/native-src/src/{crypto → librats/crypto}/chacha.h +14 -13
  82. package/native-src/src/{crypto → librats/crypto}/chachapoly.c +56 -56
  83. package/native-src/src/{crypto → librats/crypto}/chachapoly.h +24 -17
  84. package/native-src/src/{crc32.cpp → librats/crypto/crc32.cpp} +1 -1
  85. package/native-src/src/{crc32.h → librats/crypto/crc32.h} +3 -1
  86. package/native-src/src/{crypto → librats/crypto}/curve25519.c +6 -4
  87. package/native-src/src/{crypto → librats/crypto}/curve25519.h +6 -3
  88. package/native-src/src/librats/crypto/hkdf.c +266 -0
  89. package/native-src/src/{crypto → librats/crypto}/hkdf.h +19 -19
  90. package/native-src/src/{noise.cpp → librats/crypto/noise.cpp} +84 -73
  91. package/native-src/src/{noise.h → librats/crypto/noise.h} +18 -8
  92. package/native-src/src/{crypto → librats/crypto}/poly1305.c +47 -46
  93. package/native-src/src/librats/crypto/poly1305.h +37 -0
  94. package/native-src/src/{sha1.cpp → librats/crypto/sha1.cpp} +33 -1
  95. package/native-src/src/{sha1.h → librats/crypto/sha1.h} +14 -6
  96. package/native-src/src/{crypto → librats/crypto}/sha256.c +15 -14
  97. package/native-src/src/{crypto → librats/crypto}/sha256.h +8 -7
  98. package/native-src/src/{crypto → librats/crypto}/sha512.c +15 -14
  99. package/native-src/src/{crypto → librats/crypto}/sha512.h +8 -7
  100. package/native-src/src/librats/dht/announce.cpp +37 -0
  101. package/native-src/src/librats/dht/announce.h +41 -0
  102. package/native-src/src/librats/dht/bep42.cpp +109 -0
  103. package/native-src/src/librats/dht/bep42.h +48 -0
  104. package/native-src/src/librats/dht/dht.cpp +501 -0
  105. package/native-src/src/librats/dht/dht.h +119 -0
  106. package/native-src/src/librats/dht/dht_runner.cpp +103 -0
  107. package/native-src/src/librats/dht/dht_runner.h +71 -0
  108. package/native-src/src/librats/dht/dos_blocker.cpp +42 -0
  109. package/native-src/src/librats/dht/dos_blocker.h +47 -0
  110. package/native-src/src/librats/dht/find_peers.cpp +52 -0
  111. package/native-src/src/librats/dht/find_peers.h +73 -0
  112. package/native-src/src/librats/dht/id.h +167 -0
  113. package/native-src/src/{krpc.cpp → librats/dht/krpc.cpp} +32 -81
  114. package/native-src/src/{krpc.h → librats/dht/krpc.h} +19 -23
  115. package/native-src/src/librats/dht/log.h +38 -0
  116. package/native-src/src/librats/dht/node.cpp +473 -0
  117. package/native-src/src/librats/dht/node.h +164 -0
  118. package/native-src/src/librats/dht/node_entry.h +81 -0
  119. package/native-src/src/librats/dht/observer.h +72 -0
  120. package/native-src/src/librats/dht/persistence.cpp +90 -0
  121. package/native-src/src/librats/dht/persistence.h +32 -0
  122. package/native-src/src/librats/dht/routing_table.cpp +559 -0
  123. package/native-src/src/librats/dht/routing_table.h +185 -0
  124. package/native-src/src/librats/dht/rpc_manager.cpp +127 -0
  125. package/native-src/src/librats/dht/rpc_manager.h +77 -0
  126. package/native-src/src/librats/dht/storage.cpp +92 -0
  127. package/native-src/src/librats/dht/storage.h +74 -0
  128. package/native-src/src/librats/dht/transport.h +27 -0
  129. package/native-src/src/librats/dht/traversal.cpp +326 -0
  130. package/native-src/src/librats/dht/traversal.h +120 -0
  131. package/native-src/src/librats/dht/udp_transport.cpp +49 -0
  132. package/native-src/src/librats/dht/udp_transport.h +51 -0
  133. package/native-src/src/librats/mdns/log.h +22 -0
  134. package/native-src/src/{mdns.cpp → librats/mdns/mdns.cpp} +75 -40
  135. package/native-src/src/{mdns.h → librats/mdns/mdns.h} +9 -8
  136. package/native-src/src/{natpmp.cpp → librats/nat/natpmp.cpp} +12 -9
  137. package/native-src/src/{natpmp.h → librats/nat/natpmp.h} +3 -3
  138. package/native-src/src/{port_mapping.h → librats/nat/port_mapping.h} +3 -2
  139. package/native-src/src/{stun.cpp → librats/nat/stun.cpp} +4 -4
  140. package/native-src/src/{stun.h → librats/nat/stun.h} +1 -1
  141. package/native-src/src/{upnp.cpp → librats/nat/upnp.cpp} +6 -6
  142. package/native-src/src/{upnp.h → librats/nat/upnp.h} +2 -2
  143. package/native-src/src/librats/node/config.h +110 -0
  144. package/native-src/src/librats/node/dial_service.h +54 -0
  145. package/native-src/src/librats/node/dialer.cpp +264 -0
  146. package/native-src/src/librats/node/dialer.h +188 -0
  147. package/native-src/src/librats/node/host_events.h +26 -0
  148. package/native-src/src/librats/node/identify.cpp +130 -0
  149. package/native-src/src/librats/node/identify.h +71 -0
  150. package/native-src/src/librats/node/nat_status.cpp +103 -0
  151. package/native-src/src/librats/node/nat_status.h +118 -0
  152. package/native-src/src/librats/node/node.cpp +826 -0
  153. package/native-src/src/librats/node/node.h +333 -0
  154. package/native-src/src/librats/node/node_context.h +33 -0
  155. package/native-src/src/librats/node/peer_network.h +91 -0
  156. package/native-src/src/librats/peer/peer.h +49 -0
  157. package/native-src/src/librats/peer/peer_book.cpp +181 -0
  158. package/native-src/src/librats/peer/peer_book.h +88 -0
  159. package/native-src/src/librats/peer/peer_id.cpp +72 -0
  160. package/native-src/src/librats/peer/peer_id.h +62 -0
  161. package/native-src/src/librats/peer/peer_info.h +37 -0
  162. package/native-src/src/librats/peer/peer_table.cpp +137 -0
  163. package/native-src/src/librats/peer/peer_table.h +148 -0
  164. package/native-src/src/librats/security/handshaker.h +66 -0
  165. package/native-src/src/librats/security/identity.h +43 -0
  166. package/native-src/src/librats/security/noise_security.cpp +122 -0
  167. package/native-src/src/librats/security/noise_security.h +37 -0
  168. package/native-src/src/librats/security/plaintext_security.h +106 -0
  169. package/native-src/src/librats/security/session.h +37 -0
  170. package/native-src/src/{storage.cpp → librats/storage/storage.cpp} +369 -522
  171. package/native-src/src/{storage.h → librats/storage/storage.h} +135 -299
  172. package/native-src/src/librats/subsystems/bittorrent.cpp +211 -0
  173. package/native-src/src/librats/subsystems/bittorrent.h +136 -0
  174. package/native-src/src/librats/subsystems/dht_discovery.cpp +202 -0
  175. package/native-src/src/librats/subsystems/dht_discovery.h +123 -0
  176. package/native-src/src/librats/subsystems/dht_service.h +36 -0
  177. package/native-src/src/librats/subsystems/file_transfer.cpp +972 -0
  178. package/native-src/src/librats/subsystems/file_transfer.h +367 -0
  179. package/native-src/src/librats/subsystems/hole_punch.cpp +532 -0
  180. package/native-src/src/librats/subsystems/hole_punch.h +266 -0
  181. package/native-src/src/librats/subsystems/hole_punch_service.h +38 -0
  182. package/native-src/src/librats/subsystems/mdns_discovery.cpp +66 -0
  183. package/native-src/src/librats/subsystems/mdns_discovery.h +55 -0
  184. package/native-src/src/librats/subsystems/message_json.cpp +112 -0
  185. package/native-src/src/librats/subsystems/message_json.h +88 -0
  186. package/native-src/src/librats/subsystems/peer_exchange.cpp +241 -0
  187. package/native-src/src/librats/subsystems/peer_exchange.h +136 -0
  188. package/native-src/src/librats/subsystems/ping_service.cpp +98 -0
  189. package/native-src/src/librats/subsystems/ping_service.h +66 -0
  190. package/native-src/src/librats/subsystems/port_mapping_service.cpp +192 -0
  191. package/native-src/src/librats/subsystems/port_mapping_service.h +84 -0
  192. package/native-src/src/librats/subsystems/pubsub.cpp +567 -0
  193. package/native-src/src/librats/subsystems/pubsub.h +175 -0
  194. package/native-src/src/librats/subsystems/reconnection.cpp +239 -0
  195. package/native-src/src/librats/subsystems/reconnection.h +126 -0
  196. package/native-src/src/librats/transport/connection.cpp +343 -0
  197. package/native-src/src/librats/transport/connection.h +283 -0
  198. package/native-src/src/librats/transport/link.h +96 -0
  199. package/native-src/src/librats/transport/reactor.cpp +540 -0
  200. package/native-src/src/librats/transport/reactor.h +224 -0
  201. package/native-src/src/librats/transport/reactor_pool.h +81 -0
  202. package/native-src/src/librats/transport/tcp_link.cpp +49 -0
  203. package/native-src/src/librats/transport/tcp_link.h +43 -0
  204. package/native-src/src/librats/transport/udp_mux.cpp +617 -0
  205. package/native-src/src/librats/transport/udp_mux.h +363 -0
  206. package/native-src/src/librats/transport/udp_packet.cpp +121 -0
  207. package/native-src/src/librats/transport/udp_packet.h +190 -0
  208. package/native-src/src/librats/transport/udp_stream.cpp +1194 -0
  209. package/native-src/src/librats/transport/udp_stream.h +614 -0
  210. package/native-src/src/librats/util/features.h.in +51 -0
  211. package/native-src/src/{fs.cpp → librats/util/fs.cpp} +51 -3
  212. package/native-src/src/librats/util/fs.h +136 -0
  213. package/native-src/src/librats/util/json.cpp +1002 -0
  214. package/native-src/src/librats/util/json.h +444 -0
  215. package/native-src/src/{logger.cpp → librats/util/logger.cpp} +1 -1
  216. package/native-src/src/{logger.h → librats/util/logger.h} +43 -31
  217. package/native-src/src/{network_monitor.cpp → librats/util/network_monitor.cpp} +12 -4
  218. package/native-src/src/{network_monitor.h → librats/util/network_monitor.h} +2 -1
  219. package/native-src/src/{network_utils.cpp → librats/util/network_utils.cpp} +38 -23
  220. package/native-src/src/{network_utils.h → librats/util/network_utils.h} +15 -8
  221. package/native-src/src/{os.cpp → librats/util/os.cpp} +48 -18
  222. package/native-src/src/librats/util/rats_export.h +69 -0
  223. package/native-src/src/{version.cpp → librats/util/version.cpp} +2 -2
  224. package/native-src/src/{version.h.in → librats/util/version.h.in} +1 -1
  225. package/native-src/src/librats/wire/frame.cpp +76 -0
  226. package/native-src/src/librats/wire/frame.h +110 -0
  227. package/native-src/src/librats/wire/message_router.cpp +45 -0
  228. package/native-src/src/librats/wire/message_router.h +47 -0
  229. package/package.json +5 -4
  230. package/scripts/build-librats.js +1 -0
  231. package/scripts/postinstall.js +3 -3
  232. package/scripts/prepare-package.js +4 -4
  233. package/scripts/verify-installation.js +63 -105
  234. package/src/librats_node.cpp +1042 -1324
  235. package/native-src/src/bencode.cpp +0 -485
  236. package/native-src/src/bencode.h +0 -145
  237. package/native-src/src/bittorrent.cpp +0 -14
  238. package/native-src/src/bittorrent.h +0 -74
  239. package/native-src/src/bt_bitfield.cpp +0 -372
  240. package/native-src/src/bt_bitfield.h +0 -316
  241. package/native-src/src/bt_choker.cpp +0 -228
  242. package/native-src/src/bt_choker.h +0 -147
  243. package/native-src/src/bt_client.cpp +0 -1047
  244. package/native-src/src/bt_client.h +0 -445
  245. package/native-src/src/bt_create_torrent.cpp +0 -677
  246. package/native-src/src/bt_create_torrent.h +0 -473
  247. package/native-src/src/bt_extension.cpp +0 -469
  248. package/native-src/src/bt_extension.h +0 -309
  249. package/native-src/src/bt_file_storage.cpp +0 -261
  250. package/native-src/src/bt_file_storage.h +0 -298
  251. package/native-src/src/bt_handshake.cpp +0 -134
  252. package/native-src/src/bt_handshake.h +0 -157
  253. package/native-src/src/bt_messages.cpp +0 -364
  254. package/native-src/src/bt_messages.h +0 -324
  255. package/native-src/src/bt_network.cpp +0 -1007
  256. package/native-src/src/bt_network.h +0 -417
  257. package/native-src/src/bt_peer_connection.cpp +0 -742
  258. package/native-src/src/bt_peer_connection.h +0 -592
  259. package/native-src/src/bt_piece_picker.cpp +0 -786
  260. package/native-src/src/bt_piece_picker.h +0 -473
  261. package/native-src/src/bt_resume_data.cpp +0 -410
  262. package/native-src/src/bt_resume_data.h +0 -249
  263. package/native-src/src/bt_torrent.cpp +0 -2120
  264. package/native-src/src/bt_torrent.h +0 -641
  265. package/native-src/src/bt_torrent_info.cpp +0 -659
  266. package/native-src/src/bt_torrent_info.h +0 -418
  267. package/native-src/src/bt_types.h +0 -621
  268. package/native-src/src/chained_send_buffer.cpp +0 -75
  269. package/native-src/src/chained_send_buffer.h +0 -137
  270. package/native-src/src/crypto/hkdf.c +0 -266
  271. package/native-src/src/crypto/poly1305.h +0 -36
  272. package/native-src/src/dht.cpp +0 -3311
  273. package/native-src/src/dht.h +0 -717
  274. package/native-src/src/disk_io.cpp +0 -632
  275. package/native-src/src/disk_io.h +0 -315
  276. package/native-src/src/file_transfer.cpp +0 -1415
  277. package/native-src/src/file_transfer.h +0 -286
  278. package/native-src/src/fs.h +0 -108
  279. package/native-src/src/gossipsub.cpp +0 -1139
  280. package/native-src/src/gossipsub.h +0 -403
  281. package/native-src/src/ice.cpp +0 -893
  282. package/native-src/src/ice.h +0 -559
  283. package/native-src/src/json.hpp +0 -25526
  284. package/native-src/src/librats.cpp +0 -2378
  285. package/native-src/src/librats.h +0 -2324
  286. package/native-src/src/librats_bittorrent.cpp +0 -601
  287. package/native-src/src/librats_c.cpp +0 -1557
  288. package/native-src/src/librats_c.h +0 -323
  289. package/native-src/src/librats_discovery.cpp +0 -402
  290. package/native-src/src/librats_encryption.cpp +0 -275
  291. package/native-src/src/librats_file_transfer.cpp +0 -144
  292. package/native-src/src/librats_gossipsub.cpp +0 -289
  293. package/native-src/src/librats_ice.cpp +0 -213
  294. package/native-src/src/librats_log_macros.h +0 -36
  295. package/native-src/src/librats_logging.cpp +0 -173
  296. package/native-src/src/librats_mdns.cpp +0 -166
  297. package/native-src/src/librats_persistence.cpp +0 -796
  298. package/native-src/src/librats_portmap.cpp +0 -419
  299. package/native-src/src/librats_reconnection.cpp +0 -218
  300. package/native-src/src/librats_statistic.cpp +0 -105
  301. package/native-src/src/librats_storage.cpp +0 -189
  302. package/native-src/src/rats_export.h +0 -17
  303. package/native-src/src/receive_buffer.cpp +0 -82
  304. package/native-src/src/receive_buffer.h +0 -127
  305. package/native-src/src/socket.h +0 -228
  306. package/native-src/src/threadmanager.cpp +0 -105
  307. package/native-src/src/threadmanager.h +0 -53
  308. package/native-src/src/tracker.cpp +0 -1264
  309. package/native-src/src/tracker.h +0 -319
  310. package/native-src/src/turn.cpp +0 -762
  311. package/native-src/src/turn.h +0 -460
  312. package/native-src/src/wakeup_pipe.h +0 -60
  313. /package/native-src/src/{os.h → librats/util/os.h} +0 -0
@@ -1,286 +0,0 @@
1
- #pragma once
2
-
3
- // =============================================================================
4
- // File / directory transfer for librats
5
- // =============================================================================
6
- //
7
- // A transfer streams one file or a whole directory tree to a connected peer
8
- // over the reliable (and, when enabled, encrypted) RatsClient connection.
9
- //
10
- // Wire protocol (see file_transfer.cpp for the exact framing):
11
- // * Control messages travel on the named-message channel as JSON:
12
- // ft_offer sender -> receiver : manifest of files to be sent
13
- // ft_response receiver-> sender : accepted / rejected
14
- // ft_file_end sender -> receiver : SHA-256 of a file once fully streamed
15
- // ft_progress receiver-> sender : bytes received so far (drives backpressure)
16
- // ft_complete receiver-> sender : transfer finished (success / failure)
17
- // ft_control either -> either : pause / resume / cancel
18
- // * File data travels on the binary channel as self-describing chunk frames.
19
- //
20
- // Design notes:
21
- // * The transport is reliable and ordered, so chunks are streamed strictly
22
- // sequentially - there is no per-chunk retransmission. Integrity is checked
23
- // with a per-chunk CRC32 and a whole-file SHA-256.
24
- // * Backpressure: the sender keeps at most `window_bytes` un-acknowledged and
25
- // waits for `ft_progress` before sending more, so memory stays bounded.
26
- // * Received data is written to a temp file and only moved to its final path
27
- // after the SHA-256 matches, so a failed transfer never leaves a bad file.
28
- //
29
- // =============================================================================
30
-
31
- #include "json.hpp"
32
-
33
- #include <atomic>
34
- #include <chrono>
35
- #include <condition_variable>
36
- #include <cstdint>
37
- #include <functional>
38
- #include <memory>
39
- #include <mutex>
40
- #include <queue>
41
- #include <string>
42
- #include <thread>
43
- #include <unordered_map>
44
- #include <vector>
45
-
46
- namespace librats {
47
-
48
- class RatsClient;
49
- struct Transfer; // internal per-transfer state, defined in file_transfer.cpp
50
-
51
- // -----------------------------------------------------------------------------
52
- // Public enums
53
- // -----------------------------------------------------------------------------
54
-
55
- enum class FileTransferStatus {
56
- PENDING, // incoming offer awaiting accept/reject, or outgoing offer awaiting response
57
- STARTING, // accepted, about to move data
58
- IN_PROGRESS, // actively transferring
59
- PAUSED, // paused by either side
60
- COMPLETED, // finished successfully
61
- FAILED, // finished with an error
62
- CANCELLED, // cancelled by either side
63
- RESUMING // transient state between PAUSED and IN_PROGRESS
64
- };
65
-
66
- enum class FileTransferDirection {
67
- SENDING,
68
- RECEIVING
69
- };
70
-
71
- // Human-readable name of a status, e.g. "IN_PROGRESS". Never returns null.
72
- const char* file_transfer_status_name(FileTransferStatus status);
73
-
74
- // -----------------------------------------------------------------------------
75
- // Public data structures
76
- // -----------------------------------------------------------------------------
77
-
78
- // One file inside a transfer. A single-file transfer has exactly one entry; a
79
- // directory transfer has one per regular file.
80
- struct FileInfo {
81
- std::string relative_path; // POSIX-style path relative to the transfer root
82
- uint64_t size = 0; // file size in bytes
83
- };
84
-
85
- // Description of an incoming transfer, delivered to the offer callback so the
86
- // application can decide whether to accept() or reject() it.
87
- struct IncomingTransferOffer {
88
- std::string transfer_id;
89
- std::string peer_id;
90
- std::string name; // file name, or directory name
91
- bool is_directory = false;
92
- uint64_t total_size = 0; // sum of all file sizes
93
- std::vector<FileInfo> files; // full manifest
94
- };
95
-
96
- // Immutable snapshot of a transfer's progress. Returned by queries and passed
97
- // to the progress callback.
98
- struct FileTransferProgress {
99
- std::string transfer_id;
100
- std::string peer_id;
101
- FileTransferDirection direction = FileTransferDirection::SENDING;
102
- FileTransferStatus status = FileTransferStatus::PENDING;
103
-
104
- std::string filename; // file name, or directory name
105
- std::string local_path; // local source (sending) or destination (receiving)
106
- bool is_directory = false;
107
-
108
- uint64_t bytes_transferred = 0;
109
- uint64_t total_bytes = 0;
110
- uint32_t files_completed = 0;
111
- uint32_t total_files = 0;
112
-
113
- double transfer_rate_bps = 0.0; // recent throughput, bytes/second
114
- double average_rate_bps = 0.0; // average throughput since start
115
-
116
- std::chrono::milliseconds elapsed_time{0};
117
- std::chrono::milliseconds estimated_time_remaining{0};
118
-
119
- std::string error_message; // populated when status == FAILED
120
-
121
- // Completion as a percentage in [0, 100].
122
- double get_completion_percentage() const {
123
- if (total_bytes == 0) {
124
- return status == FileTransferStatus::COMPLETED ? 100.0 : 0.0;
125
- }
126
- return (static_cast<double>(bytes_transferred) / static_cast<double>(total_bytes)) * 100.0;
127
- }
128
-
129
- std::chrono::milliseconds get_elapsed_time() const { return elapsed_time; }
130
- };
131
-
132
- // -----------------------------------------------------------------------------
133
- // Configuration
134
- // -----------------------------------------------------------------------------
135
-
136
- struct FileTransferConfig {
137
- uint32_t chunk_size = 64 * 1024; // payload bytes per network chunk
138
- uint32_t window_bytes = 4 * 1024 * 1024; // max un-acknowledged bytes in flight
139
- uint32_t progress_interval = 256 * 1024; // receiver sends an ack every N bytes
140
- uint32_t transfer_timeout_secs = 60; // abort a transfer idle for this long
141
- uint32_t worker_threads = 4; // concurrent outgoing transfers
142
- bool verify_integrity = true; // per-chunk CRC32 + whole-file SHA-256
143
- std::string temp_directory = "./rats_file_transfers"; // holds in-progress downloads
144
- };
145
-
146
- // -----------------------------------------------------------------------------
147
- // Callback types
148
- // -----------------------------------------------------------------------------
149
-
150
- // Invoked when a peer offers a transfer. The handler should eventually call
151
- // accept()/reject() (it may do so synchronously or later, from any thread).
152
- // If no offer callback is registered, incoming offers are auto-rejected.
153
- using TransferOfferCallback = std::function<void(const IncomingTransferOffer&)>;
154
-
155
- // Invoked periodically with a progress snapshot, for both directions.
156
- using TransferProgressCallback = std::function<void(const FileTransferProgress&)>;
157
-
158
- // Invoked once when a transfer reaches a terminal state.
159
- using TransferCompletedCallback =
160
- std::function<void(const std::string& transfer_id, bool success, const std::string& error)>;
161
-
162
- // -----------------------------------------------------------------------------
163
- // FileTransferManager
164
- // -----------------------------------------------------------------------------
165
-
166
- class FileTransferManager {
167
- public:
168
- explicit FileTransferManager(RatsClient& client,
169
- const FileTransferConfig& config = FileTransferConfig());
170
- ~FileTransferManager();
171
-
172
- FileTransferManager(const FileTransferManager&) = delete;
173
- FileTransferManager& operator=(const FileTransferManager&) = delete;
174
-
175
- // --- configuration ---
176
- void set_config(const FileTransferConfig& config);
177
- FileTransferConfig get_config() const;
178
-
179
- // --- starting transfers ---
180
- // Returns a transfer id, or "" on immediate failure (e.g. missing file).
181
- std::string send_file(const std::string& peer_id, const std::string& file_path,
182
- const std::string& remote_name = "");
183
- std::string send_directory(const std::string& peer_id, const std::string& directory_path,
184
- const std::string& remote_name = "");
185
-
186
- // --- responding to an incoming offer ---
187
- // For a single file, local_path is the destination file path.
188
- // For a directory, local_path is the destination directory.
189
- bool accept(const std::string& transfer_id, const std::string& local_path);
190
- bool reject(const std::string& transfer_id, const std::string& reason = "");
191
-
192
- // --- controlling an active transfer (works from either side) ---
193
- bool pause(const std::string& transfer_id);
194
- bool resume(const std::string& transfer_id);
195
- bool cancel(const std::string& transfer_id);
196
-
197
- // --- queries ---
198
- std::shared_ptr<FileTransferProgress> get_progress(const std::string& transfer_id) const;
199
- std::vector<std::shared_ptr<FileTransferProgress>> get_active_transfers() const;
200
- nlohmann::json get_statistics() const;
201
-
202
- // --- callbacks ---
203
- void set_offer_callback(TransferOfferCallback callback);
204
- void set_progress_callback(TransferProgressCallback callback);
205
- void set_completed_callback(TransferCompletedCallback callback);
206
-
207
- // --- hooks invoked by RatsClient (not part of the application API) ---
208
- // Returns true if the binary data was a file-transfer chunk frame.
209
- bool handle_binary_data(const std::string& peer_id, const std::vector<uint8_t>& data);
210
- void on_peer_disconnected(const std::string& peer_id);
211
-
212
- // --- utilities ---
213
- // Hex-encoded SHA-256 of a file, or "" if it cannot be read.
214
- static std::string compute_file_sha256(const std::string& path);
215
-
216
- private:
217
- // --- setup / teardown ---
218
- void register_handlers();
219
- void worker_loop();
220
- void maintenance_loop();
221
-
222
- // --- sending ---
223
- std::string start_send(const std::string& peer_id, const std::shared_ptr<Transfer>& t,
224
- const std::string& name);
225
- void run_send(const std::shared_ptr<Transfer>& t);
226
- void queue_send(const std::string& transfer_id);
227
-
228
- // --- control message handlers ---
229
- void on_offer(const std::string& peer_id, const nlohmann::json& msg);
230
- void on_response(const std::string& peer_id, const nlohmann::json& msg);
231
- void on_file_end(const std::string& peer_id, const nlohmann::json& msg);
232
- void on_progress(const std::string& peer_id, const nlohmann::json& msg);
233
- void on_complete(const std::string& peer_id, const nlohmann::json& msg);
234
- void on_control(const std::string& peer_id, const nlohmann::json& msg);
235
-
236
- // --- receiving ---
237
- void on_chunk(const std::string& transfer_id, const std::string& peer_id,
238
- uint32_t file_index, uint64_t offset,
239
- const uint8_t* data, uint32_t len, uint32_t crc);
240
- void try_finalize_file(const std::shared_ptr<Transfer>& t, size_t file_index);
241
-
242
- // --- shared helpers ---
243
- std::shared_ptr<Transfer> find(const std::string& transfer_id) const;
244
- void finish(const std::shared_ptr<Transfer>& t, bool success, const std::string& error);
245
- void emit_progress(const std::shared_ptr<Transfer>& t);
246
- FileTransferProgress snapshot(const std::shared_ptr<Transfer>& t) const; // call with t->mtx held
247
- void send_control(const std::shared_ptr<Transfer>& t, const std::string& action);
248
- void cleanup_temp_files(const std::shared_ptr<Transfer>& t);
249
-
250
- RatsClient& client_;
251
-
252
- mutable std::mutex config_mutex_;
253
- FileTransferConfig config_;
254
-
255
- // All transfers, active and recently finished, keyed by transfer id.
256
- mutable std::mutex transfers_mutex_;
257
- std::unordered_map<std::string, std::shared_ptr<Transfer>> transfers_;
258
-
259
- // Outgoing transfers ready to be streamed by a worker thread.
260
- std::mutex queue_mutex_;
261
- std::condition_variable queue_cv_;
262
- std::queue<std::string> send_queue_;
263
-
264
- std::vector<std::thread> workers_;
265
- std::thread maintenance_thread_;
266
- std::mutex maintenance_mutex_;
267
- std::condition_variable maintenance_cv_;
268
- std::atomic<bool> running_{true};
269
-
270
- mutable std::mutex callbacks_mutex_;
271
- TransferOfferCallback offer_callback_;
272
- TransferProgressCallback progress_callback_;
273
- TransferCompletedCallback completed_callback_;
274
-
275
- // Aggregate statistics.
276
- mutable std::mutex stats_mutex_;
277
- uint64_t stat_bytes_sent_ = 0;
278
- uint64_t stat_bytes_received_ = 0;
279
- uint64_t stat_files_sent_ = 0;
280
- uint64_t stat_files_received_ = 0;
281
- uint64_t stat_completed_ = 0;
282
- uint64_t stat_failed_ = 0;
283
- std::chrono::steady_clock::time_point started_at_;
284
- };
285
-
286
- } // namespace librats
@@ -1,108 +0,0 @@
1
- #pragma once
2
-
3
- #include <string>
4
- #include <vector>
5
- #include <cstdint>
6
- #include <cstdio>
7
- #include "rats_export.h"
8
-
9
- namespace librats {
10
-
11
- // File/Directory existence check
12
- bool file_or_directory_exists(const char* path);
13
- RATS_API bool directory_exists(const char* path);
14
- bool file_exists(const char* path);
15
-
16
- // File creation and writing
17
- bool create_file(const char* path, const char* content);
18
- bool create_file_binary(const char* path, const void* data, size_t size);
19
- bool append_to_file(const char* path, const char* content);
20
-
21
- // File reading
22
- char* read_file_text(const char* path, size_t* size_out = nullptr);
23
- void* read_file_binary(const char* path, size_t* size_out);
24
-
25
- // Directory operations
26
- bool create_directory(const char* path);
27
- RATS_API bool create_directories(const char* path); // Create parent directories if needed
28
-
29
- // File information
30
- int64_t get_file_size(const char* path);
31
- bool is_file(const char* path);
32
- bool is_directory(const char* path);
33
-
34
- // File operations
35
- bool delete_file(const char* path);
36
- bool delete_directory(const char* path);
37
- bool copy_file(const char* src_path, const char* dest_path);
38
- bool move_file(const char* src_path, const char* dest_path);
39
-
40
- // File metadata operations
41
- uint64_t get_file_modified_time(const char* path);
42
- std::string get_file_extension(const char* path);
43
- std::string get_filename_from_path(const char* path);
44
- std::string get_parent_directory(const char* path);
45
-
46
- // File chunk operations
47
- bool write_file_chunk(const char* path, uint64_t offset, const void* data, size_t size);
48
- bool read_file_chunk(const char* path, uint64_t offset, void* buffer, size_t size);
49
-
50
- // Advanced file operations
51
- bool create_file_with_size(const char* path, uint64_t size); // Pre-allocate file space
52
- bool rename_file(const char* old_path, const char* new_path);
53
-
54
- // Directory listing
55
- struct DirectoryEntry {
56
- std::string name;
57
- std::string path;
58
- bool is_directory;
59
- uint64_t size;
60
- uint64_t modified_time;
61
- };
62
- bool list_directory(const char* path, std::vector<DirectoryEntry>& entries);
63
-
64
- // Path utilities
65
- std::string combine_paths(const std::string& base, const std::string& relative);
66
- bool validate_path(const char* path, bool check_write_access = false);
67
-
68
- // Utility functions
69
- void free_file_buffer(void* buffer); // Free memory allocated by read functions
70
- bool get_current_directory(char* buffer, size_t buffer_size);
71
- bool set_current_directory(const char* path);
72
-
73
- // C++ convenience wrappers
74
- inline bool file_or_directory_exists(const std::string& path) { return file_or_directory_exists(path.c_str()); }
75
- inline bool file_exists(const std::string& path) { return file_exists(path.c_str()); }
76
- inline bool directory_exists(const std::string& path) { return directory_exists(path.c_str()); }
77
- inline bool create_file(const std::string& path, const std::string& content) {
78
- return create_file(path.c_str(), content.c_str());
79
- }
80
- inline std::string read_file_text_cpp(const std::string& path) {
81
- size_t size;
82
- char* content = read_file_text(path.c_str(), &size);
83
- if (!content) return "";
84
- std::string result(content, size);
85
- free_file_buffer(content);
86
- return result;
87
- }
88
-
89
- // Additional C++ wrappers for new functions
90
- inline uint64_t get_file_modified_time(const std::string& path) { return get_file_modified_time(path.c_str()); }
91
- inline std::string get_file_extension(const std::string& path) { return get_file_extension(path.c_str()); }
92
- inline std::string get_filename_from_path(const std::string& path) { return get_filename_from_path(path.c_str()); }
93
- inline std::string get_parent_directory(const std::string& path) { return get_parent_directory(path.c_str()); }
94
- inline bool write_file_chunk(const std::string& path, uint64_t offset, const void* data, size_t size) {
95
- return write_file_chunk(path.c_str(), offset, data, size);
96
- }
97
- inline bool read_file_chunk(const std::string& path, uint64_t offset, void* buffer, size_t size) {
98
- return read_file_chunk(path.c_str(), offset, buffer, size);
99
- }
100
- inline bool create_file_with_size(const std::string& path, uint64_t size) { return create_file_with_size(path.c_str(), size); }
101
- inline bool rename_file(const std::string& old_path, const std::string& new_path) {
102
- return rename_file(old_path.c_str(), new_path.c_str());
103
- }
104
- inline bool validate_path(const std::string& path, bool check_write_access = false) {
105
- return validate_path(path.c_str(), check_write_access);
106
- }
107
-
108
- } // namespace librats