pub struct UdSocket(/* private fields */);
tokio_support
only.Expand description
A Unix domain datagram socket, obtained either from UdSocketListener
or by connecting to an existing server.
§Examples
Implementations§
Source§impl UdSocket
impl UdSocket
Sourcepub fn bind<'a>(path: impl ToUdSocketPath<'a>) -> Result<Self>
pub fn bind<'a>(path: impl ToUdSocketPath<'a>) -> Result<Self>
Creates a named datagram socket assigned to the specified path. This will be the “home” of this socket. Then, packets from somewhere else directed to this socket with [.send_to()
] or .connect()
will go here.
See ToUdSocketPath
for an example of using various string types to specify socket paths.
Sourcepub fn set_destination<'a>(&self, path: impl ToUdSocketPath<'a>) -> Result<()>
pub fn set_destination<'a>(&self, path: impl ToUdSocketPath<'a>) -> Result<()>
Selects the Unix domain socket to send packets to. You can also just use .send_to()
instead, but supplying the address to the kernel once is more efficient.
See ToUdSocketPath
for an example of using various string types to specify socket paths.
Sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
Shuts down the read, write, or both halves of the socket. See Shutdown
.
Attempting to call this method with the same how
argument multiple times may return Ok(())
every time or it may return an error the second time it is called, depending on the platform. You must either avoid using the same value twice or ignore the error entirely.
Sourcepub async fn recv(&self, buf: &mut ReadBuf<'_>) -> Result<()>
pub async fn recv(&self, buf: &mut ReadBuf<'_>) -> Result<()>
Receives a single datagram from the socket, advancing the ReadBuf
cursor by the datagram length.
Uses Tokio’s ReadBuf
interface. See .recv_stdbuf()
for a &mut [u8]
version.
Sourcepub async fn recv_stdbuf(&self, buf: &mut [u8]) -> Result<usize>
pub async fn recv_stdbuf(&self, buf: &mut [u8]) -> Result<usize>
Receives a single datagram from the socket, advancing the ReadBuf
cursor by the datagram length.
Uses an std
-like &mut [u8]
interface. See .recv()
for a version which uses Tokio’s ReadBuf
instead.
Sourcepub async fn recv_ready(&self) -> Result<()>
pub async fn recv_ready(&self) -> Result<()>
Asynchronously waits until readable data arrives to the socket.
May finish spuriously – do not perform a blocking read when this future finishes and do handle a WouldBlock
or Poll::Pending
.
Sourcepub async fn send(&self, buf: &[u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>
Sends a single datagram into the socket, returning how many bytes were actually sent.
Sourcepub async fn send_to(
&self,
buf: &[u8],
path: impl ToUdSocketPath<'_>,
) -> Result<usize>
pub async fn send_to( &self, buf: &[u8], path: impl ToUdSocketPath<'_>, ) -> Result<usize>
Sends a single datagram to the given address, returning how many bytes were actually sent.
Sourcepub async fn send_ready(&self) -> Result<()>
pub async fn send_ready(&self) -> Result<()>
Asynchronously waits until the socket becomes writable due to the other side freeing up space in its OS receive buffer.
May finish spuriously – do not perform a blocking write when this future finishes and do handle a WouldBlock
or Poll::Pending
.
Sourcepub fn poll_recv(
&self,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<Result<()>>
pub fn poll_recv( &self, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<Result<()>>
Raw polling interface for receiving datagrams. You probably want .recv()
instead.
Sourcepub fn poll_recv_stdbuf(
&self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<()>>
pub fn poll_recv_stdbuf( &self, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<()>>
Raw polling interface for receiving datagrams with an std
-like receive buffer. You probably want .recv_stdbuf()
instead.
Sourcepub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
pub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
Raw polling interface for sending datagrams. You probably want .send()
instead.
Sourcepub fn poll_send_to<'a>(
&self,
cx: &mut Context<'_>,
buf: &[u8],
path: impl ToUdSocketPath<'a>,
) -> Poll<Result<usize>>
pub fn poll_send_to<'a>( &self, cx: &mut Context<'_>, buf: &[u8], path: impl ToUdSocketPath<'a>, ) -> Poll<Result<usize>>
Raw polling interface for sending datagrams. You probably want .send_to()
instead.
Sourcepub fn get_peer_credentials(&self) -> Result<ucred>
Available on Linux and (GNU or musl or target_env="musleabi"
or target_env="musleabihf"
), or Emscripten, or Redox, or Haiku only.
pub fn get_peer_credentials(&self) -> Result<ucred>
target_env="musleabi"
or target_env="musleabihf"
), or Emscripten, or Redox, or Haiku only.Fetches the credentials of the other end of the connection without using ancillary data. The returned structure contains the process identifier, user identifier and group identifier of the peer.
Sourcepub fn into_sync(self) -> Result<SyncUdSocket>
pub fn into_sync(self) -> Result<SyncUdSocket>
Detaches the async object from the Tokio runtime (therefore has to be called within the runtime) and converts it to a blocking one.
Sourcepub fn from_sync(sync: SyncUdSocket) -> Result<Self>
pub fn from_sync(sync: SyncUdSocket) -> Result<Self>
Creates a Tokio-based async object from a blocking one. This will also attach the object to the Tokio runtime this function is called in, so calling it outside a runtime will result in an error.
Sourcepub fn into_std(self) -> Result<StdUdSocket>
pub fn into_std(self) -> Result<StdUdSocket>
Detaches the async object from the Tokio runtime and converts it to a blocking one from the standard library. Returns an error if called outside a Tokio runtime context.
Sourcepub fn from_std(std: StdUdSocket) -> Result<Self>
pub fn from_std(std: StdUdSocket) -> Result<Self>
Creates a Tokio-based async object from a blocking one from the standard library. This will also attach the object to the Tokio runtime this function is called in, so calling it outside a runtime will result in an error.
Sourcepub fn into_tokio(self) -> TokioUdSocket
pub fn into_tokio(self) -> TokioUdSocket
Unwraps into Tokio’s corresponding type. This is a zero-cost operation.
Sourcepub fn from_tokio(tokio: TokioUdSocket) -> Self
pub fn from_tokio(tokio: TokioUdSocket) -> Self
Wraps Tokio’s corresponding type. This is a zero-cost operation.
Sourcepub unsafe fn from_raw_fd(fd: c_int) -> Result<Self>
pub unsafe fn from_raw_fd(fd: c_int) -> Result<Self>
Creates a Tokio-based async object from a given raw file descriptor. This will also attach the object to the Tokio runtime this function is called in, so calling it outside a runtime will result in an error (which is why the FromRawFd
trait can’t be implemented instead).
§Safety
The given file descriptor must be valid (i.e. refer to an existing kernel object) and must not be owned by any other file descriptor container. If this is not upheld, an arbitrary file descriptor will be closed when the returned object is dropped.
Sourcepub fn into_raw_fd(self) -> Result<c_int>
pub fn into_raw_fd(self) -> Result<c_int>
Releases ownership of the raw file descriptor, detaches the object from the Tokio runtime (therefore has to be called within the runtime) and returns the file descriptor as an integer.