Struct std::old_io::net::tcp::TcpAcceptorUnstable
[-] [+]
[src]
pub struct TcpAcceptor { // some fields omitted }
The accepting half of a TCP socket server. This structure is created through
a TcpListener
's listen
method, and this object can be used to accept new
TcpStream
instances.
Methods
impl TcpAcceptor
fn set_timeout(&mut self, ms: Option<u64>)
Prevents blocking on all future accepts after ms
milliseconds have
elapsed.
This function is used to set a deadline after which this acceptor will time out accepting any connections. The argument is the relative distance, in milliseconds, to a point in the future after which all accepts will fail.
If the argument specified is None
, then any previously registered
timeout is cleared.
A timeout of 0
can be used to "poll" this acceptor to see if it has
any pending connections. All pending connections will be accepted,
regardless of whether the timeout has expired or not (the accept will
not block in this case).
Example
fn main() { use std::old_io::TcpListener; use std::old_io::{Listener, Acceptor, TimedOut}; let mut a = TcpListener::bind("127.0.0.1:8482").listen().unwrap(); // After 100ms have passed, all accepts will fail a.set_timeout(Some(100)); match a.accept() { Ok(..) => println!("accepted a socket"), Err(ref e) if e.kind == TimedOut => { println!("timed out!"); } Err(e) => println!("err: {}", e), } // Reset the timeout and try again a.set_timeout(Some(100)); let socket = a.accept(); // Clear the timeout and block indefinitely waiting for a connection a.set_timeout(None); let socket = a.accept(); }use std::old_io::TcpListener; use std::old_io::{Listener, Acceptor, TimedOut}; let mut a = TcpListener::bind("127.0.0.1:8482").listen().unwrap(); // After 100ms have passed, all accepts will fail a.set_timeout(Some(100)); match a.accept() { Ok(..) => println!("accepted a socket"), Err(ref e) if e.kind == TimedOut => { println!("timed out!"); } Err(e) => println!("err: {}", e), } // Reset the timeout and try again a.set_timeout(Some(100)); let socket = a.accept(); // Clear the timeout and block indefinitely waiting for a connection a.set_timeout(None); let socket = a.accept();
fn close_accept(&mut self) -> IoResult<()>
Closes the accepting capabilities of this acceptor.
This function is similar to TcpStream
's close_{read,write}
methods
in that it will affect all cloned handles of this acceptor's original
handle.
Once this function succeeds, all future calls to accept
will return
immediately with an error, preventing all future calls to accept. The
underlying socket will not be relinquished back to the OS until all
acceptors have been deallocated.
This is useful for waking up a thread in an accept loop to indicate that it should exit.
Example
fn main() { use std::old_io::{TcpListener, Listener, Acceptor, EndOfFile}; use std::thread; let mut a = TcpListener::bind("127.0.0.1:8482").listen().unwrap(); let a2 = a.clone(); let _t = thread::spawn(move|| { let mut a2 = a2; for socket in a2.incoming() { match socket { Ok(s) => { /* handle s */ } Err(ref e) if e.kind == EndOfFile => break, // closed Err(e) => panic!("unexpected error: {}", e), } } }); fn wait_for_sigint() {} // Now that our accept loop is running, wait for the program to be // requested to exit. wait_for_sigint(); // Signal our accept loop to exit assert!(a.close_accept().is_ok()); }use std::old_io::{TcpListener, Listener, Acceptor, EndOfFile}; use std::thread; let mut a = TcpListener::bind("127.0.0.1:8482").listen().unwrap(); let a2 = a.clone(); let _t = thread::spawn(move|| { let mut a2 = a2; for socket in a2.incoming() { match socket { Ok(s) => { /* handle s */ } Err(ref e) if e.kind == EndOfFile => break, // closed Err(e) => panic!("unexpected error: {}", e), } } }); // Now that our accept loop is running, wait for the program to be // requested to exit. wait_for_sigint(); // Signal our accept loop to exit assert!(a.close_accept().is_ok());
Trait Implementations
impl Acceptor<TcpStream> for TcpAcceptor
fn accept(&mut self) -> IoResult<TcpStream>
fn incoming<'r>(&'r mut self) -> IncomingConnections<'r, Self>
impl Clone for TcpAcceptor
fn clone(&self) -> TcpAcceptor
Creates a new handle to this TCP acceptor, allowing for simultaneous accepts.
The underlying TCP acceptor will not be closed until all handles to the acceptor have been deallocated. Incoming connections will be received on at most once acceptor, the same connection will not be accepted twice.
The close_accept
method will shut down all acceptors cloned from the
same original acceptor, whereas the set_timeout
method only affects
the selector that it is called on.
This function is useful for creating a handle to invoke close_accept
on to wake up any other task blocked in accept
.