futures_net/driver/sys/net/tcp.rs
1//! Primitives for working with TCP
2//!
3//! The types provided in this module are non-blocking by default and are
4//! designed to be portable across all supported Mio platforms. As long as the
5//! [portability guidelines] are followed, the behavior should be identical no
6//! matter the target platform.
7//!
8/// [portability guidelines]: ../struct.Poll.html#portability
9use std::fmt;
10use std::io::{Read, Write};
11use std::net::Shutdown;
12use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
13use std::time::Duration;
14
15use iovec::IoVec;
16use net2::TcpBuilder;
17use std::io;
18
19use crate::driver::sys::event::{Evented, PollOpt, Ready};
20use crate::driver::sys::poll::SelectorId;
21use crate::driver::sys::{linux, Poll, Token};
22
23/*
24 *
25 * ===== TcpStream =====
26 *
27 */
28
29/// A non-blocking TCP stream between a local socket and a remote socket.
30///
31/// The socket will be closed when the value is dropped.
32///
33/// # Examples
34///
35/// ```
36/// # use std::net::TcpListener;
37/// # use std::error::Error;
38/// #
39/// # fn try_main() -> Result<(), Box<Error>> {
40/// # let _listener = TcpListener::bind("127.0.0.1:34254")?;
41/// use futures_net::driver::sys::{Poll, Token};
42/// use futures_net::driver::sys::event::{Events, Ready, PollOpt};
43/// use futures_net::driver::sys::net::TcpStream;
44/// use std::time::Duration;
45///
46/// let stream = TcpStream::connect(&"127.0.0.1:34254".parse()?)?;
47///
48/// let poll = Poll::new()?;
49/// let mut events = Events::with_capacity(128);
50///
51/// // Register the socket with `Poll`
52/// poll.register(&stream, Token(0), Ready::writable(),
53/// PollOpt::edge())?;
54///
55/// poll.poll(&mut events, Some(Duration::from_millis(100)))?;
56///
57/// // The socket might be ready at this point
58/// # Ok(())
59/// # }
60/// #
61/// # fn main() {
62/// # try_main().unwrap();
63/// # }
64/// ```
65pub struct TcpStream {
66 sys: linux::TcpStream,
67 selector_id: SelectorId,
68}
69
70fn set_nonblocking(stream: &net::TcpStream) -> io::Result<()> {
71 stream.set_nonblocking(true)
72}
73
74impl TcpStream {
75 /// Create a new TCP stream and issue a non-blocking connect to the
76 /// specified address.
77 ///
78 /// This convenience method is available and uses the system's default
79 /// options when creating a socket which is then connected. If fine-grained
80 /// control over the creation of the socket is desired, you can use
81 /// `net2::TcpBuilder` to configure a socket and then pass its socket to
82 /// `TcpStream::connect_stream` to transfer ownership into mio and schedule
83 /// the connect operation.
84 pub fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
85 let sock = match *addr {
86 SocketAddr::V4(..) => TcpBuilder::new_v4(),
87 SocketAddr::V6(..) => TcpBuilder::new_v6(),
88 }?;
89
90 TcpStream::connect_stream(sock.to_tcp_stream()?, addr)
91 }
92
93 /// Creates a new `TcpStream` from the pending socket inside the given
94 /// `std::net::TcpBuilder`, connecting it to the address specified.
95 ///
96 /// This constructor allows configuring the socket before it's actually
97 /// connected, and this function will transfer ownership to the returned
98 /// `TcpStream` if successful. An unconnected `TcpStream` can be created
99 /// with the `net2::TcpBuilder` type (and also configured via that route).
100 ///
101 /// The platform specific behavior of this function looks like:
102 ///
103 /// * On Unix, the socket is placed into nonblocking mode and then a
104 /// `connect` call is issued.
105 ///
106 /// * On Windows, the address is stored internally and the connect operation
107 /// is issued when the returned `TcpStream` is registered with an event
108 /// loop. Note that on Windows you must `bind` a socket before it can be
109 /// connected, so if a custom `TcpBuilder` is used it should be bound
110 /// (perhaps to `INADDR_ANY`) before this method is called.
111 pub fn connect_stream(
112 stream: net::TcpStream,
113 addr: &SocketAddr,
114 ) -> io::Result<TcpStream> {
115 Ok(TcpStream {
116 sys: linux::TcpStream::connect(stream, addr)?,
117 selector_id: SelectorId::new(),
118 })
119 }
120
121 /// Creates a new `TcpStream` from a standard `net::TcpStream`.
122 ///
123 /// This function is intended to be used to wrap a TCP stream from the
124 /// standard library in the mio equivalent. The conversion here will
125 /// automatically set `stream` to nonblocking and the returned object should
126 /// be ready to get associated with an event loop.
127 ///
128 /// Note that the TCP stream here will not have `connect` called on it, so
129 /// it should already be connected via some other means (be it manually, the
130 /// net2 crate, or the standard library).
131 pub fn from_stream(stream: net::TcpStream) -> io::Result<TcpStream> {
132 set_nonblocking(&stream)?;
133
134 Ok(TcpStream {
135 sys: linux::TcpStream::from_stream(stream),
136 selector_id: SelectorId::new(),
137 })
138 }
139
140 /// Returns the socket address of the remote peer of this TCP connection.
141 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
142 self.sys.peer_addr()
143 }
144
145 /// Returns the socket address of the local half of this TCP connection.
146 pub fn local_addr(&self) -> io::Result<SocketAddr> {
147 self.sys.local_addr()
148 }
149
150 /// Creates a new independently owned handle to the underlying socket.
151 ///
152 /// The returned `TcpStream` is a reference to the same stream that this
153 /// object references. Both handles will read and write the same stream of
154 /// data, and options set on one stream will be propagated to the other
155 /// stream.
156 pub fn try_clone(&self) -> io::Result<TcpStream> {
157 self.sys.try_clone().map(|s| TcpStream {
158 sys: s,
159 selector_id: self.selector_id.clone(),
160 })
161 }
162
163 /// Shuts down the read, write, or both halves of this connection.
164 ///
165 /// This function will cause all pending and future I/O on the specified
166 /// portions to return immediately with an appropriate value (see the
167 /// documentation of `Shutdown`).
168 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
169 self.sys.shutdown(how)
170 }
171
172 /// Sets the value of the `TCP_NODELAY` option on this socket.
173 ///
174 /// If set, this option disables the Nagle algorithm. This means that
175 /// segments are always sent as soon as possible, even if there is only a
176 /// small amount of data. When not set, data is buffered until there is a
177 /// sufficient amount to send out, thereby avoiding the frequent sending of
178 /// small packets.
179 pub fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
180 self.sys.set_nodelay(nodelay)
181 }
182
183 /// Gets the value of the `TCP_NODELAY` option on this socket.
184 ///
185 /// For more information about this option, see [`set_nodelay`][link].
186 ///
187 /// [link]: #method.set_nodelay
188 pub fn nodelay(&self) -> io::Result<bool> {
189 self.sys.nodelay()
190 }
191
192 /// Sets the value of the `SO_RCVBUF` option on this socket.
193 ///
194 /// Changes the size of the operating system's receive buffer associated
195 /// with the socket.
196 pub fn set_recv_buffer_size(&self, size: usize) -> io::Result<()> {
197 self.sys.set_recv_buffer_size(size)
198 }
199
200 /// Gets the value of the `SO_RCVBUF` option on this socket.
201 ///
202 /// For more information about this option, see
203 /// [`set_recv_buffer_size`][link].
204 ///
205 /// [link]: #method.set_recv_buffer_size
206 pub fn recv_buffer_size(&self) -> io::Result<usize> {
207 self.sys.recv_buffer_size()
208 }
209
210 /// Sets the value of the `SO_SNDBUF` option on this socket.
211 ///
212 /// Changes the size of the operating system's send buffer associated with
213 /// the socket.
214 pub fn set_send_buffer_size(&self, size: usize) -> io::Result<()> {
215 self.sys.set_send_buffer_size(size)
216 }
217
218 /// Gets the value of the `SO_SNDBUF` option on this socket.
219 ///
220 /// For more information about this option, see
221 /// [`set_send_buffer_size`][link].
222 ///
223 /// [link]: #method.set_send_buffer_size
224 pub fn send_buffer_size(&self) -> io::Result<usize> {
225 self.sys.send_buffer_size()
226 }
227
228 /// Sets whether keepalive messages are enabled to be sent on this socket.
229 ///
230 /// On Unix, this option will set the `SO_KEEPALIVE` as well as the
231 /// `TCP_KEEPALIVE` or `TCP_KEEPIDLE` option (depending on your platform).
232 /// On Windows, this will set the `SIO_KEEPALIVE_VALS` option.
233 ///
234 /// If `None` is specified then keepalive messages are disabled, otherwise
235 /// the duration specified will be the time to remain idle before sending a
236 /// TCP keepalive probe.
237 ///
238 /// Some platforms specify this value in seconds, so sub-second
239 /// specifications may be omitted.
240 pub fn set_keepalive(&self, keepalive: Option<Duration>) -> io::Result<()> {
241 self.sys.set_keepalive(keepalive)
242 }
243
244 /// Returns whether keepalive messages are enabled on this socket, and if so
245 /// the duration of time between them.
246 ///
247 /// For more information about this option, see [`set_keepalive`][link].
248 ///
249 /// [link]: #method.set_keepalive
250 pub fn keepalive(&self) -> io::Result<Option<Duration>> {
251 self.sys.keepalive()
252 }
253
254 /// Sets the value for the `IP_TTL` option on this socket.
255 ///
256 /// This value sets the time-to-live field that is used in every packet sent
257 /// from this socket.
258 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
259 self.sys.set_ttl(ttl)
260 }
261
262 /// Gets the value of the `IP_TTL` option for this socket.
263 ///
264 /// For more information about this option, see [`set_ttl`][link].
265 ///
266 /// [link]: #method.set_ttl
267 pub fn ttl(&self) -> io::Result<u32> {
268 self.sys.ttl()
269 }
270
271 /// Sets the value for the `IPV6_V6ONLY` option on this socket.
272 ///
273 /// If this is set to `true` then the socket is restricted to sending and
274 /// receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
275 /// can bind the same port at the same time.
276 ///
277 /// If this is set to `false` then the socket can be used to send and
278 /// receive packets from an IPv4-mapped IPv6 address.
279 pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
280 self.sys.set_only_v6(only_v6)
281 }
282
283 /// Gets the value of the `IPV6_V6ONLY` option for this socket.
284 ///
285 /// For more information about this option, see [`set_only_v6`][link].
286 ///
287 /// [link]: #method.set_only_v6
288 pub fn only_v6(&self) -> io::Result<bool> {
289 self.sys.only_v6()
290 }
291
292 /// Sets the value for the `SO_LINGER` option on this socket.
293 pub fn set_linger(&self, dur: Option<Duration>) -> io::Result<()> {
294 self.sys.set_linger(dur)
295 }
296
297 /// Gets the value of the `SO_LINGER` option on this socket.
298 ///
299 /// For more information about this option, see [`set_linger`][link].
300 ///
301 /// [link]: #method.set_linger
302 pub fn linger(&self) -> io::Result<Option<Duration>> {
303 self.sys.linger()
304 }
305
306 /// Get the value of the `SO_ERROR` option on this socket.
307 ///
308 /// This will retrieve the stored error in the underlying socket, clearing
309 /// the field in the process. This can be useful for checking errors between
310 /// calls.
311 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
312 self.sys.take_error()
313 }
314
315 /// Receives data on the socket from the remote address to which it is
316 /// connected, without removing that data from the queue. On success,
317 /// returns the number of bytes peeked.
318 ///
319 /// Successive calls return the same data. This is accomplished by passing
320 /// `MSG_PEEK` as a flag to the underlying recv system call.
321 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
322 self.sys.peek(buf)
323 }
324
325 /// Read in a list of buffers all at once.
326 ///
327 /// This operation will attempt to read bytes from this socket and place
328 /// them into the list of buffers provided. Note that each buffer is an
329 /// `IoVec` which can be created from a byte slice.
330 ///
331 /// The buffers provided will be filled in sequentially. A buffer will be
332 /// entirely filled up before the next is written to.
333 ///
334 /// The number of bytes read is returned, if successful, or an error is
335 /// returned otherwise. If no bytes are available to be read yet then
336 /// a "would block" error is returned. This operation does not block.
337 ///
338 /// On Unix this corresponds to the `readv` syscall.
339 pub fn read_bufs(&self, bufs: &mut [&mut IoVec]) -> io::Result<usize> {
340 self.sys.readv(bufs)
341 }
342
343 /// Write a list of buffers all at once.
344 ///
345 /// This operation will attempt to write a list of byte buffers to this
346 /// socket. Note that each buffer is an `IoVec` which can be created from a
347 /// byte slice.
348 ///
349 /// The buffers provided will be written sequentially. A buffer will be
350 /// entirely written before the next is written.
351 ///
352 /// The number of bytes written is returned, if successful, or an error is
353 /// returned otherwise. If the socket is not currently writable then a
354 /// "would block" error is returned. This operation does not block.
355 ///
356 /// On Unix this corresponds to the `writev` syscall.
357 pub fn write_bufs(&self, bufs: &[&IoVec]) -> io::Result<usize> {
358 self.sys.writev(bufs)
359 }
360}
361
362fn inaddr_any(other: &SocketAddr) -> SocketAddr {
363 match *other {
364 SocketAddr::V4(..) => {
365 let any = Ipv4Addr::new(0, 0, 0, 0);
366 let addr = SocketAddrV4::new(any, 0);
367 SocketAddr::V4(addr)
368 }
369 SocketAddr::V6(..) => {
370 let any = Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0);
371 let addr = SocketAddrV6::new(any, 0, 0, 0);
372 SocketAddr::V6(addr)
373 }
374 }
375}
376
377impl Read for TcpStream {
378 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
379 (&self.sys).read(buf)
380 }
381}
382
383impl<'a> Read for &'a TcpStream {
384 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
385 (&self.sys).read(buf)
386 }
387}
388
389impl Write for TcpStream {
390 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
391 (&self.sys).write(buf)
392 }
393
394 fn flush(&mut self) -> io::Result<()> {
395 (&self.sys).flush()
396 }
397}
398
399impl<'a> Write for &'a TcpStream {
400 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
401 (&self.sys).write(buf)
402 }
403
404 fn flush(&mut self) -> io::Result<()> {
405 (&self.sys).flush()
406 }
407}
408
409impl Evented for TcpStream {
410 fn register(
411 &self,
412 poll: &Poll,
413 token: Token,
414 interest: Ready,
415 opts: PollOpt,
416 ) -> io::Result<()> {
417 self.selector_id.associate_selector(poll)?;
418 self.sys.register(poll, token, interest, opts)
419 }
420
421 fn reregister(
422 &self,
423 poll: &Poll,
424 token: Token,
425 interest: Ready,
426 opts: PollOpt,
427 ) -> io::Result<()> {
428 self.sys.reregister(poll, token, interest, opts)
429 }
430
431 fn deregister(&self, poll: &Poll) -> io::Result<()> {
432 self.sys.deregister(poll)
433 }
434}
435
436impl fmt::Debug for TcpStream {
437 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
438 fmt::Debug::fmt(&self.sys, f)
439 }
440}
441
442/*
443 *
444 * ===== TcpListener =====
445 *
446 */
447
448/// A structure representing a socket server
449///
450/// # Examples
451///
452/// ```
453/// # use std::error::Error;
454/// # fn try_main() -> Result<(), Box<Error>> {
455/// use futures_net::driver::sys::{Poll, Token};
456/// use futures_net::driver::sys::event::{Events, Ready, PollOpt};
457/// use futures_net::driver::sys::net::TcpListener;
458/// use std::time::Duration;
459///
460/// let listener = TcpListener::bind(&"127.0.0.1:34255".parse()?)?;
461///
462/// let poll = Poll::new()?;
463/// let mut events = Events::with_capacity(128);
464///
465/// // Register the socket with `Poll`
466/// poll.register(&listener, Token(0), Ready::readable(),
467/// PollOpt::edge())?;
468///
469/// poll.poll(&mut events, Some(Duration::from_millis(100)))?;
470///
471/// // There may be a socket ready to be accepted
472/// # Ok(())
473/// # }
474/// #
475/// # fn main() {
476/// # try_main().unwrap();
477/// # }
478/// ```
479pub struct TcpListener {
480 sys: linux::TcpListener,
481 selector_id: SelectorId,
482}
483
484impl TcpListener {
485 /// Convenience method to bind a new TCP listener to the specified address
486 /// to receive new connections.
487 ///
488 /// This function will take the following steps:
489 ///
490 /// 1. Create a new TCP socket.
491 /// 2. Set the `SO_REUSEADDR` option on the socket.
492 /// 3. Bind the socket to the specified address.
493 /// 4. Call `listen` on the socket to prepare it to receive new connections.
494 ///
495 /// If fine-grained control over the binding and listening process for a
496 /// socket is desired then the `net2::TcpBuilder` methods can be used in
497 /// combination with the `TcpListener::from_listener` method to transfer
498 /// ownership into mio.
499 pub fn bind(addr: &SocketAddr) -> io::Result<TcpListener> {
500 // Create the socket
501 let sock = match *addr {
502 SocketAddr::V4(..) => TcpBuilder::new_v4(),
503 SocketAddr::V6(..) => TcpBuilder::new_v6(),
504 }?;
505
506 // Set SO_REUSEADDR, but only on Unix (mirrors what libstd does)
507 sock.reuse_address(true)?;
508
509 // Bind the socket
510 sock.bind(addr)?;
511
512 // listen
513 let listener = sock.listen(1024)?;
514 Ok(TcpListener {
515 sys: linux::TcpListener::new(listener)?,
516 selector_id: SelectorId::new(),
517 })
518 }
519
520 /// Creates a new `TcpListener` from an instance of a
521 /// `std::net::TcpListener` type.
522 ///
523 /// This function will set the `listener` provided into nonblocking mode on
524 /// Unix, and otherwise the stream will just be wrapped up in an mio stream
525 /// ready to accept new connections and become associated with an event
526 /// loop.
527 ///
528 /// The address provided must be the address that the listener is bound to.
529 pub fn from_std(listener: net::TcpListener) -> io::Result<TcpListener> {
530 linux::TcpListener::new(listener).map(|s| TcpListener {
531 sys: s,
532 selector_id: SelectorId::new(),
533 })
534 }
535
536 /// Accepts a new `TcpStream`.
537 ///
538 /// This may return an `Err(e)` where `e.kind()` is
539 /// `io::ErrorKind::WouldBlock`. This means a stream may be ready at a later
540 /// point and one should wait for a notification before calling `accept`
541 /// again.
542 ///
543 /// If an accepted stream is returned, the remote address of the peer is
544 /// returned along with it.
545 pub fn accept(&self) -> io::Result<(TcpStream, SocketAddr)> {
546 let (s, a) = self.accept_std()?;
547 Ok((TcpStream::from_stream(s)?, a))
548 }
549
550 /// Accepts a new `std::net::TcpStream`.
551 ///
552 /// This method is the same as `accept`, except that it returns a TCP socket
553 /// *in blocking mode* which isn't bound to `mio`. This can be later then
554 /// converted to a `mio` type, if necessary.
555 pub fn accept_std(&self) -> io::Result<(net::TcpStream, SocketAddr)> {
556 self.sys.accept()
557 }
558
559 /// Returns the local socket address of this listener.
560 pub fn local_addr(&self) -> io::Result<SocketAddr> {
561 self.sys.local_addr()
562 }
563
564 /// Creates a new independently owned handle to the underlying socket.
565 ///
566 /// The returned `TcpListener` is a reference to the same socket that this
567 /// object references. Both handles can be used to accept incoming
568 /// connections and options set on one listener will affect the other.
569 pub fn try_clone(&self) -> io::Result<TcpListener> {
570 self.sys.try_clone().map(|s| TcpListener {
571 sys: s,
572 selector_id: self.selector_id.clone(),
573 })
574 }
575
576 /// Sets the value for the `IP_TTL` option on this socket.
577 ///
578 /// This value sets the time-to-live field that is used in every packet sent
579 /// from this socket.
580 pub fn set_ttl(&self, ttl: u32) -> io::Result<()> {
581 self.sys.set_ttl(ttl)
582 }
583
584 /// Gets the value of the `IP_TTL` option for this socket.
585 ///
586 /// For more information about this option, see [`set_ttl`][link].
587 ///
588 /// [link]: #method.set_ttl
589 pub fn ttl(&self) -> io::Result<u32> {
590 self.sys.ttl()
591 }
592
593 /// Sets the value for the `IPV6_V6ONLY` option on this socket.
594 ///
595 /// If this is set to `true` then the socket is restricted to sending and
596 /// receiving IPv6 packets only. In this case two IPv4 and IPv6 applications
597 /// can bind the same port at the same time.
598 ///
599 /// If this is set to `false` then the socket can be used to send and
600 /// receive packets from an IPv4-mapped IPv6 address.
601 pub fn set_only_v6(&self, only_v6: bool) -> io::Result<()> {
602 self.sys.set_only_v6(only_v6)
603 }
604
605 /// Gets the value of the `IPV6_V6ONLY` option for this socket.
606 ///
607 /// For more information about this option, see [`set_only_v6`][link].
608 ///
609 /// [link]: #method.set_only_v6
610 pub fn only_v6(&self) -> io::Result<bool> {
611 self.sys.only_v6()
612 }
613
614 /// Get the value of the `SO_ERROR` option on this socket.
615 ///
616 /// This will retrieve the stored error in the underlying socket, clearing
617 /// the field in the process. This can be useful for checking errors between
618 /// calls.
619 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
620 self.sys.take_error()
621 }
622}
623
624impl Evented for TcpListener {
625 fn register(
626 &self,
627 poll: &Poll,
628 token: Token,
629 interest: Ready,
630 opts: PollOpt,
631 ) -> io::Result<()> {
632 self.selector_id.associate_selector(poll)?;
633 self.sys.register(poll, token, interest, opts)
634 }
635
636 fn reregister(
637 &self,
638 poll: &Poll,
639 token: Token,
640 interest: Ready,
641 opts: PollOpt,
642 ) -> io::Result<()> {
643 self.sys.reregister(poll, token, interest, opts)
644 }
645
646 fn deregister(&self, poll: &Poll) -> io::Result<()> {
647 self.sys.deregister(poll)
648 }
649}
650
651impl fmt::Debug for TcpListener {
652 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
653 fmt::Debug::fmt(&self.sys, f)
654 }
655}
656
657/*
658 *
659 * ===== UNIX ext =====
660 *
661 */
662
663use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
664
665impl IntoRawFd for TcpStream {
666 fn into_raw_fd(self) -> RawFd {
667 self.sys.into_raw_fd()
668 }
669}
670
671impl AsRawFd for TcpStream {
672 fn as_raw_fd(&self) -> RawFd {
673 self.sys.as_raw_fd()
674 }
675}
676
677impl FromRawFd for TcpStream {
678 unsafe fn from_raw_fd(fd: RawFd) -> TcpStream {
679 TcpStream {
680 sys: FromRawFd::from_raw_fd(fd),
681 selector_id: SelectorId::new(),
682 }
683 }
684}
685
686impl IntoRawFd for TcpListener {
687 fn into_raw_fd(self) -> RawFd {
688 self.sys.into_raw_fd()
689 }
690}
691
692impl AsRawFd for TcpListener {
693 fn as_raw_fd(&self) -> RawFd {
694 self.sys.as_raw_fd()
695 }
696}
697
698impl FromRawFd for TcpListener {
699 unsafe fn from_raw_fd(fd: RawFd) -> TcpListener {
700 TcpListener {
701 sys: FromRawFd::from_raw_fd(fd),
702 selector_id: SelectorId::new(),
703 }
704 }
705}