Struct std::os::unix::net::SocketAncillary

source ·
pub struct SocketAncillary<'a> { /* private fields */ }
🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)
Available on (Android or Linux) and Unix only.
Expand description

Unix 套接字辅助数据结构体。

Example

#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut fds = [0; 8];
    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;

    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }
    Ok(())
}
Run

Implementations§

source§

impl<'a> SocketAncillary<'a>

source

pub fn new(buffer: &'a mut [u8]) -> Self

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

使用给定的缓冲区创建辅助数据。

Example
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::SocketAncillary;
let mut ancillary_buffer = [0; 128];
let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
Run
source

pub fn capacity(&self) -> usize

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

返回缓冲区的容量。

source

pub fn is_empty(&self) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

如果辅助数据为空,则返回 true

source

pub fn len(&self) -> usize

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

返回使用的字节数。

source

pub fn messages(&self) -> Messages<'_>

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

返回控制消息的迭代器。

source

pub fn truncated(&self) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

如果在 recv 操作期间辅助设备被截断,则为 true

Example
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];
    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;

    println!("Is truncated: {}", ancillary.truncated());
    Ok(())
}
Run
source

pub fn add_fds(&mut self, fds: &[RawFd]) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

将文件描述符添加到辅助数据。

如果缓冲区中有足够的空间,函数将返回 true。 如果没有足够的空间,则不会追加文件描述符。 从技术上讲,这意味着此操作将添加级别为 SOL_SOCKET 和类型 SCM_RIGHTS 的控制消息。

Example
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary};
use std::os::unix::io::AsRawFd;
use std::io::IoSlice;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
    ancillary.add_fds(&[sock.as_raw_fd()][..]);

    let buf = [1; 8];
    let mut bufs = &mut [IoSlice::new(&buf[..])][..];
    sock.send_vectored_with_ancillary(bufs, &mut ancillary)?;
    Ok(())
}
Run
source

pub fn add_creds(&mut self, creds: &[SocketCred]) -> bool

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

将凭证添加到辅助数据。

如果缓冲区中有足够的空间,则函数返回 true。 如果没有足够的空间,则不会,凭据。 从技术上讲,这意味着此操作会添加一个级别为 SOL_SOCKET 且类型为 SCM_CREDENTIALSSCM_CREDSSCM_CREDS2 的控制消息。

source

pub fn clear(&mut self)

🔬This is a nightly-only experimental API. (unix_socket_ancillary_data #76915)

清除辅助数据,删除所有值。

Example
#![feature(unix_socket_ancillary_data)]
use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
use std::io::IoSliceMut;

fn main() -> std::io::Result<()> {
    let sock = UnixStream::connect("/tmp/sock")?;

    let mut fds1 = [0; 8];
    let mut fds2 = [0; 8];
    let mut ancillary_buffer = [0; 128];
    let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);

    let mut buf = [1; 8];
    let mut bufs = &mut [IoSliceMut::new(&mut buf[..])][..];

    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }

    ancillary.clear();

    sock.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
    for ancillary_result in ancillary.messages() {
        if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
            for fd in scm_rights {
                println!("receive file descriptor: {fd}");
            }
        }
    }
    Ok(())
}
Run

Trait Implementations§

source§

impl<'a> Debug for SocketAncillary<'a>

source§

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

使用给定的格式化程序格式化该值。 Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for SocketAncillary<'a>

§

impl<'a> Send for SocketAncillary<'a>

§

impl<'a> Sync for SocketAncillary<'a>

§

impl<'a> Unpin for SocketAncillary<'a>

§

impl<'a> !UnwindSafe for SocketAncillary<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

获取 selfTypeIdRead more
source§

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

source§

fn borrow(&self) -> &T

从拥有的值中一成不变地借用。 Read more
source§

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

source§

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

从拥有的值中借用。 Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

返回未更改的参数。

source§

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

source§

fn into(self) -> U

调用 U::from(self)

也就是说,这种转换是 From<T> for U 实现选择执行的任何操作。

source§

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

§

type Error = Infallible

发生转换错误时返回的类型。
source§

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

执行转换。
source§

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

§

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

发生转换错误时返回的类型。
source§

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

执行转换。