[−][src]Struct interprocess::os::unix::udsocket::UdStream
A Unix domain socket byte stream, obtained either from UdStreamListener
or by connecting to an existing server.
Examples
Basic example:
use interprocess::os::unix::udsocket::UdStream; use std::io::prelude::*; let mut conn = UdStream::connect("/tmp/example1.sock")?; conn.write(b"Hello from client!"); let mut string_buffer = String::new(); conn.read_to_string(&mut string_buffer); println!("Server answered: {}", string_buffer);
Receiving and sending ancillary data:
use interprocess::os::unix::udsocket::{UdStream, AncillaryData, AncillaryDataBuf}; use std::{ io::{self, prelude::*}, borrow::Cow, fs, os::unix::io::{IntoRawFd, FromRawFd}, }; // Create one file descriptor which we will be sending. let fd = fs::File::open("/tmp/example_file.mfa")?.into_raw_fd(); // Borrow the file descriptor in a slice right away to send it later. let fds = [fd]; let fd_ancillary = AncillaryData::FileDescriptors( Cow::Borrowed(&fds), ); // Prepare valid credentials. let credentials = AncillaryData::credentials(); // Allocate a sufficient buffer for receiving ancillary data. let mut ancillary_buffer = AncillaryDataBuf::owned_with_capacity( AncillaryData::ENCODED_SIZE_OF_CREDENTIALS + AncillaryData::encoded_size_of_file_descriptors(1), ); let conn = UdStream::connect("/tmp/example2.sock")?; conn.send_ancillary( b"File descriptor and credentials from client!", [fd_ancillary, credentials].iter().map(|x| x.clone_ref()), )?; // The receive buffer size depends on the situation, but since this example // mirrors the second one from UdSocketListener, 64 is sufficient. let mut recv_buffer = [0; 64]; conn.recv_ancillary( &mut recv_buffer, &mut ancillary_buffer, )?; println!("Server answered: {}", String::from_utf8_lossy(&recv_buffer)); // Decode the received ancillary data. let (mut file_descriptors, mut cred) = (None, None); for element in ancillary_buffer.decode() { match element { AncillaryData::FileDescriptors(fds) => file_descriptors = Some(fds), AncillaryData::Credentials {pid, uid, gid} => cred = Some((pid, uid, gid)), } } let mut files = Vec::new(); if let Some(fds) = file_descriptors { // There is a possibility that zero file descriptors were sent — let's account for that. for fd in fds.iter().copied() { // This is normally unsafe, but since we know that the descriptor is not owned somewhere // else in the current process, it's fine to do this: let file = unsafe {fs::File::from_raw_fd(fd)}; files.push(file); } } for mut file in files { file.write(b"Hello foreign file descriptor!"); } if let Some(credentials) = cred { println!("Server\tPID: {}", credentials.0); println!( "\tUID: {}", credentials.1); println!( "\tGID: {}", credentials.2); }
Implementations
impl UdStream
[src]
pub fn connect<'a>(path: impl ToUdSocketPath<'a>) -> Result<Self>
[src]
Connect to a Unix domain socket server at the specified path.
Example
use interprocess::os::unix::udsocket::UdStream; let conn = UdStream::connect("/tmp/example.sock")?; // Handle the connection to the server
See ToUdSocketPath
for an example of using various string types to specify socket paths.
System calls
socket
connect
pub fn recv(&self, buf: &mut [u8]) -> Result<usize>
[src]
Receives bytes from the socket stream.
System calls
recvmsg
- Future versions may use
read
instead; for now, this method is a wrapper aroundrecv_vectored
.
- Future versions may use
pub fn recv_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
[src]
Receives bytes from the socket stream, making use of scatter input for the main data.
System calls
recvmsg
- Future versions may use
readv
instead; for now, this method is a wrapper aroundrecv_ancillary_vectored
.
- Future versions may use
pub fn recv_ancillary<'a: 'b, 'b>(
&self,
buf: &mut [u8],
abuf: &'b mut AncillaryDataBuf<'a>
) -> Result<(usize, usize)>
[src]
&self,
buf: &mut [u8],
abuf: &'b mut AncillaryDataBuf<'a>
) -> Result<(usize, usize)>
Receives both bytes and ancillary data from the socket stream.
The ancillary data buffer is automatically converted from the supplied value, if possible. For that reason, mutable slices of bytes (u8
values) can be passed directly.
System calls
recvmsg
pub fn recv_ancillary_vectored<'a: 'b, 'b>(
&self,
bufs: &[IoSliceMut<'_>],
abuf: &'b mut AncillaryDataBuf<'a>
) -> Result<(usize, usize)>
[src]
&self,
bufs: &[IoSliceMut<'_>],
abuf: &'b mut AncillaryDataBuf<'a>
) -> Result<(usize, usize)>
Receives bytes and ancillary data from the socket stream, making use of scatter input for the main data.
The ancillary data buffer is automatically converted from the supplied value, if possible. For that reason, mutable slices of bytes (u8
values) can be passed directly.
System calls
recvmsg
pub fn send(&self, buf: &[u8]) -> Result<usize>
[src]
Sends bytes into the socket stream.
System calls
sendmsg
- Future versions of
interprocess
may usewrite
instead; for now, this method is a wrapper aroundsend_vectored
.
- Future versions of
pub fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize>
[src]
Sends bytes into the socket stream, making use of gather output for the main data.
System calls
sendmsg
- Future versions of
interprocess
may usewritev
instead; for now, this method is a wrapper aroundsend_ancillary_vectored
.
- Future versions of
pub fn send_ancillary<'a>(
&self,
buf: &[u8],
ancillary_data: impl IntoIterator<Item = AncillaryData<'a>>
) -> Result<(usize, usize)>
[src]
&self,
buf: &[u8],
ancillary_data: impl IntoIterator<Item = AncillaryData<'a>>
) -> Result<(usize, usize)>
Sends bytes and ancillary data into the socket stream.
The ancillary data buffer is automatically converted from the supplied value, if possible. For that reason, slices and Vec
s of AncillaryData
can be passed directly.
System calls
sendmsg
pub fn send_ancillary_vectored<'a>(
&self,
bufs: &[IoSlice<'_>],
ancillary_data: impl IntoIterator<Item = AncillaryData<'a>>
) -> Result<(usize, usize)>
[src]
&self,
bufs: &[IoSlice<'_>],
ancillary_data: impl IntoIterator<Item = AncillaryData<'a>>
) -> Result<(usize, usize)>
Sends bytes and ancillary data into the socket stream, making use of gather output for the main data.
The ancillary data buffer is automatically converted from the supplied value, if possible. For that reason, slices and Vec
s of AncillaryData
can be passed directly.
System calls
sendmsg
Trait Implementations
impl AsRawFd for UdStream
[src]
impl Debug for UdStream
[src]
impl FromRawFd for UdStream
[src]
unsafe fn from_raw_fd(fd: c_int) -> Self
[src]
impl IntoRawFd for UdStream
[src]
fn into_raw_fd(self) -> c_int
[src]
impl Read for UdStream
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
[src]
fn is_read_vectored(&self) -> bool
[src]
unsafe fn initializer(&self) -> Initializer
[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl Write for UdStream
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize>
[src]
fn flush(&mut self) -> Result<()>
[src]
fn is_write_vectored(&self) -> bool
[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
[src]
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl RefUnwindSafe for UdStream
impl Send for UdStream
impl Sync for UdStream
impl Unpin for UdStream
impl UnwindSafe for UdStream
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,