aria2-core 0.3.1

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Choking/interest decisions, check-have, keep-alive, flooding detection,
//! peer exchange, request generation, and same-peer-ID duplicate detection
//! for `BtPeerInteractive`.

use std::time::Duration;

use aria2_protocol::bittorrent::message::types::BtMessage;

use crate::engine::bt_message_dispatcher::InactiveReason;
use crate::engine::bt_peer_connection::BtPeerConn;
use crate::engine::bt_request_factory::{BtRequestFactory, PieceBlockRequest};
use tracing::{debug, trace, warn};

use super::super::piece_provider::PieceProvider;
use super::super::types::*;
use super::BtPeerInteractive;

impl BtPeerInteractive {
    // ── Individual processing steps ─────────────────────────────────────

    /// Check for inactive interaction and return a reason to disconnect.
    ///
    /// Mirrors C++ `checkActiveInteraction()`:
    /// - 30s mutual-uninterested → disconnect
    /// - 60s total inactivity → disconnect
    /// - seeder-to-seeder → disconnect
    ///
    /// Uses the tracked `am_interested` and `peer_interested` fields
    /// instead of heuristics.
    ///
    /// Returns `Some(InactiveReason)` if the peer should be dropped.
    pub(crate) fn check_active_interaction(&mut self, conn: &BtPeerConn) -> Option<InactiveReason> {
        // Use tracked interest state rather than heuristics.
        // For we_are_seeder, check the connection's session resource.
        let we_are_seeder = conn
            .session_resource
            .as_ref()
            .is_some_and(|res| res.is_seeder());
        let peer_is_seeder = conn.seeder;

        self.active_interaction_checker.check(
            self.am_interested,
            self.peer_interested,
            we_are_seeder,
            peer_is_seeder,
        )
    }

    /// Decide whether we should choke or unchoke the peer.
    ///
    /// Mirrors C++ `decideChoking()`:
    /// - If `shouldBeChoking()` is true and we are not choking → send Choke
    /// - If `shouldBeChoking()` is false and we are choking → send Unchoke
    ///
    /// Now properly tracks `am_choking` state to only produce a decision
    /// when the state actually needs to change.
    pub(crate) fn decide_choking(&self, conn: &BtPeerConn) -> ChokingDecision {
        if let Some(ref res) = conn.session_resource {
            let should_be_choking = res.should_be_choking();
            if should_be_choking && !self.am_choking {
                // Should be choking but currently not → send Choke
                ChokingDecision::Choke
            } else if !should_be_choking && self.am_choking {
                // Should not be choking but currently are → send Unchoke
                ChokingDecision::Unchoke
            } else {
                ChokingDecision::NoChange
            }
        } else {
            // No session resource — no choking decision possible
            ChokingDecision::NoChange
        }
    }

    /// Decide whether we should express interest or lack thereof.
    ///
    /// Mirrors C++ `decideInterest()`:
    /// - If `hasMissingPiece(peer)` and not amInterested → send Interested
    /// - If `!hasMissingPiece(peer)` and amInterested → send NotInterested
    ///
    /// Uses the provided `has_missing_piece` callback to check whether
    /// the peer has pieces we need (i.e., PieceStorage::hasMissingPiece).
    pub(crate) fn decide_interest_with_callback(
        &self,
        conn: &BtPeerConn,
        has_missing_piece: &impl Fn(&BtPeerConn) -> bool,
    ) -> InterestDecision {
        let should_be_interested = has_missing_piece(conn);
        if should_be_interested && !self.am_interested {
            InterestDecision::Interested
        } else if !should_be_interested && self.am_interested {
            InterestDecision::NotInterested
        } else {
            InterestDecision::NoChange
        }
    }

    /// Legacy decide_interest using heuristic (for backward compat).
    ///
    /// Prefer `decide_interest_with_callback` for proper PieceStorage integration.
    #[allow(dead_code)]
    pub(crate) fn decide_interest(&self, conn: &BtPeerConn) -> InterestDecision {
        // Heuristic: if peer is a seeder or has a session resource,
        // we are likely interested. This matches the original simplified
        // behavior before callback integration.
        let should_be_interested = conn.session_resource.is_some();
        if should_be_interested && !self.am_interested {
            InterestDecision::Interested
        } else if !should_be_interested && self.am_interested {
            InterestDecision::NotInterested
        } else {
            InterestDecision::NoChange
        }
    }

