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::{Buffer, FixedBuffer};
pub struct SendBytes<'buf> {
fd: RawFd,
buf: &'buf [u8],
io_request_data: Option<IoRequestData>,
}
impl<'buf> SendBytes<'buf> {
pub fn new(fd: RawFd, buf: &'buf [u8]) -> Self {
Self {
fd,
buf,
io_request_data: None,
}
}
}
impl Future for SendBytes<'_> {
type Output = Result<usize>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never send 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().send(this.fd, this.buf.as_ptr(), this.buf.len() as u32, unsafe {
this.io_request_data.as_mut().unwrap_unchecked()
}),
ret
));
}
}
unsafe impl Send for SendBytes<'_> {}
pub struct SendFixed<'buf> {
fd: RawFd,
ptr: *const u8,
len: u32,
fixed_index: u16,
io_request_data: Option<IoRequestData>,
phantom_data: PhantomData<&'buf Buffer>,
}
impl SendFixed<'_> {
pub fn new(fd: RawFd, ptr: *const u8, len: u32, fixed_index: u16) -> Self {
Self {
fd,
ptr,
len,
fixed_index,
io_request_data: None,
phantom_data: PhantomData,
}
}
}
impl Future for SendFixed<'_> {
type Output = Result<u32>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never send 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().send_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 SendFixed<'_> {}
pub struct SendBytesWithDeadline<'buf> {
fd: RawFd,
buf: &'buf [u8],
io_request_data: Option<IoRequestData>,
deadline: Instant,
}
impl<'buf> SendBytesWithDeadline<'buf> {
pub fn new(fd: RawFd, buf: &'buf [u8], deadline: Instant) -> Self {
Self {
fd,
buf,
io_request_data: None,
deadline,
}
}
}
impl Future for SendBytesWithDeadline<'_> {
type Output = Result<usize>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never send 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.send_with_deadline(
this.fd,
this.buf.as_ptr(),
this.buf.len() as u32,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() },
&mut this.deadline
),
ret
));
}
}
unsafe impl Send for SendBytesWithDeadline<'_> {}
pub struct SendFixedWithDeadline<'buf> {
fd: RawFd,
ptr: *const u8,
len: u32,
fixed_index: u16,
io_request_data: Option<IoRequestData>,
deadline: Instant,
phantom_data: PhantomData<&'buf Buffer>,
}
impl SendFixedWithDeadline<'_> {
pub fn new(fd: RawFd, ptr: *const 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 SendFixedWithDeadline<'_> {
type Output = Result<u32>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never send 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.send_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 SendFixedWithDeadline<'_> {}
pub trait AsyncSend: AsRawFd {
#[inline(always)]
fn send_bytes(&mut self, buf: &[u8]) -> impl Future<Output = Result<usize>> {
SendBytes::new(self.as_raw_fd(), buf)
}
#[inline(always)]
async fn send(&mut self, buf: &impl FixedBuffer) -> Result<u32> {
if buf.is_fixed() {
SendFixed::new(
self.as_raw_fd(),
buf.as_ptr(),
buf.len_u32(),
buf.fixed_index(),
)
.await
} else {
#[allow(
clippy::cast_possible_truncation,
reason = "It never send more than u32::MAX bytes"
)]
SendBytes::new(self.as_raw_fd(), buf.as_bytes())
.await
.map(|r| r as u32)
}
}
#[inline(always)]
fn send_bytes_with_deadline(
&mut self,
buf: &[u8],
deadline: Instant,
) -> impl Future<Output = Result<usize>> {
SendBytesWithDeadline::new(self.as_raw_fd(), buf, deadline)
}
#[inline(always)]
async fn send_with_deadline(
&mut self,
buf: &impl FixedBuffer,
deadline: Instant,
) -> Result<u32> {
if buf.is_fixed() {
SendFixedWithDeadline::new(
self.as_raw_fd(),
buf.as_ptr(),
buf.len_u32(),
buf.fixed_index(),
deadline,
)
.await
} else {
#[allow(
clippy::cast_possible_truncation,
reason = "It never send more than u32::MAX bytes"
)]
SendBytesWithDeadline::new(self.as_raw_fd(), buf.as_bytes(), deadline)
.await
.map(|r| r as u32)
}
}
#[inline(always)]
fn send_bytes_with_timeout(
&mut self,
buf: &[u8],
timeout: Duration,
) -> impl Future<Output = Result<usize>> {
SendBytesWithDeadline::new(self.as_raw_fd(), buf, Instant::now() + timeout)
}
#[inline(always)]
fn send_with_timeout(
&mut self,
buf: &impl FixedBuffer,
timeout: Duration,
) -> impl Future<Output = Result<u32>> {
self.send_with_deadline(buf, Instant::now() + timeout)
}
#[inline(always)]
async fn send_all_bytes(&mut self, buf: &[u8]) -> Result<()> {
let mut sent = 0;
while sent < buf.len() {
sent += self.send_bytes(&buf[sent..]).await?;
}
Ok(())
}
#[inline(always)]
async fn send_all(&mut self, buf: &impl FixedBuffer) -> Result<()> {
if buf.is_fixed() {
let mut sent = 0;
#[allow(
clippy::cast_possible_wrap,
reason = "We believe it never send u32::MAX bytes"
)]
while sent < buf.len_u32() {
sent += SendFixed::new(
self.as_raw_fd(),
unsafe { buf.as_ptr().offset(sent as isize) },
buf.len_u32() - sent,
buf.fixed_index(),
)
.await?;
}
} else {
let mut sent = 0;
let slice = buf.as_bytes();
while sent < slice.len() {
sent += self.send_bytes(&slice[sent..]).await?;
}
}
Ok(())
}
#[inline(always)]
async fn send_all_bytes_with_deadline(&mut self, buf: &[u8], deadline: Instant) -> Result<()> {
let mut sent = 0;
while sent < buf.len() {
sent += self
.send_bytes_with_deadline(&buf[sent..], deadline)
.await?;
}
Ok(())
}
#[inline(always)]
async fn send_all_with_deadline(
&mut self,
buf: &impl FixedBuffer,
deadline: Instant,
) -> Result<()> {
if buf.is_fixed() {
let mut sent = 0;
#[allow(
clippy::cast_possible_wrap,
reason = "We believe it never send u32::MAX bytes"
)]
while sent < buf.len_u32() {
sent += SendFixedWithDeadline::new(
self.as_raw_fd(),
unsafe { buf.as_ptr().offset(sent as isize) },
buf.len_u32() - sent,
buf.fixed_index(),
deadline,
)
.await?;
}
} else {
let mut sent = 0;
let slice = buf.as_bytes();
while sent < slice.len() {
sent += self
.send_bytes_with_deadline(&slice[sent..], deadline)
.await?;
}
}
Ok(())
}
#[inline(always)]
fn send_all_bytes_with_timeout(
&mut self,
buf: &[u8],
timeout: Duration,
) -> impl Future<Output = Result<()>> {
self.send_all_bytes_with_deadline(buf, Instant::now() + timeout)
}
#[inline(always)]
fn send_all_with_timeout(
&mut self,
buf: &impl FixedBuffer,
timeout: Duration,
) -> impl Future<Output = Result<()>> {
self.send_all_with_deadline(buf, Instant::now() + timeout)
}
}