Skip to content
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

Split starting a listener out from accepting the first connection. #8954

Closed
wants to merge 2 commits into from
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
38 changes: 32 additions & 6 deletions src/libstd/rt/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,43 @@ pub trait Seek {
fn seek(&mut self, pos: i64, style: SeekStyle);
}

/// A listener is a value that listens for connections
pub trait Listener<S> {
/// Wait for and accept an incoming connection
///
/// Returns `None` on timeout.
/// A listener is a value that can consume itself to start listening for connections.
/// Doing so produces some sort of Acceptor.
pub trait Listener<T, A: Acceptor<T>> {
/// Spin up the listener and start queueing incoming connections
///
/// # Failure
///
/// Raises `io_error` condition. If the condition is handled,
/// then `listen` returns `None`.
fn listen(self) -> Option<A>;
}

/// An acceptor is a value that presents incoming connections
pub trait Acceptor<T> {
/// Wait for and accept an incoming connection
///
/// # Failure
/// Raise `io_error` condition. If the condition is handled,
/// then `accept` returns `None`.
fn accept(&mut self) -> Option<S>;
fn accept(&mut self) -> Option<T>;

/// Create an iterator over incoming connections
fn incoming<'r>(&'r mut self) -> IncomingIterator<'r, Self> {
IncomingIterator { inc: self }
}
}

/// An infinite iterator over incoming connection attempts.
/// Calling `next` will block the task until a connection is attempted.
struct IncomingIterator<'self, A> {
priv inc: &'self mut A,
}

impl<'self, T, A: Acceptor<T>> Iterator<T> for IncomingIterator<'self, A> {
fn next(&mut self) -> Option<T> {
self.inc.accept()
}
}

/// Common trait for decorator types.
Expand Down
106 changes: 56 additions & 50 deletions src/libstd/rt/io/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use option::{Option, Some, None};
use result::{Ok, Err};
use rt::io::net::ip::SocketAddr;
use rt::io::{Reader, Writer, Listener};
use rt::io::{Reader, Writer, Listener, Acceptor};
use rt::io::{io_error, read_error, EndOfFile};
use rt::rtio::{IoFactory, IoFactoryObject,
RtioSocket, RtioTcpListener,
RtioTcpListenerObject, RtioTcpStream,
RtioTcpStreamObject};
RtioSocket,
RtioTcpListener, RtioTcpListenerObject,
RtioTcpAcceptor, RtioTcpAcceptorObject,
RtioTcpStream, RtioTcpStreamObject};
use rt::local::Local;

pub struct TcpStream(~RtioTcpStreamObject);
Expand Down Expand Up @@ -124,13 +125,27 @@ impl TcpListener {
}
}

