Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max datagram size API #159

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/xwt-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod prelude {
pub use crate::endpoint::accept::{Accept as _, Accepting as _, Request as _};
pub use crate::endpoint::connect::{Connect as _, Connecting as _};
pub use crate::session::base::{DatagramOps as _, StreamOps as _};
pub use crate::session::datagram::{Receive as _, ReceiveInto as _, Send as _};
pub use crate::session::datagram::{MaxSize as _, Receive as _, ReceiveInto as _, Send as _};
pub use crate::session::stream::{
AcceptBi as _, AcceptUni as _, OpenBi as _, OpenUni as _, OpeningBi as _, OpeningUni as _,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/xwt-core/src/session/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ impl<T> StreamOps for T where
///
/// Also, consider depending on the individual datagram APIs directly.
pub trait DatagramOps:
maybe::Send + datagram::Send + datagram::Receive + datagram::ReceiveInto
maybe::Send + datagram::MaxSize + datagram::Send + datagram::Receive + datagram::ReceiveInto
{
}

impl<T> DatagramOps for T where
T: maybe::Send + datagram::Send + datagram::Receive + datagram::ReceiveInto
T: maybe::Send + datagram::MaxSize + datagram::Send + datagram::Receive + datagram::ReceiveInto
{
}
10 changes: 10 additions & 0 deletions crates/xwt-core/src/session/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ use core::future::Future;

use crate::utils::{maybe, Error};

pub trait MaxSize: maybe::Send {
/// Gets the maximum byte length that a user-sent datagram is allowed to be.
///
/// This has no relation to how big incoming datagrams may be.
///
/// If this returns [`None`], this session does not support sending
/// datagrams.
fn max_datagram_size(&self) -> Option<usize>;
}

pub trait Receive: maybe::Send {
type Datagram: maybe::Send + AsRef<[u8]>;
type Error: Error + maybe::Send + maybe::Sync + 'static;
Expand Down
22 changes: 20 additions & 2 deletions crates/xwt-tests/src/tests/datagrams.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use xwt_core::prelude::*;

const MIN_DATAGRAM_SIZE: usize = 1024;

#[derive(Debug, thiserror::Error)]
pub enum Error<Endpoint>
where
Expand All @@ -11,6 +13,10 @@ where
{
#[error("connect: {0}")]
Connect(#[source] xwt_error::Connect<Endpoint>),
#[error("datagrams not supported")]
DatagramsNotSupported,
#[error("max datagram size is smaller than minimum - {size} / {MIN_DATAGRAM_SIZE}")]
MaxDatagramSizeTooSmall { size: usize },
#[error("send: {0}")]
Send(#[source] SendErrorFor<ConnectSessionFor<Endpoint>>),
#[error("recv: {0}")]
Expand All @@ -23,14 +29,26 @@ pub async fn run<Endpoint>(endpoint: Endpoint, url: &str) -> Result<(), Error<En
where
Endpoint: xwt_core::endpoint::Connect + std::fmt::Debug,
Endpoint::Connecting: std::fmt::Debug,
ConnectSessionFor<Endpoint>:
xwt_core::session::datagram::Send + xwt_core::session::datagram::Receive + std::fmt::Debug,
ConnectSessionFor<Endpoint>: xwt_core::session::datagram::MaxSize
+ xwt_core::session::datagram::Send
+ xwt_core::session::datagram::Receive
+ std::fmt::Debug,
ReceiveDatagramFor<ConnectSessionFor<Endpoint>>: std::fmt::Debug,
{
let session = crate::utils::connect(&endpoint, url)
.await
.map_err(Error::Connect)?;

let max_datagram_size = session
.max_datagram_size()
.ok_or(Error::DatagramsNotSupported)?;

if max_datagram_size < MIN_DATAGRAM_SIZE {
return Err(Error::MaxDatagramSizeTooSmall {
size: max_datagram_size,
});
}

session
.send_datagram(&b"hello"[..])
.await
Expand Down
7 changes: 7 additions & 0 deletions crates/xwt-web-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ impl Session {
}
}

impl xwt_core::session::datagram::MaxSize for Session {
fn max_datagram_size(&self) -> Option<usize> {
let max_datagram_size = self.transport.datagrams().max_datagram_size();
Some(usize::try_from(max_datagram_size).expect("u32 should fit in a usize on WASM"))
MOZGIII marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl xwt_core::session::datagram::Receive for Session {
type Datagram = Vec<u8>;
type Error = Error;
Expand Down
6 changes: 6 additions & 0 deletions crates/xwt-wtransport/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ impl xwt_core::stream::WriteChunk<xwt_core::stream::chunk::U8> for SendStream {
}
}

impl xwt_core::session::datagram::MaxSize for Connection {
fn max_datagram_size(&self) -> Option<usize> {
self.0.max_datagram_size()
}
}

impl xwt_core::session::datagram::Receive for Connection {
type Datagram = Datagram;
type Error = wtransport::error::ConnectionError;
Expand Down
Loading