pub struct UniSocket<Ty = ()> { /* private fields */ }Expand description
An async Socket.
Implementations§
Source§impl<Ty> UniSocket<Ty>
impl<Ty> UniSocket<Ty>
Sourcepub fn bind(self, addr: &UniAddr) -> Result<Self>
pub fn bind(self, addr: &UniAddr) -> Result<Self>
Binds the socket to the specified address.
Notes that the address must be the one used to create the socket.
Sourcepub fn bind_device(self, addr: &UniAddr, device: Option<&str>) -> Result<Self>
pub fn bind_device(self, addr: &UniAddr, device: Option<&str>) -> Result<Self>
Sets the value for the SO_BINDTODEVICE option on this socket, then
binds the socket to the specified address.
If a socket is bound to an interface, only packets received from that
particular interface are processed by the socket. Note that this only
works for some socket types, particularly AF_INET sockets.
For those platforms, like macOS, that do not support SO_BINDTODEVICE,
this function will fallback to bind_device_by_index_v(4|6), while the
if_index obtained from the interface name with if_nametoindex(3).
When device is None, this is equivalent to calling
bind, instead of clearing the option like
Socket::bind_device.
Sourcepub fn listen(self, backlog: u32) -> Result<UniListener>
pub fn listen(self, backlog: u32) -> Result<UniListener>
Mark a socket as ready to accept incoming connection requests using
UniListener::accept.
This function directly corresponds to the listen(2) function on Unix.
An error will be returned if listen or connect has already been
called on this builder.
Sourcepub async fn connect(self, addr: &UniAddr) -> Result<UniStream>
pub async fn connect(self, addr: &UniAddr) -> Result<UniStream>
Initiates and completes a connection on this socket to the specified address.
This function directly corresponds to the connect(2) function on Unix.
An error will be returned if connect has already been called.
Sourcepub fn local_addr(&self) -> Result<UniAddr>
pub fn local_addr(&self) -> Result<UniAddr>
Sourcepub fn as_socket_ref(&self) -> SockRef<'_>
pub fn as_socket_ref(&self) -> SockRef<'_>
Returns a SockRef to the underlying socket for configuration.
Source§impl UniSocket<ListenerTy>
impl UniSocket<ListenerTy>
Sourcepub async fn accept(&self) -> Result<(UniStream, UniAddr)>
pub async fn accept(&self) -> Result<(UniStream, UniAddr)>
Accepts an incoming connection to this listener, and returns the accepted stream and the peer address.
This method will retry on non-deadly errors, including:
ECONNREFUSED.ECONNABORTED.ECONNRESET.EMFILE.
Sourcepub fn poll_accept(
&self,
cx: &mut Context<'_>,
) -> Poll<Result<(UniStream, UniAddr)>>
pub fn poll_accept( &self, cx: &mut Context<'_>, ) -> Poll<Result<(UniStream, UniAddr)>>
Accepts an incoming connection to this listener, and returns the accepted stream and the peer address.
Notes that on multiple calls to poll_accept, only
the waker from the Context passed to the most recent call is
scheduled to receive a wakeup. Unless you are implementing your own
future accepting connections, you probably want to use the asynchronous
accept method instead.
Unlike accept, this method does not handle EMFILE
(i.e., too many open files) errors and the caller may need to handle
it by itself.
Source§impl UniSocket<StreamTy>
impl UniSocket<StreamTy>
Sourcepub async fn peek(&mut self, buf: &mut [MaybeUninit<u8>]) -> Result<usize>
pub async fn peek(&mut self, buf: &mut [MaybeUninit<u8>]) -> Result<usize>
Receives data on the socket from the remote adress to which it is connected, without removing that data from the queue. On success, returns the number of bytes peeked.
Successive calls return the same data. This is accomplished by passing
MSG_PEEK as a flag to the underlying recv system call.
Sourcepub fn poll_peek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<Result<usize>>
pub fn poll_peek( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<Result<usize>>
Receives data on the socket from the remote adress to which it is connected, without removing that data from the queue. On success, returns the number of bytes peeked.
Successive calls return the same data. This is accomplished by passing
MSG_PEEK as a flag to the underlying recv system call.
Notes that on multiple calls to poll_peek, only
the waker from the Context passed to the most recent call is
scheduled to receive a wakeup. Unless you are implementing your own
future accepting connections, you probably want to use the asynchronous
accept method instead.
Sourcepub async fn read(&mut self, buf: &mut [MaybeUninit<u8>]) -> Result<usize>
pub async fn read(&mut self, buf: &mut [MaybeUninit<u8>]) -> Result<usize>
Receives data on the socket from the remote address to which it is connected.
§Cancel safety
This method is cancel safe. Once a readiness event occurs, the method
will continue to return immediately until the readiness event is
consumed by an attempt to read or write that fails with WouldBlock or
Poll::Pending.
Sourcepub async fn write(&mut self, buf: &[u8]) -> Result<usize>
pub async fn write(&mut self, buf: &[u8]) -> Result<usize>
Sends data on the socket to a connected peer.
§Cancel safety
This method is cancel safe. Once a readiness event occurs, the method
will continue to return immediately until the readiness event is
consumed by an attempt to read or write that fails with WouldBlock or
Poll::Pending.
Sourcepub fn shutdown(&mut self, shutdown: Shutdown) -> Result<()>
pub fn shutdown(&mut self, shutdown: Shutdown) -> Result<()>
Shuts down the read, write, or both halves of this connection.
This function will cause all pending and future I/O on the specified portions to return immediately with an appropriate value.
Sourcepub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf)
pub fn into_split(self) -> (OwnedReadHalf, OwnedWriteHalf)
Splits a UniStream into a read half and a write half, which can be
used to read and write the stream concurrently.
Note: dropping the write half will shutdown the write half of the stream.