use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::net::TcpStream;
use tokio_rustls::client::TlsStream as ClientTlsStream;
use tokio_rustls::server::TlsStream as ServerTlsStream;
use crate::error::{Error, Result};
use crate::mse::MseStream;
use crate::secure::SecureTransport;
pub enum PeerIo {
TlsClient(ClientTlsStream<TcpStream>),
TlsServer(ServerTlsStream<TcpStream>),
Mse(MseStream),
Plain(TcpStream),
}
impl PeerIo {
pub fn from_secure(transport: SecureTransport) -> Self {
match transport {
SecureTransport::Client(s) => Self::TlsClient(s),
SecureTransport::Server(s) => Self::TlsServer(s),
}
}
pub fn from_mse(stream: MseStream) -> Self {
Self::Mse(stream)
}
pub fn from_plain(stream: TcpStream) -> Self {
Self::Plain(stream)
}
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
match self {
Self::TlsClient(stream) => stream.read(buf).await.map_err(Error::from),
Self::TlsServer(stream) => stream.read(buf).await.map_err(Error::from),
Self::Mse(stream) => stream.read(buf).await,
Self::Plain(stream) => stream.read(buf).await.map_err(Error::from),
}
}
pub async fn write_all(&mut self, buf: &[u8]) -> Result<()> {
match self {
Self::TlsClient(stream) => stream.write_all(buf).await.map_err(Error::from),
Self::TlsServer(stream) => stream.write_all(buf).await.map_err(Error::from),
Self::Mse(stream) => stream.write_all(buf).await,
Self::Plain(stream) => stream.write_all(buf).await.map_err(Error::from),
}
}
pub async fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
match self {
Self::TlsClient(stream) => {
stream.read_exact(buf).await.map_err(Error::from)?;
Ok(())
}
Self::TlsServer(stream) => {
stream.read_exact(buf).await.map_err(Error::from)?;
Ok(())
}
Self::Mse(stream) => stream.read_exact(buf).await,
Self::Plain(stream) => {
stream.read_exact(buf).await.map_err(Error::from)?;
Ok(())
}
}
}
}
impl AsyncRead for PeerIo {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
match &mut *self {
Self::TlsClient(stream) => Pin::new(stream).poll_read(cx, buf),
Self::TlsServer(stream) => Pin::new(stream).poll_read(cx, buf),
Self::Mse(stream) => Pin::new(stream).poll_read(cx, buf),
Self::Plain(stream) => Pin::new(stream).poll_read(cx, buf),
}
}
}
impl AsyncWrite for PeerIo {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
data: &[u8],
) -> Poll<std::io::Result<usize>> {
match &mut *self {
Self::TlsClient(stream) => Pin::new(stream).poll_write(cx, data),
Self::TlsServer(stream) => Pin::new(stream).poll_write(cx, data),
Self::Mse(stream) => Pin::new(stream).poll_write(cx, data),
Self::Plain(stream) => Pin::new(stream).poll_write(cx, data),
}
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
match &mut *self {
Self::TlsClient(stream) => Pin::new(stream).poll_flush(cx),
Self::TlsServer(stream) => Pin::new(stream).poll_flush(cx),
Self::Mse(stream) => Pin::new(stream).poll_flush(cx),
Self::Plain(stream) => Pin::new(stream).poll_flush(cx),
}
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
match &mut *self {
Self::TlsClient(stream) => Pin::new(stream).poll_shutdown(cx),
Self::TlsServer(stream) => Pin::new(stream).poll_shutdown(cx),
Self::Mse(stream) => Pin::new(stream).poll_shutdown(cx),
Self::Plain(stream) => Pin::new(stream).poll_shutdown(cx),
}
}
}