|
| 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::{recv, socket::Socket, TransportFeatures}, |
| 9 | + sync::mpsc::Receiver, |
| 10 | +}; |
| 11 | +use core::task::{Context, Poll}; |
| 12 | +use s2n_quic_core::ensure; |
| 13 | +use std::{collections::VecDeque, io}; |
| 14 | + |
| 15 | +pub struct Channel { |
| 16 | + pending: VecDeque<Filled>, |
| 17 | + receiver: Receiver<Filled>, |
| 18 | +} |
| 19 | + |
| 20 | +impl super::Buffer for Channel { |
| 21 | + #[inline] |
| 22 | + fn is_empty(&self) -> bool { |
| 23 | + self.pending.is_empty() |
| 24 | + } |
| 25 | + |
| 26 | + #[inline] |
| 27 | + fn poll_fill<S, Pub>( |
| 28 | + &mut self, |
| 29 | + cx: &mut Context, |
| 30 | + socket: &S, |
| 31 | + publisher: &mut Pub, |
| 32 | + ) -> Poll<io::Result<usize>> |
| 33 | + where |
| 34 | + S: ?Sized + Socket, |
| 35 | + Pub: event::ConnectionPublisher, |
| 36 | + { |
| 37 | + // check if we have any pending packets |
| 38 | + debug_assert!(self.pending.is_empty(), "pending packets should be empty"); |
| 39 | + ensure!(self.pending.is_empty(), Ok(1).into()); |
| 40 | + |
| 41 | + let capacity = u16::MAX as usize; |
| 42 | + |
| 43 | + // the socket isn't actually used since we're relying on another task to fill the `receiver` channel |
| 44 | + let _ = socket; |
| 45 | + |
| 46 | + let result = self |
| 47 | + .receiver |
| 48 | + .poll_swap(cx, &mut self.pending) |
| 49 | + .map_err(|_err| io::Error::from(io::ErrorKind::BrokenPipe)); |
| 50 | + |
| 51 | + match result { |
| 52 | + Poll::Ready(Ok(())) => { |
| 53 | + let committed_len = self |
| 54 | + .pending |
| 55 | + .iter() |
| 56 | + .map(|segment| segment.len() as usize) |
| 57 | + .sum::<usize>(); |
| 58 | + publisher.on_stream_read_socket_flushed(event::builder::StreamReadSocketFlushed { |
| 59 | + capacity, |
| 60 | + committed_len, |
| 61 | + }); |
| 62 | + Ok(committed_len).into() |
| 63 | + } |
| 64 | + Poll::Ready(Err(error)) => { |
| 65 | + let errno = error.raw_os_error(); |
| 66 | + publisher.on_stream_read_socket_errored(event::builder::StreamReadSocketErrored { |
| 67 | + capacity, |
| 68 | + errno, |
| 69 | + }); |
| 70 | + Err(error).into() |
| 71 | + } |
| 72 | + Poll::Pending => { |
| 73 | + publisher.on_stream_read_socket_blocked(event::builder::StreamReadSocketBlocked { |
| 74 | + capacity, |
| 75 | + }); |
| 76 | + Poll::Pending |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #[inline] |
| 82 | + fn process<R>(&mut self, features: TransportFeatures, router: &mut R) -> Result<(), recv::Error> |
| 83 | + where |
| 84 | + R: Dispatch, |
| 85 | + { |
| 86 | + debug_assert!( |
| 87 | + !features.is_stream(), |
| 88 | + "only datagram oriented transport is supported" |
| 89 | + ); |
| 90 | + |
| 91 | + for mut segment in self.pending.drain(..) { |
| 92 | + let remote_addr = segment.remote_address().get(); |
| 93 | + let ecn = segment.ecn(); |
| 94 | + router.on_datagram_segment(&remote_addr, ecn, segment.payload_mut())?; |
| 95 | + } |
| 96 | + |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | +} |
0 commit comments