-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
172 additions
and
50 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
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
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
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,77 @@ | ||
use std::future::{Future, IntoFuture}; | ||
use std::io::Result; | ||
|
||
use futures_util::{pin_mut, TryFutureExt}; | ||
use hyper_util::{ | ||
rt::{TokioExecutor, TokioIo}, | ||
server::conn::auto::Builder, | ||
}; | ||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
|
||
use crate::{BoxFuture, Responder, Tree}; | ||
|
||
mod accept; | ||
pub use accept::Accept; | ||
|
||
mod tcp; | ||
pub use tcp::*; | ||
|
||
mod unix; | ||
pub use unix::*; | ||
|
||
pub struct Server<L, E> { | ||
tree: Tree, | ||
listener: L, | ||
builder: Builder<E>, | ||
} | ||
|
||
impl<L, E> Server<L, E> { | ||
pub fn listener(&self) -> &L { | ||
&self.listener | ||
} | ||
|
||
pub fn builder(&mut self) -> &mut Builder<E> { | ||
&mut self.builder | ||
} | ||
} | ||
|
||
impl<L> Server<L, TokioExecutor> { | ||
pub fn new(listener: L, tree: Tree) -> Self { | ||
Self { | ||
tree, | ||
listener, | ||
builder: Builder::new(TokioExecutor::new()), | ||
} | ||
} | ||
} | ||
|
||
impl<L> IntoFuture for Server<L, TokioExecutor> | ||
where | ||
L: Accept + Send + 'static, | ||
L::Conn: AsyncWrite + AsyncRead + Unpin + Send, | ||
L::Addr: Clone + Send + Sync + 'static, | ||
{ | ||
type Output = Result<()>; | ||
type IntoFuture = BoxFuture<Self::Output>; | ||
|
||
fn into_future(self) -> Self::IntoFuture { | ||
let Self { | ||
tree, | ||
listener, | ||
builder, | ||
} = self; | ||
|
||
Box::pin(async move { | ||
loop { | ||
let (stream, remote_addr) = listener.accept().await?; | ||
let io = TokioIo::new(stream); | ||
let builder = builder.clone(); | ||
let responder = Responder::<L::Addr>::new(tree.clone(), Some(remote_addr)); | ||
|
||
tokio::spawn(async move { | ||
if let Err(_) = builder.serve_connection(io, responder).await {} | ||
}); | ||
} | ||
}) | ||
} | ||
} |
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,9 @@ | ||
use std::future::Future; | ||
use std::io::Result; | ||
|
||
pub trait Accept { | ||
type Conn; | ||
type Addr; | ||
|
||
fn accept(&self) -> impl Future<Output = Result<(Self::Conn, Self::Addr)>> + Send; | ||
} |
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,14 @@ | ||
use std::future::Future; | ||
use std::io::Result; | ||
use std::net::SocketAddr; | ||
|
||
use tokio::net::{TcpListener, TcpStream}; | ||
|
||
impl super::Accept for TcpListener { | ||
type Conn = TcpStream; | ||
type Addr = SocketAddr; | ||
|
||
fn accept(&self) -> impl Future<Output = Result<(Self::Conn, Self::Addr)>> + Send { | ||
TcpListener::accept(self) | ||
} | ||
} |
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,13 @@ | ||
use std::future::Future; | ||
use std::io::Result; | ||
|
||
use tokio::net::{unix::SocketAddr, UnixListener, UnixStream}; | ||
|
||
impl super::Accept for UnixListener { | ||
type Conn = UnixStream; | ||
type Addr = SocketAddr; | ||
|
||
fn accept(&self) -> impl Future<Output = Result<(Self::Conn, Self::Addr)>> + Send { | ||
UnixListener::accept(self) | ||
} | ||
} |