|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use super::Dispatch; |
| 5 | +use crate::{ |
| 6 | + event, |
| 7 | + socket::recv::descriptor::Filled, |
| 8 | + stream::{ |
| 9 | + recv::{ |
| 10 | + self, |
| 11 | + dispatch::{Control, Stream}, |
| 12 | + }, |
| 13 | + socket::Socket, |
| 14 | + Actor, TransportFeatures, |
| 15 | + }, |
| 16 | +}; |
| 17 | +use core::task::{Context, Poll}; |
| 18 | +use s2n_quic_core::ensure; |
| 19 | +use std::{collections::VecDeque, io}; |
| 20 | + |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct Channel<Recv = Stream> { |
| 23 | + pending: VecDeque<Filled>, |
| 24 | + receiver: Recv, |
| 25 | +} |
| 26 | + |
| 27 | +impl<Recv> Channel<Recv> { |
| 28 | + #[inline] |
| 29 | + pub fn new(receiver: Recv) -> Self { |
| 30 | + Self { |
| 31 | + pending: VecDeque::new(), |
| 32 | + receiver, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +macro_rules! impl_buffer { |
| 38 | + ($recv:ident) => { |
| 39 | + impl super::Buffer for Channel<$recv> { |
| 40 | + #[inline] |
| 41 | + fn is_empty(&self) -> bool { |
| 42 | + self.pending.is_empty() |
| 43 | + } |
| 44 | + |
| 45 | + #[inline] |
| 46 | + fn poll_fill<S, Pub>( |
| 47 | + &mut self, |
| 48 | + cx: &mut Context, |
| 49 | + actor: Actor, |
| 50 | + socket: &S, |
| 51 | + publisher: &mut Pub, |
| 52 | + ) -> Poll<io::Result<usize>> |
| 53 | + where |
| 54 | + S: ?Sized + Socket, |
| 55 | + Pub: event::ConnectionPublisher, |
| 56 | + { |
| 57 | + // check if we've already filled the queue |
| 58 | + ensure!(self.pending.is_empty(), Ok(1).into()); |
| 59 | + |
| 60 | + let capacity = u16::MAX as usize; |
| 61 | + |
| 62 | + // the socket isn't actually used since we're relying on another task to fill the `receiver` channel |
| 63 | + let _ = socket; |
| 64 | + |
| 65 | + let result = self |
| 66 | + .receiver |
| 67 | + .poll_swap(cx, actor, &mut self.pending) |
| 68 | + .map_err(|_err| io::Error::from(io::ErrorKind::BrokenPipe)); |
| 69 | + |
| 70 | + match result { |
| 71 | + Poll::Ready(Ok(())) => { |
| 72 | + let committed_len = self |
| 73 | + .pending |
| 74 | + .iter() |
| 75 | + .map(|segment| { |
| 76 | + debug_assert!( |
| 77 | + !segment.is_empty(), |
| 78 | + "the channel should not contain empty packets" |
| 79 | + ); |
| 80 | + segment.len() as usize |
| 81 | + }) |
| 82 | + .sum::<usize>(); |
| 83 | + publisher.on_stream_read_socket_flushed( |
| 84 | + event::builder::StreamReadSocketFlushed { |
| 85 | + capacity, |
| 86 | + committed_len, |
| 87 | + }, |
| 88 | + ); |
| 89 | + Ok(committed_len).into() |
| 90 | + } |
| 91 | + Poll::Ready(Err(error)) => { |
| 92 | + let errno = error.raw_os_error(); |
| 93 | + publisher.on_stream_read_socket_errored( |
| 94 | + event::builder::StreamReadSocketErrored { capacity, errno }, |
| 95 | + ); |
| 96 | + Err(error).into() |
| 97 | + } |
| 98 | + Poll::Pending => { |
| 99 | + publisher.on_stream_read_socket_blocked( |
| 100 | + event::builder::StreamReadSocketBlocked { capacity }, |
| 101 | + ); |
| 102 | + Poll::Pending |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + #[inline] |
| 108 | + fn process<R>( |
| 109 | + &mut self, |
| 110 | + features: TransportFeatures, |
| 111 | + router: &mut R, |
| 112 | + ) -> Result<(), recv::Error> |
| 113 | + where |
| 114 | + R: Dispatch, |
| 115 | + { |
| 116 | + debug_assert!( |
| 117 | + !features.is_stream(), |
| 118 | + "only datagram oriented transport is supported" |
| 119 | + ); |
| 120 | + |
| 121 | + for mut segment in self.pending.drain(..) { |
| 122 | + let remote_addr = segment.remote_address().get(); |
| 123 | + let ecn = segment.ecn(); |
| 124 | + router.on_datagram_segment(&remote_addr, ecn, segment.payload_mut())?; |
| 125 | + } |
| 126 | + |
| 127 | + Ok(()) |
| 128 | + } |
| 129 | + } |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +impl_buffer!(Stream); |
| 134 | +impl_buffer!(Control); |
0 commit comments