-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
proxy: introduce Acceptor and Connector traits
- Loading branch information
Showing
4 changed files
with
107 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::future::Future; | ||
use std::io; | ||
use std::net::SocketAddr; | ||
use std::task::{Context, Poll}; | ||
|
||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio::net::{TcpListener, TcpStream}; | ||
|
||
pub trait Acceptor { | ||
type Connection: AsyncRead + AsyncWrite + Send + Unpin + 'static; | ||
type Error: std::error::Error + Send + Sync + 'static; | ||
|
||
#[inline] | ||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
let _ = cx; | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn accept( | ||
&self, | ||
) -> impl Future<Output = Result<(Self::Connection, SocketAddr), Self::Error>> + Send; | ||
} | ||
|
||
pub trait Connector { | ||
type Connection: AsyncRead + AsyncWrite + Send + Unpin + 'static; | ||
type Error: std::error::Error + Send + Sync + 'static; | ||
|
||
#[inline] | ||
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
let _ = cx; | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn connect( | ||
&self, | ||
addr: SocketAddr, | ||
) -> impl Future<Output = Result<Self::Connection, Self::Error>> + Send; | ||
} | ||
|
||
pub struct TokioTcpAcceptor { | ||
listener: TcpListener, | ||
tcp_nodelay: Option<bool>, | ||
tcp_keepalive: Option<bool>, | ||
} | ||
|
||
impl TokioTcpAcceptor { | ||
pub async fn bind(addr: SocketAddr) -> io::Result<Self> { | ||
let listener = TcpListener::bind(addr).await?; | ||
// When set for the server socket, the keepalive setting | ||
// will be inherited by all accepted client sockets. | ||
socket2::SockRef::from(&listener).set_keepalive(true)?; | ||
Ok(Self { | ||
listener, | ||
tcp_nodelay: Some(true), | ||
tcp_keepalive: None, | ||
}) | ||
} | ||
} | ||
|
||
impl Acceptor for TokioTcpAcceptor { | ||
type Connection = TcpStream; | ||
type Error = io::Error; | ||
|
||
fn accept(&self) -> impl Future<Output = Result<(Self::Connection, SocketAddr), Self::Error>> { | ||
async move { | ||
let (stream, addr) = self.listener.accept().await?; | ||
|
||
let socket = socket2::SockRef::from(&stream); | ||
if let Some(nodelay) = self.tcp_nodelay { | ||
socket.set_nodelay(nodelay)?; | ||
} | ||
if let Some(keepalive) = self.tcp_keepalive { | ||
socket.set_keepalive(keepalive)?; | ||
} | ||
|
||
Ok((stream, addr)) | ||
} | ||
} | ||
} | ||
|
||
pub struct TokioTcpConnector; | ||
|
||
impl Connector for TokioTcpConnector { | ||
type Connection = TcpStream; | ||
type Error = io::Error; | ||
|
||
fn connect( | ||
&self, | ||
addr: SocketAddr, | ||
) -> impl Future<Output = Result<Self::Connection, Self::Error>> { | ||
async move { | ||
let socket = TcpStream::connect(addr).await?; | ||
socket.set_nodelay(true)?; | ||
Ok(socket) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters