UniSocket

Struct UniSocket 

Source
pub struct UniSocket<Ty = ()> { /* private fields */ }
Expand description

An async Socket.

Implementations§

Source§

impl<Ty> UniSocket<Ty>

Source

pub const fn as_inner(&self) -> &AsyncFd<Socket>

Returns a reference to the inner value.

Source§

impl UniSocket

Source

pub fn new(addr: &UniAddr) -> Result<Self>

Creates a new UniSocket, and applies the given initialization function to the underlying socket.

The given address determines the socket type, and the caller should bind to / connect to the address later.

Source§

impl<Ty> UniSocket<Ty>

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn local_addr(&self) -> Result<UniAddr>

Returns the socket address of the local half of this socket.

This function directly corresponds to the getsockname(2) function on Windows and Unix.

§Notes

Depending on the OS this may return an error if the socket is not bound.

Source

pub fn as_socket_ref(&self) -> SockRef<'_>

Returns a SockRef to the underlying socket for configuration.

Source§

impl UniSocket<ListenerTy>

Source

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.
Source

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>

Source

pub fn peer_addr(&self) -> Result<UniAddr>

Returns the socket address of the remote peer of this socket.

This function directly corresponds to the getpeername(2) function on Unix.

§Notes

This returns an error if the socket is not connected.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<Ty> AsFd for UniSocket<Ty>

Source§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
Source§

impl<Ty> AsRawFd for UniSocket<Ty>

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl<Ty> AsRef<AsyncFd<Socket>> for UniSocket<Ty>

Source§

fn as_ref(&self) -> &AsyncFd<Socket>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<UniSocket<StreamTy>> for OwnedReadHalf

Source§

fn as_ref(&self) -> &UniStream

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<UniSocket<StreamTy>> for OwnedWriteHalf

Source§

fn as_ref(&self) -> &UniStream

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Debug for UniSocket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Ty> Freeze for UniSocket<Ty>

§

impl<Ty = ()> !RefUnwindSafe for UniSocket<Ty>

§

impl<Ty> Send for UniSocket<Ty>
where Ty: Send,

§

impl<Ty> Sync for UniSocket<Ty>
where Ty: Sync,

§

impl<Ty> Unpin for UniSocket<Ty>
where Ty: Unpin,

§

impl<Ty = ()> !UnwindSafe for UniSocket<Ty>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.