use std::future::Future;
use std::io::Result;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, Instant};
use orengine_macros::{poll_for_io_request, poll_for_time_bounded_io_request};
use crate as orengine;
use crate::io::io_request_data::IoRequestData;
use crate::io::sys::{AsRawFd, RawFd};
use crate::io::worker::{local_worker, IoWorker};
use crate::io::FixedBufferMut;
pub struct RecvBytes<'buf> {
fd: RawFd,
buf: &'buf mut [u8],
io_request_data: Option<IoRequestData>,
}
impl<'buf> RecvBytes<'buf> {
pub fn new(fd: RawFd, buf: &'buf mut [u8]) -> Self {
Self {
fd,
buf,
io_request_data: None,
}
}
}
impl Future for RecvBytes<'_> {
type Output = Result<usize>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never receive more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let ret;
poll_for_io_request!((
local_worker().recv(
this.fd,
this.buf.as_mut_ptr(),
this.buf.len() as u32,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() }
),
ret
));
}
}
unsafe impl Send for RecvBytes<'_> {}
pub struct RecvFixed<'buf> {
fd: RawFd,
ptr: *mut u8,
len: u32,
fixed_index: u16,
io_request_data: Option<IoRequestData>,
phantom_data: PhantomData<&'buf [u8]>,
}
impl RecvFixed<'_> {
pub fn new(fd: RawFd, ptr: *mut u8, len: u32, fixed_index: u16) -> Self {
Self {
fd,
ptr,
len,
fixed_index,
io_request_data: None,
phantom_data: PhantomData,
}
}
}
impl Future for RecvFixed<'_> {
type Output = Result<u32>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never receive more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let ret;
poll_for_io_request!((
local_worker().recv_fixed(this.fd, this.ptr, this.len, this.fixed_index, unsafe {
this.io_request_data.as_mut().unwrap_unchecked()
}),
ret as u32
));
}
}
unsafe impl Send for RecvFixed<'_> {}
pub struct RecvBytesWithDeadline<'buf> {
fd: RawFd,
buf: &'buf mut [u8],
io_request_data: Option<IoRequestData>,
deadline: Instant,
}
impl<'buf> RecvBytesWithDeadline<'buf> {
pub fn new(fd: RawFd, buf: &'buf mut [u8], deadline: Instant) -> Self {
Self {
fd,
buf,
io_request_data: None,
deadline,
}
}
}
impl Future for RecvBytesWithDeadline<'_> {
type Output = Result<usize>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never receive more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let worker = local_worker();
let ret;
poll_for_time_bounded_io_request!((
worker.recv_with_deadline(
this.fd,
this.buf.as_mut_ptr(),
this.buf.len() as u32,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() },
&mut this.deadline
),
ret
));
}
}
unsafe impl Send for RecvBytesWithDeadline<'_> {}
pub struct RecvFixedWithDeadline<'buf> {
fd: RawFd,
ptr: *mut u8,
len: u32,
fixed_index: u16,
io_request_data: Option<IoRequestData>,
deadline: Instant,
phantom_data: PhantomData<&'buf [u8]>,
}
impl RecvFixedWithDeadline<'_> {
pub fn new(fd: RawFd, ptr: *mut u8, len: u32, fixed_index: u16, deadline: Instant) -> Self {
Self {
fd,
ptr,
len,
fixed_index,
io_request_data: None,
deadline,
phantom_data: PhantomData,
}
}
}
impl Future for RecvFixedWithDeadline<'_> {
type Output = Result<u32>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never receive more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let worker = local_worker();
let ret;
poll_for_time_bounded_io_request!((
worker.recv_fixed_with_deadline(
this.fd,
this.ptr,
this.len,
this.fixed_index,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() },
&mut this.deadline
),
ret as u32
));
}
}
unsafe impl Send for RecvFixedWithDeadline<'_> {}
pub trait AsyncRecv: AsRawFd {
#[inline(always)]
fn recv_bytes(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> {
RecvBytes::new(self.as_raw_fd(), buf)
}
#[inline(always)]
async fn recv(&mut self, buf: &mut impl FixedBufferMut) -> Result<u32> {
if buf.is_fixed() {
RecvFixed::new(
self.as_raw_fd(),
buf.as_mut_ptr(),
buf.len_u32(),
buf.fixed_index(),
)
.await
} else {
#[allow(
clippy::cast_possible_truncation,
reason = "It never receive more than u32::MAX bytes"
)]
RecvBytes::new(self.as_raw_fd(), buf.as_bytes_mut())
.await
.map(|r| r as u32)
}
}
#[inline(always)]
fn recv_bytes_with_deadline(
&mut self,
buf: &mut [u8],
deadline: Instant,
) -> impl Future<Output = Result<usize>> {
RecvBytesWithDeadline::new(self.as_raw_fd(), buf, deadline)
}
#[inline(always)]
async fn recv_with_deadline(
&mut self,
buf: &mut impl FixedBufferMut,
deadline: Instant,
) -> Result<u32> {
if buf.is_fixed() {
RecvFixedWithDeadline::new(
self.as_raw_fd(),
buf.as_mut_ptr(),
buf.len_u32(),
buf.fixed_index(),
deadline,
)
.await
} else {
#[allow(
clippy::cast_possible_truncation,
reason = "It never receive more than u32::MAX bytes"
)]
RecvBytesWithDeadline::new(self.as_raw_fd(), buf.as_bytes_mut(), deadline)
.await
.map(|r| r as u32)
}
}
#[inline(always)]
fn recv_bytes_with_timeout(
&mut self,
buf: &mut [u8],
timeout: Duration,
) -> impl Future<Output = Result<usize>> {
self.recv_bytes_with_deadline(buf, Instant::now() + timeout)
}
#[inline(always)]
fn recv_with_timeout(
&mut self,
buf: &mut impl FixedBufferMut,
timeout: Duration,
) -> impl Future<Output = Result<u32>> {
self.recv_with_deadline(buf, Instant::now() + timeout)
}
#[inline(always)]
async fn recv_bytes_exact(&mut self, buf: &mut [u8]) -> Result<()> {
let mut received = 0;
while received < buf.len() {
received += self.recv_bytes(&mut buf[received..]).await?;
}
Ok(())
}
#[inline(always)]
async fn recv_exact(&mut self, buf: &mut impl FixedBufferMut) -> Result<()> {
if buf.is_fixed() {
let mut received = 0;
#[allow(
clippy::cast_possible_wrap,
reason = "We believe it never receive u32::MAX bytes"
)]
while received < buf.len_u32() {
received += RecvFixed::new(
self.as_raw_fd(),
unsafe { buf.as_mut_ptr().offset(received as isize) },
buf.len_u32() - received,
buf.fixed_index(),
)
.await?;
}
} else {
let mut received = 0;
let slice = buf.as_bytes_mut();
while received < slice.len() {
received += self.recv_bytes(&mut slice[received..]).await?;
}
}
Ok(())
}
#[inline(always)]
async fn recv_bytes_exact_with_deadline(
&mut self,
buf: &mut [u8],
deadline: Instant,
) -> Result<()> {
let mut received = 0;
while received < buf.len() {
received += self
.recv_bytes_with_deadline(&mut buf[received..], deadline)
.await?;
}
Ok(())
}
#[inline(always)]
async fn recv_exact_with_deadline(
&mut self,
buf: &mut impl FixedBufferMut,
deadline: Instant,
) -> Result<()> {
if buf.is_fixed() {
let mut received = 0;
#[allow(
clippy::cast_possible_wrap,
reason = "We believe it never receive u32::MAX bytes"
)]
while received < buf.len_u32() {
received += RecvFixedWithDeadline::new(
self.as_raw_fd(),
unsafe { buf.as_mut_ptr().offset(received as isize) },
buf.len_u32() - received,
buf.fixed_index(),
deadline,
)
.await?;
}
} else {
let mut received = 0;
let slice = buf.as_bytes_mut();
while received < slice.len() {
received += self
.recv_bytes_with_deadline(&mut slice[received..], deadline)
.await?;
}
}
Ok(())
}
#[inline(always)]
fn recv_bytes_exact_with_timeout(
&mut self,
buf: &mut [u8],
timeout: Duration,
) -> impl Future<Output = Result<()>> {
self.recv_bytes_exact_with_deadline(buf, Instant::now() + timeout)
}
#[inline(always)]
fn recv_exact_with_timeout(
&mut self,
buf: &mut impl FixedBufferMut,
timeout: Duration,
) -> impl Future<Output = Result<()>> {
self.recv_exact_with_deadline(buf, Instant::now() + timeout)
}
}