use std::{
marker::PhantomData,
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
time::Duration,
};
use tokio::{
io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf},
net::{
TcpStream,
tcp::{ReadHalf, WriteHalf},
},
};
use super::proto::{Address, AsyncStreamOperation, Reply, Response};
#[derive(Debug)]
pub struct Bind<S> {
stream: TcpStream,
_state: PhantomData<S>,
}
#[derive(Debug, Default)]
pub struct NeedFirstReply;
#[derive(Debug, Default)]
pub struct NeedSecondReply;
#[derive(Debug, Default)]
pub struct Ready;
impl Bind<NeedFirstReply> {
#[inline]
pub(super) fn new(stream: TcpStream) -> Self {
Self {
stream,
_state: PhantomData,
}
}
pub async fn reply(
mut self,
reply: Reply,
addr: Address,
) -> std::io::Result<Bind<NeedSecondReply>> {
let resp = Response::new(reply, addr);
resp.write_to_async_stream(&mut self.stream).await?;
Ok(Bind::<NeedSecondReply>::new(self.stream))
}
#[inline]
pub async fn shutdown(&mut self) -> std::io::Result<()> {
self.stream.shutdown().await
}
#[inline]
pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
self.stream.local_addr()
}
#[inline]
pub fn peer_addr(&self) -> std::io::Result<SocketAddr> {
self.stream.peer_addr()
}
#[inline]
pub fn linger(&self) -> std::io::Result<Option<Duration>> {
self.stream.linger()
}
#[inline]
pub fn set_linger(&self, dur: Option<Duration>) -> std::io::Result<()> {
self.stream.set_linger(dur)
}
#[inline]
pub fn nodelay(&self) -> std::io::Result<bool> {
self.stream.nodelay()
}
#[inline]
pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
self.stream.set_nodelay(nodelay)
}
#[inline]
pub fn ttl(&self) -> std::io::Result<u32> {
self.stream.ttl()
}
#[inline]
pub fn set_ttl(&self, ttl: u32) -> std::io::Result<()> {
self.stream.set_ttl(ttl)
}
}
impl Bind<NeedSecondReply> {
#[inline]
fn new(stream: TcpStream) -> Self {
Self {
stream,
_state: PhantomData,
}
}
pub async fn reply(
mut self,
reply: Reply,
addr: Address,
) -> Result<Bind<Ready>, (std::io::Error, TcpStream)> {
let resp = Response::new(reply, addr);
if let Err(err) = resp.write_to_async_stream(&mut self.stream).await {
return Err((err, self.stream));
}
Ok(Bind::<Ready>::new(self.stream))
}
#[inline]
pub async fn shutdown(&mut self) -> std::io::Result<()> {
self.stream.shutdown().await
}
#[inline]
pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
self.stream.local_addr()
}
#[inline]
pub fn peer_addr(&self) -> std::io::Result<SocketAddr> {
self.stream.peer_addr()
}
#[inline]
pub fn linger(&self) -> std::io::Result<Option<Duration>> {
self.stream.linger()
}
#[inline]
pub fn set_linger(&self, dur: Option<Duration>) -> std::io::Result<()> {
self.stream.set_linger(dur)
}
#[inline]
pub fn nodelay(&self) -> std::io::Result<bool> {
self.stream.nodelay()
}
pub fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()> {
self.stream.set_nodelay(nodelay)
}
pub fn ttl(&self) -> std::io::Result<u32> {
self.stream.ttl()
}
pub fn set_ttl(&self, ttl: u32) -> std::io::Result<()> {
self.stream.set_ttl(ttl)
}
}
impl Bind<Ready> {
#[inline]
fn new(stream: TcpStream) -> Self {
Self {
stream,
_state: PhantomData,
}
}
#[inline]
pub fn split(&mut self) -> (ReadHalf<'_>, WriteHalf<'_>) {
self.stream.split()
}
}
impl std::ops::Deref for Bind<Ready> {
type Target = TcpStream;
#[inline]
fn deref(&self) -> &Self::Target {
&self.stream
}
}
impl std::ops::DerefMut for Bind<Ready> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.stream
}
}
impl AsyncRead for Bind<Ready> {
#[inline]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.stream).poll_read(cx, buf)
}
}
impl AsyncWrite for Bind<Ready> {
#[inline]
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.stream).poll_write(cx, buf)
}
#[inline]
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.stream).poll_flush(cx)
}
#[inline]
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.stream).poll_shutdown(cx)
}
}
impl<S> From<Bind<S>> for TcpStream {
#[inline]
fn from(conn: Bind<S>) -> Self {
conn.stream
}
}