impl Listener<TcpStream> for TcpListener {
impl Listener<TcpStream, TcpAcceptor> for TcpListener {
fn listen(self) -> Option<TcpAcceptor> {
match (**self).listen() {
Ok(acceptor) => Some(TcpAcceptor(acceptor)),
Err(ioerr) => {
io_error::cond.raise(ioerr);
None
}
}
}
}

pub struct TcpAcceptor(~RtioTcpAcceptorObject);

impl Acceptor<TcpStream> for TcpAcceptor {
fn accept(&mut self) -> Option<TcpStream> {
match (**self).accept() {
Ok(s) => Some(TcpStream::new(s)),
Err(ioerr) => {
io_error::cond.raise(ioerr);
return None;
None
}
}
}
Expand Down Expand Up @@ -184,8 +199,8 @@ mod test {
let addr = next_test_ip4();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let mut buf = [0];
stream.read(buf);
assert!(buf[0] == 99);
Expand All @@ -204,8 +219,8 @@ mod test {
let addr = next_test_ip6();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let mut buf = [0];
stream.read(buf);
assert!(buf[0] == 99);
Expand All @@ -224,8 +239,8 @@ mod test {
let addr = next_test_ip4();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let mut buf = [0];
let nread = stream.read(buf);
assert!(nread.is_none());
Expand All @@ -244,8 +259,8 @@ mod test {
let addr = next_test_ip6();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let mut buf = [0];
let nread = stream.read(buf);
assert!(nread.is_none());
Expand All @@ -265,8 +280,8 @@ mod test {
let addr = next_test_ip4();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let mut buf = [0];
let nread = stream.read(buf);
assert!(nread.is_none());
Expand All @@ -288,8 +303,8 @@ mod test {
let addr = next_test_ip6();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let mut buf = [0];
let nread = stream.read(buf);
assert!(nread.is_none());
Expand All @@ -311,8 +326,8 @@ mod test {
let addr = next_test_ip4();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let buf = [0];
loop {
let mut stop = false;
Expand Down Expand Up @@ -341,8 +356,8 @@ mod test {
let addr = next_test_ip6();

do spawntask {
let mut listener = TcpListener::bind(addr);
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
let mut stream = acceptor.accept();
let buf = [0];
loop {
let mut stop = false;
Expand Down Expand Up @@ -371,9 +386,8 @@ mod test {
let max = 10;

do spawntask {
let mut listener = TcpListener::bind(addr);
do max.times {
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
for ref mut stream in acceptor.incoming().take(max) {
let mut buf = [0];
stream.read(buf);
assert_eq!(buf[0], 99);
Expand All @@ -396,9 +410,8 @@ mod test {
let max = 10;

do spawntask {
let mut listener = TcpListener::bind(addr);
do max.times {
let mut stream = listener.accept();
let mut acceptor = TcpListener::bind(addr).listen();
for ref mut stream in acceptor.incoming().take(max) {
let mut buf = [0];
stream.read(buf);
assert_eq!(buf[0], 99);
Expand All @@ -421,10 +434,9 @@ mod test {
static MAX: int = 10;

do spawntask {
let mut listener = TcpListener::bind(addr);
for i in range(0, MAX) {
let stream = Cell::new(listener.accept());
rtdebug!("accepted");
let mut acceptor = TcpListener::bind(addr).listen();
for (i, stream) in acceptor.incoming().enumerate().take(MAX as uint) {
let stream = Cell::new(stream);
// Start another task to handle the connection
do spawntask {
let mut stream = stream.take();
Expand Down Expand Up @@ -460,10 +472,9 @@ mod test {
static MAX: int = 10;

do spawntask {
let mut listener = TcpListener::bind(addr);
for i in range(0, MAX) {
let stream = Cell::new(listener.accept());
rtdebug!("accepted");
let mut acceptor = TcpListener::bind(addr).listen();
for (i, stream) in acceptor.incoming().enumerate().take(MAX as uint) {
let stream = Cell::new(stream);
// Start another task to handle the connection
do spawntask {
let mut stream = stream.take();
Expand Down Expand Up @@ -499,10 +510,9 @@ mod test {
static MAX: int = 10;

do spawntask {
let mut listener = TcpListener::bind(addr);
for _ in range(0, MAX) {
let stream = Cell::new(listener.accept());
rtdebug!("accepted");
let mut acceptor = TcpListener::bind(addr).listen();
for stream in acceptor.incoming().take(MAX as uint) {
let stream = Cell::new(stream);
// Start another task to handle the connection
do spawntask_later {
let mut stream = stream.take();
Expand Down Expand Up @@ -537,10 +547,9 @@ mod test {
static MAX: int = 10;

do spawntask {
let mut listener = TcpListener::bind(addr);
for _ in range(0, MAX) {
let stream = Cell::new(listener.accept());
rtdebug!("accepted");
let mut acceptor = TcpListener::bind(addr).listen();
for stream in acceptor.incoming().take(MAX as uint) {
let stream = Cell::new(stream);
// Start another task to handle the connection
do spawntask_later {
let mut stream = stream.take();
Expand Down Expand Up @@ -573,10 +582,7 @@ mod test {
fn socket_name(addr: SocketAddr) {
do run_in_newsched_task {
do spawntask {
let listener = TcpListener::bind(addr);

assert!(listener.is_some());
let mut listener = listener.unwrap();
let mut listener = TcpListener::bind(addr).unwrap();

// Make sure socket_name gives
// us the socket we binded to.
Expand All @@ -592,9 +598,9 @@ mod test {
fn peer_name(addr: SocketAddr) {
do run_in_newsched_task {
do spawntask {
let mut listener = TcpListener::bind(addr);
let mut acceptor = TcpListener::bind(addr).listen();

listener.accept();
acceptor.accept();
}

do spawntask {
Expand Down
8 changes: 7 additions & 1 deletion src/libstd/rt/io/net/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ impl UnixListener {
}
}

impl Listener<UnixStream> for UnixListener {
impl Listener<UnixStream, UnixAcceptor> for UnixListener {
fn listen(self) -> Option<UnixAcceptor> { fail!() }
}

pub struct UnixAcceptor;

impl Acceptor<UnixStream> for UnixAcceptor {
fn accept(&mut self) -> Option<UnixStream> { fail!() }
}
20 changes: 16 additions & 4 deletions src/libstd/rt/io/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! # XXX Seek and Close

use option::*;
use super::{Reader, Writer, Listener};
use super::{Reader, Writer, Listener, Acceptor};
use super::{standard_error, PreviousIoError, io_error, read_error, IoError};

fn prev_io_error() -> IoError {
Expand Down Expand Up @@ -62,10 +62,22 @@ impl<R: Reader> Reader for Option<R> {
}
}

impl<L: Listener<S>, S> Listener<S> for Option<L> {
fn accept(&mut self) -> Option<S> {
impl<T, A: Acceptor<T>, L: Listener<T, A>> Listener<T, A> for Option<L> {
fn listen(self) -> Option<A> {
match self {
Some(listener) => listener.listen(),
None => {
io_error::cond.raise(prev_io_error());
None
}
}
}
}

impl<T, A: Acceptor<T>> Acceptor<T> for Option<A> {
fn accept(&mut self) -> Option<T> {
match *self {
Some(ref mut listener) => listener.accept(),
Some(ref mut acceptor) => acceptor.accept(),
None => {
io_error::cond.raise(prev_io_error());
None
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/rt/rtio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub type EventLoopObject = uvio::UvEventLoop;
pub type RemoteCallbackObject = uvio::UvRemoteCallback;
pub type IoFactoryObject = uvio::UvIoFactory;
pub type RtioTcpStreamObject = uvio::UvTcpStream;
pub type RtioTcpAcceptorObject = uvio::UvTcpAcceptor;
pub type RtioTcpListenerObject = uvio::UvTcpListener;
pub type RtioUdpSocketObject = uvio::UvUdpSocket;
pub type RtioTimerObject = uvio::UvTimer;
Expand Down Expand Up @@ -75,6 +76,10 @@ pub trait IoFactory {
}

pub trait RtioTcpListener : RtioSocket {
fn listen(self) -> Result<~RtioTcpAcceptorObject, IoError>;
}

pub trait RtioTcpAcceptor : RtioSocket {
fn accept(&mut self) -> Result<~RtioTcpStreamObject, IoError>;
fn accept_simultaneously(&mut self) -> Result<(), IoError>;
fn dont_accept_simultaneously(&mut self) -> Result<(), IoError>;
Expand Down
Loading