Skip to content

Commit bdc9b10

Browse files
committed
tcp_stream_try_clone function
1 parent 94568ba commit bdc9b10

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/server.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ static ALIVE: AtomicBool = AtomicBool::new(true);
4545
/// a count of connected clients
4646
static CLIENTS: AtomicU16 = AtomicU16::new(0);
4747

48+
fn tcp_stream_try_clone(stream: &TcpStream) -> BoxResult<TcpStream> {
49+
use std::os::fd::{AsRawFd, BorrowedFd};
50+
let fd = unsafe { BorrowedFd::borrow_raw(stream.as_raw_fd()) };
51+
let fd = fd.try_clone_to_owned()?;
52+
let socket: socket2::Socket = socket2::Socket::from(fd);
53+
let stream: std::net::TcpStream = socket.into();
54+
let socket = TcpStream::from_std(stream);
55+
Ok(socket)
56+
}
57+
4858
fn handle_client(
4959
stream: &mut TcpStream,
5060
cpu_affinity_manager: Arc<Mutex<crate::utils::cpu_affinity::CpuAffinityManager>>,
@@ -64,7 +74,7 @@ fn handle_client(
6474
) = channel();
6575

6676
//a closure used to pass results from stream-handlers to the client-communication stream
67-
let mut forwarding_send_stream = stream.try_clone()?;
77+
let mut forwarding_send_stream = tcp_stream_try_clone(stream)?;
6878
let mut results_handler = || -> BoxResult<()> {
6979
// drain all results every time this closer is invoked
7080
while let Ok(result) = results_rx.try_recv() {

0 commit comments

Comments
 (0)