asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! ATP Stream Management and Scheduling
//!
//! Implements reliable QUIC streams with flow control, reassembly, reset handling,
//! and ATP-specific priority classes for control, data, repair, proof, and diagnostics.

pub mod flow_control;
pub mod reassembly;
pub mod scheduler;
pub mod stream;

pub use flow_control::*;
pub use reassembly::*;
pub use scheduler::*;
pub use stream::*;

use crate::bytes::Bytes;
use crate::cx::Cx;
use crate::net::atp::protocol::quic_frames::QuicFrame;
use crate::net::atp::protocol::varint::VarInt;
use crate::types::outcome::Outcome;
use std::collections::HashMap;

/// Stream priority classes for ATP traffic
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum StreamPriority {
    /// ATP control frames (highest priority)
    Control = 0,
    /// Proof bundles and verification data
    Proof = 1,
    /// Primary data transfer
    #[default]
    Data = 2,
    /// Repair symbols and recovery data
    Repair = 3,
    /// Diagnostics and logging (lowest priority)
    Diagnostics = 4,
}

/// Stream identifier with direction
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StreamId {
    pub id: u64,
}

impl StreamId {
    /// Create a new stream ID
    pub fn new(id: u64) -> Self {
        Self { id }
    }

    /// Check if this is a bidirectional stream
    pub fn is_bidirectional(&self) -> bool {
        (self.id & 0x02) == 0
    }

    /// Check if this is a client-initiated stream
    pub fn is_client_initiated(&self) -> bool {
        (self.id & 0x01) == 0
    }

    /// Check if this is a unidirectional stream
    pub fn is_unidirectional(&self) -> bool {
        !self.is_bidirectional()
    }

    /// Check if this is a server-initiated stream
    pub fn is_server_initiated(&self) -> bool {
        !self.is_client_initiated()
    }
}

/// Stream reset codes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StreamResetCode {
    /// Application requested close
    ApplicationClose = 0,
    /// Internal error
    InternalError = 1,
    /// Flow control violation
    FlowControlViolation = 2,
    /// Final size mismatch
    FinalSizeMismatch = 3,
    /// Connection close
    ConnectionClose = 4,
}

/// Stop sending codes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StopSendingCode {
    /// Application requested stop
    ApplicationStop = 0,
    /// Internal error
    InternalError = 1,
    /// Flow control violation
    FlowControlViolation = 2,
    /// Connection close
    ConnectionClose = 3,
}

/// Stream errors
#[derive(Debug, Clone)]
pub enum StreamError {
    /// Stream not found
    StreamNotFound { stream_id: StreamId },
    /// Stream already exists
    StreamAlreadyExists { stream_id: StreamId },
    /// Stream is closed
    StreamClosed {
        stream_id: StreamId,
        reset_code: Option<StreamResetCode>,
    },
    /// Flow control violation
    FlowControlViolation {
        stream_id: StreamId,
        limit: u64,
        attempted: u64,
    },
    /// Final size mismatch
    FinalSizeMismatch {
        stream_id: StreamId,
        expected: u64,
        actual: u64,
    },
    /// Invalid stream state
    InvalidState { stream_id: StreamId, state: String },
    /// Connection error
    ConnectionError { reason: String },
}

/// Stream manager coordinates all streams for a connection
pub struct StreamManager {
    streams: HashMap<StreamId, AtpStream>,
    scheduler: StreamScheduler,
    next_client_bidi: u64,
    next_client_uni: u64,
    next_server_bidi: u64,
    next_server_uni: u64,
    is_server: bool,
}

impl StreamManager {
    /// Create a new stream manager
    pub fn new(is_server: bool) -> Self {
        Self {
            streams: HashMap::new(),
            scheduler: StreamScheduler::new(),
            next_client_bidi: 0,
            next_client_uni: 2,
            next_server_bidi: 1,
            next_server_uni: 3,
            is_server,
        }
    }

    /// Open a new outgoing stream
    pub fn open_stream(
        &mut self,
        cx: &Cx,
        is_bidirectional: bool,
        priority: StreamPriority,
    ) -> Outcome<StreamId, StreamError> {
        let stream_id = if self.is_server {
            if is_bidirectional {
                let id = StreamId::new(self.next_server_bidi);
                self.next_server_bidi += 4;
                id
            } else {
                let id = StreamId::new(self.next_server_uni);
                self.next_server_uni += 4;
                id
            }
        } else {
            if is_bidirectional {
                let id = StreamId::new(self.next_client_bidi);
                self.next_client_bidi += 4;
                id
            } else {
                let id = StreamId::new(self.next_client_uni);
                self.next_client_uni += 4;
                id
            }
        };

        if self.streams.contains_key(&stream_id) {
            return Outcome::err(StreamError::StreamAlreadyExists { stream_id });
        }

        let stream = AtpStream::new(stream_id, is_bidirectional, priority, true);
        self.streams.insert(stream_id, stream);
        self.scheduler.register_stream(stream_id, priority);

        cx.trace(&format!(
            "stream_opened stream_id={:?} priority={:?}",
            stream_id, priority
        ));

        Outcome::ok(stream_id)
    }

    /// Accept an incoming stream
    pub fn accept_stream(
        &mut self,
        cx: &Cx,
        stream_id: StreamId,
        priority: StreamPriority,
    ) -> Outcome<(), StreamError> {
        if self.streams.contains_key(&stream_id) {
            return Outcome::err(StreamError::StreamAlreadyExists { stream_id });
        }

        let is_bidirectional = stream_id.is_bidirectional();
        let stream = AtpStream::new(stream_id, is_bidirectional, priority, false);
        self.streams.insert(stream_id, stream);
        self.scheduler.register_stream(stream_id, priority);

        cx.trace(&format!(
            "stream_accepted stream_id={:?} priority={:?}",
            stream_id, priority
        ));

        Outcome::ok(())
    }

    /// Get a mutable reference to a stream
    pub fn get_stream_mut(&mut self, stream_id: StreamId) -> Option<&mut AtpStream> {
        self.streams.get_mut(&stream_id)
    }

    /// Get a reference to a stream
    pub fn get_stream(&self, stream_id: StreamId) -> Option<&AtpStream> {
        self.streams.get(&stream_id)
    }

    /// Queue outbound bytes on a managed stream and update scheduler readiness.
    pub fn queue_stream_data(
        &mut self,
        cx: &Cx,
        stream_id: StreamId,
        data: Bytes,
        fin: bool,
    ) -> Outcome<(), StreamError> {
        let has_send_data = {
            let Some(stream) = self.streams.get_mut(&stream_id) else {
                return Outcome::err(StreamError::StreamNotFound { stream_id });
            };

            match stream.queue_send(cx, data, fin) {
                Outcome::Ok(()) => stream.has_send_data(),
                Outcome::Err(error) => return Outcome::err(error),
                Outcome::Cancelled(reason) => return Outcome::cancelled(reason),
                Outcome::Panicked(payload) => return Outcome::panicked(payload),
            }
        };

        if has_send_data {
            self.scheduler.mark_ready(stream_id);
        } else {
            self.scheduler.mark_blocked(stream_id);
        }

        Outcome::ok(())
    }

    /// Drain schedulable stream payload into QUIC STREAM frames.
    pub fn drain_quic_stream_frames(
        &mut self,
        max_frames: usize,
        max_frame_payload: usize,
    ) -> Outcome<Vec<QuicFrame>, StreamError> {
        let mut frames = Vec::new();
        if max_frames == 0 || max_frame_payload == 0 {
            return Outcome::ok(frames);
        }

        while frames.len() < max_frames {
            let Some(stream_id) = self.scheduler.next_stream() else {
                break;
            };

            let Some((offset, data, fin, has_more_send_data)) =
                self.streams.get_mut(&stream_id).and_then(|stream| {
                    stream
                        .get_send_data(max_frame_payload as u64)
                        .map(|(offset, data, fin)| (offset, data, fin, stream.has_send_data()))
                })
            else {
                self.scheduler.mark_blocked(stream_id);
                continue;
            };

            if data.is_empty() && !fin {
                if has_more_send_data {
                    self.scheduler.mark_ready(stream_id);
                } else {
                    self.scheduler.mark_blocked(stream_id);
                }
                continue;
            }

            let frame = match quic_stream_frame(stream_id, offset, data, fin) {
                Outcome::Ok(frame) => frame,
                Outcome::Err(error) => return Outcome::err(error),
                Outcome::Cancelled(reason) => return Outcome::cancelled(reason),
                Outcome::Panicked(payload) => return Outcome::panicked(payload),
            };
            frames.push(frame);

            if has_more_send_data {
                self.scheduler.mark_ready(stream_id);
            } else {
                self.scheduler.mark_blocked(stream_id);
            }
        }

        Outcome::ok(frames)
    }

