compio-net 0.12.0

Networking IO for compio
Documentation
use compio_buf::BufResult;
use compio_io::{AsyncReadExt, AsyncWriteZerocopy};
use compio_net::{TcpListener, TcpStream, UdpSocket};

#[compio_macros::test]
async fn tcp_zerocopy() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let task = compio_runtime::spawn(async move { listener.accept().await.unwrap() });

    let mut tx = TcpStream::connect(&addr).await.unwrap();
    let (mut rx, _) = task.await.unwrap();

    let buffer = Vec::from(b"hello world" as &[u8]);
    let BufResult(res, fut) = tx.write_zerocopy(buffer).await;
    assert_eq!(res.unwrap(), 11);
    let buffer = fut.await;
    assert_eq!(buffer, b"hello world");

    let buf = Vec::with_capacity(11);
    let (_, buf) = rx.read_exact(buf).await.unwrap();
    assert_eq!(buf, b"hello world");
}

#[compio_macros::test]
async fn tcp_zerocopy_vectored() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let task = compio_runtime::spawn(async move { listener.accept().await.unwrap() });

    let mut tx = TcpStream::connect(&addr).await.unwrap();
    let (mut rx, _) = task.await.unwrap();

    let buffer = [
        Vec::from(b"hello" as &[u8]),
        Vec::from(b" " as &[u8]),
        Vec::from(b"world" as &[u8]),
    ];
    let BufResult(res, fut) = tx.write_zerocopy_vectored(buffer).await;
    assert_eq!(res.unwrap(), 11);
    let buffer = fut.await;
    assert_eq!(buffer[0], b"hello");
    assert_eq!(buffer[1], b" ");
    assert_eq!(buffer[2], b"world");

    let buf = Vec::with_capacity(11);
    let (_, buf) = rx.read_exact(buf).await.unwrap();
    assert_eq!(buf, b"hello world");
}

#[compio_macros::test]
async fn tcp_owned_write_half_zerocopy() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let task = compio_runtime::spawn(async move { listener.accept().await.unwrap() });

    let tx = TcpStream::connect(&addr).await.unwrap();
    let (_tx_read, mut tx_write) = tx.into_split();

    let (mut rx, _) = task.await.unwrap();

    let buffer = Vec::from(b"hello world" as &[u8]);
    let BufResult(res, fut) = tx_write.write_zerocopy(buffer).await;
    assert_eq!(res.unwrap(), 11);
    let buffer = fut.await;
    assert_eq!(buffer, b"hello world");

    let buf = Vec::with_capacity(11);
    let (_, buf) = rx.read_exact(buf).await.unwrap();
    assert_eq!(buf, b"hello world");
}

#[compio_macros::test]
async fn tcp_owned_write_half_zerocopy_vectored() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();

    let task = compio_runtime::spawn(async move { listener.accept().await.unwrap() });

    let tx = TcpStream::connect(&addr).await.unwrap();
    let (_tx_read, mut tx_write) = tx.into_split();

    let (mut rx, _) = task.await.unwrap();

    let buffer = [
        Vec::from(b"hello" as &[u8]),
        Vec::from(b" " as &[u8]),
        Vec::from(b"world" as &[u8]),
    ];
    let BufResult(res, _) = tx_write.write_zerocopy_vectored(buffer).await;
    assert_eq!(res.unwrap(), 11);

    let buf = Vec::with_capacity(11);
    let (_, buf) = rx.read_exact(buf).await.unwrap();
    assert_eq!(buf, b"hello world");
}

#[compio_macros::test]
async fn udp_zerocopy() {
    let receiver = UdpSocket::bind("127.0.0.1:0").await.unwrap();
    let addr = receiver.local_addr().unwrap();

    let sender = UdpSocket::bind("127.0.0.1:0").await.unwrap();

    let buffer = Vec::from(b"hello world" as &[u8]);
    let BufResult(res, fut) = sender.send_to_zerocopy(buffer, addr).await;
    assert_eq!(res.unwrap(), 11);
    let buffer = fut.await;
    assert_eq!(buffer, b"hello world");

    let (len, buf) = receiver.recv(Vec::with_capacity(11)).await.unwrap();
    assert_eq!(len, 11);
    assert_eq!(buf, b"hello world");
}

#[compio_macros::test]
async fn udp_zerocopy_vectored() {
    let receiver = UdpSocket::bind("127.0.0.1:0").await.unwrap();
    let addr = receiver.local_addr().unwrap();

    let sender = UdpSocket::bind("127.0.0.1:0").await.unwrap();

    let buffer = [
        Vec::from(b"hello" as &[u8]),
        Vec::from(b" " as &[u8]),
        Vec::from(b"world" as &[u8]),
    ];
    let BufResult(res, fut) = sender.send_to_zerocopy_vectored(buffer, addr).await;
    assert_eq!(res.unwrap(), 11);
    let buffer = fut.await;
    assert_eq!(buffer[0], b"hello");
    assert_eq!(buffer[1], b" ");
    assert_eq!(buffer[2], b"world");

    let (len, buf) = receiver.recv(Vec::with_capacity(11)).await.unwrap();
    assert_eq!(len, 11);
    assert_eq!(buf, b"hello world");
}