11use crate :: net:: { TcpListener , TcpStream } ;
22
3+ use std:: convert:: TryInto ;
34use std:: fmt;
45use std:: io;
56use std:: net:: SocketAddr ;
@@ -84,7 +85,7 @@ cfg_net! {
8485 /// [`socket2`]: https://docs.rs/socket2/
8586 #[ cfg_attr( docsrs, doc( alias = "connect_std" ) ) ]
8687 pub struct TcpSocket {
87- inner: mio :: net :: TcpSocket ,
88+ inner: socket2 :: Socket ,
8889 }
8990}
9091
@@ -119,7 +120,11 @@ impl TcpSocket {
119120 /// }
120121 /// ```
121122 pub fn new_v4 ( ) -> io:: Result < TcpSocket > {
122- let inner = mio:: net:: TcpSocket :: new_v4 ( ) ?;
123+ let inner = socket2:: Socket :: new (
124+ socket2:: Domain :: IPV4 ,
125+ socket2:: Type :: STREAM ,
126+ Some ( socket2:: Protocol :: TCP ) ,
127+ ) ?;
123128 Ok ( TcpSocket { inner } )
124129 }
125130
@@ -153,7 +158,11 @@ impl TcpSocket {
153158 /// }
154159 /// ```
155160 pub fn new_v6 ( ) -> io:: Result < TcpSocket > {
156- let inner = mio:: net:: TcpSocket :: new_v6 ( ) ?;
161+ let inner = socket2:: Socket :: new (
162+ socket2:: Domain :: IPV6 ,
163+ socket2:: Type :: STREAM ,
164+ Some ( socket2:: Protocol :: TCP ) ,
165+ ) ?;
157166 Ok ( TcpSocket { inner } )
158167 }
159168
@@ -184,7 +193,7 @@ impl TcpSocket {
184193 /// }
185194 /// ```
186195 pub fn set_reuseaddr ( & self , reuseaddr : bool ) -> io:: Result < ( ) > {
187- self . inner . set_reuseaddr ( reuseaddr)
196+ self . inner . set_reuse_address ( reuseaddr)
188197 }
189198
190199 /// Retrieves the value set for `SO_REUSEADDR` on this socket.
@@ -210,7 +219,7 @@ impl TcpSocket {
210219 /// }
211220 /// ```
212221 pub fn reuseaddr ( & self ) -> io:: Result < bool > {
213- self . inner . get_reuseaddr ( )
222+ self . inner . reuse_address ( )
214223 }
215224
216225 /// Allows the socket to bind to an in-use port. Only available for unix systems
@@ -244,7 +253,7 @@ impl TcpSocket {
244253 doc( cfg( all( unix, not( target_os = "solaris" ) , not( target_os = "illumos" ) ) ) )
245254 ) ]
246255 pub fn set_reuseport ( & self , reuseport : bool ) -> io:: Result < ( ) > {
247- self . inner . set_reuseport ( reuseport)
256+ self . inner . set_reuse_port ( reuseport)
248257 }
249258
250259 /// Allows the socket to bind to an in-use port. Only available for unix systems
@@ -279,14 +288,14 @@ impl TcpSocket {
279288 doc( cfg( all( unix, not( target_os = "solaris" ) , not( target_os = "illumos" ) ) ) )
280289 ) ]
281290 pub fn reuseport ( & self ) -> io:: Result < bool > {
282- self . inner . get_reuseport ( )
291+ self . inner . reuse_port ( )
283292 }
284293
285294 /// Sets the size of the TCP send buffer on this socket.
286295 ///
287296 /// On most operating systems, this sets the `SO_SNDBUF` socket option.
288297 pub fn set_send_buffer_size ( & self , size : u32 ) -> io:: Result < ( ) > {
289- self . inner . set_send_buffer_size ( size)
298+ self . inner . set_send_buffer_size ( size as usize )
290299 }
291300
292301 /// Returns the size of the TCP send buffer for this socket.
@@ -313,14 +322,14 @@ impl TcpSocket {
313322 ///
314323 /// [`set_send_buffer_size`]: #method.set_send_buffer_size
315324 pub fn send_buffer_size ( & self ) -> io:: Result < u32 > {
316- self . inner . get_send_buffer_size ( )
325+ self . inner . send_buffer_size ( ) . map ( |n| n as u32 )
317326 }
318327
319328 /// Sets the size of the TCP receive buffer on this socket.
320329 ///
321330 /// On most operating systems, this sets the `SO_RCVBUF` socket option.
322331 pub fn set_recv_buffer_size ( & self , size : u32 ) -> io:: Result < ( ) > {
323- self . inner . set_recv_buffer_size ( size)
332+ self . inner . set_recv_buffer_size ( size as usize )
324333 }
325334
326335 /// Returns the size of the TCP receive buffer for this socket.
@@ -347,7 +356,7 @@ impl TcpSocket {
347356 ///
348357 /// [`set_recv_buffer_size`]: #method.set_recv_buffer_size
349358 pub fn recv_buffer_size ( & self ) -> io:: Result < u32 > {
350- self . inner . get_recv_buffer_size ( )
359+ self . inner . recv_buffer_size ( ) . map ( |n| n as u32 )
351360 }
352361
353362 /// Sets the linger duration of this socket by setting the SO_LINGER option.
@@ -395,7 +404,9 @@ impl TcpSocket {
395404 /// }
396405 /// ```
397406 pub fn local_addr ( & self ) -> io:: Result < SocketAddr > {
398- self . inner . get_localaddr ( )
407+ self . inner
408+ . local_addr ( )
409+ . map ( |addr| addr. as_socket ( ) . unwrap ( ) )
399410 }
400411
401412 /// Binds the socket to the given address.
@@ -427,7 +438,7 @@ impl TcpSocket {
427438 /// }
428439 /// ```
429440 pub fn bind ( & self , addr : SocketAddr ) -> io:: Result < ( ) > {
430- self . inner . bind ( addr)
441+ self . inner . bind ( & addr. into ( ) )
431442 }
432443
433444 /// Establishes a TCP connection with a peer at the specified socket address.
@@ -463,7 +474,13 @@ impl TcpSocket {
463474 /// }
464475 /// ```
465476 pub async fn connect ( self , addr : SocketAddr ) -> io:: Result < TcpStream > {
466- let mio = self . inner . connect ( addr) ?;
477+ self . inner . connect ( & addr. into ( ) ) ?;
478+
479+ #[ cfg( windows) ]
480+ let mio = unsafe { mio:: net:: TcpStream :: from_raw_socket ( self . inner . into_raw_socket ( ) ) } ;
481+ #[ cfg( unix) ]
482+ let mio = unsafe { mio:: net:: TcpStream :: from_raw_fd ( self . inner . into_raw_fd ( ) ) } ;
483+
467484 TcpStream :: connect_mio ( mio) . await
468485 }
469486
@@ -503,7 +520,14 @@ impl TcpSocket {
503520 /// }
504521 /// ```
505522 pub fn listen ( self , backlog : u32 ) -> io:: Result < TcpListener > {
506- let mio = self . inner . listen ( backlog) ?;
523+ let backlog = backlog. try_into ( ) . unwrap_or ( i32:: MAX ) ;
524+ self . inner . listen ( backlog) ?;
525+
526+ #[ cfg( windows) ]
527+ let mio = unsafe { mio:: net:: TcpListener :: from_raw_socket ( self . inner . into_raw_socket ( ) ) } ;
528+ #[ cfg( unix) ]
529+ let mio = unsafe { mio:: net:: TcpListener :: from_raw_fd ( self . inner . into_raw_fd ( ) ) } ;
530+
507531 TcpListener :: new ( mio)
508532 }
509533
@@ -523,7 +547,7 @@ impl TcpSocket {
523547 ///
524548 /// #[tokio::main]
525549 /// async fn main() -> std::io::Result<()> {
526- ///
550+ ///
527551 /// let socket2_socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
528552 ///
529553 /// let socket = TcpSocket::from_std_stream(socket2_socket.into());
@@ -534,16 +558,12 @@ impl TcpSocket {
534558 pub fn from_std_stream ( std_stream : std:: net:: TcpStream ) -> TcpSocket {
535559 #[ cfg( unix) ]
536560 {
537- use std:: os:: unix:: io:: { FromRawFd , IntoRawFd } ;
538-
539561 let raw_fd = std_stream. into_raw_fd ( ) ;
540562 unsafe { TcpSocket :: from_raw_fd ( raw_fd) }
541563 }
542564
543565 #[ cfg( windows) ]
544566 {
545- use std:: os:: windows:: io:: { FromRawSocket , IntoRawSocket } ;
546-
547567 let raw_socket = std_stream. into_raw_socket ( ) ;
548568 unsafe { TcpSocket :: from_raw_socket ( raw_socket) }
549569 }
@@ -572,7 +592,7 @@ impl FromRawFd for TcpSocket {
572592 /// The caller is responsible for ensuring that the socket is in
573593 /// non-blocking mode.
574594 unsafe fn from_raw_fd ( fd : RawFd ) -> TcpSocket {
575- let inner = mio :: net :: TcpSocket :: from_raw_fd ( fd) ;
595+ let inner = socket2 :: Socket :: from_raw_fd ( fd) ;
576596 TcpSocket { inner }
577597 }
578598}
@@ -607,7 +627,7 @@ impl FromRawSocket for TcpSocket {
607627 /// The caller is responsible for ensuring that the socket is in
608628 /// non-blocking mode.
609629 unsafe fn from_raw_socket ( socket : RawSocket ) -> TcpSocket {
610- let inner = mio :: net :: TcpSocket :: from_raw_socket ( socket) ;
630+ let inner = socket2 :: Socket :: from_raw_socket ( socket) ;
611631 TcpSocket { inner }
612632 }
613633}
0 commit comments