    /// Close a stream gracefully
    pub fn close_stream(&mut self, cx: &Cx, stream_id: StreamId) -> Outcome<(), StreamError> {
        if let Some(stream) = self.streams.get_mut(&stream_id) {
            stream.close();
            if stream.has_send_data() {
                self.scheduler.mark_ready(stream_id);
            } else if stream.is_closed() {
                self.scheduler.unregister_stream(stream_id);
            } else {
                self.scheduler.mark_blocked(stream_id);
            }
            cx.trace(&format!("stream_closed stream_id={:?}", stream_id));
            Outcome::ok(())
        } else {
            Outcome::err(StreamError::StreamNotFound { stream_id })
        }
    }

    /// Reset a stream with error code
    pub fn reset_stream(
        &mut self,
        cx: &Cx,
        stream_id: StreamId,
        reset_code: StreamResetCode,
    ) -> Outcome<(), StreamError> {
        if let Some(stream) = self.streams.get_mut(&stream_id) {
            stream.reset(reset_code);
            self.scheduler.unregister_stream(stream_id);
            cx.trace(&format!(
                "stream_reset stream_id={:?} code={:?}",
                stream_id, reset_code
            ));
            Outcome::ok(())
        } else {
            Outcome::err(StreamError::StreamNotFound { stream_id })
        }
    }

    /// Send stop_sending to peer
    pub fn stop_sending(
        &mut self,
        cx: &Cx,
        stream_id: StreamId,
        stop_code: StopSendingCode,
    ) -> Outcome<(), StreamError> {
        if let Some(stream) = self.streams.get_mut(&stream_id) {
            stream.stop_sending(stop_code);
            cx.trace(&format!(
                "stop_sending stream_id={:?} code={:?}",
                stream_id, stop_code
            ));
            Outcome::ok(())
        } else {
            Outcome::err(StreamError::StreamNotFound { stream_id })
        }
    }

    /// Get the next stream to schedule for sending
    pub fn next_scheduled_stream(&mut self) -> Option<StreamId> {
        self.scheduler.next_stream()
    }

    /// Mark a stream eligible for scheduling after flow-control or drain progress.
    pub fn mark_stream_ready(&mut self, stream_id: StreamId) -> Outcome<(), StreamError> {
        if self.streams.contains_key(&stream_id) {
            self.scheduler.mark_ready(stream_id);
            Outcome::ok(())
        } else {
            Outcome::err(StreamError::StreamNotFound { stream_id })
        }
    }

    /// Mark a stream ineligible for scheduling while blocked by flow control or drain state.
    pub fn mark_stream_blocked(&mut self, stream_id: StreamId) -> Outcome<(), StreamError> {
        if self.streams.contains_key(&stream_id) {
            self.scheduler.mark_blocked(stream_id);
            Outcome::ok(())
        } else {
            Outcome::err(StreamError::StreamNotFound { stream_id })
        }
    }

    /// Remove closed streams
    pub fn cleanup_closed_streams(&mut self) {
        self.streams.retain(|stream_id, stream| {
            if stream.is_closed() {
                self.scheduler.unregister_stream(*stream_id);
                false
            } else {
                true
            }
        });
    }

    /// Check if all streams are closed (for connection drain)
    pub fn all_streams_closed(&self) -> bool {
        self.streams.values().all(|stream| stream.is_closed())
    }
}

fn quic_stream_frame(
    stream_id: StreamId,
    offset: u64,
    data: Bytes,
    fin: bool,
) -> Outcome<QuicFrame, StreamError> {
    let stream_id_varint = match VarInt::try_from(stream_id.id) {
        Ok(stream_id) => stream_id,
        Err(error) => {
            return Outcome::err(StreamError::InvalidState {
                stream_id,
                state: format!("stream id cannot be encoded as QUIC varint: {error}"),
            });
        }
    };
    let offset = if offset == 0 {
        None
    } else {
        match VarInt::try_from(offset) {
            Ok(offset) => Some(offset),
            Err(error) => {
                return Outcome::err(StreamError::InvalidState {
                    stream_id,
                    state: format!("stream offset cannot be encoded as QUIC varint: {error}"),
                });
            }
        }
    };

    Outcome::ok(QuicFrame::Stream {
        stream_id: stream_id_varint,
        offset,
        data,
        fin,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cx::cap;
    use crate::net::atp::protocol::packet_assembly::{
        PacketAssembler, PacketConstraints, PacketNumberSpace,
    };

    fn test_cx() -> Cx<cap::All> {
        Cx::for_testing()
    }

    fn assert_stream_id(outcome: Outcome<StreamId, StreamError>, context: &str) -> StreamId {
        match outcome {
            Outcome::Ok(stream_id) => stream_id,
            other => panic!("{context}: expected stream id, got {other:?}"),
        }
    }

    #[test]
    fn close_stream_keeps_fin_schedulable_when_previously_blocked() {
        let cx = test_cx();
        let mut manager = StreamManager::new(false);
        let stream_id = assert_stream_id(
            manager.open_stream(&cx, true, StreamPriority::Data),
            "open client bidirectional stream",
        );

        assert!(manager.mark_stream_blocked(stream_id).is_ok());
        assert!(manager.next_scheduled_stream().is_none());

        assert!(manager.close_stream(&cx, stream_id).is_ok());
        assert_eq!(manager.next_scheduled_stream(), Some(stream_id));
    }

    #[test]
    fn close_stream_does_not_reschedule_after_fin_is_drained() {
        let cx = test_cx();
        let mut manager = StreamManager::new(false);
        let stream_id = assert_stream_id(
            manager.open_stream(&cx, true, StreamPriority::Data),
            "open client bidirectional stream",
        );

        assert!(manager.close_stream(&cx, stream_id).is_ok());
        assert_eq!(manager.next_scheduled_stream(), Some(stream_id));

        let Some(stream) = manager.get_stream_mut(stream_id) else {
            panic!("test stream should remain registered until both halves close");
        };
        let Some((offset, data, fin)) = stream.get_send_data(1024) else {
            panic!("close should produce a FIN-only frame");
        };
        assert_eq!(offset, 0);
        assert!(data.is_empty());
        assert!(fin);

        assert!(manager.close_stream(&cx, stream_id).is_ok());
        assert!(manager.next_scheduled_stream().is_none());
    }

    #[test]
    fn queued_stream_bytes_drain_into_quic_frames_and_packet_bytes() {
        let cx = test_cx();
        let mut manager = StreamManager::new(false);
        let data_stream = assert_stream_id(
            manager.open_stream(&cx, true, StreamPriority::Data),
            "open data stream",
        );
        let control_stream = assert_stream_id(
            manager.open_stream(&cx, true, StreamPriority::Control),
            "open control stream",
        );

        assert!(
            manager
                .queue_stream_data(&cx, data_stream, Bytes::from_static(b"abcdef"), true)
                .is_ok()
        );
        assert!(
            manager
                .queue_stream_data(&cx, control_stream, Bytes::from_static(b"go"), false)
                .is_ok()
        );

        let frames = match manager.drain_quic_stream_frames(3, 3) {
            Outcome::Ok(frames) => frames,
            other => panic!("stream frames should drain cleanly, got {other:?}"),
        };

        assert_eq!(frames.len(), 3);
        assert!(matches!(
            &frames[0],
            QuicFrame::Stream {
                stream_id,
                offset: None,
                data,
                fin: false
            } if stream_id.value() == control_stream.id && data.as_ref() == b"go"
        ));
        assert!(matches!(
            &frames[1],
            QuicFrame::Stream {
                stream_id,
                offset: None,
                data,
                fin: false
            } if stream_id.value() == data_stream.id && data.as_ref() == b"abc"
        ));
        assert!(matches!(
            &frames[2],
            QuicFrame::Stream {
                stream_id,
                offset: Some(offset),
                data,
                fin: true
            } if stream_id.value() == data_stream.id
                && offset.value() == 3
                && data.as_ref() == b"def"
        ));
        assert!(manager.next_scheduled_stream().is_none());

        let mut assembler = PacketAssembler::new(
            PacketConstraints::new()
                .with_packet_number_space(PacketNumberSpace::ApplicationData)
                .without_anti_amplification(),
        );
        for frame in frames {
            assembler.add_quic_frame(frame);
        }

        let packet = assembler
            .assemble_packet()
            .expect("packet assembly should not fail")
            .expect("queued stream frames should produce one packet");
        assert_eq!(packet.frames.len(), 3);
        assert!(packet.ack_eliciting);
        assert!(packet.retransmittable);

        let encoded = packet.encode_frames().expect("encode assembled frames");
        assert!(
            encoded.len() > b"abcdefgo".len(),
            "encoded packet payload should include QUIC frame metadata"
        );
    }

    #[test]
    fn empty_non_fin_send_does_not_emit_stream_frame() {
        let cx = test_cx();
        let mut manager = StreamManager::new(false);
        let stream_id = assert_stream_id(
            manager.open_stream(&cx, true, StreamPriority::Data),
            "open stream",
        );

        assert!(
            manager
                .queue_stream_data(&cx, stream_id, Bytes::new(), false)
                .is_ok()
        );

        let frames = match manager.drain_quic_stream_frames(2, 16) {
            Outcome::Ok(frames) => frames,
            other => panic!("empty non-FIN drain should not fail, got {other:?}"),
        };

        assert!(frames.is_empty());
        assert!(manager.next_scheduled_stream().is_none());
    }
}