mirror of
https://github.com/MercuryWorkshop/epoxy-tls.git
synced 2025-05-12 14:00:01 -04:00
clippy pedantic
This commit is contained in:
parent
272610f904
commit
7efda6c533
14 changed files with 148 additions and 129 deletions
|
@ -1,7 +1,6 @@
|
|||
#![doc(html_no_source)]
|
||||
#![deny(clippy::todo)]
|
||||
#![allow(unexpected_cfgs)]
|
||||
#![warn(clippy::large_futures)]
|
||||
|
||||
use std::{collections::HashMap, fs::read_to_string, net::IpAddr};
|
||||
|
||||
|
|
|
@ -29,8 +29,8 @@ pub enum CertAuthError {
|
|||
impl std::fmt::Display for CertAuthError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Ed25519(x) => write!(f, "ED25519: {:?}", x),
|
||||
Self::Getrandom(x) => write!(f, "getrandom: {:?}", x),
|
||||
Self::Ed25519(x) => write!(f, "ED25519: {x:?}"),
|
||||
Self::Getrandom(x) => write!(f, "getrandom: {x:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ bitflags::bitflags! {
|
|||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct SupportedCertificateTypes: u8 {
|
||||
/// ED25519 certificate.
|
||||
const Ed25519 = 0b00000001;
|
||||
const Ed25519 = 0b0000_0001;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,7 +160,7 @@ impl ProtocolExtension for CertAuthProtocolExtension {
|
|||
required,
|
||||
} => {
|
||||
let mut out = BytesMut::with_capacity(2 + challenge.len());
|
||||
out.put_u8(*required as u8);
|
||||
out.put_u8(u8::from(*required));
|
||||
out.put_u8(cert_types.bits());
|
||||
out.extend_from_slice(challenge);
|
||||
out.freeze()
|
||||
|
@ -176,8 +176,7 @@ impl ProtocolExtension for CertAuthProtocolExtension {
|
|||
out.extend_from_slice(signature);
|
||||
out.freeze()
|
||||
}
|
||||
Self::ClientRecieved => Bytes::new(),
|
||||
Self::ServerVerified => Bytes::new(),
|
||||
Self::ServerVerified | Self::ClientRecieved => Bytes::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -262,10 +261,10 @@ impl CertAuthProtocolExtensionBuilder {
|
|||
/// sent the certificate authentication protocol extension.
|
||||
pub fn is_required(&self) -> Option<bool> {
|
||||
match self {
|
||||
Self::ServerBeforeChallenge { required, .. } => Some(*required),
|
||||
Self::ServerAfterChallenge { required, .. } => Some(*required),
|
||||
Self::ServerBeforeChallenge { required, .. }
|
||||
| Self::ServerAfterChallenge { required, .. }
|
||||
| Self::ClientAfterChallenge { required, .. } => Some(*required),
|
||||
Self::ClientBeforeChallenge { .. } => None,
|
||||
Self::ClientAfterChallenge { required, .. } => Some(*required),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -294,8 +293,6 @@ impl ProtocolExtensionBuilder for CertAuthProtocolExtensionBuilder {
|
|||
_: Role,
|
||||
) -> Result<AnyProtocolExtension, WispError> {
|
||||
match self {
|
||||
// server should have already sent the challenge before recieving a response to parse
|
||||
Self::ServerBeforeChallenge { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
Self::ServerAfterChallenge {
|
||||
verifiers,
|
||||
challenge,
|
||||
|
@ -332,8 +329,12 @@ impl ProtocolExtensionBuilder for CertAuthProtocolExtensionBuilder {
|
|||
|
||||
Ok(CertAuthProtocolExtension::ClientRecieved.into())
|
||||
}
|
||||
// client has already recieved a challenge
|
||||
Self::ClientAfterChallenge { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
|
||||
// client has already recieved a challenge or
|
||||
// server should have already sent the challenge before recieving a response to parse
|
||||
Self::ClientAfterChallenge { .. } | Self::ServerBeforeChallenge { .. } => {
|
||||
Err(WispError::ExtensionImplNotSupported)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -352,7 +353,7 @@ impl ProtocolExtensionBuilder for CertAuthProtocolExtensionBuilder {
|
|||
let required = *required;
|
||||
|
||||
*self = Self::ServerAfterChallenge {
|
||||
verifiers: verifiers.to_vec(),
|
||||
verifiers: verifiers.clone(),
|
||||
challenge: challenge.clone(),
|
||||
required,
|
||||
};
|
||||
|
@ -364,10 +365,6 @@ impl ProtocolExtensionBuilder for CertAuthProtocolExtensionBuilder {
|
|||
}
|
||||
.into())
|
||||
}
|
||||
// server has already sent a challenge
|
||||
Self::ServerAfterChallenge { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
// client needs to recieve a challenge
|
||||
Self::ClientBeforeChallenge { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
Self::ClientAfterChallenge {
|
||||
signer,
|
||||
challenge,
|
||||
|
@ -393,6 +390,12 @@ impl ProtocolExtensionBuilder for CertAuthProtocolExtensionBuilder {
|
|||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
// server has already sent a challenge or
|
||||
// client needs to recieve a challenge
|
||||
Self::ClientBeforeChallenge { .. } | Self::ServerAfterChallenge { .. } => {
|
||||
Err(WispError::ExtensionImplNotSupported)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ pub mod udp;
|
|||
use std::{
|
||||
any::TypeId,
|
||||
ops::{Deref, DerefMut},
|
||||
ptr,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
@ -47,13 +48,13 @@ impl AnyProtocolExtension {
|
|||
impl Deref for AnyProtocolExtension {
|
||||
type Target = dyn ProtocolExtension;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0.deref()
|
||||
&*self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for AnyProtocolExtension {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.0.deref_mut()
|
||||
&mut *self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,7 +138,7 @@ impl dyn ProtocolExtension {
|
|||
if self.__is::<T>() {
|
||||
unsafe {
|
||||
let raw: *mut dyn ProtocolExtension = Box::into_raw(self);
|
||||
Ok(Box::from_raw(raw as *mut T))
|
||||
Ok(Box::from_raw(raw.cast::<T>()))
|
||||
}
|
||||
} else {
|
||||
Err(self)
|
||||
|
@ -146,7 +147,7 @@ impl dyn ProtocolExtension {
|
|||
|
||||
fn __downcast_ref<T: ProtocolExtension>(&self) -> Option<&T> {
|
||||
if self.__is::<T>() {
|
||||
unsafe { Some(&*(self as *const dyn ProtocolExtension as *const T)) }
|
||||
unsafe { Some(&*ptr::from_ref::<dyn ProtocolExtension>(self).cast::<T>()) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -154,7 +155,7 @@ impl dyn ProtocolExtension {
|
|||
|
||||
fn __downcast_mut<T: ProtocolExtension>(&mut self) -> Option<&mut T> {
|
||||
if self.__is::<T>() {
|
||||
unsafe { Some(&mut *(self as *mut dyn ProtocolExtension as *mut T)) }
|
||||
unsafe { Some(&mut *ptr::from_mut::<dyn ProtocolExtension>(self).cast::<T>()) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -198,7 +199,7 @@ impl dyn ProtocolExtensionBuilder {
|
|||
if self.__is::<T>() {
|
||||
unsafe {
|
||||
let raw: *mut dyn ProtocolExtensionBuilder = Box::into_raw(self);
|
||||
Ok(Box::from_raw(raw as *mut T))
|
||||
Ok(Box::from_raw(raw.cast::<T>()))
|
||||
}
|
||||
} else {
|
||||
Err(self)
|
||||
|
@ -207,7 +208,7 @@ impl dyn ProtocolExtensionBuilder {
|
|||
|
||||
fn __downcast_ref<T: ProtocolExtensionBuilder>(&self) -> Option<&T> {
|
||||
if self.__is::<T>() {
|
||||
unsafe { Some(&*(self as *const dyn ProtocolExtensionBuilder as *const T)) }
|
||||
unsafe { Some(&*ptr::from_ref::<dyn ProtocolExtensionBuilder>(self).cast::<T>()) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -215,7 +216,7 @@ impl dyn ProtocolExtensionBuilder {
|
|||
|
||||
fn __downcast_mut<T: ProtocolExtensionBuilder>(&mut self) -> Option<&mut T> {
|
||||
if self.__is::<T>() {
|
||||
unsafe { Some(&mut *(self as *mut dyn ProtocolExtensionBuilder as *mut T)) }
|
||||
unsafe { Some(&mut *ptr::from_mut::<dyn ProtocolExtensionBuilder>(self).cast::<T>()) }
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -250,13 +251,13 @@ impl AnyProtocolExtensionBuilder {
|
|||
impl Deref for AnyProtocolExtensionBuilder {
|
||||
type Target = dyn ProtocolExtensionBuilder;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0.deref()
|
||||
&*self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for AnyProtocolExtensionBuilder {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.0.deref_mut()
|
||||
&mut *self.0
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,11 +76,9 @@ impl ProtocolExtension for PasswordProtocolExtension {
|
|||
match self {
|
||||
Self::ServerBeforeClientInfo { required } => {
|
||||
let mut out = BytesMut::with_capacity(1);
|
||||
out.put_u8(*required as u8);
|
||||
out.put_u8(u8::from(*required));
|
||||
out.freeze()
|
||||
}
|
||||
Self::ServerAfterClientInfo { .. } => Bytes::new(),
|
||||
Self::ClientBeforeServerInfo => Bytes::new(),
|
||||
Self::ClientAfterServerInfo { user, password } => {
|
||||
let mut out = BytesMut::with_capacity(1 + 2 + user.len() + password.len());
|
||||
out.put_u8(user.len().try_into().unwrap());
|
||||
|
@ -89,6 +87,8 @@ impl ProtocolExtension for PasswordProtocolExtension {
|
|||
out.extend_from_slice(password.as_bytes());
|
||||
out.freeze()
|
||||
}
|
||||
|
||||
Self::ServerAfterClientInfo { .. } | Self::ClientBeforeServerInfo => Bytes::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,10 +164,10 @@ impl PasswordProtocolExtensionBuilder {
|
|||
/// sent the password protocol extension.
|
||||
pub fn is_required(&self) -> Option<bool> {
|
||||
match self {
|
||||
Self::ServerBeforeClientInfo { required, .. } => Some(*required),
|
||||
Self::ServerAfterClientInfo { required, .. } => Some(*required),
|
||||
Self::ServerBeforeClientInfo { required, .. }
|
||||
| Self::ServerAfterClientInfo { required, .. }
|
||||
| Self::ClientAfterServerInfo { required, .. } => Some(*required),
|
||||
Self::ClientBeforeServerInfo { .. } => None,
|
||||
Self::ClientAfterServerInfo { required, .. } => Some(*required),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -195,8 +195,9 @@ impl ProtocolExtensionBuilder for PasswordProtocolExtensionBuilder {
|
|||
}
|
||||
.into())
|
||||
}
|
||||
Self::ServerAfterClientInfo { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
Self::ClientBeforeServerInfo { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
Self::ServerAfterClientInfo { .. } | Self::ClientBeforeServerInfo { .. } => {
|
||||
Err(WispError::ExtensionImplNotSupported)
|
||||
}
|
||||
Self::ClientAfterServerInfo { creds, .. } => {
|
||||
let (user, password) = creds.clone().ok_or(WispError::PasswordExtensionNoCreds)?;
|
||||
Ok(PasswordProtocolExtension::ClientAfterServerInfo { user, password }.into())
|
||||
|
@ -218,24 +219,23 @@ impl ProtocolExtensionBuilder for PasswordProtocolExtensionBuilder {
|
|||
let password =
|
||||
std::str::from_utf8(&bytes.split_to(password_len as usize))?.to_string();
|
||||
|
||||
let valid = users.get(&user).map(|x| *x == password).unwrap_or(false);
|
||||
let valid = users.get(&user).is_some_and(|x| *x == password);
|
||||
|
||||
*self = Self::ServerAfterClientInfo {
|
||||
users: users.clone(),
|
||||
required: *required,
|
||||
};
|
||||
|
||||
if !valid {
|
||||
Err(WispError::PasswordExtensionCredsInvalid)
|
||||
} else {
|
||||
if valid {
|
||||
Ok(PasswordProtocolExtension::ServerAfterClientInfo {
|
||||
chosen_user: user,
|
||||
chosen_password: password,
|
||||
}
|
||||
.into())
|
||||
} else {
|
||||
Err(WispError::PasswordExtensionCredsInvalid)
|
||||
}
|
||||
}
|
||||
Self::ServerAfterClientInfo { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
Self::ClientBeforeServerInfo { creds } => {
|
||||
let required = bytes.get_u8() != 0;
|
||||
|
||||
|
@ -246,7 +246,9 @@ impl ProtocolExtensionBuilder for PasswordProtocolExtensionBuilder {
|
|||
|
||||
Ok(PasswordProtocolExtension::ClientBeforeServerInfo.into())
|
||||
}
|
||||
Self::ClientAfterServerInfo { .. } => Err(WispError::ExtensionImplNotSupported),
|
||||
Self::ClientAfterServerInfo { .. } | Self::ServerAfterClientInfo { .. } => {
|
||||
Err(WispError::ExtensionImplNotSupported)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//! WebSocketRead + WebSocketWrite implementation for generic `Stream + Sink`s.
|
||||
//! `WebSocketRead` and `WebSocketWrite` implementation for generic `Stream`s and `Sink`s.
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{Sink, SinkExt, Stream, StreamExt};
|
||||
|
@ -9,7 +9,7 @@ use crate::{
|
|||
WispError,
|
||||
};
|
||||
|
||||
/// WebSocketRead implementation for generic `Stream`s.
|
||||
/// `WebSocketRead` implementation for generic `Stream`s.
|
||||
pub struct GenericWebSocketRead<
|
||||
T: Stream<Item = Result<BytesMut, E>> + Send + Unpin,
|
||||
E: Error + Sync + Send + 'static,
|
||||
|
@ -18,12 +18,12 @@ pub struct GenericWebSocketRead<
|
|||
impl<T: Stream<Item = Result<BytesMut, E>> + Send + Unpin, E: Error + Sync + Send + 'static>
|
||||
GenericWebSocketRead<T, E>
|
||||
{
|
||||
/// Create a new wrapper WebSocketRead implementation.
|
||||
/// Create a new wrapper `WebSocketRead` implementation.
|
||||
pub fn new(stream: T) -> Self {
|
||||
Self(stream)
|
||||
}
|
||||
|
||||
/// Get the inner Stream from the wrapper.
|
||||
/// Get the inner `Stream` from the wrapper.
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ impl<T: Stream<Item = Result<BytesMut, E>> + Send + Unpin, E: Error + Sync + Sen
|
|||
}
|
||||
}
|
||||
|
||||
/// WebSocketWrite implementation for generic `Sink`s.
|
||||
/// `WebSocketWrite` implementation for generic `Sink`s.
|
||||
pub struct GenericWebSocketWrite<
|
||||
T: Sink<Bytes, Error = E> + Send + Unpin,
|
||||
E: Error + Sync + Send + 'static,
|
||||
|
@ -54,12 +54,12 @@ pub struct GenericWebSocketWrite<
|
|||
impl<T: Sink<Bytes, Error = E> + Send + Unpin, E: Error + Sync + Send + 'static>
|
||||
GenericWebSocketWrite<T, E>
|
||||
{
|
||||
/// Create a new wrapper WebSocketWrite implementation.
|
||||
/// Create a new wrapper `WebSocketWrite` implementation.
|
||||
pub fn new(stream: T) -> Self {
|
||||
Self(stream)
|
||||
}
|
||||
|
||||
/// Get the inner Sink from the wrapper.
|
||||
/// Get the inner `Sink` from the wrapper.
|
||||
pub fn into_inner(self) -> T {
|
||||
self.0
|
||||
}
|
||||
|
|
|
@ -1,5 +1,12 @@
|
|||
#![deny(missing_docs, clippy::todo)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![warn(clippy::pedantic)]
|
||||
#![deny(missing_docs, clippy::todo)]
|
||||
#![allow(
|
||||
clippy::must_use_candidate,
|
||||
clippy::missing_errors_doc,
|
||||
clippy::module_name_repetitions
|
||||
)]
|
||||
|
||||
//! A library for easily creating [Wisp] clients and servers.
|
||||
//!
|
||||
//! [Wisp]: https://github.com/MercuryWorkshop/wisp-protocol
|
||||
|
|
|
@ -39,14 +39,14 @@ async fn handshake<R: WebSocketRead + 'static, W: WebSocketWrite>(
|
|||
|
||||
if let PacketType::Info(info) = packet.packet_type {
|
||||
// v2 server
|
||||
let buffer_size = validate_continue_packet(rx.wisp_read_frame(tx).await?.try_into()?)?;
|
||||
let buffer_size = validate_continue_packet(&rx.wisp_read_frame(tx).await?.try_into()?)?;
|
||||
|
||||
(closure)(&mut builders).await?;
|
||||
send_info_packet(tx, &mut builders).await?;
|
||||
|
||||
let mut supported_extensions = get_supported_extensions(info.extensions, &mut builders);
|
||||
|
||||
for extension in supported_extensions.iter_mut() {
|
||||
for extension in &mut supported_extensions {
|
||||
extension
|
||||
.handle_handshake(DynWebSocketRead::from_mut(rx), tx)
|
||||
.await?;
|
||||
|
@ -63,7 +63,7 @@ async fn handshake<R: WebSocketRead + 'static, W: WebSocketWrite>(
|
|||
))
|
||||
} else {
|
||||
// downgrade to v1
|
||||
let buffer_size = validate_continue_packet(packet)?;
|
||||
let buffer_size = validate_continue_packet(&packet)?;
|
||||
|
||||
Ok((
|
||||
WispHandshakeResult {
|
||||
|
@ -75,7 +75,7 @@ async fn handshake<R: WebSocketRead + 'static, W: WebSocketWrite>(
|
|||
}
|
||||
} else {
|
||||
// user asked for a v1 client
|
||||
let buffer_size = validate_continue_packet(rx.wisp_read_frame(tx).await?.try_into()?)?;
|
||||
let buffer_size = validate_continue_packet(&rx.wisp_read_frame(tx).await?.try_into()?)?;
|
||||
|
||||
Ok((
|
||||
WispHandshakeResult {
|
||||
|
|
|
@ -43,7 +43,7 @@ struct MuxMapValue {
|
|||
is_closed_event: Arc<Event>,
|
||||
}
|
||||
|
||||
pub struct MuxInner<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> {
|
||||
pub(crate) struct MuxInner<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> {
|
||||
// gets taken by the mux task
|
||||
rx: Option<R>,
|
||||
// gets taken by the mux task
|
||||
|
@ -68,7 +68,7 @@ pub struct MuxInner<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> {
|
|||
server_tx: mpsc::Sender<(ConnectPacket, MuxStream<W>)>,
|
||||
}
|
||||
|
||||
pub struct MuxInnerResult<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> {
|
||||
pub(crate) struct MuxInnerResult<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> {
|
||||
pub mux: MuxInner<R, W>,
|
||||
pub actor_exited: Arc<AtomicBool>,
|
||||
pub actor_tx: mpsc::Sender<WsEvent<W>>,
|
||||
|
@ -84,7 +84,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
#[expect(clippy::type_complexity)]
|
||||
pub fn new_server(
|
||||
rx: R,
|
||||
maybe_downgrade_packet: Option<Packet<'static>>,
|
||||
|
@ -100,6 +100,10 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
let ret_fut_tx = fut_tx.clone();
|
||||
let fut_exited = Arc::new(AtomicBool::new(false));
|
||||
|
||||
// 90% of the buffer size, not possible to overflow
|
||||
#[expect(clippy::cast_possible_truncation)]
|
||||
let target_buffer_size = ((u64::from(buffer_size) * 90) / 100) as u32;
|
||||
|
||||
(
|
||||
MuxInnerResult {
|
||||
mux: Self {
|
||||
|
@ -114,7 +118,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
tcp_extensions: Self::get_tcp_extensions(&extensions),
|
||||
extensions: Some(extensions),
|
||||
buffer_size,
|
||||
target_buffer_size: ((buffer_size as u64 * 90) / 100) as u32,
|
||||
target_buffer_size,
|
||||
|
||||
role: Role::Server,
|
||||
|
||||
|
@ -172,8 +176,8 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
|
||||
self.fut_exited.store(true, Ordering::Release);
|
||||
|
||||
for (_, stream) in self.stream_map.iter() {
|
||||
self.close_stream(stream, ClosePacket::new(CloseReason::Unknown));
|
||||
for stream in self.stream_map.values() {
|
||||
Self::close_stream(stream, ClosePacket::new(CloseReason::Unknown));
|
||||
}
|
||||
self.stream_map.clear();
|
||||
|
||||
|
@ -181,11 +185,11 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
ret
|
||||
}
|
||||
|
||||
async fn create_new_stream(
|
||||
fn create_new_stream(
|
||||
&mut self,
|
||||
stream_id: u32,
|
||||
stream_type: StreamType,
|
||||
) -> Result<(MuxMapValue, MuxStream<W>), WispError> {
|
||||
) -> (MuxMapValue, MuxStream<W>) {
|
||||
let (ch_tx, ch_rx) = mpsc::bounded(if self.role == Role::Server {
|
||||
self.buffer_size as usize
|
||||
} else {
|
||||
|
@ -201,7 +205,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
AtomicCloseReason::new(CloseReason::Unknown).into();
|
||||
let is_closed_event: Arc<Event> = Event::new().into();
|
||||
|
||||
Ok((
|
||||
(
|
||||
MuxMapValue {
|
||||
stream: ch_tx,
|
||||
stream_type,
|
||||
|
@ -229,10 +233,10 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
flow_control_event,
|
||||
self.target_buffer_size,
|
||||
),
|
||||
))
|
||||
)
|
||||
}
|
||||
|
||||
fn close_stream(&self, stream: &MuxMapValue, close_packet: ClosePacket) {
|
||||
fn close_stream(stream: &MuxMapValue, close_packet: ClosePacket) {
|
||||
stream
|
||||
.close_reason
|
||||
.store(close_packet.reason, Ordering::Release);
|
||||
|
@ -319,8 +323,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
.checked_add(1)
|
||||
.ok_or(WispError::MaxStreamCountReached)?;
|
||||
|
||||
let (map_value, stream) =
|
||||
self.create_new_stream(stream_id, stream_type).await?;
|
||||
let (map_value, stream) = self.create_new_stream(stream_id, stream_type);
|
||||
|
||||
self.tx
|
||||
.write_frame(
|
||||
|
@ -340,7 +343,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
WsEvent::Close(packet, channel) => {
|
||||
if let Some(stream) = self.stream_map.remove(&packet.stream_id) {
|
||||
if let PacketType::Close(close) = packet.packet_type {
|
||||
self.close_stream(&stream, close);
|
||||
Self::close_stream(&stream, close);
|
||||
}
|
||||
let _ = channel.send(self.tx.write_frame(packet.into()).await);
|
||||
} else {
|
||||
|
@ -383,20 +386,16 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_close_packet(
|
||||
&mut self,
|
||||
stream_id: u32,
|
||||
inner_packet: ClosePacket,
|
||||
) -> Result<bool, WispError> {
|
||||
fn handle_close_packet(&mut self, stream_id: u32, inner_packet: ClosePacket) -> bool {
|
||||
if stream_id == 0 {
|
||||
return Ok(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
if let Some(stream) = self.stream_map.remove(&stream_id) {
|
||||
self.close_stream(&stream, inner_packet);
|
||||
Self::close_stream(&stream, inner_packet);
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
false
|
||||
}
|
||||
|
||||
fn handle_data_packet(
|
||||
|
@ -404,7 +403,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
stream_id: u32,
|
||||
optional_frame: Option<Frame<'static>>,
|
||||
data: Payload<'static>,
|
||||
) -> Result<bool, WispError> {
|
||||
) -> bool {
|
||||
let mut data = BytesMut::from(data);
|
||||
|
||||
if let Some(stream) = self.stream_map.get(&stream_id) {
|
||||
|
@ -427,7 +426,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
false
|
||||
}
|
||||
|
||||
async fn handle_packet(
|
||||
|
@ -437,12 +436,12 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
) -> Result<bool, WispError> {
|
||||
use PacketType as P;
|
||||
match packet.packet_type {
|
||||
P::Data(data) => self.handle_data_packet(packet.stream_id, optional_frame, data),
|
||||
P::Close(inner_packet) => self.handle_close_packet(packet.stream_id, inner_packet),
|
||||
P::Data(data) => Ok(self.handle_data_packet(packet.stream_id, optional_frame, data)),
|
||||
P::Close(inner_packet) => Ok(self.handle_close_packet(packet.stream_id, inner_packet)),
|
||||
|
||||
_ => match self.role {
|
||||
Role::Server => self.server_handle_packet(packet, optional_frame).await,
|
||||
Role::Client => self.client_handle_packet(packet, optional_frame).await,
|
||||
Role::Client => self.client_handle_packet(&packet),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -455,9 +454,8 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
use PacketType as P;
|
||||
match packet.packet_type {
|
||||
P::Connect(inner_packet) => {
|
||||
let (map_value, stream) = self
|
||||
.create_new_stream(packet.stream_id, inner_packet.stream_type)
|
||||
.await?;
|
||||
let (map_value, stream) =
|
||||
self.create_new_stream(packet.stream_id, inner_packet.stream_type);
|
||||
self.server_tx
|
||||
.send_async((inner_packet, stream))
|
||||
.await
|
||||
|
@ -472,11 +470,7 @@ impl<R: WebSocketRead + 'static, W: WebSocketWrite + 'static> MuxInner<R, W> {
|
|||
}
|
||||
}
|
||||
|
||||
async fn client_handle_packet(
|
||||
&mut self,
|
||||
packet: Packet<'static>,
|
||||
_optional_frame: Option<Frame<'static>>,
|
||||
) -> Result<bool, WispError> {
|
||||
fn client_handle_packet(&mut self, packet: &Packet<'static>) -> Result<bool, WispError> {
|
||||
use PacketType as P;
|
||||
match packet.packet_type {
|
||||
P::Continue(inner_packet) => {
|
||||
|
|
|
@ -52,7 +52,7 @@ async fn send_info_packet<W: WebSocketWrite>(
|
|||
.await
|
||||
}
|
||||
|
||||
fn validate_continue_packet(packet: Packet<'_>) -> Result<u32, WispError> {
|
||||
fn validate_continue_packet(packet: &Packet<'_>) -> Result<u32, WispError> {
|
||||
if packet.stream_id != 0 {
|
||||
return Err(WispError::InvalidStreamId);
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ async fn handshake<R: WebSocketRead + 'static, W: WebSocketWrite>(
|
|||
if let PacketType::Info(info) = packet.packet_type {
|
||||
let mut supported_extensions = get_supported_extensions(info.extensions, &mut builders);
|
||||
|
||||
for extension in supported_extensions.iter_mut() {
|
||||
for extension in &mut supported_extensions {
|
||||
extension
|
||||
.handle_handshake(DynWebSocketRead::from_mut(rx), tx)
|
||||
.await?;
|
||||
|
|
|
@ -492,9 +492,9 @@ impl<'a> Packet<'a> {
|
|||
return Err(WispError::PacketTooSmall);
|
||||
}
|
||||
if let Some(builder) = extension_builders.iter_mut().find(|x| x.get_id() == id) {
|
||||
extensions.push(builder.build_from_bytes(bytes.copy_to_bytes(length), role)?)
|
||||
extensions.push(builder.build_from_bytes(bytes.copy_to_bytes(length), role)?);
|
||||
} else {
|
||||
bytes.advance(length)
|
||||
bytes.advance(length);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -205,7 +205,7 @@ impl AsyncBufRead for MuxStreamAsyncRW {
|
|||
}
|
||||
|
||||
fn consume(self: Pin<&mut Self>, amt: usize) {
|
||||
self.project().rx.consume(amt)
|
||||
self.project().rx.consume(amt);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -270,7 +270,7 @@ impl AsyncBufRead for MuxStreamAsyncRead {
|
|||
self.project().rx.poll_fill_buf(cx)
|
||||
}
|
||||
fn consume(self: Pin<&mut Self>, amt: usize) {
|
||||
self.project().rx.consume(amt)
|
||||
self.project().rx.consume(amt);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -319,7 +319,7 @@ impl AsyncWrite for MuxStreamAsyncWrite {
|
|||
Poll::Ready(Err(err)) => {
|
||||
self.error = Some(err);
|
||||
}
|
||||
Poll::Ready(Ok(_)) | Poll::Pending => {}
|
||||
Poll::Ready(Ok(())) | Poll::Pending => {}
|
||||
}
|
||||
|
||||
Poll::Ready(Ok(buf.len()))
|
||||
|
|
|
@ -50,7 +50,7 @@ impl<W: WebSocketWrite + 'static> MuxStreamRead<W> {
|
|||
}
|
||||
let bytes = select! {
|
||||
x = self.rx.recv_async() => x.map_err(|_| WispError::MuxMessageFailedToRecv)?,
|
||||
_ = self.is_closed_event.listen().fuse() => return Ok(None)
|
||||
() = self.is_closed_event.listen().fuse() => return Ok(None)
|
||||
};
|
||||
if self.role == Role::Server && self.should_flow_control {
|
||||
let val = self.flow_control_read.fetch_add(1, Ordering::AcqRel) + 1;
|
||||
|
@ -288,11 +288,14 @@ impl<W: WebSocketWrite + 'static> MuxStream<W> {
|
|||
stream_id,
|
||||
stream_type,
|
||||
role,
|
||||
|
||||
tx: tx.clone(),
|
||||
rx,
|
||||
|
||||
is_closed: is_closed.clone(),
|
||||
is_closed_event: is_closed_event.clone(),
|
||||
is_closed_event,
|
||||
close_reason: close_reason.clone(),
|
||||
|
||||
should_flow_control,
|
||||
flow_control: flow_control.clone(),
|
||||
flow_control_read: AtomicU32::new(0),
|
||||
|
@ -302,13 +305,16 @@ impl<W: WebSocketWrite + 'static> MuxStream<W> {
|
|||
stream_id,
|
||||
stream_type,
|
||||
role,
|
||||
|
||||
mux_tx,
|
||||
tx,
|
||||
is_closed: is_closed.clone(),
|
||||
close_reason: close_reason.clone(),
|
||||
|
||||
is_closed,
|
||||
close_reason,
|
||||
|
||||
continue_recieved,
|
||||
should_flow_control,
|
||||
flow_control: flow_control.clone(),
|
||||
continue_recieved: continue_recieved.clone(),
|
||||
flow_control,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use futures::{lock::Mutex, TryFutureExt};
|
|||
pub enum Payload<'a> {
|
||||
/// Borrowed payload. Currently used when writing data.
|
||||
Borrowed(&'a [u8]),
|
||||
/// BytesMut payload. Currently used when reading data.
|
||||
/// `BytesMut` payload. Currently used when reading data.
|
||||
Bytes(BytesMut),
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,7 @@ impl<'a> From<&'a [u8]> for Payload<'a> {
|
|||
|
||||
impl Payload<'_> {
|
||||
/// Turn a Payload<'a> into a Payload<'static> by copying the data.
|
||||
#[must_use]
|
||||
pub fn into_owned(self) -> Self {
|
||||
match self {
|
||||
Self::Bytes(x) => Self::Bytes(x),
|
||||
|
@ -54,7 +55,7 @@ impl Deref for Payload<'_> {
|
|||
type Target = [u8];
|
||||
fn deref(&self) -> &Self::Target {
|
||||
match self {
|
||||
Self::Bytes(x) => x.deref(),
|
||||
Self::Bytes(x) => x,
|
||||
Self::Borrowed(x) => x,
|
||||
}
|
||||
}
|
||||
|
@ -175,7 +176,7 @@ pub trait WebSocketRead: Send {
|
|||
|
||||
// similar to what dynosaur does
|
||||
mod wsr_inner {
|
||||
use std::{future::Future, pin::Pin};
|
||||
use std::{future::Future, pin::Pin, ptr};
|
||||
|
||||
use crate::WispError;
|
||||
|
||||
|
@ -187,7 +188,7 @@ mod wsr_inner {
|
|||
tx: &'a dyn LockingWebSocketWrite,
|
||||
) -> Pin<Box<dyn Future<Output = Result<Frame<'static>, WispError>> + Send + 'a>>;
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
#[expect(clippy::type_complexity)]
|
||||
fn wisp_read_split<'a>(
|
||||
&'a mut self,
|
||||
tx: &'a dyn LockingWebSocketWrite,
|
||||
|
@ -222,7 +223,7 @@ mod wsr_inner {
|
|||
}
|
||||
}
|
||||
|
||||
/// WebSocketRead trait object.
|
||||
/// `WebSocketRead` trait object.
|
||||
#[repr(transparent)]
|
||||
pub struct DynWebSocketRead {
|
||||
ptr: dyn ErasedWebSocketRead + 'static,
|
||||
|
@ -243,24 +244,26 @@ mod wsr_inner {
|
|||
}
|
||||
}
|
||||
impl DynWebSocketRead {
|
||||
/// Create a WebSocketRead trait object from a boxed WebSocketRead.
|
||||
/// Create a `WebSocketRead` trait object from a boxed `WebSocketRead`.
|
||||
pub fn new(val: Box<impl WebSocketRead + 'static>) -> Box<Self> {
|
||||
let val: Box<dyn ErasedWebSocketRead + 'static> = val;
|
||||
unsafe { std::mem::transmute(val) }
|
||||
}
|
||||
/// Create a WebSocketRead trait object from a WebSocketRead.
|
||||
/// Create a `WebSocketRead` trait object from a `WebSocketRead`.
|
||||
pub fn boxed(val: impl WebSocketRead + 'static) -> Box<Self> {
|
||||
Self::new(Box::new(val))
|
||||
}
|
||||
/// Create a WebSocketRead trait object from a WebSocketRead reference.
|
||||
/// Create a `WebSocketRead` trait object from a `WebSocketRead` reference.
|
||||
pub fn from_ref(val: &(impl WebSocketRead + 'static)) -> &Self {
|
||||
let val: &(dyn ErasedWebSocketRead + 'static) = val;
|
||||
unsafe { std::mem::transmute(val) }
|
||||
unsafe { &*(ptr::from_ref::<dyn ErasedWebSocketRead>(val) as *const DynWebSocketRead) }
|
||||
}
|
||||
/// Create a WebSocketRead trait object from a mutable WebSocketRead reference.
|
||||
/// Create a `WebSocketRead` trait object from a mutable `WebSocketRead` reference.
|
||||
pub fn from_mut(val: &mut (impl WebSocketRead + 'static)) -> &mut Self {
|
||||
let val: &mut (dyn ErasedWebSocketRead + 'static) = &mut *val;
|
||||
unsafe { std::mem::transmute(val) }
|
||||
unsafe {
|
||||
&mut *(ptr::from_mut::<dyn ErasedWebSocketRead>(val) as *mut DynWebSocketRead)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -294,7 +297,7 @@ pub trait WebSocketWrite: Send {
|
|||
|
||||
// similar to what dynosaur does
|
||||
mod wsw_inner {
|
||||
use std::{future::Future, pin::Pin};
|
||||
use std::{future::Future, pin::Pin, ptr};
|
||||
|
||||
use crate::WispError;
|
||||
|
||||
|
@ -340,7 +343,7 @@ mod wsw_inner {
|
|||
}
|
||||
}
|
||||
|
||||
/// WebSocketWrite trait object.
|
||||
/// `WebSocketWrite` trait object.
|
||||
#[repr(transparent)]
|
||||
pub struct DynWebSocketWrite {
|
||||
ptr: dyn ErasedWebSocketWrite + 'static,
|
||||
|
@ -363,24 +366,28 @@ mod wsw_inner {
|
|||
}
|
||||
}
|
||||
impl DynWebSocketWrite {
|
||||
/// Create a new WebSocketWrite trait object from a boxed WebSocketWrite.
|
||||
/// Create a new `WebSocketWrite` trait object from a boxed `WebSocketWrite`.
|
||||
pub fn new(val: Box<impl WebSocketWrite + 'static>) -> Box<Self> {
|
||||
let val: Box<dyn ErasedWebSocketWrite + 'static> = val;
|
||||
unsafe { std::mem::transmute(val) }
|
||||
}
|
||||
/// Create a new WebSocketWrite trait object from a WebSocketWrite.
|
||||
/// Create a new `WebSocketWrite` trait object from a `WebSocketWrite`.
|
||||
pub fn boxed(val: impl WebSocketWrite + 'static) -> Box<Self> {
|
||||
Self::new(Box::new(val))
|
||||
}
|
||||
/// Create a new WebSocketWrite trait object from a WebSocketWrite reference.
|
||||
/// Create a new `WebSocketWrite` trait object from a `WebSocketWrite` reference.
|
||||
pub fn from_ref(val: &(impl WebSocketWrite + 'static)) -> &Self {
|
||||
let val: &(dyn ErasedWebSocketWrite + 'static) = val;
|
||||
unsafe { std::mem::transmute(val) }
|
||||
unsafe {
|
||||
&*(ptr::from_ref::<dyn ErasedWebSocketWrite>(val) as *const DynWebSocketWrite)
|
||||
}
|
||||
}
|
||||
/// Create a new WebSocketWrite trait object from a mutable WebSocketWrite reference.
|
||||
/// Create a new `WebSocketWrite` trait object from a mutable `WebSocketWrite` reference.
|
||||
pub fn from_mut(val: &mut (impl WebSocketWrite + 'static)) -> &mut Self {
|
||||
let val: &mut (dyn ErasedWebSocketWrite + 'static) = &mut *val;
|
||||
unsafe { std::mem::transmute(val) }
|
||||
unsafe {
|
||||
&mut *(ptr::from_mut::<dyn ErasedWebSocketWrite>(val) as *mut DynWebSocketWrite)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -390,7 +397,7 @@ mod private {
|
|||
pub trait Sealed {}
|
||||
}
|
||||
|
||||
/// Helper trait object for LockedWebSocketWrite.
|
||||
/// Helper trait object for `LockedWebSocketWrite`.
|
||||
pub trait LockingWebSocketWrite: private::Sealed + Sync {
|
||||
/// Write a frame to the websocket.
|
||||
fn wisp_write_frame<'a>(
|
||||
|
@ -471,11 +478,11 @@ impl<T: WebSocketWrite> LockingWebSocketWrite for LockedWebSocketWrite<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Combines two different WebSocketReads together.
|
||||
/// Combines two different `WebSocketRead`s together.
|
||||
pub enum EitherWebSocketRead<A: WebSocketRead, B: WebSocketRead> {
|
||||
/// First WebSocketRead variant.
|
||||
/// First `WebSocketRead` variant.
|
||||
Left(A),
|
||||
/// Second WebSocketRead variant.
|
||||
/// Second `WebSocketRead` variant.
|
||||
Right(B),
|
||||
}
|
||||
impl<A: WebSocketRead, B: WebSocketRead> WebSocketRead for EitherWebSocketRead<A, B> {
|
||||
|
@ -500,11 +507,11 @@ impl<A: WebSocketRead, B: WebSocketRead> WebSocketRead for EitherWebSocketRead<A
|
|||
}
|
||||
}
|
||||
|
||||
/// Combines two different WebSocketWrites together.
|
||||
/// Combines two different `WebSocketWrite`s together.
|
||||
pub enum EitherWebSocketWrite<A: WebSocketWrite, B: WebSocketWrite> {
|
||||
/// First WebSocketWrite variant.
|
||||
/// First `WebSocketWrite` variant.
|
||||
Left(A),
|
||||
/// Second WebSocketWrite variant.
|
||||
/// Second `WebSocketWrite` variant.
|
||||
Right(B),
|
||||
}
|
||||
impl<A: WebSocketWrite, B: WebSocketWrite> WebSocketWrite for EitherWebSocketWrite<A, B> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue