From 764721ab6041222240729fbb13044405234e5e38 Mon Sep 17 00:00:00 2001 From: ivegotasthma Date: Wed, 7 Mar 2018 13:16:12 +0100 Subject: [PATCH] new: server can bind to raw sockets --- src/server/srv.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/server/srv.rs b/src/server/srv.rs index 33a7e432a..e5965facf 100644 --- a/src/server/srv.rs +++ b/src/server/srv.rs @@ -19,6 +19,9 @@ use native_tls::TlsAcceptor; #[cfg(feature="alpn")] use openssl::ssl::{AlpnError, SslAcceptorBuilder}; +#[cfg(target_family="unix")] +use std::os::unix::io::{FromRawFd, RawFd}; + use helpers; use super::{IntoHttpHandler, IoStream}; use super::{PauseServer, ResumeServer, StopServer}; @@ -68,7 +71,7 @@ impl HttpServer where H: IntoHttpHandler + 'static let f = move || { (factory)().into_iter().collect() }; - + HttpServer{ h: None, threads: num_cpus::get(), backlog: 2048, @@ -173,9 +176,22 @@ impl HttpServer where H: IntoHttpHandler + 'static self.sockets.keys().cloned().collect() } + #[cfg(target_family="unix")] + /// The socket to bind to + /// + /// To bind multiple sockets this method can be called multiple times. + pub fn bind_socket(mut self, sock: RawFd) -> io::Result { + unsafe { + let lst = net::TcpListener::from_raw_fd(sock); + self.sockets.insert(lst.local_addr().unwrap(), lst); + } + + Ok(self) + } + /// The socket address to bind /// - /// To mind multiple addresses this method can be call multiple times. + /// To bind multiple addresses this method can be called multiple times. pub fn bind(mut self, addr: S) -> io::Result { let mut err = None; let mut succ = false;