Skip to content

add Server::from_tcp() #1624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ pub mod conn;
#[cfg(feature = "runtime")] mod tcp;

use std::fmt;
#[cfg(feature = "runtime")] use std::net::SocketAddr;
#[cfg(feature = "runtime")] use std::net::{SocketAddr, TcpListener as StdTcpListener};

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

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

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 @@ -117,6 +119,16 @@ impl Server<AddrIncoming, ()> {
AddrIncoming::new(addr, None)
.map(Server::builder)
}

/// Create a new instance from a `std::net::TcpListener` instance.
pub fn from_tcp(
listener: StdTcpListener,
) -> Result<Builder<AddrIncoming>, ::Error> {
let handle = tokio_reactor::Handle::current();
let incoming = AddrIncoming::from_tcp(listener, &handle)
.map_err(|err| ::Error::new_listen(err))?;
Ok(Self::builder(incoming))
}
}

#[cfg(feature = "runtime")]
Expand Down Expand Up @@ -275,4 +287,3 @@ impl Builder<AddrIncoming> {
self
}
}

14 changes: 14 additions & 0 deletions src/server/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ impl AddrIncoming {
})
}

pub(super) fn from_tcp(std_listener: StdTcpListener, handle: &Handle) -> ::Result<Self>{
let listener = TcpListener::from_std(std_listener, &handle)
.map_err(::Error::new_listen)?;
let addr = listener.local_addr().map_err(::Error::new_listen)?;
Ok(AddrIncoming {
listener,
addr: addr,
sleep_on_errors: true,
tcp_keepalive_timeout: None,
tcp_nodelay: false,
timeout: None,
})
}

/// Get the local address bound to this listener.
pub fn local_addr(&self) -> SocketAddr {
self.addr
Expand Down