feat: Support backend socket connections (UDS)
This commit is contained in:
parent
cd1790972e
commit
cfa4b8a437
4 changed files with 117 additions and 23 deletions
66
src/stream.rs
Normal file
66
src/stream.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
|
||||
use tokio::net::{TcpStream, UnixStream};
|
||||
|
||||
use std::io::Result;
|
||||
use std::marker::Unpin;
|
||||
use std::pin::{Pin, pin};
|
||||
use std::task::{Context, Poll};
|
||||
|
||||
pub enum AbstractStream {
|
||||
Tcp(TcpStream),
|
||||
Uds(UnixStream),
|
||||
}
|
||||
|
||||
impl From<TcpStream> for AbstractStream {
|
||||
fn from(stream: TcpStream) -> Self {
|
||||
Self::Tcp(stream)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UnixStream> for AbstractStream {
|
||||
fn from(stream: UnixStream) -> Self {
|
||||
Self::Uds(stream)
|
||||
}
|
||||
}
|
||||
|
||||
impl Unpin for AbstractStream {}
|
||||
|
||||
impl AsyncRead for AbstractStream {
|
||||
fn poll_read(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut ReadBuf<'_>,
|
||||
) -> Poll<Result<()>> {
|
||||
match &mut *self {
|
||||
Self::Tcp(stream) => pin!(stream).poll_read(cx, buf),
|
||||
Self::Uds(stream) => pin!(stream).poll_read(cx, buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AsyncWrite for AbstractStream {
|
||||
fn poll_write(
|
||||
mut self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize>> {
|
||||
match &mut *self {
|
||||
Self::Tcp(stream) => pin!(stream).poll_write(cx, buf),
|
||||
Self::Uds(stream) => pin!(stream).poll_write(cx, buf),
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
match &mut *self {
|
||||
Self::Tcp(stream) => pin!(stream).poll_flush(cx),
|
||||
Self::Uds(stream) => pin!(stream).poll_flush(cx),
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
|
||||
match &mut *self {
|
||||
Self::Tcp(stream) => pin!(stream).poll_shutdown(cx),
|
||||
Self::Uds(stream) => pin!(stream).poll_shutdown(cx),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue