use std::{
io,
pin::Pin,
task::{Context, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, DuplexStream, ReadBuf, ReadHalf, WriteHalf};
use crate::multiplexor::{ChannelId, ChannelState, Error};
pub struct Channel {
pub(crate) state: std::sync::Arc<ChannelState>,
pub(crate) io: Option<DuplexStream>,
}
impl Channel {
#[must_use]
pub fn id(&self) -> ChannelId {
self.state.id
}
#[must_use]
pub fn name(&self) -> &str {
&self.state.name
}
#[must_use]
pub fn into_split(mut self) -> (ChannelReadHalf, ChannelWriteHalf) {
let io = self
.io
.take()
.expect("channel I/O is present until it is split");
let (read, write) = tokio::io::split(io);
(
ChannelReadHalf {
state: std::sync::Arc::clone(&self.state),
io: read,
},
ChannelWriteHalf {
state: std::sync::Arc::clone(&self.state),
io: Some(write),
},
)
}
pub async fn close_read(&self) -> Result<(), Error> {
self.state.close_read()
}
pub async fn terminate(&self, error: Option<&str>) -> Result<(), Error> {
self.state.terminate(error).await
}
pub async fn completion(&self) -> Result<(), Error> {
self.state.completion().await
}
}
impl AsyncRead for Channel {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let before = buf.filled().len();
let result = Pin::new(
self.io
.as_mut()
.expect("channel I/O is present until it is split"),
)
.poll_read(cx, buf);
self.state
.set_application_read_pending(matches!(result, Poll::Pending));
if matches!(result, Poll::Ready(Ok(()))) {
let consumed = buf.filled().len() - before;
if consumed != 0 {
self.state.content_consumed(consumed);
}
}
result
}
}
impl AsyncWrite for Channel {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(
self.io
.as_mut()
.expect("channel I/O is present until it is split"),
)
.poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(
self.io
.as_mut()
.expect("channel I/O is present until it is split"),
)
.poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(
self.io
.as_mut()
.expect("channel I/O is present until it is split"),
)
.poll_shutdown(cx)
}
}
impl Drop for Channel {
fn drop(&mut self) {
if self.io.is_some() {
self.state.drop_channel();
}
}
}
pub struct ChannelReadHalf {
state: std::sync::Arc<ChannelState>,
io: ReadHalf<DuplexStream>,
}
impl ChannelReadHalf {
#[must_use]
pub fn id(&self) -> ChannelId {
self.state.id
}
#[must_use]
pub fn name(&self) -> &str {
&self.state.name
}
pub async fn close_read(&self) -> Result<(), Error> {
self.state.close_read()
}
pub async fn completion(&self) -> Result<(), Error> {
self.state.completion().await
}
}
impl AsyncRead for ChannelReadHalf {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let before = buf.filled().len();
let result = Pin::new(&mut self.io).poll_read(cx, buf);
self.state
.set_application_read_pending(matches!(result, Poll::Pending));
if matches!(result, Poll::Ready(Ok(()))) {
let consumed = buf.filled().len() - before;
if consumed != 0 {
self.state.content_consumed(consumed);
}
}
result
}
}
impl Drop for ChannelReadHalf {
fn drop(&mut self) {
let _ = self.state.close_read();
}
}
pub struct ChannelWriteHalf {
state: std::sync::Arc<ChannelState>,
io: Option<WriteHalf<DuplexStream>>,
}
impl ChannelWriteHalf {
#[must_use]
pub fn id(&self) -> ChannelId {
self.state.id
}
#[must_use]
pub fn name(&self) -> &str {
&self.state.name
}
pub async fn terminate(&self, error: Option<&str>) -> Result<(), Error> {
self.state.terminate(error).await
}
pub async fn completion(&self) -> Result<(), Error> {
self.state.completion().await
}
}
impl AsyncWrite for ChannelWriteHalf {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(
self.io
.as_mut()
.expect("write half I/O is present until it is dropped"),
)
.poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(
self.io
.as_mut()
.expect("write half I/O is present until it is dropped"),
)
.poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(
self.io
.as_mut()
.expect("write half I/O is present until it is dropped"),
)
.poll_shutdown(cx)
}
}
impl Drop for ChannelWriteHalf {
fn drop(&mut self) {
let Some(mut io) = self.io.take() else {
return;
};
if let Ok(runtime) = tokio::runtime::Handle::try_current() {
runtime.spawn(async move {
let _ = tokio::io::AsyncWriteExt::shutdown(&mut io).await;
});
}
}
}