use crate::protocol::{Address, AsyncStreamOperation, Reply, Response};
use std::{
marker::PhantomData,
net::{SocketAddr, ToSocketAddrs},
pin::Pin,
task::{Context, Poll},
};
use tokio::{
io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf},
net::{
TcpListener, TcpStream,
tcp::{ReadHalf, WriteHalf},
},
};
#[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))
}
pub async fn accept(self, bind_addr: Address) -> std::io::Result<(Bind<NeedSecondReply>, TcpStream)> {
let bind_addr = bind_addr
.to_socket_addrs()?
.next()
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid bind address"))?;
let listener = match TcpListener::bind(bind_addr).await {
Ok(listener) => listener,
Err(err) => {
let _ = self.reply(Reply::GeneralFailure, Address::unspecified()).await;
return Err(err);
}
};
let local_addr = listener.local_addr()?;
let bind = self.reply(Reply::Succeeded, Address::from(local_addr)).await?;
let (incoming, _) = listener.accept().await?;
Ok((bind, incoming))
}
pub async fn bind(self, bind_addr: Address) -> std::io::Result<(Bind<Ready>, TcpStream)> {
let (bind, incoming) = self.accept(bind_addr).await?;
let remote_addr = incoming.peer_addr()?;
let conn = match bind.reply(Reply::Succeeded, Address::from(remote_addr)).await {
Ok(conn) => conn,
Err((err, _stream)) => return Err(err),
};
Ok((conn, incoming))
}
#[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 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<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 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
}
}