1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
17 use super::{last_error, last_net_error, retry, sock_t};
19 use sync::atomic::{AtomicBool, Ordering};
20 use sys::fs::FileDesc;
21 use sys::{self, c, set_nonblocking, wouldblock, timer};
22 use sys_common::{self, timeout, eof, net};
24 pub use sys_common::net::TcpStream;
26 pub struct Event(c::WSAEVENT);
28 unsafe impl Send for Event {}
29 unsafe impl Sync for Event {}
32 pub fn new() -> IoResult<Event> {
33 let event = unsafe { c::WSACreateEvent() };
34 if event == c::WSA_INVALID_EVENT {
35 Err(super::last_error())
41 pub fn handle(&self) -> c::WSAEVENT { let Event(handle) = *self; handle }
46 unsafe { let _ = c::WSACloseEvent(self.handle()); }
50 ////////////////////////////////////////////////////////////////////////////////
52 ////////////////////////////////////////////////////////////////////////////////
54 pub struct TcpListener { sock: sock_t }
56 unsafe impl Send for TcpListener {}
57 unsafe impl Sync for TcpListener {}
60 pub fn bind(addr: ip::SocketAddr) -> IoResult<TcpListener> {
63 let sock = try!(net::socket(addr, libc::SOCK_STREAM));
64 let ret = TcpListener { sock: sock };
66 let mut storage = unsafe { mem::zeroed() };
67 let len = net::addr_to_sockaddr(addr, &mut storage);
68 let addrp = &storage as *const _ as *const libc::sockaddr;
70 match unsafe { libc::bind(sock, addrp, len) } {
71 -1 => Err(last_net_error()),
76 pub fn socket(&self) -> sock_t { self.sock }
78 pub fn listen(self, backlog: int) -> IoResult<TcpAcceptor> {
79 match unsafe { libc::listen(self.socket(), backlog as libc::c_int) } {
80 -1 => Err(last_net_error()),
83 let accept = try!(Event::new());
85 c::WSAEventSelect(self.socket(), accept.handle(), c::FD_ACCEPT)
88 return Err(last_net_error())
91 inner: Arc::new(AcceptorInner {
93 abort: try!(Event::new()),
95 closed: AtomicBool::new(false),
103 pub fn socket_name(&mut self) -> IoResult<ip::SocketAddr> {
104 net::sockname(self.socket(), libc::getsockname)
108 impl Drop for TcpListener {
110 unsafe { super::close_sock(self.sock); }
114 pub struct TcpAcceptor {
115 inner: Arc<AcceptorInner>,
119 unsafe impl Send for TcpAcceptor {}
120 unsafe impl Sync for TcpAcceptor {}
122 struct AcceptorInner {
123 listener: TcpListener,
129 unsafe impl Send for AcceptorInner {}
130 unsafe impl Sync for AcceptorInner {}
133 pub fn socket(&self) -> sock_t { self.inner.listener.socket() }
135 pub fn accept(&mut self) -> IoResult<TcpStream> {
136 // Unlink unix, windows cannot invoke `select` on arbitrary file
137 // descriptors like pipes, only sockets. Consequently, windows cannot
138 // use the same implementation as unix for accept() when close_accept()
141 // In order to implement close_accept() and timeouts, windows uses
142 // event handles. An acceptor-specific abort event is created which
143 // will only get set in close_accept(), and it will never be un-set.
144 // Additionally, another acceptor-specific event is associated with the
145 // FD_ACCEPT network event.
147 // These two events are then passed to WaitForMultipleEvents to see
148 // which one triggers first, and the timeout passed to this function is
149 // the local timeout for the acceptor.
151 // If the wait times out, then the accept timed out. If the wait
152 // succeeds with the abort event, then we were closed, and if the wait
153 // succeeds otherwise, then we do a nonblocking poll via `accept` to
154 // see if we can accept a connection. The connection is candidate to be
155 // stolen, so we do all of this in a loop as well.
156 let events = [self.inner.abort.handle(), self.inner.accept.handle()];
158 while !self.inner.closed.load(Ordering::SeqCst) {
159 let ms = if self.deadline == 0 {
160 c::WSA_INFINITE as u64
162 let now = timer::now();
163 if self.deadline < now {0} else {self.deadline - now}
166 c::WSAWaitForMultipleEvents(2, events.as_ptr(), libc::FALSE,
167 ms as libc::DWORD, libc::FALSE)
170 c::WSA_WAIT_TIMEOUT => {
171 return Err(timeout("accept timed out"))
173 c::WSA_WAIT_FAILED => return Err(last_net_error()),
174 c::WSA_WAIT_EVENT_0 => break,
175 n => assert_eq!(n, c::WSA_WAIT_EVENT_0 + 1),
178 let mut wsaevents: c::WSANETWORKEVENTS = unsafe { mem::zeroed() };
180 c::WSAEnumNetworkEvents(self.socket(), events[1], &mut wsaevents)
182 if ret != 0 { return Err(last_net_error()) }
184 if wsaevents.lNetworkEvents & c::FD_ACCEPT == 0 { continue }
186 libc::accept(self.socket(), ptr::null_mut(), ptr::null_mut())
188 -1 if wouldblock() => {}
189 -1 => return Err(last_net_error()),
191 // Accepted sockets inherit the same properties as the caller,
192 // so we need to deregister our event and switch the socket back
195 let stream = TcpStream::new(socket);
197 c::WSAEventSelect(socket, events[1], 0)
199 if ret != 0 { return Err(last_net_error()) }
200 try!(set_nonblocking(socket, false));
209 pub fn socket_name(&mut self) -> IoResult<ip::SocketAddr> {
210 net::sockname(self.socket(), libc::getsockname)
213 pub fn set_timeout(&mut self, timeout: Option<u64>) {
214 self.deadline = timeout.map(|a| timer::now() + a).unwrap_or(0);
217 pub fn close_accept(&mut self) -> IoResult<()> {
218 self.inner.closed.store(true, Ordering::SeqCst);
219 let ret = unsafe { c::WSASetEvent(self.inner.abort.handle()) };
220 if ret == libc::TRUE {
223 Err(last_net_error())
228 impl Clone for TcpAcceptor {
229 fn clone(&self) -> TcpAcceptor {
231 inner: self.inner.clone(),