Skip to content

Commit

Permalink
feat(server): add from_tcp method
Browse files Browse the repository at this point in the history
Adds the `from_tcp()` method as per
hyperium#1602.
  • Loading branch information
yoshuawuyts committed Aug 7, 2018
1 parent 4020a2f commit 9c8b113
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,33 @@
//! ```

pub mod conn;
#[cfg(feature = "runtime")] mod tcp;
#[cfg(feature = "runtime")]
mod tcp;

#[cfg(feature = "runtime")]
use std::error;
use std::fmt;
#[cfg(feature = "runtime")] use std::net::SocketAddr;
#[cfg(feature = "runtime")] use std::time::Duration;
#[cfg(feature = "runtime")]
use std::net;
#[cfg(feature = "runtime")]
use std::net::SocketAddr;
#[cfg(feature = "runtime")]
use std::time::Duration;

use futures::{Future, Stream, Poll};
use futures::{Future, Poll, Stream};
use tokio_io::{AsyncRead, AsyncWrite};
#[cfg(feature = "runtime")]
use tokio_reactor;
#[cfg(feature = "runtime")]
use tokio_tcp;

use body::{Body, Payload};
use service::{NewService, Service};
// Renamed `Http` as `Http_` for now so that people upgrading don't see an
// error that `hyper::server::Http` is private...
use self::conn::{Http as Http_, SpawnAll};
#[cfg(feature = "runtime")] use self::tcp::{AddrIncoming};
#[cfg(feature = "runtime")]
use self::tcp::AddrIncoming;

/// A listening HTTP server that accepts connections in both HTTP1 and HTTP2 by default.
///
Expand Down Expand Up @@ -96,6 +108,18 @@ impl<I> Server<I, ()> {
}
}

impl Server<tokio_tcp::Incoming, ()> {
/// Create a new instance from a `std::net::TcpListener` instance.
#[cfg(feature = "runtime")]
pub fn from_tcp(
listener: net::TcpListener,
) -> Result<Builder<tokio_tcp::Incoming>, Box<dyn error::Error>> {
let handle = tokio_reactor::Handle::current();
let listener = tokio_tcp::TcpListener::from_std(listener, &handle)?;
Ok(Self::builder(listener.incoming()))
}
}

#[cfg(feature = "runtime")]
impl Server<AddrIncoming, ()> {
/// Binds to the provided address, and returns a [`Builder`](Builder).
Expand All @@ -105,17 +129,15 @@ impl Server<AddrIncoming, ()> {
/// This method will panic if binding to the address fails. For a method
/// to bind to an address and return a `Result`, see `Server::try_bind`.
pub fn bind(addr: &SocketAddr) -> Builder<AddrIncoming> {
let incoming = AddrIncoming::new(addr, None)
.unwrap_or_else(|e| {
panic!("error binding to {}: {}", addr, e);
});
let incoming = AddrIncoming::new(addr, None).unwrap_or_else(|e| {
panic!("error binding to {}: {}", addr, e);
});
Server::builder(incoming)
}

/// Tries to bind to the provided address, and returns a [`Builder`](Builder).
pub fn try_bind(addr: &SocketAddr) -> ::Result<Builder<AddrIncoming>> {
AddrIncoming::new(addr, None)
.map(Server::builder)
AddrIncoming::new(addr, None).map(Server::builder)
}
}

Expand All @@ -132,7 +154,7 @@ where
I: Stream,
I::Error: Into<Box<::std::error::Error + Send + Sync>>,
I::Item: AsyncRead + AsyncWrite + Send + 'static,
S: NewService<ReqBody=Body, ResBody=B> + Send + 'static,
S: NewService<ReqBody = Body, ResBody = B> + Send + 'static,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Service: Send,
S::Future: Send + 'static,
Expand Down Expand Up @@ -162,10 +184,7 @@ impl<I> Builder<I> {
///
/// For a more convenient constructor, see [`Server::bind`](Server::bind).
pub fn new(incoming: I, protocol: Http_) -> Self {
Builder {
incoming,
protocol,
}
Builder { incoming, protocol }
}

/// Sets whether HTTP/1 is required.
Expand Down Expand Up @@ -243,17 +262,15 @@ impl<I> Builder<I> {
I: Stream,
I::Error: Into<Box<::std::error::Error + Send + Sync>>,
I::Item: AsyncRead + AsyncWrite + Send + 'static,
S: NewService<ReqBody=Body, ResBody=B> + Send + 'static,
S: NewService<ReqBody = Body, ResBody = B> + Send + 'static,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Service: Send,
<S::Service as Service>::Future: Send + 'static,
B: Payload,
{
let serve = self.protocol.serve_incoming(self.incoming, new_service);
let spawn_all = serve.spawn_all();
Server {
spawn_all,
}
Server { spawn_all }
}
}

Expand All @@ -275,4 +292,3 @@ impl Builder<AddrIncoming> {
self
}
}

0 comments on commit 9c8b113

Please sign in to comment.