    /// Check for new Have messages to send.
    ///
    /// Mirrors C++ `checkHave()`: queries `PieceStorage` for piece indexes
    /// that have been completed since `lastHaveId_` and returns them.
    ///
    /// In the C++ code, this calls `pieceStorage_->getAdvertisedPieceIndexes()`.
    /// Without piece storage integration, this returns an empty vector.
    #[allow(dead_code)]
    pub(crate) fn check_have(&mut self) -> Vec<u32> {
        Vec::new()
    }

    /// Check for new Have messages using a callback for piece storage.
    ///
    /// Mirrors C++ `checkHave()`: calls the `get_advertised_pieces` callback
    /// which should return piece indexes completed since `lastHaveIndex_`.
    ///
    /// After sending these Have messages, `lastHaveIndex_` is updated.
    pub(crate) fn check_have_with_callback(
        &mut self,
        get_advertised_pieces: &impl Fn() -> Vec<u32>,
    ) -> Vec<u32> {
        let pieces = get_advertised_pieces();
        if !pieces.is_empty() {
            // Update last_have_index to the maximum advertised index
            if let Some(&max_idx) = pieces.iter().max() {
                self.last_have_index = self.last_have_index.max(max_idx as u64);
            }
            trace!("checkHave: advertising {} new pieces", pieces.len());
        }
        pieces
    }

    /// Check for new Have messages and decide whether to send individual
    /// Have messages or a single Bitfield/HaveAll/HaveNone message.
    ///
    /// Mirrors C++ `DefaultBtInteractive::checkHave()`:
    /// - If `5 + bitfieldLength <= haveIndexes.size() * 9`, send a single
    ///   Bitfield message (or HaveAll/HaveNone if fast extension is enabled)
    /// - Otherwise, send individual Have messages
    ///
    /// Returns a `CheckHaveResult` indicating what type of message(s) to send.
    pub(crate) fn check_have_optimized(
        &mut self,
        get_advertised_pieces: &impl Fn(u64) -> (Vec<usize>, u64),
        bitfield_length: usize,
        fast_extension_enabled: bool,
        all_download_finished: bool,
        completed_length: u64,
    ) -> CheckHaveResult {
        let (have_indexes, new_last) = get_advertised_pieces(self.last_have_index);
        self.last_have_index = new_last;

        if have_indexes.is_empty() {
            return CheckHaveResult::None;
        }

        // C++ optimization: use bitfield message if it is equal to or less
        // than the total size of have messages.
        // Have message = 5 bytes (4 length + 1 ID) + 4 bytes (piece index) = 9 bytes each
        // Bitfield message = 5 bytes (4 length + 1 ID) + bitfieldLength bytes
        if 5 + bitfield_length <= have_indexes.len() * 9 {
            if fast_extension_enabled && all_download_finished {
                return CheckHaveResult::HaveAll;
            }
            // Only send bitfield if we have some completed data
            if completed_length > 0 {
                return CheckHaveResult::Bitfield;
            }
        }

        CheckHaveResult::HaveIndexes(have_indexes)
    }

    /// Set the last advertised have index (called by the caller after
    /// checking piece storage).
    pub fn set_last_have_index(&mut self, index: u64) {
        self.last_have_index = index;
    }

    /// Get the last advertised have index.
    pub fn last_have_index(&self) -> u64 {
        self.last_have_index
    }

    /// Check whether we should send a keep-alive message.
    ///
    /// Mirrors C++ `sendKeepAlive()`: returns true if
    /// `keepAliveTimer_.difference() >= keepAliveInterval_`.
    pub fn should_send_keepalive(&self) -> bool {
        self.keep_alive_timer.elapsed() >= Duration::from_secs(self.keep_alive_interval_secs)
    }

    /// Reset the keep-alive timer after sending a keep-alive.
    pub fn reset_keep_alive_timer(&mut self) {
        self.keep_alive_timer = std::time::Instant::now();
    }

