-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stream recv buffer trait and impls (#2505)
- Loading branch information
Showing
13 changed files
with
627 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
mod ack; | ||
pub mod application; | ||
pub(crate) mod buffer; | ||
mod error; | ||
mod packet; | ||
mod probes; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{ | ||
event, | ||
stream::{recv, socket::Socket, TransportFeatures}, | ||
}; | ||
use core::task::{Context, Poll}; | ||
use std::io; | ||
|
||
mod dispatch; | ||
mod local; | ||
|
||
pub use dispatch::Dispatch; | ||
pub use local::Local; | ||
|
||
pub trait Buffer { | ||
fn is_empty(&self) -> bool; | ||
|
||
fn poll_fill<S, Pub>( | ||
&mut self, | ||
cx: &mut Context, | ||
socket: &S, | ||
publisher: &mut Pub, | ||
) -> Poll<io::Result<usize>> | ||
where | ||
S: ?Sized + Socket, | ||
Pub: event::ConnectionPublisher; | ||
|
||
fn process<R>( | ||
&mut self, | ||
features: TransportFeatures, | ||
router: &mut R, | ||
) -> Result<(), recv::Error> | ||
where | ||
R: Dispatch; | ||
} | ||
|
||
#[allow(dead_code)] // TODO remove this once we start using the channel buffer | ||
pub enum Either<A, B> { | ||
A(A), | ||
B(B), | ||
} | ||
|
||
impl<A, B> Buffer for Either<A, B> | ||
where | ||
A: Buffer, | ||
B: Buffer, | ||
{ | ||
#[inline] | ||
fn is_empty(&self) -> bool { | ||
match self { | ||
Self::A(a) => a.is_empty(), | ||
Self::B(b) => b.is_empty(), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn poll_fill<S, Pub>( | ||
&mut self, | ||
cx: &mut Context, | ||
socket: &S, | ||
publisher: &mut Pub, | ||
) -> Poll<io::Result<usize>> | ||
where | ||
S: ?Sized + Socket, | ||
Pub: event::ConnectionPublisher, | ||
{ | ||
match self { | ||
Self::A(a) => a.poll_fill(cx, socket, publisher), | ||
Self::B(b) => b.poll_fill(cx, socket, publisher), | ||
} | ||
} | ||
|
||
#[inline] | ||
fn process<R>(&mut self, features: TransportFeatures, router: &mut R) -> Result<(), recv::Error> | ||
where | ||
R: Dispatch, | ||
{ | ||
match self { | ||
Self::A(a) => a.process(features, router), | ||
Self::B(b) => b.process(features, router), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{packet, stream::recv}; | ||
use s2n_codec::DecoderBufferMut; | ||
use s2n_quic_core::inet::{ExplicitCongestionNotification, SocketAddress}; | ||
|
||
pub trait Dispatch { | ||
#[inline(always)] | ||
fn tag_len(&self) -> usize { | ||
16 | ||
} | ||
|
||
fn on_packet( | ||
&mut self, | ||
remote_addr: &SocketAddress, | ||
ecn: ExplicitCongestionNotification, | ||
packet: packet::Packet, | ||
) -> Result<(), recv::Error>; | ||
|
||
#[inline] | ||
fn on_datagram_segment( | ||
&mut self, | ||
remote_addr: &SocketAddress, | ||
ecn: ExplicitCongestionNotification, | ||
segment: &mut [u8], | ||
) -> Result<(), recv::Error> { | ||
let tag_len = self.tag_len(); | ||
let segment_len = segment.len(); | ||
let mut decoder = DecoderBufferMut::new(segment); | ||
|
||
while !decoder.is_empty() { | ||
let packet = match decoder.decode_parameterized(tag_len) { | ||
Ok((packet, remaining)) => { | ||
decoder = remaining; | ||
packet | ||
} | ||
Err(decoder_error) => { | ||
// the packet was likely corrupted so log it and move on to the | ||
// next segment | ||
tracing::warn!( | ||
%decoder_error, | ||
segment_len | ||
); | ||
|
||
break; | ||
} | ||
}; | ||
|
||
self.on_packet(remote_addr, ecn, packet)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.