@@ -108,46 +108,11 @@ impl AddrIncoming {
108108 match ready ! ( self . listener. poll_accept( cx) ) {
109109 Ok ( ( socket, addr) ) => {
110110 if let Some ( dur) = self . tcp_keepalive_timeout {
111- // Convert the Tokio `TcpStream` into a `socket2` socket
112- // so we can call `set_keepalive`.
113- // TODO(eliza): if Tokio's `TcpSocket` API grows a few
114- // more methods in the future, hopefully we shouldn't
115- // have to do the `from_raw_fd` dance any longer...
116- #[ cfg( unix) ]
117- let socket = unsafe {
118- // Safety: `socket2`'s socket will try to close the
119- // underlying fd when it's dropped. However, we
120- // can't take ownership of the fd from the tokio
121- // TcpStream, so instead we will call `into_raw_fd`
122- // on the socket2 socket before dropping it. This
123- // prevents it from trying to close the fd.
124- use std:: os:: unix:: io:: { AsRawFd , FromRawFd } ;
125- socket2:: Socket :: from_raw_fd ( socket. as_raw_fd ( ) )
126- } ;
127- #[ cfg( windows) ]
128- let socket = unsafe {
129- // Safety: `socket2`'s socket will try to close the
130- // underlying SOCKET when it's dropped. However, we
131- // can't take ownership of the SOCKET from the tokio
132- // TcpStream, so instead we will call `into_raw_socket`
133- // on the socket2 socket before dropping it. This
134- // prevents it from trying to close the SOCKET.
135- use std:: os:: windows:: io:: { AsRawSocket , FromRawSocket } ;
136- socket2:: Socket :: from_raw_socket ( socket. as_raw_socket ( ) )
137- } ;
138-
139- // Actually set the TCP keepalive timeout.
140- if let Err ( e) = socket. set_keepalive ( Some ( dur) ) {
111+ let socket = socket2:: SockRef :: from ( & socket) ;
112+ let conf = socket2:: TcpKeepalive :: new ( ) . with_time ( dur) ;
113+ if let Err ( e) = socket. set_tcp_keepalive ( & conf) {
141114 trace ! ( "error trying to set TCP keepalive: {}" , e) ;
142115 }
143-
144- // Take ownershop of the fd/socket back from the socket2
145- // `Socket`, so that socket2 doesn't try to close it
146- // when it's dropped.
147- #[ cfg( unix) ]
148- drop ( std:: os:: unix:: io:: IntoRawFd :: into_raw_fd ( socket) ) ;
149- #[ cfg( windows) ]
150- drop ( std:: os:: windows:: io:: IntoRawSocket :: into_raw_socket ( socket) ) ;
151116 }
152117 if let Err ( e) = socket. set_nodelay ( self . tcp_nodelay ) {
153118 trace ! ( "error trying to set TCP nodelay: {}" , e) ;
0 commit comments