Make bigtcp error wrappers consistent

This commit is contained in:
Ruihan Li
2024-12-30 15:47:08 +08:00
committed by Tate, Hongliang Tian
parent bfc71d1b27
commit 7334c93ff5
2 changed files with 29 additions and 14 deletions

View File

@ -12,6 +12,9 @@ pub enum BindError {
pub mod tcp { pub mod tcp {
pub use smoltcp::socket::tcp::{RecvError, SendError}; pub use smoltcp::socket::tcp::{RecvError, SendError};
/// An error returned by [`TcpListener::new_listen`].
///
/// [`TcpListener::new_listen`]: crate::socket::TcpListener::new_listen
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ListenError { pub enum ListenError {
InvalidState, InvalidState,
@ -29,6 +32,9 @@ pub mod tcp {
} }
} }
/// An error returned by [`TcpConnection::new_connect`].
///
/// [`TcpConnection::new_connect`]: crate::socket::TcpConnection::new_connect
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ConnectError { pub enum ConnectError {
InvalidState, InvalidState,
@ -50,13 +56,23 @@ pub mod tcp {
pub mod udp { pub mod udp {
pub use smoltcp::socket::udp::RecvError; pub use smoltcp::socket::udp::RecvError;
/// An error returned by [`BoundTcpSocket::recv`]. /// An error returned by [`UdpSocket::send`].
/// ///
/// [`BoundTcpSocket::recv`]: crate::socket::BoundTcpSocket::recv /// [`UdpSocket::send`]: crate::socket::UdpSocket::send
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum SendError { pub enum SendError {
TooLarge,
Unaddressable, Unaddressable,
BufferFull, BufferFull,
/// The packet is too large.
TooLarge,
}
impl From<smoltcp::socket::udp::SendError> for SendError {
fn from(value: smoltcp::socket::udp::SendError) -> Self {
match value {
smoltcp::socket::udp::SendError::Unaddressable => Self::Unaddressable,
smoltcp::socket::udp::SendError::BufferFull => Self::BufferFull,
}
}
} }
} }

View File

@ -23,7 +23,10 @@ use super::{
RawTcpSocket, RawUdpSocket, TcpStateCheck, RawTcpSocket, RawUdpSocket, TcpStateCheck,
}; };
use crate::{ use crate::{
errors::tcp::{ConnectError, ListenError}, errors::{
tcp::{ConnectError, ListenError},
udp::SendError,
},
ext::Ext, ext::Ext,
iface::{BindPortConfig, BoundPort, Iface}, iface::{BindPortConfig, BoundPort, Iface},
socket_table::{ConnectionKey, ListenerKey}, socket_table::{ConnectionKey, ListenerKey},
@ -31,7 +34,7 @@ use crate::{
pub struct Socket<T: Inner<E>, E: Ext>(Takeable<Arc<SocketBg<T, E>>>); pub struct Socket<T: Inner<E>, E: Ext>(Takeable<Arc<SocketBg<T, E>>>);
/// [`TcpConnectionInner`] or [`UdpSocketInner`]. /// [`TcpConnectionInner`], [`TcpListenerInner`], or [`UdpSocketInner`].
pub trait Inner<E: Ext> { pub trait Inner<E: Ext> {
type Observer: SocketEventObserver; type Observer: SocketEventObserver;
@ -59,7 +62,7 @@ pub struct SocketBg<T: Inner<E>, E: Ext> {
next_poll_at_ms: AtomicU64, next_poll_at_ms: AtomicU64,
} }
/// States needed by [`TcpConnectionBg`] but not [`UdpSocketBg`]. /// States needed by [`TcpConnectionBg`].
pub struct TcpConnectionInner<E: Ext> { pub struct TcpConnectionInner<E: Ext> {
socket: SpinLock<RawTcpSocketExt<E>, LocalIrqDisabled>, socket: SpinLock<RawTcpSocketExt<E>, LocalIrqDisabled>,
is_dead: AtomicBool, is_dead: AtomicBool,
@ -204,6 +207,7 @@ pub struct TcpBacklog<E: Ext> {
connected: Vec<TcpConnection<E>>, connected: Vec<TcpConnection<E>>,
} }
/// States needed by [`TcpListenerBg`].
pub struct TcpListenerInner<E: Ext> { pub struct TcpListenerInner<E: Ext> {
backlog: SpinLock<TcpBacklog<E>, LocalIrqDisabled>, backlog: SpinLock<TcpBacklog<E>, LocalIrqDisabled>,
listener_key: ListenerKey, listener_key: ListenerKey,
@ -242,7 +246,7 @@ impl<E: Ext> Inner<E> for TcpListenerInner<E> {
} }
} }
/// States needed by [`UdpSocketBg`] but not [`TcpConnectionBg`]. /// States needed by [`UdpSocketBg`].
type UdpSocketInner = SpinLock<Box<RawUdpSocket>, LocalIrqDisabled>; type UdpSocketInner = SpinLock<Box<RawUdpSocket>, LocalIrqDisabled>;
impl<E: Ext> Inner<E> for UdpSocketInner { impl<E: Ext> Inner<E> for UdpSocketInner {
@ -631,14 +635,10 @@ impl<E: Ext> UdpSocket<E> {
size: usize, size: usize,
meta: impl Into<UdpMetadata>, meta: impl Into<UdpMetadata>,
f: F, f: F,
) -> Result<R, crate::errors::udp::SendError> ) -> Result<R, SendError>
where where
F: FnOnce(&mut [u8]) -> R, F: FnOnce(&mut [u8]) -> R,
{ {
use smoltcp::socket::udp::SendError as SendErrorInner;
use crate::errors::udp::SendError;
let mut socket = self.0.inner.lock(); let mut socket = self.0.inner.lock();
if size > socket.packet_send_capacity() { if size > socket.packet_send_capacity() {
@ -647,8 +647,7 @@ impl<E: Ext> UdpSocket<E> {
let buffer = match socket.send(size, meta) { let buffer = match socket.send(size, meta) {
Ok(data) => data, Ok(data) => data,
Err(SendErrorInner::Unaddressable) => return Err(SendError::Unaddressable), Err(err) => return Err(err.into()),
Err(SendErrorInner::BufferFull) => return Err(SendError::BufferFull),
}; };
let result = f(buffer); let result = f(buffer);
self.0.update_next_poll_at_ms(PollAt::Now); self.0.update_next_poll_at_ms(PollAt::Now);