2014-10-10 12:11:49 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-23 12:46:14 -06:00
|
|
|
use old_io::net::ip;
|
|
|
|
use old_io::IoResult;
|
2014-10-10 12:11:49 -05:00
|
|
|
use libc;
|
|
|
|
use mem;
|
|
|
|
use ptr;
|
2014-12-22 11:04:23 -06:00
|
|
|
use prelude::v1::*;
|
2015-01-27 14:20:58 -06:00
|
|
|
use super::{last_error, last_net_error, sock_t};
|
2015-01-02 01:53:35 -06:00
|
|
|
use sync::Arc;
|
|
|
|
use sync::atomic::{AtomicBool, Ordering};
|
2015-01-03 21:42:21 -06:00
|
|
|
use sys::{self, c, set_nonblocking, wouldblock, timer};
|
2015-01-27 14:20:58 -06:00
|
|
|
use sys_common::{timeout, eof, net};
|
2014-10-10 12:11:49 -05:00
|
|
|
|
|
|
|
pub use sys_common::net::TcpStream;
|
|
|
|
|
|
|
|
pub struct Event(c::WSAEVENT);
|
|
|
|
|
2014-12-26 16:01:47 -06:00
|
|
|
unsafe impl Send for Event {}
|
|
|
|
unsafe impl Sync for Event {}
|
|
|
|
|
2014-10-10 12:11:49 -05:00
|
|
|
impl Event {
|
|
|
|
pub fn new() -> IoResult<Event> {
|
|
|
|
let event = unsafe { c::WSACreateEvent() };
|
|
|
|
if event == c::WSA_INVALID_EVENT {
|
|
|
|
Err(super::last_error())
|
|
|
|
} else {
|
|
|
|
Ok(Event(event))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle(&self) -> c::WSAEVENT { let Event(handle) = *self; handle }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Event {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { let _ = c::WSACloseEvent(self.handle()); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TCP listeners
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-11-25 23:53:08 -06:00
|
|
|
pub struct TcpListener { sock: sock_t }
|
2014-10-10 12:11:49 -05:00
|
|
|
|
2014-12-26 16:01:47 -06:00
|
|
|
unsafe impl Send for TcpListener {}
|
|
|
|
unsafe impl Sync for TcpListener {}
|
|
|
|
|
2014-10-10 12:11:49 -05:00
|
|
|
impl TcpListener {
|
|
|
|
pub fn bind(addr: ip::SocketAddr) -> IoResult<TcpListener> {
|
|
|
|
sys::init_net();
|
|
|
|
|
2014-12-14 22:03:00 -06:00
|
|
|
let sock = try!(net::socket(addr, libc::SOCK_STREAM));
|
2014-11-25 23:53:08 -06:00
|
|
|
let ret = TcpListener { sock: sock };
|
2014-10-10 12:11:49 -05:00
|
|
|
|
|
|
|
let mut storage = unsafe { mem::zeroed() };
|
2014-12-14 22:03:00 -06:00
|
|
|
let len = net::addr_to_sockaddr(addr, &mut storage);
|
2014-10-10 12:11:49 -05:00
|
|
|
let addrp = &storage as *const _ as *const libc::sockaddr;
|
|
|
|
|
2014-11-25 23:53:08 -06:00
|
|
|
match unsafe { libc::bind(sock, addrp, len) } {
|
2014-10-10 12:11:49 -05:00
|
|
|
-1 => Err(last_net_error()),
|
|
|
|
_ => Ok(ret),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 23:53:08 -06:00
|
|
|
pub fn socket(&self) -> sock_t { self.sock }
|
2014-10-10 12:11:49 -05:00
|
|
|
|
|
|
|
pub fn listen(self, backlog: int) -> IoResult<TcpAcceptor> {
|
2014-11-25 23:53:08 -06:00
|
|
|
match unsafe { libc::listen(self.socket(), backlog as libc::c_int) } {
|
2014-10-10 12:11:49 -05:00
|
|
|
-1 => Err(last_net_error()),
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
let accept = try!(Event::new());
|
|
|
|
let ret = unsafe {
|
2014-11-25 23:53:08 -06:00
|
|
|
c::WSAEventSelect(self.socket(), accept.handle(), c::FD_ACCEPT)
|
2014-10-10 12:11:49 -05:00
|
|
|
};
|
|
|
|
if ret != 0 {
|
|
|
|
return Err(last_net_error())
|
|
|
|
}
|
|
|
|
Ok(TcpAcceptor {
|
|
|
|
inner: Arc::new(AcceptorInner {
|
|
|
|
listener: self,
|
|
|
|
abort: try!(Event::new()),
|
|
|
|
accept: accept,
|
2015-01-02 01:53:35 -06:00
|
|
|
closed: AtomicBool::new(false),
|
2014-10-10 12:11:49 -05:00
|
|
|
}),
|
|
|
|
deadline: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn socket_name(&mut self) -> IoResult<ip::SocketAddr> {
|
2014-12-14 22:03:00 -06:00
|
|
|
net::sockname(self.socket(), libc::getsockname)
|
2014-11-25 23:53:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TcpListener {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { super::close_sock(self.sock); }
|
2014-10-10 12:11:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TcpAcceptor {
|
|
|
|
inner: Arc<AcceptorInner>,
|
|
|
|
deadline: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AcceptorInner {
|
|
|
|
listener: TcpListener,
|
|
|
|
abort: Event,
|
|
|
|
accept: Event,
|
2015-01-02 01:53:35 -06:00
|
|
|
closed: AtomicBool,
|
2014-10-10 12:11:49 -05:00
|
|
|
}
|
|
|
|
|
2014-12-26 16:01:47 -06:00
|
|
|
unsafe impl Sync for AcceptorInner {}
|
|
|
|
|
2014-10-10 12:11:49 -05:00
|
|
|
impl TcpAcceptor {
|
2014-11-25 23:53:08 -06:00
|
|
|
pub fn socket(&self) -> sock_t { self.inner.listener.socket() }
|
2014-10-10 12:11:49 -05:00
|
|
|
|
|
|
|
pub fn accept(&mut self) -> IoResult<TcpStream> {
|
|
|
|
// Unlink unix, windows cannot invoke `select` on arbitrary file
|
|
|
|
// descriptors like pipes, only sockets. Consequently, windows cannot
|
|
|
|
// use the same implementation as unix for accept() when close_accept()
|
|
|
|
// is considered.
|
|
|
|
//
|
|
|
|
// In order to implement close_accept() and timeouts, windows uses
|
|
|
|
// event handles. An acceptor-specific abort event is created which
|
|
|
|
// will only get set in close_accept(), and it will never be un-set.
|
|
|
|
// Additionally, another acceptor-specific event is associated with the
|
|
|
|
// FD_ACCEPT network event.
|
|
|
|
//
|
|
|
|
// These two events are then passed to WaitForMultipleEvents to see
|
|
|
|
// which one triggers first, and the timeout passed to this function is
|
|
|
|
// the local timeout for the acceptor.
|
|
|
|
//
|
|
|
|
// If the wait times out, then the accept timed out. If the wait
|
|
|
|
// succeeds with the abort event, then we were closed, and if the wait
|
|
|
|
// succeeds otherwise, then we do a nonblocking poll via `accept` to
|
|
|
|
// see if we can accept a connection. The connection is candidate to be
|
|
|
|
// stolen, so we do all of this in a loop as well.
|
|
|
|
let events = [self.inner.abort.handle(), self.inner.accept.handle()];
|
|
|
|
|
2015-01-02 01:53:35 -06:00
|
|
|
while !self.inner.closed.load(Ordering::SeqCst) {
|
2014-10-10 12:11:49 -05:00
|
|
|
let ms = if self.deadline == 0 {
|
|
|
|
c::WSA_INFINITE as u64
|
|
|
|
} else {
|
|
|
|
let now = timer::now();
|
|
|
|
if self.deadline < now {0} else {self.deadline - now}
|
|
|
|
};
|
|
|
|
let ret = unsafe {
|
|
|
|
c::WSAWaitForMultipleEvents(2, events.as_ptr(), libc::FALSE,
|
|
|
|
ms as libc::DWORD, libc::FALSE)
|
|
|
|
};
|
|
|
|
match ret {
|
|
|
|
c::WSA_WAIT_TIMEOUT => {
|
|
|
|
return Err(timeout("accept timed out"))
|
|
|
|
}
|
|
|
|
c::WSA_WAIT_FAILED => return Err(last_net_error()),
|
|
|
|
c::WSA_WAIT_EVENT_0 => break,
|
|
|
|
n => assert_eq!(n, c::WSA_WAIT_EVENT_0 + 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut wsaevents: c::WSANETWORKEVENTS = unsafe { mem::zeroed() };
|
|
|
|
let ret = unsafe {
|
2014-11-25 23:53:08 -06:00
|
|
|
c::WSAEnumNetworkEvents(self.socket(), events[1], &mut wsaevents)
|
2014-10-10 12:11:49 -05:00
|
|
|
};
|
|
|
|
if ret != 0 { return Err(last_net_error()) }
|
|
|
|
|
|
|
|
if wsaevents.lNetworkEvents & c::FD_ACCEPT == 0 { continue }
|
|
|
|
match unsafe {
|
2014-11-25 23:53:08 -06:00
|
|
|
libc::accept(self.socket(), ptr::null_mut(), ptr::null_mut())
|
2014-10-10 12:11:49 -05:00
|
|
|
} {
|
|
|
|
-1 if wouldblock() => {}
|
|
|
|
-1 => return Err(last_net_error()),
|
|
|
|
|
|
|
|
// Accepted sockets inherit the same properties as the caller,
|
|
|
|
// so we need to deregister our event and switch the socket back
|
|
|
|
// to blocking mode
|
2014-11-25 23:53:08 -06:00
|
|
|
socket => {
|
|
|
|
let stream = TcpStream::new(socket);
|
2014-10-10 12:11:49 -05:00
|
|
|
let ret = unsafe {
|
2014-11-25 23:53:08 -06:00
|
|
|
c::WSAEventSelect(socket, events[1], 0)
|
2014-10-10 12:11:49 -05:00
|
|
|
};
|
|
|
|
if ret != 0 { return Err(last_net_error()) }
|
2014-11-25 23:53:08 -06:00
|
|
|
try!(set_nonblocking(socket, false));
|
2014-10-10 12:11:49 -05:00
|
|
|
return Ok(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(eof())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_timeout(&mut self, timeout: Option<u64>) {
|
|
|
|
self.deadline = timeout.map(|a| timer::now() + a).unwrap_or(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_accept(&mut self) -> IoResult<()> {
|
2015-01-02 01:53:35 -06:00
|
|
|
self.inner.closed.store(true, Ordering::SeqCst);
|
2014-10-10 12:11:49 -05:00
|
|
|
let ret = unsafe { c::WSASetEvent(self.inner.abort.handle()) };
|
|
|
|
if ret == libc::TRUE {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(last_net_error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for TcpAcceptor {
|
|
|
|
fn clone(&self) -> TcpAcceptor {
|
|
|
|
TcpAcceptor {
|
|
|
|
inner: self.inner.clone(),
|
|
|
|
deadline: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|