    /// Detect message flooding from the peer.
    ///
    /// Mirrors C++ `detectMessageFlooding()`: checks if the peer has
    /// sent >= 2 choke/unchoke transitions or >= 2 keepalive messages
    /// within the flooding check interval (5 seconds).
    ///
    /// The check interval is managed by this struct's `flooding_timer`,
    /// matching the C++ design where `DefaultBtInteractive` owns the timer
    /// and `FloodingStat` only holds the counts.
    ///
    /// Returns `true` if flooding was detected.
    pub(crate) fn detect_flooding(&mut self) -> bool {
        if self.flooding_timer.elapsed() >= Duration::from_secs(FLOODING_CHECK_INTERVAL_SECS) {
            let choke_count = self.flooding_stat.choke_unchoke_count();
            let keepalive_count = self.flooding_stat.keepalive_count();
            let detected = choke_count >= 2 || keepalive_count >= 2;

            if detected {
                warn!(
                    "Flooding detected: choke_unchoke={}, keepalive={}",
                    choke_count, keepalive_count
                );
            }

            // Reset counters regardless of detection result
            self.flooding_stat.reset();
            self.flooding_timer = std::time::Instant::now();
            detected
        } else {
            false
        }
    }

    // ── Request generation (C++ addRequests / fillPiece) ────────────────

    /// Get a reference to the per-peer request factory.
    pub fn request_factory(&self) -> &BtRequestFactory {
        &self.request_factory
    }

    /// Get a mutable reference to the per-peer request factory.
    pub fn request_factory_mut(&mut self) -> &mut BtRequestFactory {
        &mut self.request_factory
    }

    /// Check whether end-game mode is active.
    pub fn is_endgame(&self) -> bool {
        self.endgame
    }

    /// Fill target pieces from piece storage, up to `max_missing_block` total
    /// missing blocks across all target pieces.
    ///
    /// Mirrors C++ `DefaultBtInteractive::fillPiece(maxMissingBlock)`:
    ///
    /// 1. If `piece_storage.has_missing_piece(peer)`:
    ///    - Count current missing blocks in the request factory
    ///    - If `numMissingBlock >= maxMissingBlock`, return (already have enough)
    ///    - Calculate `diffMissingBlock = maxMissingBlock - numMissingBlock`
    ///    - If peer is choking us AND fast extension enabled: get fast pieces
    ///    - If peer is not choking us: get regular pieces
    ///    - For each piece: `request_factory.addTargetPiece(piece)`
    ///
    /// # Arguments
    ///
    /// * `piece_storage` — The piece storage provider (trait abstraction)
    /// * `conn` — The peer connection (for peer state and fast extension check)
    /// * `cuid` — Command ID for piece storage operations
    pub(crate) fn fill_piece(
        &mut self,
        piece_storage: &mut dyn PieceProvider,
        conn: &BtPeerConn,
        cuid: u64,
    ) {
        if !piece_storage.has_missing_piece(conn) {
            return;
        }

        let num_missing_block = self.request_factory.count_missing_block();
        if num_missing_block >= self.max_outstanding_request {
            return;
        }

        let diff_missing_block = self.max_outstanding_request - num_missing_block;
        let target_indexes = self.request_factory.get_target_piece_indexes();

        let pieces = if self.peer_choking {
            // Peer is choking us — only get fast pieces if fast extension enabled.
            // C++: if(peer_->peerChoking() && peer_->isFastExtensionEnabled())
            let fast_ext = conn
                .session_resource
                .as_ref()
                .is_some_and(|r| r.is_fast_extension_enabled());
            if fast_ext {
                piece_storage.get_missing_fast_pieces(
                    diff_missing_block,
                    conn,
                    &target_indexes,
                    cuid,
                )
            } else {
                Vec::new()
            }
        } else {
            // Peer is not choking us — get regular pieces.
            // C++: else { pieceStorage_->getMissingPiece(...) }
            piece_storage.get_missing_pieces(diff_missing_block, conn, &target_indexes, cuid)
        };

        for piece in pieces {
            self.request_factory.add_target_piece(piece);
        }
    }

    /// Generate and queue piece requests, matching C++ `addRequests()`.
    ///
    /// This is the core request generation step called each iteration of
    /// the interaction loop. It:
    ///
    /// 1. Checks if end-game should be entered (no missing unused pieces
    ///    left but we still have target pieces with missing blocks).
    /// 2. Calls `fillPiece()` to ensure we have enough target pieces.
    /// 3. Calculates how many new requests to create based on the gap
    ///    between `maxOutstandingRequest` and current outstanding count.
    /// 4. Creates requests via `BtRequestFactory::create_request_messages()`
    ///    and queues them through the handler (actual sending happens in
    ///    step 12 of `do_interaction_processing()`).
    ///
    /// # Arguments
    ///
    /// * `piece_storage` — The piece storage provider (trait abstraction)
    /// * `conn` — The peer connection (for peer state checks)
    /// * `cuid` — Command ID for piece storage operations
    ///
    /// # Returns
    ///
    /// A vector of `PieceBlockRequest` descriptors for the requests that
    /// were generated. The caller can use this for tracking or logging.
    pub(crate) fn add_requests(
        &mut self,
        piece_storage: &mut dyn PieceProvider,
        conn: &BtPeerConn,
        cuid: u64,
    ) -> Vec<PieceBlockRequest> {
        // Check if we should enter end-game mode.
        // C++: if(!pieceStorage_->isEndGame() && !pieceStorage_->hasMissingUnusedPiece())
        if !self.endgame && !piece_storage.has_missing_unused_piece() {
            self.endgame = true;
            piece_storage.enter_end_game();
            debug!("Entered end-game mode");
        }

        // Fill target pieces from piece storage
        self.fill_piece(piece_storage, conn, cuid);

        // Calculate how many new requests to create
        // C++: reqNumToCreate = max(maxOutstandingRequest - countOutstandingRequest, 0)
        let outstanding = self.handler.count_outstanding_requests();
        let req_num_to_create = self.max_outstanding_request.saturating_sub(outstanding);

        let mut all_requests = Vec::new();

        if req_num_to_create > 0 {
            // Create request messages via the factory
            // C++ calls: btRequestFactory_->createRequestMessages(reqNumToCreate, isEndGame)
            let is_endgame = self.endgame;
            let requests = self.request_factory.create_request_messages(
                req_num_to_create,
                is_endgame,
                |index, block_index| self.handler.is_outstanding_request(index, block_index),
            );

            // Send each request through the handler and connection
            for req in &requests {
                // Serialize the Request message
                let serialized = aria2_protocol::bittorrent::message::serializer::serialize(
                    &BtMessage::Request {
                        request: aria2_protocol::bittorrent::message::types::PieceBlockRequest::new(
                            req.index, req.begin, req.length,
                        ),
                    },
                );

                // Queue through the handler (tracks request slots + outgoing queue)
                if let Some(_msg_bytes) = self
                    .handler
                    .send_request(req.index, req.begin, req.length, serialized)
                {
                    trace!(
                        "addRequests: queued request piece={} begin={} len={}",
                        req.index, req.begin, req.length
                    );
                }
            }

            all_requests = requests;
        }

        all_requests
    }

    /// Cancel all target pieces and remove outstanding requests.
    ///
    /// Mirrors C++ `DefaultBtInteractive::cancelAllPiece()`. Called when
    /// the peer connection is being torn down.
    ///
    /// Returns the indices of pieces that were removed (for the caller to
    /// abort outstanding requests in the dispatcher).
    pub fn cancel_all_piece(&mut self) -> Vec<u32> {
        let removed = self.request_factory.remove_all_target_pieces();
        removed.iter().map(|p| p.index() as u32).collect()
    }

    /// Remove completed pieces from the request factory.
    ///
    /// Mirrors C++ `btRequestFactory_->removeCompletedPiece()` called
    /// in `doInteractionProcessing()` step 9.
    ///
    /// Returns the indices of removed completed pieces (for the caller to
    /// abort outstanding requests in the dispatcher).
    pub fn remove_completed_piece(&mut self) -> Vec<u32> {
        self.request_factory.remove_completed_piece()
    }

    // ── Message-received helpers ────────────────────────────────────────

    /// Process a received message and update internal state.
    ///
    /// This method updates flooding stats and inactive timer based on
    /// the message type, matching the C++ `receiveMessages()` switch.
    ///
    /// # Arguments
    /// * `msg_id` — The BT message type ID (0=Choke, 1=Unchoke, etc.)
    /// * `was_peer_choking` — Whether the peer was choking us before
    ///   this message (needed to detect choke/unchoke transitions)
    pub fn on_message_received(&mut self, msg_id: u8, was_peer_choking: bool) {
        match msg_id {
            // Choke (ID=0)
            0 => {
                if !was_peer_choking {
                    self.flooding_stat.inc_choke_unchoke_count();
                }
            }
            // Unchoke (ID=1)
            1 => {
                if was_peer_choking {
                    self.flooding_stat.inc_choke_unchoke_count();
                }
            }
            // Request (ID=6) or Piece (ID=7) — data exchange
            6 | 7 => {
                self.active_interaction_checker.record_data_exchange();
            }
            // KeepAlive (ID implied by zero-length)
            _ => {
                // KeepAlive messages increment flooding counter
                // In C++, this is handled by matching BtKeepAliveMessage::ID
                // We treat any unrecognized as potential keepalive for safety
            }
        }
    }

    /// Process a keepalive message for flooding detection.
    ///
    /// Call this when a KeepAlive message is received.
    pub fn on_keepalive_received(&mut self) {
        self.flooding_stat.inc_keepalive_count();
    }

    /// Dynamically scale `max_outstanding_request` based on request
    /// fulfillment rate.
    ///
    /// Mirrors the C++ logic at the end of `receiveMessages()`:
    /// if not in end-game and we lost >= 1/4 of outstanding requests,
    /// double `maxOutstandingRequest_` (up to `UB_MAX_OUTSTANDING_REQUEST`).
    pub fn scale_max_outstanding_request(
        &mut self,
        old_outstanding: usize,
        new_outstanding: usize,
        is_end_game: bool,
    ) {
        if !is_end_game
            && old_outstanding > new_outstanding
            && (old_outstanding - new_outstanding) * 4 >= self.max_outstanding_request
        {
            self.max_outstanding_request =
                (self.max_outstanding_request * 2).min(UB_MAX_OUTSTANDING_REQUEST);
            debug!(
                "Scaled max_outstanding_request to {}",
                self.max_outstanding_request
            );
        }
    }

    // ── Handshake duplicate detection ────────────────────────────────────

    /// Check whether a received peer ID indicates a self-connection or a
    /// duplicate peer, matching C++ `DefaultBtInteractive::receiveHandshake()`.
    ///
    /// # C++ reference
    ///
    /// ```cpp
    /// if (memcmp(message->getPeerId(), bittorrent::getStaticPeerId(),
    ///            PEER_ID_LENGTH) == 0) {
    ///   throw DL_ABORT_EX("Drop connection from the same Peer ID");
    /// }
    /// for (auto& peer : peerStorage_->getUsedPeers()) {
    ///   if (peer->isActive() &&
    ///       memcmp(peer->getPeerId(), message->getPeerId(), PEER_ID_LENGTH) == 0) {
    ///     throw DL_ABORT_EX("Same Peer ID has been already seen.");
    ///   }
    /// }
    /// ```
    ///
    /// # Arguments
    ///
    /// * `received_peer_id` — The 20-byte peer ID from the handshake message.
    /// * `our_peer_id` — Our own static peer ID (generated once at startup,
    ///   equivalent to C++ `bittorrent::getStaticPeerId()`).
    /// * `connected_peer_ids` — Iterator of peer IDs from currently active
    ///   connections (equivalent to C++ `peerStorage_->getUsedPeers()`
    ///   filtered by `isActive()`).
    ///
    /// # Returns
    ///
    /// * [`PeerIdCheckResult::SelfConnection`] — Remote peer ID matches our
    ///   own; drop the connection.
    /// * [`PeerIdCheckResult::DuplicatePeer`] — Remote peer ID matches
    ///   another active peer; drop the connection.
    /// * [`PeerIdCheckResult::Ok`] — Peer ID is unique; proceed normally.
    pub fn check_duplicate_peer_id<'a>(
        received_peer_id: &[u8; 20],
        our_peer_id: &[u8; 20],
        connected_peer_ids: impl IntoIterator<Item = &'a [u8; 20]>,
    ) -> PeerIdCheckResult {
        // Self-connection check: does the remote peer ID match our own?
        // C++: memcmp(message->getPeerId(), bittorrent::getStaticPeerId(), PEER_ID_LENGTH) == 0
        if received_peer_id == our_peer_id {
            return PeerIdCheckResult::SelfConnection;
        }

        // Duplicate check: is this peer ID already present in an active connection?
        // C++: iterating peerStorage_->getUsedPeers() for matching peer_id
        for id in connected_peer_ids {
            if received_peer_id == id {
                return PeerIdCheckResult::DuplicatePeer;
            }
        }

        PeerIdCheckResult::Ok
    }
}