@@ -45,10 +45,20 @@ static ALIVE: AtomicBool = AtomicBool::new(true);
4545static CLIENTS : AtomicU16 = AtomicU16 :: new ( 0 ) ;
4646
4747fn tcp_stream_try_clone ( stream : & TcpStream ) -> BoxResult < TcpStream > {
48- use std:: os:: fd:: { AsRawFd , BorrowedFd } ;
49- let fd = unsafe { BorrowedFd :: borrow_raw ( stream. as_raw_fd ( ) ) } ;
50- let fd = fd. try_clone_to_owned ( ) ?;
51- let socket: socket2:: Socket = socket2:: Socket :: from ( fd) ;
48+ cfg_if:: cfg_if! {
49+ if #[ cfg( unix) ] {
50+ use std:: os:: fd:: { AsRawFd , BorrowedFd } ;
51+ let fd = unsafe { BorrowedFd :: borrow_raw( stream. as_raw_fd( ) ) } ;
52+ let fd = fd. try_clone_to_owned( ) ?;
53+ let socket: socket2:: Socket = socket2:: Socket :: from( fd) ;
54+ } else {
55+ use std:: os:: windows:: io:: { AsRawSocket , BorrowedSocket } ;
56+ let socket = unsafe { BorrowedSocket :: borrow_raw( stream. as_raw_socket( ) ) } ;
57+ let socket = socket. try_clone_to_owned( ) ?;
58+ let socket: socket2:: Socket = socket2:: Socket :: from( socket) ;
59+ }
60+ }
61+
5262 let stream: std:: net:: TcpStream = socket. into ( ) ;
5363 let socket = TcpStream :: from_std ( stream) ;
5464 Ok ( socket)
@@ -359,14 +369,26 @@ pub fn serve(args: &Args) -> BoxResult<()> {
359369 log:: info!( "connection from {}" , address) ;
360370
361371 let mut stream = {
362- use std:: os:: fd:: { FromRawFd , IntoRawFd } ;
363- let fd = stream. into_raw_fd ( ) ;
364- let socket: socket2:: Socket = unsafe { socket2:: Socket :: from_raw_fd ( fd) } ;
365-
366- let keepalive = socket2:: TcpKeepalive :: new ( )
367- . with_time ( KEEPALIVE_DURATION )
368- . with_interval ( KEEPALIVE_DURATION )
369- . with_retries ( 4 ) ;
372+ cfg_if:: cfg_if! {
373+ if #[ cfg( unix) ] {
374+ use std:: os:: fd:: { FromRawFd , IntoRawFd } ;
375+ let fd = stream. into_raw_fd( ) ;
376+ let socket: socket2:: Socket = unsafe { socket2:: Socket :: from_raw_fd( fd) } ;
377+
378+ let keepalive = socket2:: TcpKeepalive :: new( )
379+ . with_time( KEEPALIVE_DURATION )
380+ . with_interval( KEEPALIVE_DURATION )
381+ . with_retries( 4 ) ;
382+ } else {
383+ // Only Windows supports raw sockets
384+ use std:: os:: windows:: io:: { FromRawSocket , IntoRawSocket } ;
385+ let socket = stream. into_raw_socket( ) ;
386+ let socket: socket2:: Socket = unsafe { socket2:: Socket :: from_raw_socket( socket) } ;
387+ let keepalive = socket2:: TcpKeepalive :: new( )
388+ . with_time( KEEPALIVE_DURATION )
389+ . with_interval( KEEPALIVE_DURATION ) ;
390+ }
391+ }
370392 socket. set_tcp_keepalive ( & keepalive) ?;
371393
372394 socket. set_nodelay ( true ) ?;
0 commit comments