Skip to content
Closed
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
24 changes: 23 additions & 1 deletion src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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::<TcpStream, io::Error>(error).boxed()
},
};
TcpStreamNew { inner: future }
}
}
}

#[cfg(windows)]
Expand Down
9 changes: 9 additions & 0 deletions src/reactor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,3 +647,12 @@ impl<F: FnOnce(&Core) + Send + 'static> FnBox for F {
(*self)(lp)
}
}

/// Retrieves a `Handle` for the currently running reactor Core
pub fn current() -> Option<Handle> {
if CURRENT_LOOP.is_set() {
CURRENT_LOOP.with(|lp| Some(lp.handle().clone()))
} else {
None
}
}