mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-30 13:43:55 +00:00
Create backlog sockets on demand
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
a739848464
commit
776fd6a892
@ -1,75 +1,99 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use alloc::{boxed::Box, sync::Arc};
|
||||
use alloc::{boxed::Box, collections::btree_set::BTreeSet, sync::Arc, vec::Vec};
|
||||
use core::{
|
||||
borrow::Borrow,
|
||||
ops::{Deref, DerefMut},
|
||||
sync::atomic::{AtomicBool, AtomicU64, AtomicU8, Ordering},
|
||||
};
|
||||
|
||||
use ostd::sync::{LocalIrqDisabled, RwLock, SpinLock, SpinLockGuard, WriteIrqDisabled};
|
||||
use keyable_arc::KeyableArc;
|
||||
use ostd::sync::{LocalIrqDisabled, SpinLock, SpinLockGuard};
|
||||
use smoltcp::{
|
||||
iface::Context,
|
||||
socket::{tcp::State, udp::UdpMetadata, PollAt},
|
||||
time::{Duration, Instant},
|
||||
wire::{IpAddress, IpEndpoint, IpRepr, TcpControl, TcpRepr, UdpRepr},
|
||||
wire::{IpEndpoint, IpRepr, TcpControl, TcpRepr, UdpRepr},
|
||||
};
|
||||
use spin::Once;
|
||||
use takeable::Takeable;
|
||||
|
||||
use super::{
|
||||
event::{SocketEventObserver, SocketEvents},
|
||||
option::RawTcpSetOption,
|
||||
option::{RawTcpOption, RawTcpSetOption},
|
||||
unbound::{new_tcp_socket, new_udp_socket},
|
||||
RawTcpSocket, RawUdpSocket, TcpStateCheck,
|
||||
};
|
||||
use crate::{ext::Ext, iface::Iface};
|
||||
use crate::{
|
||||
ext::Ext,
|
||||
iface::{BindPortConfig, BoundPort, Iface},
|
||||
};
|
||||
|
||||
pub struct BoundSocket<T: AnySocket<E>, E: Ext>(Arc<BoundSocketInner<T, E>>);
|
||||
pub struct Socket<T: Inner<E>, E: Ext>(Takeable<KeyableArc<SocketBg<T, E>>>);
|
||||
|
||||
/// [`TcpSocket`] or [`UdpSocket`].
|
||||
pub trait AnySocket<E> {
|
||||
type RawSocket;
|
||||
impl<T: Inner<E>, E: Ext> PartialEq for Socket<T, E> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0.eq(&other.0)
|
||||
}
|
||||
}
|
||||
impl<T: Inner<E>, E: Ext> Eq for Socket<T, E> {}
|
||||
impl<T: Inner<E>, E: Ext> PartialOrd for Socket<T, E> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
impl<T: Inner<E>, E: Ext> Ord for Socket<T, E> {
|
||||
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
|
||||
self.0.cmp(&other.0)
|
||||
}
|
||||
}
|
||||
impl<T: Inner<E>, E: Ext> Borrow<KeyableArc<SocketBg<T, E>>> for Socket<T, E> {
|
||||
fn borrow(&self) -> &KeyableArc<SocketBg<T, E>> {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
/// [`TcpConnectionInner`] or [`UdpSocketInner`].
|
||||
pub trait Inner<E: Ext> {
|
||||
type Observer: SocketEventObserver;
|
||||
|
||||
/// Called by [`BoundSocket::new`].
|
||||
fn new(socket: Box<Self::RawSocket>) -> Self;
|
||||
|
||||
/// Called by [`BoundSocket::drop`].
|
||||
fn on_drop(this: &Arc<BoundSocketInner<Self, E>>)
|
||||
/// Called by [`Socket::drop`].
|
||||
fn on_drop(this: &KeyableArc<SocketBg<Self, E>>)
|
||||
where
|
||||
E: Ext,
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub type BoundTcpSocket<E> = BoundSocket<TcpSocket, E>;
|
||||
pub type BoundUdpSocket<E> = BoundSocket<UdpSocket, E>;
|
||||
pub type TcpConnection<E> = Socket<TcpConnectionInner<E>, E>;
|
||||
pub type TcpListener<E> = Socket<TcpListenerInner<E>, E>;
|
||||
pub type UdpSocket<E> = Socket<UdpSocketInner, E>;
|
||||
|
||||
/// Common states shared by [`BoundTcpSocketInner`] and [`BoundUdpSocketInner`].
|
||||
pub struct BoundSocketInner<T: AnySocket<E>, E> {
|
||||
iface: Arc<dyn Iface<E>>,
|
||||
port: u16,
|
||||
socket: T,
|
||||
observer: RwLock<T::Observer, WriteIrqDisabled>,
|
||||
/// Common states shared by [`TcpConnectionBg`] and [`UdpSocketBg`].
|
||||
///
|
||||
/// In the type name, `Bg` means "background". Its meaning is described below:
|
||||
/// - A foreground socket (e.g., [`TcpConnection`]) handles system calls from the user program.
|
||||
/// - A background socket (e.g., [`TcpConnectionBg`]) handles packets from the network.
|
||||
pub struct SocketBg<T: Inner<E>, E: Ext> {
|
||||
bound: BoundPort<E>,
|
||||
inner: T,
|
||||
observer: Once<T::Observer>,
|
||||
events: AtomicU8,
|
||||
next_poll_at_ms: AtomicU64,
|
||||
}
|
||||
|
||||
/// States needed by [`BoundTcpSocketInner`] but not [`BoundUdpSocketInner`].
|
||||
pub struct TcpSocket {
|
||||
socket: SpinLock<RawTcpSocketExt, LocalIrqDisabled>,
|
||||
/// States needed by [`TcpConnectionBg`] but not [`UdpSocketBg`].
|
||||
pub struct TcpConnectionInner<E: Ext> {
|
||||
socket: SpinLock<RawTcpSocketExt<E>, LocalIrqDisabled>,
|
||||
is_dead: AtomicBool,
|
||||
}
|
||||
|
||||
struct RawTcpSocketExt {
|
||||
struct RawTcpSocketExt<E: Ext> {
|
||||
socket: Box<RawTcpSocket>,
|
||||
listener: Option<Arc<TcpListenerBg<E>>>,
|
||||
has_connected: bool,
|
||||
/// Whether the socket is in the background.
|
||||
///
|
||||
/// A background socket is a socket with its corresponding [`BoundSocket`] dropped. This means
|
||||
/// that no more user events (like `send`/`recv`) can reach the socket, but it can be in a
|
||||
/// state of waiting for certain network events (e.g., remote FIN/ACK packets), so
|
||||
/// [`BoundSocketInner`] may still be alive for a while.
|
||||
in_background: bool,
|
||||
}
|
||||
|
||||
impl Deref for RawTcpSocketExt {
|
||||
impl<E: Ext> Deref for RawTcpSocketExt<E> {
|
||||
type Target = RawTcpSocket;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@ -77,18 +101,28 @@ impl Deref for RawTcpSocketExt {
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for RawTcpSocketExt {
|
||||
impl<E: Ext> DerefMut for RawTcpSocketExt<E> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.socket
|
||||
}
|
||||
}
|
||||
|
||||
impl RawTcpSocketExt {
|
||||
fn on_new_state(&mut self) -> SocketEvents {
|
||||
if self.may_send() {
|
||||
impl<E: Ext> RawTcpSocketExt<E> {
|
||||
fn on_new_state(&mut self, this: &KeyableArc<TcpConnectionBg<E>>) -> SocketEvents {
|
||||
if self.may_send() && !self.has_connected {
|
||||
self.has_connected = true;
|
||||
|
||||
if let Some(ref listener) = self.listener {
|
||||
let mut backlog = listener.inner.lock();
|
||||
if let Some(value) = backlog.connecting.take(this) {
|
||||
backlog.connected.push(value);
|
||||
}
|
||||
listener.add_events(SocketEvents::CAN_RECV);
|
||||
}
|
||||
}
|
||||
|
||||
self.update_dead(this);
|
||||
|
||||
if self.is_peer_closed() {
|
||||
SocketEvents::PEER_CLOSED
|
||||
} else if self.is_closed() {
|
||||
@ -97,148 +131,178 @@ impl RawTcpSocketExt {
|
||||
SocketEvents::empty()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TcpSocket {
|
||||
fn lock(&self) -> SpinLockGuard<RawTcpSocketExt, LocalIrqDisabled> {
|
||||
self.socket.lock()
|
||||
}
|
||||
|
||||
/// Returns whether the TCP socket is dead.
|
||||
/// Updates whether the TCP connection is dead.
|
||||
///
|
||||
/// See [`BoundTcpSocketInner::is_dead`] for the definition of dead TCP sockets.
|
||||
fn is_dead(&self) -> bool {
|
||||
self.is_dead.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Updates whether the TCP socket is dead.
|
||||
///
|
||||
/// See [`BoundTcpSocketInner::is_dead`] for the definition of dead TCP sockets.
|
||||
/// See [`TcpConnectionBg::is_dead`] for the definition of dead TCP connections.
|
||||
///
|
||||
/// This method must be called after handling network events. However, it is not necessary to
|
||||
/// call this method after handling non-closing user events, because the socket can never be
|
||||
/// dead if user events can reach the socket.
|
||||
fn update_dead(&self, socket: &RawTcpSocketExt) {
|
||||
if socket.in_background && socket.state() == smoltcp::socket::tcp::State::Closed {
|
||||
self.is_dead.store(true, Ordering::Relaxed);
|
||||
/// dead if it is not closed.
|
||||
fn update_dead(&self, this: &KeyableArc<TcpConnectionBg<E>>) {
|
||||
if self.state() == smoltcp::socket::tcp::State::Closed {
|
||||
this.inner.is_dead.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the TCP socket in [`TimeWait`] state as dead.
|
||||
///
|
||||
/// See [`BoundTcpSocketInner::is_dead`] for the definition of dead TCP sockets.
|
||||
///
|
||||
/// [`TimeWait`]: smoltcp::socket::tcp::State::TimeWait
|
||||
fn set_dead_timewait(&self, socket: &RawTcpSocketExt) {
|
||||
debug_assert!(
|
||||
socket.in_background && socket.state() == smoltcp::socket::tcp::State::TimeWait
|
||||
);
|
||||
self.is_dead.store(true, Ordering::Relaxed);
|
||||
// According to the current smoltcp implementation, a backlog socket will return back to
|
||||
// the `Listen` state if the connection is RSTed before its establishment.
|
||||
if self.state() == smoltcp::socket::tcp::State::Listen {
|
||||
this.inner.is_dead.store(true, Ordering::Relaxed);
|
||||
|
||||
if let Some(ref listener) = self.listener {
|
||||
let mut backlog = listener.inner.lock();
|
||||
// This may fail due to race conditions, but it's fine.
|
||||
let _ = backlog.connecting.remove(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> AnySocket<E> for TcpSocket {
|
||||
type RawSocket = RawTcpSocket;
|
||||
type Observer = E::TcpEventObserver;
|
||||
|
||||
fn new(socket: Box<Self::RawSocket>) -> Self {
|
||||
impl<E: Ext> TcpConnectionInner<E> {
|
||||
fn new(socket: Box<RawTcpSocket>, listener: Option<Arc<TcpListenerBg<E>>>) -> Self {
|
||||
let socket_ext = RawTcpSocketExt {
|
||||
socket,
|
||||
listener,
|
||||
has_connected: false,
|
||||
in_background: false,
|
||||
};
|
||||
|
||||
Self {
|
||||
TcpConnectionInner {
|
||||
socket: SpinLock::new(socket_ext),
|
||||
is_dead: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
fn on_drop(this: &Arc<BoundSocketInner<Self, E>>) {
|
||||
let mut socket = this.socket.lock();
|
||||
fn lock(&self) -> SpinLockGuard<RawTcpSocketExt<E>, LocalIrqDisabled> {
|
||||
self.socket.lock()
|
||||
}
|
||||
|
||||
socket.in_background = true;
|
||||
/// Returns whether the TCP connection is dead.
|
||||
///
|
||||
/// See [`TcpConnectionBg::is_dead`] for the definition of dead TCP connections.
|
||||
fn is_dead(&self) -> bool {
|
||||
self.is_dead.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Sets the TCP connection in [`TimeWait`] state as dead.
|
||||
///
|
||||
/// See [`TcpConnectionBg::is_dead`] for the definition of dead TCP connections.
|
||||
///
|
||||
/// [`TimeWait`]: smoltcp::socket::tcp::State::TimeWait
|
||||
fn set_dead_timewait(&self, socket: &RawTcpSocketExt<E>) {
|
||||
debug_assert!(socket.state() == smoltcp::socket::tcp::State::TimeWait);
|
||||
self.is_dead.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> Inner<E> for TcpConnectionInner<E> {
|
||||
type Observer = E::TcpEventObserver;
|
||||
|
||||
fn on_drop(this: &KeyableArc<SocketBg<Self, E>>) {
|
||||
let mut socket = this.inner.lock();
|
||||
|
||||
// FIXME: Send RSTs when there is unread data.
|
||||
socket.close();
|
||||
|
||||
// A TCP socket may not be appropriate for immediate removal. We leave the removal decision
|
||||
// to the polling logic.
|
||||
// A TCP connection may not be appropriate for immediate removal. We leave the removal
|
||||
// decision to the polling logic.
|
||||
this.update_next_poll_at_ms(PollAt::Now);
|
||||
this.socket.update_dead(&socket);
|
||||
socket.update_dead(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// States needed by [`BoundUdpSocketInner`] but not [`BoundTcpSocketInner`].
|
||||
type UdpSocket = SpinLock<Box<RawUdpSocket>, LocalIrqDisabled>;
|
||||
pub struct TcpBacklog<E: Ext> {
|
||||
socket: Box<RawTcpSocket>,
|
||||
max_conn: usize,
|
||||
connecting: BTreeSet<TcpConnection<E>>,
|
||||
connected: Vec<TcpConnection<E>>,
|
||||
}
|
||||
|
||||
impl<E: Ext> AnySocket<E> for UdpSocket {
|
||||
type RawSocket = RawUdpSocket;
|
||||
pub type TcpListenerInner<E> = SpinLock<TcpBacklog<E>, LocalIrqDisabled>;
|
||||
|
||||
impl<E: Ext> Inner<E> for TcpListenerInner<E> {
|
||||
type Observer = E::TcpEventObserver;
|
||||
|
||||
fn on_drop(this: &KeyableArc<SocketBg<Self, E>>) {
|
||||
// A TCP listener can be removed immediately.
|
||||
this.bound.iface().common().remove_tcp_listener(this);
|
||||
|
||||
let (connecting, connected) = {
|
||||
let mut socket = this.inner.lock();
|
||||
(
|
||||
core::mem::take(&mut socket.connecting),
|
||||
core::mem::take(&mut socket.connected),
|
||||
)
|
||||
};
|
||||
|
||||
// The lock on `connecting`/`connected` cannot be locked after locking `self`, otherwise we
|
||||
// might get a deadlock. due to inconsistent lock order problems.
|
||||
//
|
||||
// FIXME: Send RSTs instead of going through the normal socket close process.
|
||||
drop(connecting);
|
||||
drop(connected);
|
||||
}
|
||||
}
|
||||
|
||||
/// States needed by [`UdpSocketBg`] but not [`TcpConnectionBg`].
|
||||
type UdpSocketInner = SpinLock<Box<RawUdpSocket>, LocalIrqDisabled>;
|
||||
|
||||
impl<E: Ext> Inner<E> for UdpSocketInner {
|
||||
type Observer = E::UdpEventObserver;
|
||||
|
||||
fn new(socket: Box<Self::RawSocket>) -> Self {
|
||||
Self::new(socket)
|
||||
}
|
||||
|
||||
fn on_drop(this: &Arc<BoundSocketInner<Self, E>>)
|
||||
where
|
||||
E: Ext,
|
||||
{
|
||||
this.socket.lock().close();
|
||||
fn on_drop(this: &KeyableArc<SocketBg<Self, E>>) {
|
||||
this.inner.lock().close();
|
||||
|
||||
// A UDP socket can be removed immediately.
|
||||
this.iface.common().remove_udp_socket(this);
|
||||
this.bound.iface().common().remove_udp_socket(this);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnySocket<E>, E: Ext> Drop for BoundSocket<T, E> {
|
||||
impl<T: Inner<E>, E: Ext> Drop for Socket<T, E> {
|
||||
fn drop(&mut self) {
|
||||
T::on_drop(&self.0);
|
||||
if self.0.is_usable() {
|
||||
T::on_drop(&self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) type BoundTcpSocketInner<E> = BoundSocketInner<TcpSocket, E>;
|
||||
pub(crate) type BoundUdpSocketInner<E> = BoundSocketInner<UdpSocket, E>;
|
||||
pub(crate) type TcpConnectionBg<E> = SocketBg<TcpConnectionInner<E>, E>;
|
||||
pub(crate) type TcpListenerBg<E> = SocketBg<TcpListenerInner<E>, E>;
|
||||
pub(crate) type UdpSocketBg<E> = SocketBg<UdpSocketInner, E>;
|
||||
|
||||
impl<T: AnySocket<E>, E: Ext> BoundSocket<T, E> {
|
||||
pub(crate) fn new(
|
||||
iface: Arc<dyn Iface<E>>,
|
||||
port: u16,
|
||||
socket: Box<T::RawSocket>,
|
||||
observer: T::Observer,
|
||||
) -> Self {
|
||||
Self(Arc::new(BoundSocketInner {
|
||||
iface,
|
||||
port,
|
||||
socket: T::new(socket),
|
||||
observer: RwLock::new(observer),
|
||||
impl<T: Inner<E>, E: Ext> Socket<T, E> {
|
||||
pub(crate) fn new(bound: BoundPort<E>, inner: T) -> Self {
|
||||
Self(Takeable::new(KeyableArc::new(SocketBg {
|
||||
bound,
|
||||
inner,
|
||||
observer: Once::new(),
|
||||
events: AtomicU8::new(0),
|
||||
next_poll_at_ms: AtomicU64::new(u64::MAX),
|
||||
}))
|
||||
})))
|
||||
}
|
||||
|
||||
pub(crate) fn inner(&self) -> &Arc<BoundSocketInner<T, E>> {
|
||||
pub(crate) fn inner(&self) -> &KeyableArc<SocketBg<T, E>> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnySocket<E>, E: Ext> BoundSocket<T, E> {
|
||||
/// Sets the observer whose `on_events` will be called when certain iface events happen.
|
||||
impl<T: Inner<E>, E: Ext> Socket<T, E> {
|
||||
/// Initializes the observer whose `on_events` will be called when certain iface events happen.
|
||||
///
|
||||
/// The caller needs to be responsible for race conditions if network events can occur
|
||||
/// simultaneously.
|
||||
pub fn set_observer(&self, new_observer: T::Observer) {
|
||||
*self.0.observer.write() = new_observer;
|
||||
///
|
||||
/// Calling this method on a socket whose observer has already been initialized will have no
|
||||
/// effect.
|
||||
pub fn init_observer(&self, new_observer: T::Observer) {
|
||||
self.0.observer.call_once(|| new_observer);
|
||||
}
|
||||
|
||||
pub fn local_endpoint(&self) -> Option<IpEndpoint> {
|
||||
let ip_addr = {
|
||||
let ipv4_addr = self.0.iface.ipv4_addr()?;
|
||||
IpAddress::Ipv4(ipv4_addr)
|
||||
};
|
||||
Some(IpEndpoint::new(ip_addr, self.0.port))
|
||||
self.0.bound.endpoint()
|
||||
}
|
||||
|
||||
pub fn iface(&self) -> &Arc<dyn Iface<E>> {
|
||||
&self.0.iface
|
||||
self.0.bound.iface()
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,50 +328,76 @@ impl Deref for NeedIfacePoll {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> BoundTcpSocket<E> {
|
||||
impl<E: Ext> TcpConnection<E> {
|
||||
/// Connects to a remote endpoint.
|
||||
///
|
||||
/// Polling the iface is _always_ required after this method succeeds.
|
||||
pub fn connect(
|
||||
&self,
|
||||
pub fn new_connect(
|
||||
bound: BoundPort<E>,
|
||||
remote_endpoint: IpEndpoint,
|
||||
) -> Result<(), smoltcp::socket::tcp::ConnectError> {
|
||||
let common = self.iface().common();
|
||||
let mut iface = common.interface();
|
||||
option: &RawTcpOption,
|
||||
observer: E::TcpEventObserver,
|
||||
) -> Result<Self, (BoundPort<E>, smoltcp::socket::tcp::ConnectError)> {
|
||||
let socket = {
|
||||
let mut socket = new_tcp_socket();
|
||||
|
||||
let mut socket = self.0.socket.lock();
|
||||
option.apply(&mut socket);
|
||||
|
||||
socket.connect(iface.context(), remote_endpoint, self.0.port)?;
|
||||
let common = bound.iface().common();
|
||||
let mut iface = common.interface();
|
||||
|
||||
socket.has_connected = false;
|
||||
self.0.update_next_poll_at_ms(PollAt::Now);
|
||||
if let Err(err) = socket.connect(iface.context(), remote_endpoint, bound.port()) {
|
||||
drop(iface);
|
||||
return Err((bound, err));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
socket
|
||||
};
|
||||
|
||||
let inner = TcpConnectionInner::new(socket, None);
|
||||
|
||||
let connection = Self::new(bound, inner);
|
||||
connection.0.update_next_poll_at_ms(PollAt::Now);
|
||||
connection.init_observer(observer);
|
||||
connection
|
||||
.iface()
|
||||
.common()
|
||||
.register_tcp_connection(connection.inner().clone());
|
||||
|
||||
Ok(connection)
|
||||
}
|
||||
|
||||
/// Returns the state of the connecting procedure.
|
||||
pub fn connect_state(&self) -> ConnectState {
|
||||
let socket = self.0.socket.lock();
|
||||
let socket = self.0.inner.lock();
|
||||
|
||||
if socket.state() == State::SynSent || socket.state() == State::SynReceived {
|
||||
ConnectState::Connecting
|
||||
} else if socket.has_connected {
|
||||
ConnectState::Connected
|
||||
} else if KeyableArc::strong_count(self.0.as_ref()) > 1 {
|
||||
// Now we should return `ConnectState::Refused`. However, when we do this, we must
|
||||
// guarantee that `into_bound_port` can succeed (see the method's doc comments). We can
|
||||
// only guarantee this after we have removed all `Arc<TcpConnectionBg>` in the iface's
|
||||
// socket set.
|
||||
//
|
||||
// This branch serves to avoid a race condition: if the removal process hasn't
|
||||
// finished, we will return `Connecting` so that the caller won't try to call
|
||||
// `into_bound_port` (which may fail immediately).
|
||||
ConnectState::Connecting
|
||||
} else {
|
||||
ConnectState::Refused
|
||||
}
|
||||
}
|
||||
|
||||
/// Listens at a specified endpoint.
|
||||
/// Converts back to the [`BoundPort`].
|
||||
///
|
||||
/// Polling the iface is _not_ required after this method succeeds.
|
||||
pub fn listen(
|
||||
&self,
|
||||
local_endpoint: IpEndpoint,
|
||||
) -> Result<(), smoltcp::socket::tcp::ListenError> {
|
||||
let mut socket = self.0.socket.lock();
|
||||
|
||||
socket.listen(local_endpoint)
|
||||
/// This method will succeed if the connection is fully closed and no network events can reach
|
||||
/// this connection. We guarantee that this method will always succeed if
|
||||
/// [`Self::connect_state`] returns [`ConnectState::Refused`].
|
||||
pub fn into_bound_port(mut self) -> Option<BoundPort<E>> {
|
||||
let this: TcpConnectionBg<E> = Arc::into_inner(self.0.take().into())?;
|
||||
Some(this.bound)
|
||||
}
|
||||
|
||||
/// Sends some data.
|
||||
@ -320,7 +410,7 @@ impl<E: Ext> BoundTcpSocket<E> {
|
||||
let common = self.iface().common();
|
||||
let mut iface = common.interface();
|
||||
|
||||
let mut socket = self.0.socket.lock();
|
||||
let mut socket = self.0.inner.lock();
|
||||
|
||||
let result = socket.send(f)?;
|
||||
let need_poll = self
|
||||
@ -340,7 +430,7 @@ impl<E: Ext> BoundTcpSocket<E> {
|
||||
let common = self.iface().common();
|
||||
let mut iface = common.interface();
|
||||
|
||||
let mut socket = self.0.socket.lock();
|
||||
let mut socket = self.0.inner.lock();
|
||||
|
||||
let result = socket.recv(f)?;
|
||||
let need_poll = self
|
||||
@ -354,8 +444,9 @@ impl<E: Ext> BoundTcpSocket<E> {
|
||||
///
|
||||
/// Polling the iface is _always_ required after this method succeeds.
|
||||
pub fn close(&self) {
|
||||
let mut socket = self.0.socket.lock();
|
||||
let mut socket = self.0.inner.lock();
|
||||
|
||||
socket.listener = None;
|
||||
socket.close();
|
||||
self.0.update_next_poll_at_ms(PollAt::Now);
|
||||
}
|
||||
@ -368,14 +459,14 @@ impl<E: Ext> BoundTcpSocket<E> {
|
||||
where
|
||||
F: FnOnce(&RawTcpSocket) -> R,
|
||||
{
|
||||
let socket = self.0.socket.lock();
|
||||
let socket = self.0.inner.lock();
|
||||
f(&socket)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> RawTcpSetOption for BoundTcpSocket<E> {
|
||||
fn set_keep_alive(&mut self, interval: Option<Duration>) -> NeedIfacePoll {
|
||||
let mut socket = self.0.socket.lock();
|
||||
impl<E: Ext> RawTcpSetOption for TcpConnection<E> {
|
||||
fn set_keep_alive(&self, interval: Option<Duration>) -> NeedIfacePoll {
|
||||
let mut socket = self.0.inner.lock();
|
||||
socket.set_keep_alive(interval);
|
||||
|
||||
if interval.is_some() {
|
||||
@ -386,20 +477,130 @@ impl<E: Ext> RawTcpSetOption for BoundTcpSocket<E> {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_nagle_enabled(&mut self, enabled: bool) {
|
||||
let mut socket = self.0.socket.lock();
|
||||
fn set_nagle_enabled(&self, enabled: bool) {
|
||||
let mut socket = self.0.inner.lock();
|
||||
socket.set_nagle_enabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> BoundUdpSocket<E> {
|
||||
impl<E: Ext> TcpListener<E> {
|
||||
/// Listens at a specified endpoint.
|
||||
///
|
||||
/// Polling the iface is _not_ required after this method succeeds.
|
||||
pub fn new_listen(
|
||||
bound: BoundPort<E>,
|
||||
max_conn: usize,
|
||||
option: &RawTcpOption,
|
||||
observer: E::TcpEventObserver,
|
||||
) -> Result<Self, (BoundPort<E>, smoltcp::socket::tcp::ListenError)> {
|
||||
let Some(local_endpoint) = bound.endpoint() else {
|
||||
return Err((bound, smoltcp::socket::tcp::ListenError::Unaddressable));
|
||||
};
|
||||
|
||||
let socket = {
|
||||
let mut socket = new_tcp_socket();
|
||||
|
||||
option.apply(&mut socket);
|
||||
|
||||
if let Err(err) = socket.listen(local_endpoint) {
|
||||
return Err((bound, err));
|
||||
}
|
||||
|
||||
socket
|
||||
};
|
||||
|
||||
let inner = TcpListenerInner::new(TcpBacklog {
|
||||
socket,
|
||||
max_conn,
|
||||
connecting: BTreeSet::new(),
|
||||
connected: Vec::new(),
|
||||
});
|
||||
|
||||
let listener = Self::new(bound, inner);
|
||||
listener.init_observer(observer);
|
||||
listener
|
||||
.iface()
|
||||
.common()
|
||||
.register_tcp_listener(listener.inner().clone());
|
||||
|
||||
Ok(listener)
|
||||
}
|
||||
|
||||
/// Accepts a TCP connection.
|
||||
///
|
||||
/// Polling the iface is _not_ required after this method succeeds.
|
||||
pub fn accept(&self) -> Option<(TcpConnection<E>, IpEndpoint)> {
|
||||
let accepted = {
|
||||
let mut backlog = self.0.inner.lock();
|
||||
backlog.connected.pop()?
|
||||
};
|
||||
|
||||
let remote_endpoint = {
|
||||
// The lock on `accepted` cannot be locked after locking `self`, otherwise we might get
|
||||
// a deadlock. due to inconsistent lock order problems.
|
||||
let mut socket = accepted.0.inner.lock();
|
||||
|
||||
socket.listener = None;
|
||||
socket.remote_endpoint()
|
||||
};
|
||||
|
||||
Some((accepted, remote_endpoint.unwrap()))
|
||||
}
|
||||
|
||||
/// Returns whether there is a TCP connection to accept.
|
||||
///
|
||||
/// It's the caller's responsibility to deal with race conditions when using this method.
|
||||
pub fn can_accept(&self) -> bool {
|
||||
!self.0.inner.lock().connected.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> RawTcpSetOption for TcpListener<E> {
|
||||
fn set_keep_alive(&self, interval: Option<Duration>) -> NeedIfacePoll {
|
||||
let mut backlog = self.0.inner.lock();
|
||||
backlog.socket.set_keep_alive(interval);
|
||||
|
||||
NeedIfacePoll::FALSE
|
||||
}
|
||||
|
||||
fn set_nagle_enabled(&self, enabled: bool) {
|
||||
let mut backlog = self.0.inner.lock();
|
||||
backlog.socket.set_nagle_enabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> UdpSocket<E> {
|
||||
/// Binds to a specified endpoint.
|
||||
///
|
||||
/// Polling the iface is _not_ required after this method succeeds.
|
||||
pub fn bind(&self, local_endpoint: IpEndpoint) -> Result<(), smoltcp::socket::udp::BindError> {
|
||||
let mut socket = self.0.socket.lock();
|
||||
pub fn new_bind(
|
||||
bound: BoundPort<E>,
|
||||
observer: E::UdpEventObserver,
|
||||
) -> Result<Self, (BoundPort<E>, smoltcp::socket::udp::BindError)> {
|
||||
let Some(local_endpoint) = bound.endpoint() else {
|
||||
return Err((bound, smoltcp::socket::udp::BindError::Unaddressable));
|
||||
};
|
||||
|
||||
socket.bind(local_endpoint)
|
||||
let socket = {
|
||||
let mut socket = new_udp_socket();
|
||||
|
||||
if let Err(err) = socket.bind(local_endpoint) {
|
||||
return Err((bound, err));
|
||||
}
|
||||
|
||||
socket
|
||||
};
|
||||
|
||||
let inner = UdpSocketInner::new(socket);
|
||||
|
||||
let socket = Self::new(bound, inner);
|
||||
socket.init_observer(observer);
|
||||
socket
|
||||
.iface()
|
||||
.common()
|
||||
.register_udp_socket(socket.inner().clone());
|
||||
|
||||
Ok(socket)
|
||||
}
|
||||
|
||||
/// Sends some data.
|
||||
@ -418,7 +619,7 @@ impl<E: Ext> BoundUdpSocket<E> {
|
||||
|
||||
use crate::errors::udp::SendError;
|
||||
|
||||
let mut socket = self.0.socket.lock();
|
||||
let mut socket = self.0.inner.lock();
|
||||
|
||||
if size > socket.packet_send_capacity() {
|
||||
return Err(SendError::TooLarge);
|
||||
@ -442,7 +643,7 @@ impl<E: Ext> BoundUdpSocket<E> {
|
||||
where
|
||||
F: FnOnce(&[u8], UdpMetadata) -> R,
|
||||
{
|
||||
let mut socket = self.0.socket.lock();
|
||||
let mut socket = self.0.inner.lock();
|
||||
|
||||
let (data, meta) = socket.recv()?;
|
||||
let result = f(data, meta);
|
||||
@ -458,12 +659,12 @@ impl<E: Ext> BoundUdpSocket<E> {
|
||||
where
|
||||
F: FnOnce(&RawUdpSocket) -> R,
|
||||
{
|
||||
let socket = self.0.socket.lock();
|
||||
let socket = self.0.inner.lock();
|
||||
f(&socket)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnySocket<E>, E> BoundSocketInner<T, E> {
|
||||
impl<T: Inner<E>, E: Ext> SocketBg<T, E> {
|
||||
pub(crate) fn has_events(&self) -> bool {
|
||||
self.events.load(Ordering::Relaxed) != 0
|
||||
}
|
||||
@ -474,8 +675,28 @@ impl<T: AnySocket<E>, E> BoundSocketInner<T, E> {
|
||||
let events = self.events.load(Ordering::Relaxed);
|
||||
self.events.store(0, Ordering::Relaxed);
|
||||
|
||||
let observer = self.observer.read();
|
||||
observer.on_events(SocketEvents::from_bits_truncate(events));
|
||||
if let Some(observer) = self.observer.get() {
|
||||
observer.on_events(SocketEvents::from_bits_truncate(events));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn on_dead_events(this: KeyableArc<Self>)
|
||||
where
|
||||
T::Observer: Clone,
|
||||
{
|
||||
// This method can only be called to process network events, so we assume we are holding the
|
||||
// poll lock and no race conditions can occur.
|
||||
let events = this.events.load(Ordering::Relaxed);
|
||||
this.events.store(0, Ordering::Relaxed);
|
||||
|
||||
let observer = this.observer.get().cloned();
|
||||
drop(this);
|
||||
|
||||
// Notify dead events after the `Arc` is dropped to ensure the observer sees this event
|
||||
// with the expected reference count. See `TcpConnection::connect_state` for an example.
|
||||
if let Some(ref observer) = observer {
|
||||
observer.on_events(SocketEvents::from_bits_truncate(events));
|
||||
}
|
||||
}
|
||||
|
||||
fn add_events(&self, new_events: SocketEvents) {
|
||||
@ -498,7 +719,7 @@ impl<T: AnySocket<E>, E> BoundSocketInner<T, E> {
|
||||
///
|
||||
/// The update is typically needed after new network or user events have been handled, so this
|
||||
/// method also marks that there may be new events, so that the event observer provided by
|
||||
/// [`BoundSocket::set_observer`] can be notified later.
|
||||
/// [`Socket::init_observer`] can be notified later.
|
||||
fn update_next_poll_at_ms(&self, poll_at: PollAt) -> NeedIfacePoll {
|
||||
match poll_at {
|
||||
PollAt::Now => {
|
||||
@ -522,30 +743,23 @@ impl<T: AnySocket<E>, E> BoundSocketInner<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnySocket<E>, E> BoundSocketInner<T, E> {
|
||||
pub(crate) fn port(&self) -> u16 {
|
||||
self.port
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> BoundTcpSocketInner<E> {
|
||||
/// Returns whether the TCP socket is dead.
|
||||
impl<E: Ext> TcpConnectionBg<E> {
|
||||
/// Returns whether the TCP connection is dead.
|
||||
///
|
||||
/// A TCP socket is considered dead if and only if the following two conditions are met:
|
||||
/// 1. The TCP connection is closed, so this socket cannot process any network events.
|
||||
/// 2. The socket handle [`BoundTcpSocket`] is dropped, which means that this
|
||||
/// [`BoundSocketInner`] is in background and no more user events can reach it.
|
||||
/// A TCP connection is considered dead when and only when the TCP socket is in the closed
|
||||
/// state, meaning it's no longer accepting packets from the network. This is different from
|
||||
/// the socket file being closed, which only initiates the socket close process.
|
||||
pub(crate) fn is_dead(&self) -> bool {
|
||||
self.socket.is_dead()
|
||||
self.inner.is_dead()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AnySocket<E>, E> BoundSocketInner<T, E> {
|
||||
impl<T: Inner<E>, E: Ext> SocketBg<T, E> {
|
||||
/// Returns whether an incoming packet _may_ be processed by the socket.
|
||||
///
|
||||
/// The check is intended to be lock-free and fast, but may have false positives.
|
||||
pub(crate) fn can_process(&self, dst_port: u16) -> bool {
|
||||
self.port == dst_port
|
||||
self.bound.port() == dst_port
|
||||
}
|
||||
|
||||
/// Returns whether the socket _may_ generate an outgoing packet.
|
||||
@ -563,15 +777,15 @@ pub(crate) enum TcpProcessResult {
|
||||
ProcessedWithReply(IpRepr, TcpRepr<'static>),
|
||||
}
|
||||
|
||||
impl<E: Ext> BoundTcpSocketInner<E> {
|
||||
impl<E: Ext> TcpConnectionBg<E> {
|
||||
/// Tries to process an incoming packet and returns whether the packet is processed.
|
||||
pub(crate) fn process(
|
||||
&self,
|
||||
this: &KeyableArc<Self>,
|
||||
cx: &mut Context,
|
||||
ip_repr: &IpRepr,
|
||||
tcp_repr: &TcpRepr,
|
||||
) -> TcpProcessResult {
|
||||
let mut socket = self.socket.lock();
|
||||
let mut socket = this.inner.lock();
|
||||
|
||||
if !socket.accepts(cx, ip_repr, tcp_repr) {
|
||||
return TcpProcessResult::NotProcessed;
|
||||
@ -594,7 +808,7 @@ impl<E: Ext> BoundTcpSocketInner<E> {
|
||||
&& tcp_repr.control == TcpControl::Syn
|
||||
&& tcp_repr.ack_number.is_none()
|
||||
{
|
||||
self.socket.set_dead_timewait(&socket);
|
||||
this.inner.set_dead_timewait(&socket);
|
||||
return TcpProcessResult::NotProcessed;
|
||||
}
|
||||
|
||||
@ -609,26 +823,25 @@ impl<E: Ext> BoundTcpSocketInner<E> {
|
||||
};
|
||||
|
||||
if socket.state() != old_state {
|
||||
events |= socket.on_new_state();
|
||||
events |= socket.on_new_state(this);
|
||||
}
|
||||
|
||||
self.add_events(events);
|
||||
self.update_next_poll_at_ms(socket.poll_at(cx));
|
||||
self.socket.update_dead(&socket);
|
||||
this.add_events(events);
|
||||
this.update_next_poll_at_ms(socket.poll_at(cx));
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Tries to generate an outgoing packet and dispatches the generated packet.
|
||||
pub(crate) fn dispatch<D>(
|
||||
&self,
|
||||
this: &KeyableArc<Self>,
|
||||
cx: &mut Context,
|
||||
dispatch: D,
|
||||
) -> Option<(IpRepr, TcpRepr<'static>)>
|
||||
where
|
||||
D: FnOnce(&mut Context, &IpRepr, &TcpRepr) -> Option<(IpRepr, TcpRepr<'static>)>,
|
||||
{
|
||||
let mut socket = self.socket.lock();
|
||||
let mut socket = this.inner.lock();
|
||||
|
||||
let old_state = socket.state();
|
||||
let mut events = SocketEvents::empty();
|
||||
@ -652,18 +865,76 @@ impl<E: Ext> BoundTcpSocketInner<E> {
|
||||
}
|
||||
|
||||
if socket.state() != old_state {
|
||||
events |= socket.on_new_state();
|
||||
events |= socket.on_new_state(this);
|
||||
}
|
||||
|
||||
self.add_events(events);
|
||||
self.update_next_poll_at_ms(socket.poll_at(cx));
|
||||
self.socket.update_dead(&socket);
|
||||
this.add_events(events);
|
||||
this.update_next_poll_at_ms(socket.poll_at(cx));
|
||||
|
||||
reply
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> BoundUdpSocketInner<E> {
|
||||
impl<E: Ext> TcpListenerBg<E> {
|
||||
/// Tries to process an incoming packet and returns whether the packet is processed.
|
||||
pub(crate) fn process(
|
||||
this: &KeyableArc<Self>,
|
||||
cx: &mut Context,
|
||||
ip_repr: &IpRepr,
|
||||
tcp_repr: &TcpRepr,
|
||||
) -> (TcpProcessResult, Option<KeyableArc<TcpConnectionBg<E>>>) {
|
||||
let mut backlog = this.inner.lock();
|
||||
|
||||
if !backlog.socket.accepts(cx, ip_repr, tcp_repr) {
|
||||
return (TcpProcessResult::NotProcessed, None);
|
||||
}
|
||||
|
||||
// FIXME: According to the Linux implementation, `max_conn` is the upper bound of
|
||||
// `connected.len()`. We currently limit it to `connected.len() + connecting.len()` for
|
||||
// simplicity.
|
||||
if backlog.connected.len() + backlog.connecting.len() >= backlog.max_conn {
|
||||
return (TcpProcessResult::Processed, None);
|
||||
}
|
||||
|
||||
let result = match backlog.socket.process(cx, ip_repr, tcp_repr) {
|
||||
None => TcpProcessResult::Processed,
|
||||
Some((ip_repr, tcp_repr)) => TcpProcessResult::ProcessedWithReply(ip_repr, tcp_repr),
|
||||
};
|
||||
|
||||
if backlog.socket.state() == smoltcp::socket::tcp::State::Listen {
|
||||
return (result, None);
|
||||
}
|
||||
|
||||
let new_socket = {
|
||||
let mut socket = new_tcp_socket();
|
||||
RawTcpOption::inherit(&backlog.socket, &mut socket);
|
||||
socket.listen(backlog.socket.listen_endpoint()).unwrap();
|
||||
socket
|
||||
};
|
||||
|
||||
let inner = TcpConnectionInner::new(
|
||||
core::mem::replace(&mut backlog.socket, new_socket),
|
||||
Some(this.clone().into()),
|
||||
);
|
||||
let conn = TcpConnection::new(
|
||||
this.bound
|
||||
.iface()
|
||||
.bind(BindPortConfig::CanReuse(this.bound.port()))
|
||||
.unwrap(),
|
||||
inner,
|
||||
);
|
||||
let conn_bg = conn.inner().clone();
|
||||
|
||||
let inserted = backlog.connecting.insert(conn);
|
||||
assert!(inserted);
|
||||
|
||||
conn_bg.update_next_poll_at_ms(PollAt::Now);
|
||||
|
||||
(result, Some(conn_bg))
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> UdpSocketBg<E> {
|
||||
/// Tries to process an incoming packet and returns whether the packet is processed.
|
||||
pub(crate) fn process(
|
||||
&self,
|
||||
@ -672,7 +943,7 @@ impl<E: Ext> BoundUdpSocketInner<E> {
|
||||
udp_repr: &UdpRepr,
|
||||
udp_payload: &[u8],
|
||||
) -> bool {
|
||||
let mut socket = self.socket.lock();
|
||||
let mut socket = self.inner.lock();
|
||||
|
||||
if !socket.accepts(cx, ip_repr, udp_repr) {
|
||||
return false;
|
||||
@ -697,7 +968,7 @@ impl<E: Ext> BoundUdpSocketInner<E> {
|
||||
where
|
||||
D: FnOnce(&mut Context, &IpRepr, &UdpRepr, &[u8]),
|
||||
{
|
||||
let mut socket = self.socket.lock();
|
||||
let mut socket = self.inner.lock();
|
||||
|
||||
socket
|
||||
.dispatch(cx, |cx, _meta, (ip_repr, udp_repr, udp_payload)| {
|
||||
|
@ -6,15 +6,12 @@ mod option;
|
||||
mod state;
|
||||
mod unbound;
|
||||
|
||||
pub use bound::{BoundTcpSocket, BoundUdpSocket, ConnectState, NeedIfacePoll};
|
||||
pub(crate) use bound::{BoundTcpSocketInner, BoundUdpSocketInner, TcpProcessResult};
|
||||
pub use bound::{ConnectState, NeedIfacePoll, TcpConnection, TcpListener, UdpSocket};
|
||||
pub(crate) use bound::{TcpConnectionBg, TcpListenerBg, TcpProcessResult, UdpSocketBg};
|
||||
pub use event::{SocketEventObserver, SocketEvents};
|
||||
pub use option::RawTcpSetOption;
|
||||
pub use state::{TcpState, TcpStateCheck};
|
||||
pub use unbound::{
|
||||
UnboundTcpSocket, UnboundUdpSocket, TCP_RECV_BUF_LEN, TCP_SEND_BUF_LEN, UDP_RECV_PAYLOAD_LEN,
|
||||
UDP_SEND_PAYLOAD_LEN,
|
||||
};
|
||||
pub use option::{RawTcpOption, RawTcpSetOption};
|
||||
pub use state::TcpStateCheck;
|
||||
pub use unbound::{TCP_RECV_BUF_LEN, TCP_SEND_BUF_LEN, UDP_RECV_PAYLOAD_LEN, UDP_SEND_PAYLOAD_LEN};
|
||||
|
||||
pub type RawTcpSocket = smoltcp::socket::tcp::Socket<'static>;
|
||||
pub type RawUdpSocket = smoltcp::socket::udp::Socket<'static>;
|
||||
|
@ -2,20 +2,37 @@
|
||||
|
||||
use smoltcp::time::Duration;
|
||||
|
||||
use super::NeedIfacePoll;
|
||||
use super::{NeedIfacePoll, RawTcpSocket};
|
||||
|
||||
/// A trait defines setting socket options on a raw socket.
|
||||
///
|
||||
/// TODO: When `UnboundSocket` is removed, all methods in this trait can accept
|
||||
/// `&self` instead of `&mut self` as parameter.
|
||||
pub trait RawTcpSetOption {
|
||||
/// Sets the keep alive interval.
|
||||
///
|
||||
/// Polling the iface _may_ be required after this method succeeds.
|
||||
fn set_keep_alive(&mut self, interval: Option<Duration>) -> NeedIfacePoll;
|
||||
fn set_keep_alive(&self, interval: Option<Duration>) -> NeedIfacePoll;
|
||||
|
||||
/// Enables or disables Nagle’s Algorithm.
|
||||
///
|
||||
/// Polling the iface is not required after this method succeeds.
|
||||
fn set_nagle_enabled(&mut self, enabled: bool);
|
||||
/// Polling the iface is _not_ required after this method succeeds.
|
||||
fn set_nagle_enabled(&self, enabled: bool);
|
||||
}
|
||||
|
||||
/// Socket options on a raw socket.
|
||||
pub struct RawTcpOption {
|
||||
/// The keep alive interval.
|
||||
pub keep_alive: Option<Duration>,
|
||||
/// Whether Nagle's algorithm is enabled.
|
||||
pub is_nagle_enabled: bool,
|
||||
}
|
||||
|
||||
impl RawTcpOption {
|
||||
pub(super) fn apply(&self, socket: &mut RawTcpSocket) {
|
||||
socket.set_keep_alive(self.keep_alive);
|
||||
socket.set_nagle_enabled(self.is_nagle_enabled);
|
||||
}
|
||||
|
||||
pub(super) fn inherit(from: &RawTcpSocket, to: &mut RawTcpSocket) {
|
||||
to.set_keep_alive(from.keep_alive());
|
||||
to.set_nagle_enabled(from.nagle_enabled());
|
||||
}
|
||||
}
|
||||
|
@ -2,75 +2,31 @@
|
||||
|
||||
use alloc::{boxed::Box, vec};
|
||||
|
||||
use super::{option::RawTcpSetOption, NeedIfacePoll, RawTcpSocket, RawUdpSocket};
|
||||
use super::{RawTcpSocket, RawUdpSocket};
|
||||
|
||||
pub struct UnboundSocket<T> {
|
||||
socket: Box<T>,
|
||||
pub(super) fn new_tcp_socket() -> Box<RawTcpSocket> {
|
||||
let raw_tcp_socket = {
|
||||
let rx_buffer = smoltcp::socket::tcp::SocketBuffer::new(vec![0u8; TCP_RECV_BUF_LEN]);
|
||||
let tx_buffer = smoltcp::socket::tcp::SocketBuffer::new(vec![0u8; TCP_SEND_BUF_LEN]);
|
||||
RawTcpSocket::new(rx_buffer, tx_buffer)
|
||||
};
|
||||
Box::new(raw_tcp_socket)
|
||||
}
|
||||
|
||||
pub type UnboundTcpSocket = UnboundSocket<RawTcpSocket>;
|
||||
pub type UnboundUdpSocket = UnboundSocket<RawUdpSocket>;
|
||||
|
||||
impl UnboundTcpSocket {
|
||||
pub fn new() -> Self {
|
||||
let raw_tcp_socket = {
|
||||
let rx_buffer = smoltcp::socket::tcp::SocketBuffer::new(vec![0u8; TCP_RECV_BUF_LEN]);
|
||||
let tx_buffer = smoltcp::socket::tcp::SocketBuffer::new(vec![0u8; TCP_SEND_BUF_LEN]);
|
||||
RawTcpSocket::new(rx_buffer, tx_buffer)
|
||||
};
|
||||
Self {
|
||||
socket: Box::new(raw_tcp_socket),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UnboundTcpSocket {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl RawTcpSetOption for UnboundTcpSocket {
|
||||
fn set_keep_alive(&mut self, interval: Option<smoltcp::time::Duration>) -> NeedIfacePoll {
|
||||
self.socket.set_keep_alive(interval);
|
||||
NeedIfacePoll::FALSE
|
||||
}
|
||||
|
||||
fn set_nagle_enabled(&mut self, enabled: bool) {
|
||||
self.socket.set_nagle_enabled(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
impl UnboundUdpSocket {
|
||||
pub fn new() -> Self {
|
||||
let raw_udp_socket = {
|
||||
let metadata = smoltcp::socket::udp::PacketMetadata::EMPTY;
|
||||
let rx_buffer = smoltcp::socket::udp::PacketBuffer::new(
|
||||
vec![metadata; UDP_METADATA_LEN],
|
||||
vec![0u8; UDP_RECV_PAYLOAD_LEN],
|
||||
);
|
||||
let tx_buffer = smoltcp::socket::udp::PacketBuffer::new(
|
||||
vec![metadata; UDP_METADATA_LEN],
|
||||
vec![0u8; UDP_SEND_PAYLOAD_LEN],
|
||||
);
|
||||
RawUdpSocket::new(rx_buffer, tx_buffer)
|
||||
};
|
||||
Self {
|
||||
socket: Box::new(raw_udp_socket),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UnboundUdpSocket {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> UnboundSocket<T> {
|
||||
pub(crate) fn into_raw(self) -> Box<T> {
|
||||
self.socket
|
||||
}
|
||||
pub(super) fn new_udp_socket() -> Box<RawUdpSocket> {
|
||||
let raw_udp_socket = {
|
||||
let metadata = smoltcp::socket::udp::PacketMetadata::EMPTY;
|
||||
let rx_buffer = smoltcp::socket::udp::PacketBuffer::new(
|
||||
vec![metadata; UDP_METADATA_LEN],
|
||||
vec![0u8; UDP_RECV_PAYLOAD_LEN],
|
||||
);
|
||||
let tx_buffer = smoltcp::socket::udp::PacketBuffer::new(
|
||||
vec![metadata; UDP_METADATA_LEN],
|
||||
vec![0u8; UDP_SEND_PAYLOAD_LEN],
|
||||
);
|
||||
RawUdpSocket::new(rx_buffer, tx_buffer)
|
||||
};
|
||||
Box::new(raw_udp_socket)
|
||||
}
|
||||
|
||||
// TCP socket buffer sizes:
|
||||
|
Reference in New Issue
Block a user