1use crate::Reason;
2
3use super::*;
4
5use std::task::{Context, Waker};
6use std::time::Instant;
7
8#[derive(Debug)]
20pub(super) struct Stream {
21 pub id: StreamId,
23
24 pub state: State,
26
27 pub is_counted: bool,
30
31 pub ref_count: usize,
33
34 pub next_pending_send: Option<store::Key>,
37
38 pub is_pending_send: bool,
40
41 pub send_flow: FlowControl,
43
44 pub requested_send_capacity: WindowSize,
46
47 pub buffered_send_data: usize,
50
51 send_task: Option<Waker>,
53
54 pub pending_send: buffer::Deque,
56
57 pub next_pending_send_capacity: Option<store::Key>,
60
61 pub is_pending_send_capacity: bool,
63
64 pub send_capacity_inc: bool,
66
67 pub next_open: Option<store::Key>,
69
70 pub is_pending_open: bool,
72
73 pub is_pending_push: bool,
75
76 pub next_pending_accept: Option<store::Key>,
79
80 pub is_pending_accept: bool,
82
83 pub recv_flow: FlowControl,
85
86 pub in_flight_recv_data: WindowSize,
87
88 pub next_window_update: Option<store::Key>,
90
91 pub is_pending_window_update: bool,
93
94 pub reset_at: Option<Instant>,
96
97 pub next_reset_expire: Option<store::Key>,
99
100 pub pending_recv: buffer::Deque,
102
103 pub is_recv: bool,
105
106 pub recv_task: Option<Waker>,
108
109 pub push_task: Option<Waker>,
111
112 pub pending_push_promises: store::Queue<NextAccept>,
114
115 pub content_length: ContentLength,
117}
118
119#[derive(Debug)]
121pub enum ContentLength {
122 Omitted,
123 Head,
124 Remaining(u64),
125}
126
127#[derive(Debug)]
128pub(super) struct NextAccept;
129
130#[derive(Debug)]
131pub(super) struct NextSend;
132
133#[derive(Debug)]
134pub(super) struct NextSendCapacity;
135
136#[derive(Debug)]
137pub(super) struct NextWindowUpdate;
138
139#[derive(Debug)]
140pub(super) struct NextOpen;
141
142#[derive(Debug)]
143pub(super) struct NextResetExpire;
144
145impl Stream {
146 pub fn new(id: StreamId, init_send_window: WindowSize, init_recv_window: WindowSize) -> Stream {
147 let mut send_flow = FlowControl::new();
148 let mut recv_flow = FlowControl::new();
149
150 recv_flow
151 .inc_window(init_recv_window)
152 .expect("invalid initial receive window");
153 let _res = recv_flow.assign_capacity(init_recv_window);
155 debug_assert!(_res.is_ok());
156
157 send_flow
158 .inc_window(init_send_window)
159 .expect("invalid initial send window size");
160
161 Stream {
162 id,
163 state: State::default(),
164 ref_count: 0,
165 is_counted: false,
166
167 next_pending_send: None,
169 is_pending_send: false,
170 send_flow,
171 requested_send_capacity: 0,
172 buffered_send_data: 0,
173 send_task: None,
174 pending_send: buffer::Deque::new(),
175 is_pending_send_capacity: false,
176 next_pending_send_capacity: None,
177 send_capacity_inc: false,
178 is_pending_open: false,
179 next_open: None,
180 is_pending_push: false,
181
182 next_pending_accept: None,
184 is_pending_accept: false,
185 recv_flow,
186 in_flight_recv_data: 0,
187 next_window_update: None,
188 is_pending_window_update: false,
189 reset_at: None,
190 next_reset_expire: None,
191 pending_recv: buffer::Deque::new(),
192 is_recv: true,
193 recv_task: None,
194 push_task: None,
195 pending_push_promises: store::Queue::new(),
196 content_length: ContentLength::Omitted,
197 }
198 }
199
200 pub fn ref_inc(&mut self) {
202 assert!(self.ref_count < usize::MAX);
203 self.ref_count += 1;
204 }
205
206 pub fn ref_dec(&mut self) {
208 assert!(self.ref_count > 0);
209 self.ref_count -= 1;
210 }
211
212 pub fn is_pending_reset_expiration(&self) -> bool {
215 self.reset_at.is_some()
216 }
217
218 pub fn is_send_ready(&self) -> bool {
220 !self.is_pending_open && !self.is_pending_push
236 }
237
238 pub fn is_closed(&self) -> bool {
240 self.state.is_closed() &&
242 self.pending_send.is_empty() &&
245 self.buffered_send_data == 0
251 }
252
253 pub fn is_released(&self) -> bool {
255 self.is_closed() &&
257 self.ref_count == 0 &&
259 !self.is_pending_send && !self.is_pending_send_capacity &&
261 !self.is_pending_accept && !self.is_pending_window_update &&
262 !self.is_pending_open && self.reset_at.is_none()
263 }
264
265 pub fn is_canceled_interest(&self) -> bool {
271 self.ref_count == 0 && !self.state.is_closed()
272 }
273
274 pub fn capacity(&self, max_buffer_size: usize) -> WindowSize {
276 let available = self.send_flow.available().as_size() as usize;
277 let buffered = self.buffered_send_data;
278
279 available.min(max_buffer_size).saturating_sub(buffered) as WindowSize
280 }
281
282 pub fn assign_capacity(&mut self, capacity: WindowSize, max_buffer_size: usize) {
283 let prev_capacity = self.capacity(max_buffer_size);
284 debug_assert!(capacity > 0);
285 let _res = self.send_flow.assign_capacity(capacity);
287 debug_assert!(_res.is_ok());
288
289 tracing::trace!(
290 " assigned capacity to stream; available={}; buffered={}; id={:?}; max_buffer_size={} prev={}",
291 self.send_flow.available(),
292 self.buffered_send_data,
293 self.id,
294 max_buffer_size,
295 prev_capacity,
296 );
297
298 if prev_capacity < self.capacity(max_buffer_size) {
299 self.notify_capacity();
300 }
301 }
302
303 pub fn send_data(&mut self, len: WindowSize, max_buffer_size: usize) {
304 let prev_capacity = self.capacity(max_buffer_size);
305
306 let _res = self.send_flow.send_data(len);
308 debug_assert!(_res.is_ok());
309
310 debug_assert!(self.buffered_send_data >= len as usize);
312 self.buffered_send_data -= len as usize;
313 self.requested_send_capacity -= len;
314
315 tracing::trace!(
316 " sent stream data; available={}; buffered={}; id={:?}; max_buffer_size={} prev={}",
317 self.send_flow.available(),
318 self.buffered_send_data,
319 self.id,
320 max_buffer_size,
321 prev_capacity,
322 );
323
324 if prev_capacity < self.capacity(max_buffer_size) {
325 self.notify_capacity();
326 }
327 }
328
329 pub fn notify_capacity(&mut self) {
332 self.send_capacity_inc = true;
333 tracing::trace!(" notifying task");
334 self.notify_send();
335 }
336
337 pub fn dec_content_length(&mut self, len: usize) -> Result<(), ()> {
339 match self.content_length {
340 ContentLength::Remaining(ref mut rem) => match rem.checked_sub(len as u64) {
341 Some(val) => *rem = val,
342 None => return Err(()),
343 },
344 ContentLength::Head => {
345 if len != 0 {
346 return Err(());
347 }
348 }
349 _ => {}
350 }
351
352 Ok(())
353 }
354
355 pub fn ensure_content_length_zero(&self) -> Result<(), ()> {
356 match self.content_length {
357 ContentLength::Remaining(0) => Ok(()),
358 ContentLength::Remaining(_) => Err(()),
359 _ => Ok(()),
360 }
361 }
362
363 pub fn notify_send(&mut self) {
364 if let Some(task) = self.send_task.take() {
365 task.wake();
366 }
367 }
368
369 pub fn wait_send(&mut self, cx: &Context) {
370 self.send_task = Some(cx.waker().clone());
371 }
372
373 pub fn notify_recv(&mut self) {
374 if let Some(task) = self.recv_task.take() {
375 task.wake();
376 }
377 }
378
379 pub(super) fn notify_push(&mut self) {
380 if let Some(task) = self.push_task.take() {
381 task.wake();
382 }
383 }
384
385 pub(super) fn set_reset(&mut self, reason: Reason, initiator: Initiator) {
388 self.state.set_reset(self.id, reason, initiator);
389 self.notify_push();
390 self.notify_recv();
391 }
392}
393
394impl store::Next for NextAccept {
395 fn next(stream: &Stream) -> Option<store::Key> {
396 stream.next_pending_accept
397 }
398
399 fn set_next(stream: &mut Stream, key: Option<store::Key>) {
400 stream.next_pending_accept = key;
401 }
402
403 fn take_next(stream: &mut Stream) -> Option<store::Key> {
404 stream.next_pending_accept.take()
405 }
406
407 fn is_queued(stream: &Stream) -> bool {
408 stream.is_pending_accept
409 }
410
411 fn set_queued(stream: &mut Stream, val: bool) {
412 stream.is_pending_accept = val;
413 }
414}
415
416impl store::Next for NextSend {
417 fn next(stream: &Stream) -> Option<store::Key> {
418 stream.next_pending_send
419 }
420
421 fn set_next(stream: &mut Stream, key: Option<store::Key>) {
422 stream.next_pending_send = key;
423 }
424
425 fn take_next(stream: &mut Stream) -> Option<store::Key> {
426 stream.next_pending_send.take()
427 }
428
429 fn is_queued(stream: &Stream) -> bool {
430 stream.is_pending_send
431 }
432
433 fn set_queued(stream: &mut Stream, val: bool) {
434 if val {
435 debug_assert!(!stream.is_pending_open);
438 }
439 stream.is_pending_send = val;
440 }
441}
442
443impl store::Next for NextSendCapacity {
444 fn next(stream: &Stream) -> Option<store::Key> {
445 stream.next_pending_send_capacity
446 }
447
448 fn set_next(stream: &mut Stream, key: Option<store::Key>) {
449 stream.next_pending_send_capacity = key;
450 }
451
452 fn take_next(stream: &mut Stream) -> Option<store::Key> {
453 stream.next_pending_send_capacity.take()
454 }
455
456 fn is_queued(stream: &Stream) -> bool {
457 stream.is_pending_send_capacity
458 }
459
460 fn set_queued(stream: &mut Stream, val: bool) {
461 stream.is_pending_send_capacity = val;
462 }
463}
464
465impl store::Next for NextWindowUpdate {
466 fn next(stream: &Stream) -> Option<store::Key> {
467 stream.next_window_update
468 }
469
470 fn set_next(stream: &mut Stream, key: Option<store::Key>) {
471 stream.next_window_update = key;
472 }
473
474 fn take_next(stream: &mut Stream) -> Option<store::Key> {
475 stream.next_window_update.take()
476 }
477
478 fn is_queued(stream: &Stream) -> bool {
479 stream.is_pending_window_update
480 }
481
482 fn set_queued(stream: &mut Stream, val: bool) {
483 stream.is_pending_window_update = val;
484 }
485}
486
487impl store::Next for NextOpen {
488 fn next(stream: &Stream) -> Option<store::Key> {
489 stream.next_open
490 }
491
492 fn set_next(stream: &mut Stream, key: Option<store::Key>) {
493 stream.next_open = key;
494 }
495
496 fn take_next(stream: &mut Stream) -> Option<store::Key> {
497 stream.next_open.take()
498 }
499
500 fn is_queued(stream: &Stream) -> bool {
501 stream.is_pending_open
502 }
503
504 fn set_queued(stream: &mut Stream, val: bool) {
505 if val {
506 debug_assert!(!stream.is_pending_send);
509 }
510 stream.is_pending_open = val;
511 }
512}
513
514impl store::Next for NextResetExpire {
515 fn next(stream: &Stream) -> Option<store::Key> {
516 stream.next_reset_expire
517 }
518
519 fn set_next(stream: &mut Stream, key: Option<store::Key>) {
520 stream.next_reset_expire = key;
521 }
522
523 fn take_next(stream: &mut Stream) -> Option<store::Key> {
524 stream.next_reset_expire.take()
525 }
526
527 fn is_queued(stream: &Stream) -> bool {
528 stream.reset_at.is_some()
529 }
530
531 fn set_queued(stream: &mut Stream, val: bool) {
532 if val {
533 stream.reset_at = Some(Instant::now());
534 } else {
535 stream.reset_at = None;
536 }
537 }
538}
539
540impl ContentLength {
543 pub fn is_head(&self) -> bool {
544 matches!(*self, Self::Head)
545 }
546}