98 enum class Status { Pending, Active, Paused, Completed, Failed, Cancelled };
153 std::function<void(
const PeerId& peer, uint64_t
id,
bool success,
const std::string& path)>;
170 void accept(
const PeerId& from, uint64_t
id,
const std::string& dest_path);
196 const std::string& partial_path);
220 using clock = std::chrono::steady_clock;
221 clock::time_point
start{};
222 clock::time_point mark{};
223 uint64_t mark_bytes = 0;
224 double rate_bps = 0.0;
226 void sample(uint64_t bytes, clock::time_point now) {
227 constexpr int64_t kMinMs = 250, kMaxMs = 2000;
228 constexpr double kAlpha = 0.4;
229 if (start == clock::time_point{}) { start = mark = now; mark_bytes = bytes;
return; }
230 const int64_t dt = std::chrono::duration_cast<std::chrono::milliseconds>(now - mark).count();
231 if (dt < kMinMs)
return;
232 if (dt > kMaxMs) { mark = now; mark_bytes = bytes;
return; }
233 const double inst =
static_cast<double>(bytes - mark_bytes) * 1000.0 /
static_cast<double>(dt);
234 rate_bps = rate_bps <= 0.0 ? inst : kAlpha * inst + (1.0 - kAlpha) * rate_bps;
235 mark = now; mark_bytes = bytes;
238 void fill(Progress& p, uint64_t bytes, uint64_t total, clock::time_point now)
const {
239 if (start == clock::time_point{})
return;
240 const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
242 p.transfer_rate_bps = rate_bps;
244 p.average_rate_bps =
static_cast<double>(bytes) * 1000.0 /
static_cast<double>(ms.count());
245 if (rate_bps > 1.0 && total > bytes)
246 p.estimated_time_remaining = std::chrono::milliseconds(
247 static_cast<int64_t
>(
static_cast<double>(total - bytes) / rate_bps * 1000.0));
258 bool is_directory =
false;
259 std::vector<FileEntry> files;
260 std::vector<std::string> sources;
261 uint64_t total_bytes = 0;
264 std::condition_variable cv;
266 uint64_t cur_offset = 0;
269 size_t hash_file = SIZE_MAX;
270 uint64_t bytes_done = 0;
272 uint32_t files_done = 0;
273 Status status = Status::Pending;
274 bool worker_active =
false;
277 bool finished =
false;
278 rats_sha256_context_t hash{};
279 std::chrono::steady_clock::time_point last_activity{};
283 struct IncomingFile {
284 std::string relative_path;
286 std::string final_path;
287 std::string temp_path;
288 uint64_t enqueued = 0;
289 uint64_t received = 0;
290 uint64_t resume_offset = 0;
291 bool keep_partial =
false;
292 bool temp_created =
false;
293 bool finalized =
false;
303 bool is_file_end =
false;
304 uint8_t sha[RATS_SHA256_HASH_SIZE]{};
311 bool is_directory =
false;
312 std::string dest_root;
313 std::vector<IncomingFile> files;
316 size_t recv_file = 0;
317 uint64_t bytes_done = 0;
318 uint64_t last_ack = 0;
319 uint32_t files_done = 0;
320 Status status = Status::Pending;
321 bool finished =
false;
322 std::chrono::steady_clock::time_point last_activity{};
326 std::queue<WriteJob> wq;
327 uint64_t queued_bytes = 0;
328 bool scheduled =
false;
330 size_t out_idx = SIZE_MAX;
331 int hashing_file = -1;
332 rats_sha256_context_t hash{};
338 void on_message(
const Peer& peer, ByteView payload);
339 void handle_offer(
const PeerId& from, uint64_t
id,
bool is_dir, uint64_t total,
340 std::string name, std::vector<FileEntry> files);
341 void handle_chunk(
const PeerId& from, uint64_t
id, uint32_t fidx, uint64_t offset,
343 void handle_file_end(
const PeerId& from, uint64_t
id, uint32_t fidx,
const uint8_t* sha);
346 uint64_t start_send(std::shared_ptr<Outgoing> t);
347 void queue_send(uint64_t
id);
349 void run_send(
const std::shared_ptr<Outgoing>& t);
356 void schedule_writer(
const std::shared_ptr<Incoming>& t);
357 void disk_worker_loop();
361 bool begin_hash(
const std::shared_ptr<Incoming>& t, uint32_t fidx,
const std::string& path, uint64_t prefix);
362 void drain_writes(
const std::shared_ptr<Incoming>& t);
363 void process_data(
const std::shared_ptr<Incoming>& t, WriteJob& job);
364 void process_file_end(
const std::shared_ptr<Incoming>& t, WriteJob& job);
368 bool accept_impl(
const PeerId& from, uint64_t
id,
const std::string& dest_path,
369 const std::string& partial_path);
372 void maintenance_loop();
373 void finish_outgoing(
const std::shared_ptr<Outgoing>& t,
bool success);
374 void finish_incoming(
const std::shared_ptr<Incoming>& t,
bool success,
const std::string& error);
375 void emit_progress(
const std::shared_ptr<Outgoing>& t);
376 void emit_progress(
const std::shared_ptr<Incoming>& t);
383 std::shared_ptr<Outgoing> find_outgoing(
const PeerId& peer, uint64_t
id)
const;
386 std::shared_ptr<Outgoing> find_own_outgoing(uint64_t
id)
const;
387 std::shared_ptr<Incoming> find_incoming(
const PeerId& peer, uint64_t
id)
const;
389 void send_to(
const PeerId& peer,
const Bytes& msg) {
390 if (network_) network_->send(peer, MessageType::FileChunk, ByteView(msg));
392 void send_simple(
const PeerId& peer, uint8_t op, uint64_t
id);
393 void send_complete(
const PeerId& peer, uint64_t
id,
bool ok);
395 PeerNetwork* network_ =
nullptr;
397 std::atomic<uint64_t> next_id_{1};
398 std::atomic<bool> running_{
false};
400 OfferHandler offer_handler_;
401 ProgressHandler progress_handler_;
402 CompleteHandler complete_handler_;
404 mutable std::mutex mutex_;
405 std::unordered_map<uint64_t, std::shared_ptr<Outgoing>> outgoing_;
406 std::unordered_map<PeerId, std::unordered_map<uint64_t, std::shared_ptr<Incoming>>,
407 PeerId::Hash> incoming_;
410 std::vector<std::thread> workers_;
411 std::mutex queue_mutex_;
412 std::condition_variable queue_cv_;
413 std::queue<uint64_t> send_queue_;
418 std::vector<std::thread> disk_workers_;
419 std::mutex disk_mutex_;
420 std::condition_variable disk_cv_;
421 std::queue<std::shared_ptr<Incoming>> disk_ready_;
424 std::thread maintenance_thread_;
425 std::mutex maintenance_mutex_;
426 std::condition_variable maintenance_cv_;
428 mutable std::mutex stats_mutex_;
One file inside a transfer (a single-file transfer has exactly one).