Crates.io | io_uring_buf_ring |
lib.rs | io_uring_buf_ring |
version | 0.1.0 |
source | src |
created_at | 2024-06-30 14:24:13.905986 |
updated_at | 2024-07-03 13:44:51.606854 |
description | Help user create and manage io-uring buffer-ring |
homepage | |
repository | https://github.com/Sherlock-Holo/io_uring_buf_ring |
max_upload_size | |
id | 1288017 |
size | 26,751 |
Help user create an io-uring buffer-ring, user no need to manage the underlying ring
use std::io::Write;
use std::net::{Ipv6Addr, TcpListener, TcpStream};
use std::{io, ptr, thread};
use std::os::fd::AsRawFd;
use io_uring::cqueue::buffer_select;
use io_uring::IoUring;
use io_uring::opcode::Read;
use io_uring::squeue::Flags;
use io_uring::types::Fd;
use io_uring_buf_ring::{BorrowedBuffer, Buffer, IoUringBufRing};
fn example() {
let mut io_uring = IoUring::new(1024).unwrap();
let buf_ring = IoUringBufRing::new(&io_uring, 1, 1, 4).unwrap();
let listener = TcpListener::bind((Ipv6Addr::LOCALHOST, 0)).unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
let mut stream = listener.accept().unwrap().0;
stream.write_all(b"test").unwrap();
});
let stream = TcpStream::connect(addr).unwrap();
let buffer = read_tcp(&mut io_uring, &buf_ring, 1, &stream, 0).unwrap();
assert_eq!(buffer.as_ref(), b"test");
drop(buffer);
let buffer = read_tcp(&mut io_uring, &buf_ring, 1, &stream, 0).unwrap();
assert!(buffer.is_empty());
drop(buffer);
unsafe { buf_ring.release(&io_uring).unwrap() }
fn read_tcp<'a, B: Buffer>(
ring: &mut IoUring,
buf_ring: &'a IoUringBufRing<B>,
buf_group: u16,
stream: &TcpStream,
len: impl Into<Option<usize>>,
) -> io::Result<BorrowedBuffer<'a, B>> {
let sqe = Read::new(
Fd(stream.as_raw_fd()),
ptr::null_mut(),
len.into().unwrap_or(0) as _,
)
.offset(0)
.buf_group(buf_group)
.build()
.flags(Flags::BUFFER_SELECT);
unsafe {
ring.submission().push(&sqe).unwrap();
}
ring.submit_and_wait(1)?;
let cqe = ring.completion().next().unwrap();
let res = cqe.result();
if res < 0 {
return Err(io::Error::from_raw_os_error(-res));
}
let bid = buffer_select(cqe.flags()).unwrap();
let buffer = unsafe { buf_ring.get_buf(bid, res as _) }.unwrap();
Ok(buffer)
}
}