Struct otter_api_tests::imports::uds::UnixSeqpacketConn [−]
#[repr(transparent)]pub struct UnixSeqpacketConn { /* fields omitted */ }
Expand description
An unix domain sequential packet connection.
Sequential-packet connections have an interface similar to streams, but behave more like connected datagram sockets.
They have guaranteed in-order and reliable delivery, which unix datagrams technically doesn’t.
Operating system support
Sequential-packet sockets are supported by Linux, FreeBSD, NetBSD and Illumos, but not by for example macOS or OpenBSD.
Zero-length packets
… are best avoided:
On Linux and FreeBSD zero-length packets can be sent and received,
but there is no way to distinguish receiving one from reaching
end of connection unless the packet has an ancillary payload.
Also beware of trying to receive with a zero-length buffer,
as that will on FreeBSD (and probably other BSDs with seqpacket sockets)
always succeed even if there is no packet waiting.
Illumos and Solaris doesn’t support receiving zero-length packets at all: writes succeed but recv() will block.
Examples
What is sent separately is received separately:
let (a, b) = uds::UnixSeqpacketConn::pair().expect("Cannot create seqpacket pair"); a.send(b"first").unwrap(); a.send(b"second").unwrap(); let mut buffer_big_enough_for_both = [0; 20]; let (len, _truncated) = b.recv(&mut buffer_big_enough_for_both).unwrap(); assert_eq!(&buffer_big_enough_for_both[..len], b"first"); let (len, _truncated) = b.recv(&mut buffer_big_enough_for_both).unwrap(); assert_eq!(&buffer_big_enough_for_both[..len], b"second");
Connect to a listener on a socket file and write to it:
use uds::{UnixSeqpacketListener, UnixSeqpacketConn}; let listener = UnixSeqpacketListener::bind("seqpacket.socket") .expect("create seqpacket listener"); let conn = UnixSeqpacketConn::connect("seqpacket.socket") .expect("connect to seqpacket listener"); let message = "Hello, listener"; let sent = conn.send(message.as_bytes()).unwrap(); assert_eq!(sent, message.len()); std::fs::remove_file("seqpacket.socket").unwrap(); // clean up after ourselves
Connect to a listener on an abstract address:
use uds::{UnixSeqpacketListener, UnixSeqpacketConn, UnixSocketAddr}; let addr = UnixSocketAddr::new("@seqpacket example").unwrap(); let listener = UnixSeqpacketListener::bind_unix_addr(&addr) .expect("create abstract seqpacket listener"); let _client = UnixSeqpacketConn::connect_unix_addr(&addr) .expect("connect to abstract seqpacket listener"); let (_server, _addr) = listener.accept_unix_addr().unwrap();
Implementations
impl UnixSeqpacketConn
impl UnixSeqpacketConn
Connects to an unix seqpacket server listening at path
.
This is a wrapper around connect_unix_addr()
for convenience and compatibility with std.
pub fn connect_unix_addr(
addr: &UnixSocketAddr
) -> Result<UnixSeqpacketConn, Error>
pub fn connect_unix_addr(
addr: &UnixSocketAddr
) -> Result<UnixSeqpacketConn, Error>
Connects to an unix seqpacket server listening at addr
.
pub fn connect_from_to_unix_addr(
from: &UnixSocketAddr,
to: &UnixSocketAddr
) -> Result<UnixSeqpacketConn, Error>
pub fn connect_from_to_unix_addr(
from: &UnixSocketAddr,
to: &UnixSocketAddr
) -> Result<UnixSeqpacketConn, Error>
Binds to an address before connecting to a listening seqpacet socket.
pub fn pair() -> Result<(UnixSeqpacketConn, UnixSeqpacketConn), Error>
pub fn pair() -> Result<(UnixSeqpacketConn, UnixSeqpacketConn), Error>
Creates a pair of unix-domain seqpacket conneections connected to each other.
Examples
let (a, b) = uds::UnixSeqpacketConn::pair().unwrap(); assert!(a.local_unix_addr().unwrap().is_unnamed()); assert!(b.local_unix_addr().unwrap().is_unnamed()); a.send(b"hello").unwrap(); b.recv(&mut[0; 20]).unwrap();
pub fn local_unix_addr(&self) -> Result<UnixSocketAddr, Error>
pub fn local_unix_addr(&self) -> Result<UnixSocketAddr, Error>
Returns the address of this side of the connection.
pub fn peer_unix_addr(&self) -> Result<UnixSocketAddr, Error>
pub fn peer_unix_addr(&self) -> Result<UnixSocketAddr, Error>
Returns the address of the other side of the connection.
pub fn initial_peer_credentials(&self) -> Result<ConnCredentials, Error>
pub fn initial_peer_credentials(&self) -> Result<ConnCredentials, Error>
Returns information about the process of the peer when the connection was established.
See documentation of the returned type for details.
Returns the SELinux security context of the process that created the other end of this connection.
Will return an error on other operating systems than Linux or Android,
and also if running inside kubernetes.
On success the number of bytes used is returned. (like Read
)
The default security context is unconfined
, without any trailing NUL.
A buffor of 50 bytes is probably always big enough.
Receives a packet from the peer.
The returned bool
indicates whether the packet was truncated due to
the buffer beeing too small.
Sends a packet assembled from multiple byte slices.
pub fn recv_vectored(
&self,
buffers: &mut [IoSliceMut<'_>]
) -> Result<(usize, bool), Error>
pub fn recv_vectored(
&self,
buffers: &mut [IoSliceMut<'_>]
) -> Result<(usize, bool), Error>
Reads a packet into multiple buffers.
The returned bool
indicates whether the packet was truncated due to
too short buffers.
Sends a packet with associated file descriptors.
Receives a packet and associated file descriptors.
Receives a packet without removing it from the incoming queue.
The returned bool
indicates whether the packet was truncated due to
the buffer being too small.
Examples
let (a, b) = uds::UnixSeqpacketConn::pair().unwrap(); a.send(b"hello").unwrap(); let mut buf = [0u8; 10]; assert_eq!(b.peek(&mut buf[..1]).unwrap(), (1, true)); assert_eq!(&buf[..2], &[b'h', 0]); assert_eq!(b.peek(&mut buf).unwrap(), (5, false)); assert_eq!(&buf[..5], b"hello"); assert_eq!(b.recv(&mut buf).unwrap(), (5, false)); assert_eq!(&buf[..5], b"hello");
pub fn peek_vectored(
&self,
buffers: &mut [IoSliceMut<'_>]
) -> Result<(usize, bool), Error>
pub fn peek_vectored(
&self,
buffers: &mut [IoSliceMut<'_>]
) -> Result<(usize, bool), Error>
Receives a packet without removing it from the incoming queue.
The returned bool
indicates whether the packet was truncated due to
the combined buffers being too small.
pub fn take_error(&self) -> Result<Option<Error>, Error>
pub fn take_error(&self) -> Result<Option<Error>, Error>
Returns the value of the SO_ERROR
option.
This might only provide errors generated from nonblocking connect()
s,
which this library doesn’t support. It is therefore unlikely to be
useful, but is provided for parity with stream counterpart in std.
Examples
let (a, b) = uds::UnixSeqpacketConn::pair().unwrap(); drop(b); assert!(a.send(b"anyone there?").is_err()); assert!(a.take_error().unwrap().is_none());
pub fn try_clone(&self) -> Result<UnixSeqpacketConn, Error>
pub fn try_clone(&self) -> Result<UnixSeqpacketConn, Error>
Creates a new file descriptor also pointing to this side of this connection.
Examples
Both new and old can send and receive, and share queues:
let (a1, b) = uds::nonblocking::UnixSeqpacketConn::pair().unwrap(); let a2 = a1.try_clone().unwrap(); a1.send(b"first").unwrap(); a2.send(b"second").unwrap(); let mut buf = [0u8; 20]; let (len, _truncated) = b.recv(&mut buf).unwrap(); assert_eq!(&buf[..len], b"first"); b.send(b"hello first").unwrap(); let (len, _truncated) = b.recv(&mut buf).unwrap(); assert_eq!(&buf[..len], b"second"); b.send(b"hello second").unwrap(); let (len, _truncated) = a2.recv(&mut buf).unwrap(); assert_eq!(&buf[..len], b"hello first"); let (len, _truncated) = a1.recv(&mut buf).unwrap(); assert_eq!(&buf[..len], b"hello second");
Clone can still be used after the first one has been closed:
let (a, b1) = uds::nonblocking::UnixSeqpacketConn::pair().unwrap(); a.send(b"hello").unwrap(); let b2 = b1.try_clone().unwrap(); drop(b1); assert_eq!(b2.recv(&mut[0; 10]).unwrap().0, "hello".len());
Sets the read timeout to the duration specified.
If the value specified is None
, then recv()
and its variants will
block indefinitely.
An error is returned if the duration is zero.
The duration is rounded to microsecond precission. Currently it’s rounded down except if that would make it all zero.
Operating System Support
On Illumos (and pressumably also Solaris) timeouts appears not to work for unix domain sockets.
Examples
use std::io::ErrorKind; use std::time::Duration; use uds::UnixSeqpacketConn; let (a, b) = UnixSeqpacketConn::pair().unwrap(); a.set_read_timeout(Some(Duration::new(0, 2_000_000))).unwrap(); let error = a.recv(&mut[0; 1024]).unwrap_err(); assert_eq!(error.kind(), ErrorKind::WouldBlock);
pub fn read_timeout(&self) -> Result<Option<Duration>, Error>
pub fn read_timeout(&self) -> Result<Option<Duration>, Error>
Returns the read timeout of this socket.
None
is returned if there is no timeout.
Note that subsecond parts might have been be rounded by the OS
(in addition to the rounding to microsecond in set_read_timeout()
).
Examples
use uds::UnixSeqpacketConn; use std::time::Duration; let timeout = Some(Duration::new(2, 0)); let conn = UnixSeqpacketConn::pair().unwrap().0; conn.set_read_timeout(timeout).unwrap(); assert_eq!(conn.read_timeout().unwrap(), timeout);
Sets the write timeout to the duration specified.
If the value specified is None
, then send()
and its variants will
block indefinitely.
An error is returned if the duration is zero.
Operating System Support
On Illumos (and pressumably also Solaris) timeouts appears not to work for unix domain sockets.
Examples
let (conn, _other) = UnixSeqpacketConn::pair().unwrap(); conn.set_write_timeout(Some(Duration::new(0, 500 * 1000))).unwrap(); loop { if let Err(e) = conn.send(&[0; 1000]) { assert_eq!(e.kind(), ErrorKind::WouldBlock, "{}", e); break } }
pub fn write_timeout(&self) -> Result<Option<Duration>, Error>
pub fn write_timeout(&self) -> Result<Option<Duration>, Error>
Returns the write timeout of this socket.
None
is returned if there is no timeout.
Examples
let conn = uds::UnixSeqpacketConn::pair().unwrap().0; assert!(conn.write_timeout().unwrap().is_none());
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<(), Error>
Enables or disables nonblocking mode.
Consider using the nonblocking variant of this type instead.
This method mainly exists for feature parity with std’s UnixStream
.
Examples
Trying to receive when there are no packets waiting:
let (a, b) = UnixSeqpacketConn::pair().expect("create seqpacket pair"); a.set_nonblocking(true).unwrap(); assert_eq!(a.recv(&mut[0; 20]).unwrap_err().kind(), ErrorKind::WouldBlock);
Trying to send when the OS buffer for the connection is full:
let (a, b) = UnixSeqpacketConn::pair().expect("create seqpacket pair"); a.set_nonblocking(true).unwrap(); loop { if let Err(error) = a.send(&[b'#'; 1000]) { assert_eq!(error.kind(), ErrorKind::WouldBlock); break; } }
Trait Implementations
impl AsRawFd for UnixSeqpacketConn
impl AsRawFd for UnixSeqpacketConn
impl Debug for UnixSeqpacketConn
impl Debug for UnixSeqpacketConn
impl Drop for UnixSeqpacketConn
impl Drop for UnixSeqpacketConn
impl FromRawFd for UnixSeqpacketConn
impl FromRawFd for UnixSeqpacketConn
pub unsafe fn from_raw_fd(fd: i32) -> UnixSeqpacketConn
pub unsafe fn from_raw_fd(fd: i32) -> UnixSeqpacketConn
Constructs a new instance of Self
from the given raw file
descriptor. Read more
impl IntoRawFd for UnixSeqpacketConn
impl IntoRawFd for UnixSeqpacketConn
pub fn into_raw_fd(self) -> i32
pub fn into_raw_fd(self) -> i32
Consumes this object, returning the raw underlying file descriptor. Read more
Auto Trait Implementations
impl RefUnwindSafe for UnixSeqpacketConn
impl Send for UnixSeqpacketConn
impl Sync for UnixSeqpacketConn
impl Unpin for UnixSeqpacketConn
impl UnwindSafe for UnixSeqpacketConn
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<R, Global>
impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>ⓘNotable traits for Box<R, Global>
impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
A: Allocator,
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
A: Allocator + 'static,
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
impl<A> DynCastExt for A
impl<A> DynCastExt for A
pub fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
T: ?Sized,
A: DynCastExtHelper<T>,
pub fn dyn_cast<T>(
self
) -> Result<<A as DynCastExtHelper<T>>::Target, <A as DynCastExtHelper<T>>::Source> where
T: ?Sized,
A: DynCastExtHelper<T>,
Use this to cast from one trait object type to another. Read more
pub fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
T: ?Sized,
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
pub fn dyn_upcast<T>(self) -> <A as DynCastExtAdvHelper<T, T>>::Target where
T: ?Sized,
A: DynCastExtAdvHelper<T, T, Source = <A as DynCastExtAdvHelper<T, T>>::Target>,
Use this to upcast a trait to one of its supertraits. Read more
pub fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
T: ?Sized,
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
pub fn dyn_cast_adv<F, T>(
self
) -> Result<<A as DynCastExtAdvHelper<F, T>>::Target, <A as DynCastExtAdvHelper<F, T>>::Source> where
T: ?Sized,
A: DynCastExtAdvHelper<F, T>,
F: ?Sized,
pub fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
pub fn dyn_cast_with_config<C>(
self
) -> Result<<A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Target, <A as DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>>::Source> where
C: DynCastConfig,
A: DynCastExtAdvHelper<<C as DynCastConfig>::Source, <C as DynCastConfig>::Target>,
Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>
impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
[src]
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>
impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
[src]Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>
impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
[src]
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>
impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
[src]pub fn vzip(self) -> V