pub struct NetSocket { /* private fields */ }Expand description
Net socket wrapper with optimized settings.
Implementations§
Source§impl NetSocket
impl NetSocket
Sourcepub async fn new(bind_addr: SocketAddr) -> Result<Self>
pub async fn new(bind_addr: SocketAddr) -> Result<Self>
Create a new Net socket bound to the given address with default (production) buffer sizes.
Sourcepub async fn with_config(
bind_addr: SocketAddr,
config: SocketBufferConfig,
) -> Result<Self>
pub async fn with_config( bind_addr: SocketAddr, config: SocketBufferConfig, ) -> Result<Self>
Create a new Net socket with custom buffer configuration.
Sourcepub fn from_socket(socket: UdpSocket) -> Result<Self>
pub fn from_socket(socket: UdpSocket) -> Result<Self>
Create from an existing tokio UdpSocket
Sourcepub fn local_addr(&self) -> SocketAddr
pub fn local_addr(&self) -> SocketAddr
Get the local address
Sourcepub fn socket_arc(&self) -> Arc<UdpSocket> ⓘ
pub fn socket_arc(&self) -> Arc<UdpSocket> ⓘ
Get a clone of the Arc socket
Sourcepub async fn connect(&self, addr: SocketAddr) -> Result<()>
pub async fn connect(&self, addr: SocketAddr) -> Result<()>
Connect to a remote address (for send/recv without address)
Sourcepub async fn send_to(&self, packet: &[u8], target: SocketAddr) -> Result<usize>
pub async fn send_to(&self, packet: &[u8], target: SocketAddr) -> Result<usize>
Send a packet to a specific address
Sourcepub async fn send(&self, packet: &[u8]) -> Result<usize>
pub async fn send(&self, packet: &[u8]) -> Result<usize>
Send a packet to the connected address
Sourcepub async fn recv_from(&mut self) -> Result<(Bytes, SocketAddr)>
pub async fn recv_from(&mut self) -> Result<(Bytes, SocketAddr)>
Receive a packet, returning the data and source address.
Routes through tokio’s recv_buf_from(&mut BufMut) for the same
reason as PacketReceiver::recv (crypto-session perf #130): the
legacy resize(MAX_PACKET_SIZE, 0) + recv_from(&mut [u8]) shape
memset ~1500 bytes per packet only for the kernel to overwrite them
on the next syscall. recv_buf_from writes directly into the
BytesMut’s spare capacity, so the kernel’s bytes are the first
writers and no pre-init is needed.
Sourcepub async fn recv(&mut self) -> Result<Bytes>
pub async fn recv(&mut self) -> Result<Bytes>
Receive a packet from the connected address.
Routes through tokio’s recv_buf(&mut BufMut) for the same reason
as Self::recv_from — see that method’s docs.
Sourcepub fn try_recv_from(&mut self) -> Result<Option<(Bytes, SocketAddr)>>
pub fn try_recv_from(&mut self) -> Result<Option<(Bytes, SocketAddr)>>
Try to receive a packet without blocking.
Routes through tokio’s try_recv_buf_from(&mut BufMut) for the
same reason as Self::recv_from — see that method’s docs.