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
//! SegmentMan segment lifecycle operations — checkout, cancellation, completion,
//! queries, and private helpers.
use tracing::trace;
use crate::segment::grow_segment::GrowSegment;
use crate::segment::piece::Piece;
use crate::segment::pieced_segment::PiecedSegment;
use super::SegmentMan;
use super::TrackingEntry;
use super::peer_stat::PeerStatus;
use super::segment_kind::SegmentKind;
impl SegmentMan {
// ── Segment checkout ───────────────────────────────────────────────
/// Gets the next missing segment for the given CUID.
///
/// Requests a missing piece from the piece storage and checks it out
/// as a segment. The segment is recorded in `used_segment_entries`.
///
/// # Arguments
///
/// * `cuid` — Connection ID requesting the segment
/// * `min_split_size` — Minimum split size for piece selection
///
/// # Returns
///
/// `Some(SegmentKind)` if a segment was available, `None` otherwise.
/// The caller owns the returned `SegmentKind` and must pass it back
/// to `complete_segment()` or `cancel_segment_by_segment()`.
pub fn get_segment(&mut self, cuid: u64, min_split_size: u64) -> Option<SegmentKind> {
let ignore_bf = self.ignore_bitfield.get_filter_bitfield().to_vec();
let bf_len = self.ignore_bitfield.get_bitfield_length();
let piece = self
.piece_storage
.as_mut()
.and_then(|ps| ps.get_missing_piece(min_split_size, &ignore_bf, bf_len as u64, cuid));
self.checkout_segment(cuid, piece)
}
/// Gets a segment with a specific piece index for the given CUID.
///
/// If the index is out of range, returns `None`.
pub fn get_segment_with_index(&mut self, cuid: u64, index: usize) -> Option<SegmentKind> {
if index >= self.num_pieces() {
return None;
}
let piece = self
.piece_storage
.as_mut()
.and_then(|ps| ps.get_missing_piece_by_index(index, cuid));
self.checkout_segment(cuid, piece)
}
/// Gets a clean (zero written length) segment if its owner is idle.
///
/// If the segment at `index` exists and has zero written length:
/// - If the current CUID already owns it, return it
/// - If the owner is idle, cancel the owner's segment and re-acquire
/// - Otherwise, return `None`
pub fn get_clean_segment_if_owner_is_idle(
&mut self,
cuid: u64,
index: usize,
) -> Option<SegmentKind> {
if index >= self.num_pieces() {
return None;
}
// Look for an existing entry with this index
let owner_cuid = self
.used_segment_entries
.iter()
.find(|e| e.segment_index == index)
.map(|e| e.cuid);
match owner_cuid {
Some(owner_cuid) => {
// Check if the owner is idle
let owner_is_idle = self
.get_peer_stat(owner_cuid)
.is_none_or(|ps| ps.status == PeerStatus::Idle);
if owner_cuid == cuid {
// Same CUID already owns it
return self.get_segment_with_index(cuid, index);
}
if owner_is_idle {
// Cancel the idle owner's segment and acquire it
self.cancel_segment(owner_cuid);
return self.get_segment_with_index(cuid, index);
}
None
}
None => None,
}
}
// ── Segment cancellation ───────────────────────────────────────────
/// Cancels all segments for the given CUID.
///
/// Each cancelled segment's piece is returned to the piece storage.
/// Since the caller owns the actual `SegmentKind`, we interact with
/// `PieceStorage` directly by index.
pub fn cancel_segment(&mut self, cuid: u64) {
let mut i = 0;
while i < self.used_segment_entries.len() {
if self.used_segment_entries[i].cuid == cuid {
let entry = self.used_segment_entries.remove(i);
self.cancel_segment_internal(cuid, entry.segment_index);
// Don't increment i — the next element shifted into position i
} else {
i += 1;
}
}
}
/// Cancels a specific segment for the given CUID.
///
/// Uses the caller's `SegmentKind` to accurately memoize the written
/// length for resume support, then cancels the piece in storage.
pub fn cancel_segment_by_segment(&mut self, cuid: u64, segment: &SegmentKind) {
let idx = self
.used_segment_entries
.iter()
.position(|e| e.cuid == cuid && e.segment_index == segment.index());
if let Some(i) = idx {
let entry = self.used_segment_entries.remove(i);
// Memoize the written length from the caller's segment
// (always memoize, even if 0 — C++ behavior)
let written = segment.written_length();
self.segment_written_length_memo
.insert(entry.segment_index, written);
trace!(
index = entry.segment_index,
written_length = written,
"SegmentMan: memoized written length on cancel"
);
// Mark piece as not used by segment and cancel in storage
if let Some(ref mut ps) = self.piece_storage {
if let Some(piece_mut) = segment.piece() {
let mut temp_piece = piece_mut.clone();
temp_piece.set_used_by_segment(false);
ps.cancel_piece(&mut temp_piece, cuid);
} else {
// Grow segment — just cancel by creating minimal piece
let mut temp_piece = Piece::new(entry.segment_index, 0);
temp_piece.add_user(cuid);
ps.cancel_piece(&mut temp_piece, cuid);
}
}
}
}
/// Cancels any segment with the given piece index, regardless of CUID.
///
/// Returns `true` if a segment was found and cancelled, `false` otherwise.
/// This is the aria2-next addition not present in the original aria2.
pub fn cancel_segment_by_index(&mut self, index: usize) -> bool {
let idx = self
.used_segment_entries
.iter()
.position(|e| e.segment_index == index);
if let Some(i) = idx {
let entry = self.used_segment_entries.remove(i);
self.cancel_segment_internal(entry.cuid, entry.segment_index);
true
} else {
false
}
}
/// Cancels all in-flight segments.
pub fn cancel_all_segments(&mut self) {
let entries: Vec<TrackingEntry> = self.used_segment_entries.drain(..).collect();
for entry in entries {
self.cancel_segment_internal(entry.cuid, entry.segment_index);
}
}
// ── Segment completion ─────────────────────────────────────────────
/// Marks a segment as completed.
///
/// Delegates to `PieceStorage::complete_piece()`, advertises the
/// completed piece (propagates Have messages to interested peers),
/// and removes the tracking entry. Returns `true` if the segment
/// was found and completed, `false` otherwise.
///
/// # C++ Reference
///
/// `SegmentMan::completeSegment()` calls both
/// `pieceStorage_->completePiece()` and
/// `pieceStorage_->advertisePiece(cuid, index, wallclock)`.
pub fn complete_segment(&mut self, cuid: u64, segment: &SegmentKind) -> bool {
let piece_index = segment.index();
// Complete the piece in storage
if let Some(piece) = segment.piece()
&& let Some(ref mut ps) = self.piece_storage
{
ps.complete_piece(piece);
// Advertise the completed piece so other commands send Have messages
// to their peers. C++: pieceStorage_->advertisePiece(cuid, index, wallclock)
ps.advertise_piece(cuid, piece_index);
}
// Remove the tracking entry
let idx = self
.used_segment_entries
.iter()
.position(|e| e.segment_index == piece_index);
match idx {
Some(i) => {
self.used_segment_entries.remove(i);
true
}
None => false,
}
}
// ── Segment queries ────────────────────────────────────────────────
/// Returns `true` if the piece at the given index has been downloaded.
pub fn has_segment(&self, index: usize) -> bool {
match &self.piece_storage {
Some(ps) => ps.has_piece(index),
None => false,
}
}
/// Returns all in-flight segment indices for the given CUID.
pub fn get_in_flight_segment_indices(&self, cuid: u64) -> Vec<usize> {
self.used_segment_entries
.iter()
.filter(|e| e.cuid == cuid)
.map(|e| e.segment_index)
.collect()
}
// ── Private helpers ────────────────────────────────────────────────
/// Computes the number of pieces from piece_length and total_length.
pub(crate) fn num_pieces(&self) -> usize {
if self.piece_length == 0 || self.total_length == 0 {
0
} else {
self.total_length.div_ceil(self.piece_length) as usize
}
}
/// Core logic for checking out a segment from a piece.
///
/// 1. Marks piece as `used_by_segment`
/// 2. Creates `Pieced` segment if piece length > 0, `Grow` otherwise
/// 3. Records tracking entry (cuid, index)
/// 4. Checks `segment_written_length_memo` for resume support
/// 5. Returns the `SegmentKind`
pub(crate) fn checkout_segment(
&mut self,
cuid: u64,
piece: Option<Piece>,
) -> Option<SegmentKind> {
let mut piece = piece?;
let piece_index = piece.index();
let piece_len = piece.length();
trace!(index = piece_index, cuid, "SegmentMan: attaching segment");
// TODO: Flush WrDiskCache when implemented
// Mark piece as used by segment
piece.set_used_by_segment(true);
// Create the appropriate segment type
let mut segment = if piece_len == 0 {
SegmentKind::Grow(GrowSegment::new())
} else {
SegmentKind::Pieced(Box::new(PiecedSegment::new(self.piece_length, piece)))
};
trace!(
index = segment.index(),
length = segment.length(),
segment_length = segment.segment_length(),
written_length = segment.written_length(),
"SegmentMan: segment checked out"
);
// Check written length memo for resume support (C++ behavior)
if segment.length() > 0
&& let Some(&memo_written) = self.segment_written_length_memo.get(&segment.index())
{
let current_written = segment.written_length();
trace!(
index = segment.index(),
memo_written, current_written, "SegmentMan: checking written length memo"
);
// If the memo has more written length than current, and the
// difference is less than one block, assume those bytes are
// already downloaded (matching C++ behavior)
if current_written < memo_written {
let block_length = segment.piece().map_or(0, |p| p.block_length() as u64);
if block_length > 0 && memo_written - current_written < block_length {
segment.update_written_length(memo_written - current_written);
}
}
}
// Record the tracking entry
self.used_segment_entries.push(TrackingEntry {
cuid,
segment_index: piece_index,
});
Some(segment)
}
/// Internal cancel logic — marks piece as not used by segment and
/// cancels in piece storage.
///
/// Since we don't have the caller's `SegmentKind` here (only the index),
/// we create a temporary `Piece` for the `cancel_piece` call. This works
/// correctly for the common case (one CUID per piece). For the rare
/// multi-CUID case (end-game mode), there may be minor inaccuracies
/// that self-correct on the next checkout.
pub(crate) fn cancel_segment_internal(&mut self, cuid: u64, segment_index: usize) {
trace!(index = segment_index, cuid, "SegmentMan: canceling segment");
if let Some(ref mut ps) = self.piece_storage {
// Create a minimal Piece with just the index for cancel_piece.
// We add the cuid as a user so that cancel_piece can remove it.
let mut temp_piece = Piece::new(segment_index, 0);
temp_piece.add_user(cuid);
temp_piece.set_used_by_segment(false);
ps.cancel_piece(&mut temp_piece, cuid);
}
// Memoize written length as 0 (we don't have the actual value
// from the caller's SegmentKind). This is conservative: the next
// checkout will use the Piece's completed_length() as the starting
// point, which is based on block-level tracking and is accurate.
self.segment_written_length_memo.insert(segment_index, 0);
}
}