#![deny(unsafe_op_in_unsafe_fn)]
pub use futures_io::{AsyncRead, AsyncWrite};
use std::{
future::{Future, poll_fn},
io,
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
};
pub use turnloop::{self, AsyncIo, ExecutorHandle, Instant, backend::Backend};
pub mod dns;
pub mod pool;
pub trait Stream: AsyncRead + AsyncWrite + Unpin {}
impl<T: AsyncRead + AsyncWrite + Unpin> Stream for T {}
pub fn error(e: turnloop::Error) -> io::Error {
if let Some(code) = e.os {
return io::Error::from_raw_os_error(code);
}
let kind = match e.kind {
turnloop::ErrorKind::TimedOut => io::ErrorKind::TimedOut,
turnloop::ErrorKind::Unsupported => io::ErrorKind::Unsupported,
turnloop::ErrorKind::InvalidInput => io::ErrorKind::InvalidInput,
turnloop::ErrorKind::Cancelled => io::ErrorKind::Interrupted,
turnloop::ErrorKind::BrokenPipe => io::ErrorKind::BrokenPipe,
_ => io::ErrorKind::Other,
};
io::Error::new(kind, e)
}
pub async fn read<S: AsyncRead + Unpin>(stream: &mut S, bytes: &mut [u8]) -> io::Result<usize> {
poll_fn(|cx| Pin::new(&mut *stream).poll_read(cx, bytes)).await
}
pub async fn write_all<S: AsyncWrite + Unpin>(stream: &mut S, mut bytes: &[u8]) -> io::Result<()> {
while !bytes.is_empty() {
let n = poll_fn(|cx| Pin::new(&mut *stream).poll_write(cx, bytes)).await?;
if n == 0 {
return Err(io::ErrorKind::WriteZero.into());
}
bytes = &bytes[n..];
}
flush(stream).await
}
pub async fn flush<S: AsyncWrite + Unpin>(stream: &mut S) -> io::Result<()> {
poll_fn(|cx| Pin::new(&mut *stream).poll_flush(cx)).await
}
pub async fn close<S: AsyncWrite + Unpin>(stream: &mut S) -> io::Result<()> {
poll_fn(|cx| Pin::new(&mut *stream).poll_close(cx)).await
}
pub trait HalfClose: Stream {
type Backend: Backend;
fn executor(&self) -> Option<ExecutorHandle<Self::Backend>>;
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
}
impl<B: Backend> HalfClose for AsyncIo<B> {
type Backend = B;
fn executor(&self) -> Option<ExecutorHandle<B>> {
Some(AsyncIo::executor(self))
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_mut().poll_shutdown(cx)
}
}
pub async fn shutdown<S: HalfClose>(stream: &mut S) -> io::Result<()> {
poll_fn(|cx| Pin::new(&mut *stream).poll_shutdown(cx)).await
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LingerEnd {
Eof,
Deadline,
Unsupported,
Failed(io::ErrorKind),
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Lingered {
pub end: LingerEnd,
pub reads: u64,
pub discarded: u64,
}
enum LingerPhase {
Shutdown,
Discard,
Close,
Done,
}
pub struct LingerClose<'a, S: HalfClose> {
stream: &'a mut S,
scratch: &'a mut [u8],
executor: Option<ExecutorHandle<S::Backend>>,
deadline: Option<Instant>,
sleep: Option<(Instant, turnloop::Sleep<S::Backend>)>,
phase: LingerPhase,
lingered: Lingered,
}
pub fn linger_close<'a, S: HalfClose>(
stream: &'a mut S,
scratch: &'a mut [u8],
deadline: Option<Instant>,
) -> LingerClose<'a, S> {
LingerClose {
executor: stream.executor(),
stream,
scratch,
deadline,
sleep: None,
phase: LingerPhase::Shutdown,
lingered: Lingered {
end: LingerEnd::Deadline,
reads: 0,
discarded: 0,
},
}
}
impl<S: HalfClose> LingerClose<'_, S> {
pub fn set_deadline(&mut self, deadline: Option<Instant>) {
self.deadline = deadline;
}
pub fn lingered(&self) -> Lingered {
self.lingered
}
fn poll_expired(&mut self, cx: &mut Context<'_>) -> Result<bool, io::ErrorKind> {
let Some(deadline) = self.deadline else {
self.sleep = None;
return Ok(false);
};
if self.sleep.as_ref().is_none_or(|(at, _)| *at != deadline) {
let Some(executor) = &self.executor else {
return Ok(true);
};
if executor.now() >= deadline {
self.sleep = None;
return Ok(true);
}
self.sleep = Some((deadline, executor.sleep_until(deadline)));
}
let Some((_, sleep)) = self.sleep.as_mut() else {
return Ok(false);
};
match Pin::new(sleep).poll(cx) {
Poll::Ready(Ok(())) => Ok(true),
Poll::Ready(Err(e)) => Err(error(e).kind()),
Poll::Pending => Ok(false),
}
}
fn poll_discard(&mut self, cx: &mut Context<'_>) -> Option<LingerEnd> {
loop {
match Pin::new(&mut *self.stream).poll_read(cx, self.scratch) {
Poll::Ready(Ok(0)) => return Some(LingerEnd::Eof),
Poll::Ready(Ok(n)) => {
self.lingered.reads += 1;
self.lingered.discarded += n as u64;
}
Poll::Ready(Err(e)) => return Some(LingerEnd::Failed(e.kind())),
Poll::Pending => break,
}
}
match self.poll_expired(cx) {
Ok(false) => None,
Ok(true) => Some(LingerEnd::Deadline),
Err(kind) => Some(LingerEnd::Failed(kind)),
}
}
}
impl<S: HalfClose> Future for LingerClose<'_, S> {
type Output = io::Result<Lingered>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
loop {
match this.phase {
LingerPhase::Shutdown => {
if this.scratch.is_empty() {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::InvalidInput,
"lingering close needs non-empty scratch storage",
)));
}
match Pin::new(&mut *this.stream).poll_shutdown(cx) {
Poll::Pending => {
let expired = match this.poll_expired(cx) {
Ok(false) => return Poll::Pending,
Ok(true) => io::ErrorKind::TimedOut,
Err(kind) => kind,
};
this.lingered.end = if expired == io::ErrorKind::TimedOut {
LingerEnd::Deadline
} else {
LingerEnd::Failed(expired)
};
this.sleep = None;
this.phase = LingerPhase::Done;
return Poll::Ready(Err(io::Error::new(
expired,
"half-close did not complete before the linger deadline",
)));
}
Poll::Ready(Ok(())) => this.phase = LingerPhase::Discard,
Poll::Ready(Err(e)) => {
this.lingered.end = if e.kind() == io::ErrorKind::Unsupported {
LingerEnd::Unsupported
} else {
LingerEnd::Failed(e.kind())
};
this.phase = LingerPhase::Close;
}
}
}
LingerPhase::Discard => {
let Some(end) = this.poll_discard(cx) else {
return Poll::Pending;
};
this.lingered.end = end;
this.sleep = None;
this.phase = LingerPhase::Close;
}
LingerPhase::Close => {
let result = std::task::ready!(Pin::new(&mut *this.stream).poll_close(cx));
this.phase = LingerPhase::Done;
return Poll::Ready(result.map(|()| this.lingered));
}
LingerPhase::Done => return Poll::Ready(Ok(this.lingered)),
}
}
}
}
pub async fn deadline<B: Backend, F: Future<Output = io::Result<T>>, T>(
executor: &ExecutorHandle<B>,
at: Instant,
future: F,
) -> io::Result<T> {
executor.timeout_at(at, future).await.map_err(error)?
}
pub async fn resolve<B: Backend>(
executor: &ExecutorHandle<B>,
host: &str,
port: u16,
at: Instant,
) -> io::Result<SocketAddr> {
if let Ok(ip) = host.parse() {
return Ok(SocketAddr::new(ip, port));
}
deadline(executor, at, async {
executor
.resolve(turnloop::DnsRequest {
host: host.to_owned(),
port,
})
.await
.map_err(error)?
.into_iter()
.next()
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "DNS returned no addresses"))
})
.await
}
pub trait Output {
fn output(&self) -> &[u8];
fn consume_output(&mut self, count: usize) -> io::Result<()>;
}
pub async fn drain<S: Stream, C: Output>(stream: &mut S, core: &mut C) -> io::Result<()> {
let n = core.output().len();
write_all(stream, core.output()).await?;
if n > 0 {
core.consume_output(n)?;
}
Ok(())
}
pub struct Listener<B: Backend> {
executor: ExecutorHandle<B>,
handle: turnloop::Handle,
}
impl<B: Backend> Listener<B> {
pub fn bind(executor: &ExecutorHandle<B>, address: SocketAddr) -> io::Result<Self> {
let handle = executor
.driver()
.tcp_listen(address, &Default::default())
.map_err(error)?;
Ok(Self {
executor: executor.clone(),
handle,
})
}
pub fn local_addr(&self) -> io::Result<SocketAddr> {
self.executor
.driver()
.local_addr(self.handle)
.map_err(error)
}
pub async fn accept(&self) -> io::Result<AsyncIo<B>> {
self.executor.accept(self.handle).await.map_err(error)
}
}
impl<B: Backend> Drop for Listener<B> {
fn drop(&mut self) {
let _ = self
.executor
.driver()
.close(self.handle, turnloop::Token(0));
}
}
pub struct CloseOnDrop<'a, S> {
slot: &'a mut Option<S>,
committed: bool,
}
impl<'a, S> CloseOnDrop<'a, S> {
pub fn new(slot: &'a mut Option<S>) -> Self {
Self {
slot,
committed: false,
}
}
pub fn stream(&mut self) -> io::Result<&mut S> {
self.slot
.as_mut()
.ok_or_else(|| io::ErrorKind::NotConnected.into())
}
pub fn commit(&mut self) {
self.committed = true;
}
}
impl<S> Drop for CloseOnDrop<'_, S> {
fn drop(&mut self) {
if !self.committed {
self.slot.take();
}
}
}
pub trait SansIo: Output {
type Event<'a>
where
Self: 'a;
fn event(&mut self, receive: impl FnMut(Self::Event<'_>) -> io::Result<()>)
-> io::Result<bool>;
fn ingest(&mut self, bytes: &[u8], now: Instant) -> io::Result<usize>;
fn disconnected(&mut self);
}
pub struct Driver<S, C: SansIo> {
stream: Option<S>,
core: Option<C>,
input: Box<[u8]>,
start: usize,
end: usize,
}
impl<S: Stream, C: SansIo> Driver<S, C> {
pub fn new(stream: S, core: C) -> Self {
Self {
stream: Some(stream),
core: Some(core),
input: vec![0; 16384].into_boxed_slice(),
start: 0,
end: 0,
}
}
pub fn core(&self) -> &C {
self.core.as_ref().expect("owned core")
}
pub fn core_mut(&mut self) -> &mut C {
self.core.as_mut().expect("owned core")
}
pub fn is_connected(&self) -> bool {
self.stream.is_some()
}
pub fn stream(&self) -> Option<&S> {
self.stream.as_ref()
}
pub fn abort(&mut self) {
self.stream.take();
self.core_mut().disconnected();
}
pub fn replace_stream(&mut self, stream: S) -> io::Result<()> {
if self.stream.is_some() {
return Err(io::Error::other("transport is still connected"));
}
self.start = 0;
self.end = 0;
self.stream = Some(stream);
Ok(())
}
pub fn exchange(&mut self) -> Exchange<'_, S, C> {
Exchange {
driver: self,
committed: false,
}
}
pub fn upgrade_stream(&mut self) -> io::Result<&mut S> {
if self.start != self.end || !self.core().output().is_empty() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"undrained TLS boundary",
));
}
self.stream
.as_mut()
.ok_or_else(|| io::ErrorKind::NotConnected.into())
}
pub async fn flush(&mut self) -> io::Result<()> {
let mut transport = CloseOnDrop::new(&mut self.stream);
let result = drain(transport.stream()?, self.core.as_mut().expect("owned core")).await;
if result.is_ok() {
transport.commit();
}
result
}
pub async fn next<B: Backend>(
&mut self,
executor: &ExecutorHandle<B>,
mut receive: impl FnMut(C::Event<'_>) -> io::Result<()>,
) -> io::Result<()> {
struct CoreGuard<'a, C: SansIo> {
core: &'a mut C,
done: bool,
}
impl<C: SansIo> Drop for CoreGuard<'_, C> {
fn drop(&mut self) {
if !self.done {
self.core.disconnected();
}
}
}
let mut core = CoreGuard {
core: self.core.as_mut().expect("owned core"),
done: false,
};
let mut transport = CloseOnDrop::new(&mut self.stream);
loop {
if core.core.event(&mut receive)? {
core.done = true;
transport.commit();
return Ok(());
}
drain(transport.stream()?, core.core).await?;
if core.core.event(&mut receive)? {
core.done = true;
transport.commit();
return Ok(());
}
if self.start < self.end {
let n = core
.core
.ingest(&self.input[self.start..self.end], executor.now())?;
if n == 0 {
return Err(io::Error::other("core input stalled without an event"));
}
self.start += n;
continue;
}
self.start = 0;
self.end = read(transport.stream()?, &mut self.input).await?;
if self.end == 0 {
return Err(io::ErrorKind::UnexpectedEof.into());
}
}
}
pub fn into_parts(mut self) -> io::Result<(S, C, Vec<u8>)> {
let stream = self
.stream
.take()
.ok_or_else(|| io::Error::from(io::ErrorKind::NotConnected))?;
Ok((
stream,
self.core.take().expect("owned core"),
self.input[self.start..self.end].to_vec(),
))
}
}
pub struct Exchange<'a, S: Stream, C: SansIo> {
driver: &'a mut Driver<S, C>,
committed: bool,
}
impl<S: Stream, C: SansIo> Exchange<'_, S, C> {
pub fn commit(&mut self) {
self.committed = true;
}
}
impl<S: Stream, C: SansIo> std::ops::Deref for Exchange<'_, S, C> {
type Target = Driver<S, C>;
fn deref(&self) -> &Self::Target {
self.driver
}
}
impl<S: Stream, C: SansIo> std::ops::DerefMut for Exchange<'_, S, C> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.driver
}
}
impl<S: Stream, C: SansIo> Drop for Exchange<'_, S, C> {
fn drop(&mut self) {
if !self.committed {
self.driver.abort();
}
}
}
impl<S, C: SansIo> Drop for Driver<S, C> {
fn drop(&mut self) {
if let Some(core) = self.core.as_mut() {
core.disconnected();
}
}
}