diff --git a/src/net/tcp.rs b/src/net/tcp.rs index 8f7f0d24..c13c0780 100644 --- a/src/net/tcp.rs +++ b/src/net/tcp.rs @@ -462,7 +462,10 @@ impl Future for TcpStreamConnect { #[cfg(unix)] mod sys { use std::os::unix::prelude::*; - use super::{TcpStream, TcpListener}; + use std::io; + use super::{TcpStream, TcpStreamNew, TcpListener}; + use futures::{Future, failed}; + use mio; impl AsRawFd for TcpStream { fn as_raw_fd(&self) -> RawFd { @@ -475,6 +478,25 @@ mod sys { self.io.get_ref().as_raw_fd() } } + + impl FromRawFd for TcpStreamNew { + unsafe fn from_raw_fd(fd: RawFd) -> TcpStreamNew { + let future = match ::reactor::current() { + Some(handle) => { + let tcp = mio::tcp::TcpStream::from_raw_fd(fd); + TcpStream::new(tcp, &handle) + }, + None => { + let msg = "Could not retrieve a handle to the reactor Core. \ + Did you initialise the event loop by running `Core::new()`? \ + If so, is it still in scope?"; + let error = io::Error::new(io::ErrorKind::Other, msg); + failed::(error).boxed() + }, + }; + TcpStreamNew { inner: future } + } + } } #[cfg(windows)] diff --git a/src/reactor/mod.rs b/src/reactor/mod.rs index eab8cfe2..63e629ea 100644 --- a/src/reactor/mod.rs +++ b/src/reactor/mod.rs @@ -647,3 +647,12 @@ impl FnBox for F { (*self)(lp) } } + +/// Retrieves a `Handle` for the currently running reactor Core +pub fn current() -> Option { + if CURRENT_LOOP.is_set() { + CURRENT_LOOP.with(|lp| Some(lp.handle().clone())) + } else { + None + } +}