Back to Site
Loading...
Searching...
No Matches
file_transfer.h File Reference

Stream a file or a whole directory tree to a peer, with integrity, backpressure, pause/resume/cancel, idle-timeout and crash-safe writes. More...

#include "node/peer_network.h"
#include "peer/peer.h"
#include "core/bytes.h"
#include "peer/peer_id.h"
#include "sha256.h"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
Include dependency graph for file_transfer.h:

Go to the source code of this file.

Classes

class  librats::FileTransfer
 
struct  librats::FileTransfer::Config
 
struct  librats::FileTransfer::FileEntry
 One file inside a transfer (a single-file transfer has exactly one). More...
 
struct  librats::FileTransfer::Offer
 Delivered to the offer callback so the app can accept() or reject(). More...
 
struct  librats::FileTransfer::Progress
 Snapshot passed to the progress callback (both directions). More...
 
struct  librats::FileTransfer::Stats
 Aggregate counters. More...
 

Namespaces

namespace  librats
 

Detailed Description

Stream a file or a whole directory tree to a peer, with integrity, backpressure, pause/resume/cancel, idle-timeout and crash-safe writes.

Push model: the sender offers a file/directory; the receiver accepts (choosing a destination) or rejects; the sender streams the data; the receiver verifies a per-chunk CRC32 and a whole-file SHA-256 before moving each temp file into place. All control + data ride on MessageType::FileChunk as compact binary opcodes (no JSON), implemented on the Node/Subsystem plugin model.

Integrity: every chunk carries a CRC32; every file ends with its SHA-256. A mismatch (or a disk-write failure) fails the whole transfer — a temp file is only moved to its destination after its SHA-256 verifies.

Backpressure: the sender keeps at most window_bytes un-acked; the receiver acks cumulative progress at least twice per window, so the sender never stalls.

Safety: temp file names are derived from the transfer id (never the peer's name), and every peer-supplied relative path in a directory manifest is validated against path traversal before use.

Threading: a worker pool runs the blocking send loop (one transfer per worker); receiving + all control handling run on the reactor thread; a maintenance thread reaps idle/timed-out transfers and purges finished ones. Each transfer has its own mutex+condvar; the maps are guarded by mutex_.

Wire (MessageType::FileChunk payload, big-endian): OFFER [1][id:u64][flags:u8][total:u64][name_len:u16][name][file_count:u32] { [path_len:u16][path][size:u64] } × file_count RESPONSE [2][id:u64][accept:u8] CHUNK [3][id:u64][file_index:u32][offset:u64][crc32:u32][data] FILE_END [4][id:u64][file_index:u32][sha256:32] PROGRESS [5][id:u64][received:u64] (cumulative across all files) COMPLETE [6][id:u64][ok:u8] CANCEL [7][id:u64] PAUSE [8][id:u64] RESUME [9][id:u64]

Definition in file file_transfer.h.