Simplify Socket lifetimes
This commit is contained in:
parent
f5f7037045
commit
c09ca370b2
|
@ -94,8 +94,7 @@ impl Client {
|
||||||
/// Instant::now()
|
/// Instant::now()
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new<'a, 'b, 'c>(sockets: &mut SocketSet<'a, 'b, 'c>, rx_buffer: RawSocketBuffer<'b, 'c>, tx_buffer: RawSocketBuffer<'b, 'c>, now: Instant) -> Self
|
pub fn new<'a, 'b>(sockets: &mut SocketSet<'a, 'b>, rx_buffer: RawSocketBuffer<'b>, tx_buffer: RawSocketBuffer<'b>, now: Instant) -> Self
|
||||||
where 'b: 'c,
|
|
||||||
{
|
{
|
||||||
let raw_socket = RawSocket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
|
let raw_socket = RawSocket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer);
|
||||||
let raw_handle = sockets.add(raw_socket);
|
let raw_handle = sockets.add(raw_socket);
|
||||||
|
|
|
@ -1727,7 +1727,7 @@ mod test {
|
||||||
use super::{EthernetPacket, IpPacket};
|
use super::{EthernetPacket, IpPacket};
|
||||||
|
|
||||||
fn create_loopback<'a, 'b, 'c>() -> (EthernetInterface<'static, 'b, 'c, Loopback>,
|
fn create_loopback<'a, 'b, 'c>() -> (EthernetInterface<'static, 'b, 'c, Loopback>,
|
||||||
SocketSet<'static, 'a, 'b>) {
|
SocketSet<'static, 'a>) {
|
||||||
// Create a basic device
|
// Create a basic device
|
||||||
let device = Loopback::new();
|
let device = Loopback::new();
|
||||||
let ip_addrs = [
|
let ip_addrs = [
|
||||||
|
|
|
@ -46,7 +46,7 @@ impl Default for Endpoint {
|
||||||
pub type IcmpPacketMetadata = PacketMetadata<IpAddress>;
|
pub type IcmpPacketMetadata = PacketMetadata<IpAddress>;
|
||||||
|
|
||||||
/// An ICMP packet ring buffer.
|
/// An ICMP packet ring buffer.
|
||||||
pub type IcmpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpAddress>;
|
pub type IcmpSocketBuffer<'a> = PacketBuffer<'a, IpAddress>;
|
||||||
|
|
||||||
/// A ICMP socket
|
/// A ICMP socket
|
||||||
///
|
///
|
||||||
|
@ -58,10 +58,10 @@ pub type IcmpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpAddress>;
|
||||||
/// [IcmpEndpoint]: enum.IcmpEndpoint.html
|
/// [IcmpEndpoint]: enum.IcmpEndpoint.html
|
||||||
/// [bind]: #method.bind
|
/// [bind]: #method.bind
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct IcmpSocket<'a, 'b: 'a> {
|
pub struct IcmpSocket<'a> {
|
||||||
pub(crate) meta: SocketMeta,
|
pub(crate) meta: SocketMeta,
|
||||||
rx_buffer: IcmpSocketBuffer<'a, 'b>,
|
rx_buffer: IcmpSocketBuffer<'a>,
|
||||||
tx_buffer: IcmpSocketBuffer<'a, 'b>,
|
tx_buffer: IcmpSocketBuffer<'a>,
|
||||||
/// The endpoint this socket is communicating with
|
/// The endpoint this socket is communicating with
|
||||||
endpoint: Endpoint,
|
endpoint: Endpoint,
|
||||||
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||||
|
@ -72,10 +72,10 @@ pub struct IcmpSocket<'a, 'b: 'a> {
|
||||||
tx_waker: WakerRegistration,
|
tx_waker: WakerRegistration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> IcmpSocket<'a, 'b> {
|
impl<'a> IcmpSocket<'a> {
|
||||||
/// Create an ICMP socket with the given buffers.
|
/// Create an ICMP socket with the given buffers.
|
||||||
pub fn new(rx_buffer: IcmpSocketBuffer<'a, 'b>,
|
pub fn new(rx_buffer: IcmpSocketBuffer<'a>,
|
||||||
tx_buffer: IcmpSocketBuffer<'a, 'b>) -> IcmpSocket<'a, 'b> {
|
tx_buffer: IcmpSocketBuffer<'a>) -> IcmpSocket<'a> {
|
||||||
IcmpSocket {
|
IcmpSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
rx_buffer: rx_buffer,
|
rx_buffer: rx_buffer,
|
||||||
|
@ -455,8 +455,8 @@ impl<'a, 'b> IcmpSocket<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Into<Socket<'a, 'b>> for IcmpSocket<'a, 'b> {
|
impl<'a> Into<Socket<'a>> for IcmpSocket<'a> {
|
||||||
fn into(self) -> Socket<'a, 'b> {
|
fn into(self) -> Socket<'a> {
|
||||||
Socket::Icmp(self)
|
Socket::Icmp(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -467,12 +467,12 @@ mod tests_common {
|
||||||
pub use crate::wire::IpAddress;
|
pub use crate::wire::IpAddress;
|
||||||
pub use super::*;
|
pub use super::*;
|
||||||
|
|
||||||
pub fn buffer(packets: usize) -> IcmpSocketBuffer<'static, 'static> {
|
pub fn buffer(packets: usize) -> IcmpSocketBuffer<'static> {
|
||||||
IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY; packets], vec![0; 66 * packets])
|
IcmpSocketBuffer::new(vec![IcmpPacketMetadata::EMPTY; packets], vec![0; 66 * packets])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn socket(rx_buffer: IcmpSocketBuffer<'static, 'static>,
|
pub fn socket(rx_buffer: IcmpSocketBuffer<'static>,
|
||||||
tx_buffer: IcmpSocketBuffer<'static, 'static>) -> IcmpSocket<'static, 'static> {
|
tx_buffer: IcmpSocketBuffer<'static>) -> IcmpSocket<'static> {
|
||||||
IcmpSocket::new(rx_buffer, tx_buffer)
|
IcmpSocket::new(rx_buffer, tx_buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -82,17 +82,17 @@ pub(crate) enum PollAt {
|
||||||
/// [AnySocket]: trait.AnySocket.html
|
/// [AnySocket]: trait.AnySocket.html
|
||||||
/// [SocketSet::get]: struct.SocketSet.html#method.get
|
/// [SocketSet::get]: struct.SocketSet.html#method.get
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Socket<'a, 'b: 'a> {
|
pub enum Socket<'a> {
|
||||||
#[cfg(feature = "socket-raw")]
|
#[cfg(feature = "socket-raw")]
|
||||||
Raw(RawSocket<'a, 'b>),
|
Raw(RawSocket<'a>),
|
||||||
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
||||||
Icmp(IcmpSocket<'a, 'b>),
|
Icmp(IcmpSocket<'a>),
|
||||||
#[cfg(feature = "socket-udp")]
|
#[cfg(feature = "socket-udp")]
|
||||||
Udp(UdpSocket<'a, 'b>),
|
Udp(UdpSocket<'a>),
|
||||||
#[cfg(feature = "socket-tcp")]
|
#[cfg(feature = "socket-tcp")]
|
||||||
Tcp(TcpSocket<'a>),
|
Tcp(TcpSocket<'a>),
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
__Nonexhaustive(PhantomData<(&'a (), &'b ())>)
|
__Nonexhaustive(PhantomData<&'a ()>)
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! dispatch_socket {
|
macro_rules! dispatch_socket {
|
||||||
|
@ -117,7 +117,7 @@ macro_rules! dispatch_socket {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Socket<'a, 'b> {
|
impl<'a> Socket<'a> {
|
||||||
/// Return the socket handle.
|
/// Return the socket handle.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn handle(&self) -> SocketHandle {
|
pub fn handle(&self) -> SocketHandle {
|
||||||
|
@ -137,22 +137,22 @@ impl<'a, 'b> Socket<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> SocketSession for Socket<'a, 'b> {
|
impl<'a> SocketSession for Socket<'a> {
|
||||||
fn finish(&mut self) {
|
fn finish(&mut self) {
|
||||||
dispatch_socket!(mut self, |socket| socket.finish())
|
dispatch_socket!(mut self, |socket| socket.finish())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A conversion trait for network sockets.
|
/// A conversion trait for network sockets.
|
||||||
pub trait AnySocket<'a, 'b>: SocketSession + Sized {
|
pub trait AnySocket<'a>: SocketSession + Sized {
|
||||||
fn downcast<'c>(socket_ref: SocketRef<'c, Socket<'a, 'b>>) ->
|
fn downcast<'c>(socket_ref: SocketRef<'c, Socket<'a>>) ->
|
||||||
Option<SocketRef<'c, Self>>;
|
Option<SocketRef<'c, Self>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! from_socket {
|
macro_rules! from_socket {
|
||||||
($socket:ty, $variant:ident) => {
|
($socket:ty, $variant:ident) => {
|
||||||
impl<'a, 'b> AnySocket<'a, 'b> for $socket {
|
impl<'a> AnySocket<'a> for $socket {
|
||||||
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a, 'b>>) ->
|
fn downcast<'c>(ref_: SocketRef<'c, Socket<'a>>) ->
|
||||||
Option<SocketRef<'c, Self>> {
|
Option<SocketRef<'c, Self>> {
|
||||||
match SocketRef::into_inner(ref_) {
|
match SocketRef::into_inner(ref_) {
|
||||||
&mut Socket::$variant(ref mut socket) => Some(SocketRef::new(socket)),
|
&mut Socket::$variant(ref mut socket) => Some(SocketRef::new(socket)),
|
||||||
|
@ -164,10 +164,10 @@ macro_rules! from_socket {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "socket-raw")]
|
#[cfg(feature = "socket-raw")]
|
||||||
from_socket!(RawSocket<'a, 'b>, Raw);
|
from_socket!(RawSocket<'a>, Raw);
|
||||||
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
||||||
from_socket!(IcmpSocket<'a, 'b>, Icmp);
|
from_socket!(IcmpSocket<'a>, Icmp);
|
||||||
#[cfg(feature = "socket-udp")]
|
#[cfg(feature = "socket-udp")]
|
||||||
from_socket!(UdpSocket<'a, 'b>, Udp);
|
from_socket!(UdpSocket<'a>, Udp);
|
||||||
#[cfg(feature = "socket-tcp")]
|
#[cfg(feature = "socket-tcp")]
|
||||||
from_socket!(TcpSocket<'a>, Tcp);
|
from_socket!(TcpSocket<'a>, Tcp);
|
||||||
|
|
|
@ -19,31 +19,31 @@ use crate::wire::{Ipv6Repr, Ipv6Packet};
|
||||||
pub type RawPacketMetadata = PacketMetadata<()>;
|
pub type RawPacketMetadata = PacketMetadata<()>;
|
||||||
|
|
||||||
/// A UDP packet ring buffer.
|
/// A UDP packet ring buffer.
|
||||||
pub type RawSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, ()>;
|
pub type RawSocketBuffer<'a> = PacketBuffer<'a, ()>;
|
||||||
|
|
||||||
/// A raw IP socket.
|
/// A raw IP socket.
|
||||||
///
|
///
|
||||||
/// A raw socket is bound to a specific IP protocol, and owns
|
/// A raw socket is bound to a specific IP protocol, and owns
|
||||||
/// transmit and receive packet buffers.
|
/// transmit and receive packet buffers.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RawSocket<'a, 'b: 'a> {
|
pub struct RawSocket<'a> {
|
||||||
pub(crate) meta: SocketMeta,
|
pub(crate) meta: SocketMeta,
|
||||||
ip_version: IpVersion,
|
ip_version: IpVersion,
|
||||||
ip_protocol: IpProtocol,
|
ip_protocol: IpProtocol,
|
||||||
rx_buffer: RawSocketBuffer<'a, 'b>,
|
rx_buffer: RawSocketBuffer<'a>,
|
||||||
tx_buffer: RawSocketBuffer<'a, 'b>,
|
tx_buffer: RawSocketBuffer<'a>,
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
rx_waker: WakerRegistration,
|
rx_waker: WakerRegistration,
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
tx_waker: WakerRegistration,
|
tx_waker: WakerRegistration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> RawSocket<'a, 'b> {
|
impl<'a> RawSocket<'a> {
|
||||||
/// Create a raw IP socket bound to the given IP version and datagram protocol,
|
/// Create a raw IP socket bound to the given IP version and datagram protocol,
|
||||||
/// with the given buffers.
|
/// with the given buffers.
|
||||||
pub fn new(ip_version: IpVersion, ip_protocol: IpProtocol,
|
pub fn new(ip_version: IpVersion, ip_protocol: IpProtocol,
|
||||||
rx_buffer: RawSocketBuffer<'a, 'b>,
|
rx_buffer: RawSocketBuffer<'a>,
|
||||||
tx_buffer: RawSocketBuffer<'a, 'b>) -> RawSocket<'a, 'b> {
|
tx_buffer: RawSocketBuffer<'a>) -> RawSocket<'a> {
|
||||||
RawSocket {
|
RawSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
ip_version,
|
ip_version,
|
||||||
|
@ -297,8 +297,8 @@ impl<'a, 'b> RawSocket<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Into<Socket<'a, 'b>> for RawSocket<'a, 'b> {
|
impl<'a> Into<Socket<'a>> for RawSocket<'a> {
|
||||||
fn into(self) -> Socket<'a, 'b> {
|
fn into(self) -> Socket<'a> {
|
||||||
Socket::Raw(self)
|
Socket::Raw(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ mod test {
|
||||||
use crate::wire::{Ipv6Address, Ipv6Repr};
|
use crate::wire::{Ipv6Address, Ipv6Repr};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn buffer(packets: usize) -> RawSocketBuffer<'static, 'static> {
|
fn buffer(packets: usize) -> RawSocketBuffer<'static> {
|
||||||
RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; packets], vec![0; 48 * packets])
|
RawSocketBuffer::new(vec![RawPacketMetadata::EMPTY; packets], vec![0; 48 * packets])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,9 +320,9 @@ mod test {
|
||||||
mod ipv4_locals {
|
mod ipv4_locals {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub fn socket(rx_buffer: RawSocketBuffer<'static, 'static>,
|
pub fn socket(rx_buffer: RawSocketBuffer<'static>,
|
||||||
tx_buffer: RawSocketBuffer<'static, 'static>)
|
tx_buffer: RawSocketBuffer<'static>)
|
||||||
-> RawSocket<'static, 'static> {
|
-> RawSocket<'static> {
|
||||||
RawSocket::new(IpVersion::Ipv4, IpProtocol::Unknown(IP_PROTO),
|
RawSocket::new(IpVersion::Ipv4, IpProtocol::Unknown(IP_PROTO),
|
||||||
rx_buffer, tx_buffer)
|
rx_buffer, tx_buffer)
|
||||||
}
|
}
|
||||||
|
@ -352,9 +352,9 @@ mod test {
|
||||||
mod ipv6_locals {
|
mod ipv6_locals {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub fn socket(rx_buffer: RawSocketBuffer<'static, 'static>,
|
pub fn socket(rx_buffer: RawSocketBuffer<'static>,
|
||||||
tx_buffer: RawSocketBuffer<'static, 'static>)
|
tx_buffer: RawSocketBuffer<'static>)
|
||||||
-> RawSocket<'static, 'static> {
|
-> RawSocket<'static> {
|
||||||
RawSocket::new(IpVersion::Ipv6, IpProtocol::Unknown(IP_PROTO),
|
RawSocket::new(IpVersion::Ipv6, IpProtocol::Unknown(IP_PROTO),
|
||||||
rx_buffer, tx_buffer)
|
rx_buffer, tx_buffer)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,11 @@ pub trait Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "socket-raw")]
|
#[cfg(feature = "socket-raw")]
|
||||||
impl<'a, 'b> Session for RawSocket<'a, 'b> {}
|
impl<'a> Session for RawSocket<'a> {}
|
||||||
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
#[cfg(all(feature = "socket-icmp", any(feature = "proto-ipv4", feature = "proto-ipv6")))]
|
||||||
impl<'a, 'b> Session for IcmpSocket<'a, 'b> {}
|
impl<'a> Session for IcmpSocket<'a> {}
|
||||||
#[cfg(feature = "socket-udp")]
|
#[cfg(feature = "socket-udp")]
|
||||||
impl<'a, 'b> Session for UdpSocket<'a, 'b> {}
|
impl<'a> Session for UdpSocket<'a> {}
|
||||||
#[cfg(feature = "socket-tcp")]
|
#[cfg(feature = "socket-tcp")]
|
||||||
impl<'a> Session for TcpSocket<'a> {}
|
impl<'a> Session for TcpSocket<'a> {}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,8 @@ use crate::socket::TcpState;
|
||||||
/// The only reason this struct is public is to allow the socket set storage
|
/// The only reason this struct is public is to allow the socket set storage
|
||||||
/// to be allocated externally.
|
/// to be allocated externally.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Item<'a, 'b: 'a> {
|
pub struct Item<'a> {
|
||||||
socket: Socket<'a, 'b>,
|
socket: Socket<'a>,
|
||||||
refs: usize
|
refs: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,16 +27,16 @@ impl fmt::Display for Handle {
|
||||||
|
|
||||||
/// An extensible set of sockets.
|
/// An extensible set of sockets.
|
||||||
///
|
///
|
||||||
/// The lifetimes `'b` and `'c` are used when storing a `Socket<'b, 'c>`.
|
/// The lifetime `'b` is used when storing a `Socket<'b>`.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Set<'a, 'b: 'a, 'c: 'a + 'b> {
|
pub struct Set<'a, 'b: 'a> {
|
||||||
sockets: ManagedSlice<'a, Option<Item<'b, 'c>>>
|
sockets: ManagedSlice<'a, Option<Item<'b>>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
impl<'a, 'b: 'a> Set<'a, 'b> {
|
||||||
/// Create a socket set using the provided storage.
|
/// Create a socket set using the provided storage.
|
||||||
pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a, 'b, 'c>
|
pub fn new<SocketsT>(sockets: SocketsT) -> Set<'a, 'b>
|
||||||
where SocketsT: Into<ManagedSlice<'a, Option<Item<'b, 'c>>>> {
|
where SocketsT: Into<ManagedSlice<'a, Option<Item<'b>>>> {
|
||||||
let sockets = sockets.into();
|
let sockets = sockets.into();
|
||||||
Set { sockets }
|
Set { sockets }
|
||||||
}
|
}
|
||||||
|
@ -46,10 +46,10 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// This function panics if the storage is fixed-size (not a `Vec`) and is full.
|
/// This function panics if the storage is fixed-size (not a `Vec`) and is full.
|
||||||
pub fn add<T>(&mut self, socket: T) -> Handle
|
pub fn add<T>(&mut self, socket: T) -> Handle
|
||||||
where T: Into<Socket<'b, 'c>>
|
where T: Into<Socket<'b>>
|
||||||
{
|
{
|
||||||
fn put<'b, 'c>(index: usize, slot: &mut Option<Item<'b, 'c>>,
|
fn put<'b>(index: usize, slot: &mut Option<Item<'b>>,
|
||||||
mut socket: Socket<'b, 'c>) -> Handle {
|
mut socket: Socket<'b>) -> Handle {
|
||||||
net_trace!("[{}]: adding", index);
|
net_trace!("[{}]: adding", index);
|
||||||
let handle = Handle(index);
|
let handle = Handle(index);
|
||||||
socket.meta_mut().handle = handle;
|
socket.meta_mut().handle = handle;
|
||||||
|
@ -83,7 +83,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// This function may panic if the handle does not belong to this socket set
|
/// This function may panic if the handle does not belong to this socket set
|
||||||
/// or the socket has the wrong type.
|
/// or the socket has the wrong type.
|
||||||
pub fn get<T: AnySocket<'b, 'c>>(&mut self, handle: Handle) -> SocketRef<T> {
|
pub fn get<T: AnySocket<'b>>(&mut self, handle: Handle) -> SocketRef<T> {
|
||||||
match self.sockets[handle.0].as_mut() {
|
match self.sockets[handle.0].as_mut() {
|
||||||
Some(item) => {
|
Some(item) => {
|
||||||
T::downcast(SocketRef::new(&mut item.socket))
|
T::downcast(SocketRef::new(&mut item.socket))
|
||||||
|
@ -97,7 +97,7 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
/// This function may panic if the handle does not belong to this socket set.
|
/// This function may panic if the handle does not belong to this socket set.
|
||||||
pub fn remove(&mut self, handle: Handle) -> Socket<'b, 'c> {
|
pub fn remove(&mut self, handle: Handle) -> Socket<'b> {
|
||||||
net_trace!("[{}]: removing", handle.0);
|
net_trace!("[{}]: removing", handle.0);
|
||||||
match self.sockets[handle.0].take() {
|
match self.sockets[handle.0].take() {
|
||||||
Some(item) => item.socket,
|
Some(item) => item.socket,
|
||||||
|
@ -166,12 +166,12 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate every socket in this set.
|
/// Iterate every socket in this set.
|
||||||
pub fn iter<'d>(&'d self) -> Iter<'d, 'b, 'c> {
|
pub fn iter<'d>(&'d self) -> Iter<'d, 'b> {
|
||||||
Iter { lower: self.sockets.iter() }
|
Iter { lower: self.sockets.iter() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate every socket in this set, as SocketRef.
|
/// Iterate every socket in this set, as SocketRef.
|
||||||
pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'b, 'c> {
|
pub fn iter_mut<'d>(&'d mut self) -> IterMut<'d, 'b> {
|
||||||
IterMut { lower: self.sockets.iter_mut() }
|
IterMut { lower: self.sockets.iter_mut() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,12 +180,12 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Set<'a, 'b, 'c> {
|
||||||
///
|
///
|
||||||
/// This struct is created by the [iter](struct.SocketSet.html#method.iter)
|
/// This struct is created by the [iter](struct.SocketSet.html#method.iter)
|
||||||
/// on [socket sets](struct.SocketSet.html).
|
/// on [socket sets](struct.SocketSet.html).
|
||||||
pub struct Iter<'a, 'b: 'a, 'c: 'a + 'b> {
|
pub struct Iter<'a, 'b: 'a> {
|
||||||
lower: slice::Iter<'a, Option<Item<'b, 'c>>>
|
lower: slice::Iter<'a, Option<Item<'b>>>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for Iter<'a, 'b, 'c> {
|
impl<'a, 'b: 'a> Iterator for Iter<'a, 'b> {
|
||||||
type Item = &'a Socket<'b, 'c>;
|
type Item = &'a Socket<'b>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
while let Some(item_opt) = self.lower.next() {
|
while let Some(item_opt) = self.lower.next() {
|
||||||
|
@ -201,12 +201,12 @@ impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for Iter<'a, 'b, 'c> {
|
||||||
///
|
///
|
||||||
/// This struct is created by the [iter_mut](struct.SocketSet.html#method.iter_mut)
|
/// This struct is created by the [iter_mut](struct.SocketSet.html#method.iter_mut)
|
||||||
/// on [socket sets](struct.SocketSet.html).
|
/// on [socket sets](struct.SocketSet.html).
|
||||||
pub struct IterMut<'a, 'b: 'a, 'c: 'a + 'b> {
|
pub struct IterMut<'a, 'b: 'a> {
|
||||||
lower: slice::IterMut<'a, Option<Item<'b, 'c>>>,
|
lower: slice::IterMut<'a, Option<Item<'b>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b: 'a, 'c: 'a + 'b> Iterator for IterMut<'a, 'b, 'c> {
|
impl<'a, 'b: 'a> Iterator for IterMut<'a, 'b> {
|
||||||
type Item = SocketRef<'a, Socket<'b, 'c>>;
|
type Item = SocketRef<'a, Socket<'b>>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
while let Some(item_opt) = self.lower.next() {
|
while let Some(item_opt) = self.lower.next() {
|
||||||
|
|
|
@ -1943,8 +1943,8 @@ impl<'a> TcpSocket<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Into<Socket<'a, 'b>> for TcpSocket<'a> {
|
impl<'a> Into<Socket<'a>> for TcpSocket<'a> {
|
||||||
fn into(self) -> Socket<'a, 'b> {
|
fn into(self) -> Socket<'a> {
|
||||||
Socket::Tcp(self)
|
Socket::Tcp(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,18 +13,18 @@ use crate::socket::WakerRegistration;
|
||||||
pub type UdpPacketMetadata = PacketMetadata<IpEndpoint>;
|
pub type UdpPacketMetadata = PacketMetadata<IpEndpoint>;
|
||||||
|
|
||||||
/// A UDP packet ring buffer.
|
/// A UDP packet ring buffer.
|
||||||
pub type UdpSocketBuffer<'a, 'b> = PacketBuffer<'a, 'b, IpEndpoint>;
|
pub type UdpSocketBuffer<'a> = PacketBuffer<'a, IpEndpoint>;
|
||||||
|
|
||||||
/// A User Datagram Protocol socket.
|
/// A User Datagram Protocol socket.
|
||||||
///
|
///
|
||||||
/// A UDP socket is bound to a specific endpoint, and owns transmit and receive
|
/// A UDP socket is bound to a specific endpoint, and owns transmit and receive
|
||||||
/// packet buffers.
|
/// packet buffers.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct UdpSocket<'a, 'b: 'a> {
|
pub struct UdpSocket<'a> {
|
||||||
pub(crate) meta: SocketMeta,
|
pub(crate) meta: SocketMeta,
|
||||||
endpoint: IpEndpoint,
|
endpoint: IpEndpoint,
|
||||||
rx_buffer: UdpSocketBuffer<'a, 'b>,
|
rx_buffer: UdpSocketBuffer<'a>,
|
||||||
tx_buffer: UdpSocketBuffer<'a, 'b>,
|
tx_buffer: UdpSocketBuffer<'a>,
|
||||||
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
/// The time-to-live (IPv4) or hop limit (IPv6) value used in outgoing packets.
|
||||||
hop_limit: Option<u8>,
|
hop_limit: Option<u8>,
|
||||||
#[cfg(feature = "async")]
|
#[cfg(feature = "async")]
|
||||||
|
@ -33,10 +33,10 @@ pub struct UdpSocket<'a, 'b: 'a> {
|
||||||
tx_waker: WakerRegistration,
|
tx_waker: WakerRegistration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> UdpSocket<'a, 'b> {
|
impl<'a> UdpSocket<'a> {
|
||||||
/// Create an UDP socket with the given buffers.
|
/// Create an UDP socket with the given buffers.
|
||||||
pub fn new(rx_buffer: UdpSocketBuffer<'a, 'b>,
|
pub fn new(rx_buffer: UdpSocketBuffer<'a>,
|
||||||
tx_buffer: UdpSocketBuffer<'a, 'b>) -> UdpSocket<'a, 'b> {
|
tx_buffer: UdpSocketBuffer<'a>) -> UdpSocket<'a> {
|
||||||
UdpSocket {
|
UdpSocket {
|
||||||
meta: SocketMeta::default(),
|
meta: SocketMeta::default(),
|
||||||
endpoint: IpEndpoint::default(),
|
endpoint: IpEndpoint::default(),
|
||||||
|
@ -336,8 +336,8 @@ impl<'a, 'b> UdpSocket<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b> Into<Socket<'a, 'b>> for UdpSocket<'a, 'b> {
|
impl<'a> Into<Socket<'a>> for UdpSocket<'a> {
|
||||||
fn into(self) -> Socket<'a, 'b> {
|
fn into(self) -> Socket<'a> {
|
||||||
Socket::Udp(self)
|
Socket::Udp(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -352,13 +352,13 @@ mod test {
|
||||||
use crate::wire::ip::test::{MOCK_IP_ADDR_1, MOCK_IP_ADDR_2, MOCK_IP_ADDR_3};
|
use crate::wire::ip::test::{MOCK_IP_ADDR_1, MOCK_IP_ADDR_2, MOCK_IP_ADDR_3};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn buffer(packets: usize) -> UdpSocketBuffer<'static, 'static> {
|
fn buffer(packets: usize) -> UdpSocketBuffer<'static> {
|
||||||
UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; packets], vec![0; 16 * packets])
|
UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; packets], vec![0; 16 * packets])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn socket(rx_buffer: UdpSocketBuffer<'static, 'static>,
|
fn socket(rx_buffer: UdpSocketBuffer<'static>,
|
||||||
tx_buffer: UdpSocketBuffer<'static, 'static>)
|
tx_buffer: UdpSocketBuffer<'static>)
|
||||||
-> UdpSocket<'static, 'static> {
|
-> UdpSocket<'static> {
|
||||||
UdpSocket::new(rx_buffer, tx_buffer)
|
UdpSocket::new(rx_buffer, tx_buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,19 +35,19 @@ impl<H> PacketMetadata<H> {
|
||||||
|
|
||||||
/// An UDP packet ring buffer.
|
/// An UDP packet ring buffer.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PacketBuffer<'a, 'b, H: 'a> {
|
pub struct PacketBuffer<'a, H: 'a> {
|
||||||
metadata_ring: RingBuffer<'a, PacketMetadata<H>>,
|
metadata_ring: RingBuffer<'a, PacketMetadata<H>>,
|
||||||
payload_ring: RingBuffer<'b, u8>,
|
payload_ring: RingBuffer<'a, u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, H> PacketBuffer<'a, 'b, H> {
|
impl<'a, H> PacketBuffer<'a, H> {
|
||||||
/// Create a new packet buffer with the provided metadata and payload storage.
|
/// Create a new packet buffer with the provided metadata and payload storage.
|
||||||
///
|
///
|
||||||
/// Metadata storage limits the maximum _number_ of packets in the buffer and payload
|
/// Metadata storage limits the maximum _number_ of packets in the buffer and payload
|
||||||
/// storage limits the maximum _total size_ of packets.
|
/// storage limits the maximum _total size_ of packets.
|
||||||
pub fn new<MS, PS>(metadata_storage: MS, payload_storage: PS) -> PacketBuffer<'a, 'b, H>
|
pub fn new<MS, PS>(metadata_storage: MS, payload_storage: PS) -> PacketBuffer<'a, H>
|
||||||
where MS: Into<ManagedSlice<'a, PacketMetadata<H>>>,
|
where MS: Into<ManagedSlice<'a, PacketMetadata<H>>>,
|
||||||
PS: Into<ManagedSlice<'b, u8>>,
|
PS: Into<ManagedSlice<'a, u8>>,
|
||||||
{
|
{
|
||||||
PacketBuffer {
|
PacketBuffer {
|
||||||
metadata_ring: RingBuffer::new(metadata_storage),
|
metadata_ring: RingBuffer::new(metadata_storage),
|
||||||
|
@ -184,7 +184,7 @@ impl<'a, 'b, H> PacketBuffer<'a, 'b, H> {
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn buffer() -> PacketBuffer<'static, 'static, ()> {
|
fn buffer() -> PacketBuffer<'static, ()> {
|
||||||
PacketBuffer::new(vec![PacketMetadata::EMPTY; 4],
|
PacketBuffer::new(vec![PacketMetadata::EMPTY; 4],
|
||||||
vec![0u8; 16])
|
vec![0u8; 16])
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue