Skip to content

Commit e71de77

Browse files
committed
Impl FromRawFd and AsRawFd
Provide a way for users to manage fd by themselves. Signed-off-by: Tim Zhang <[email protected]>
1 parent ec2a919 commit e71de77

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/sync/server.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use nix::unistd::close;
2121
use nix::unistd::pipe2;
2222
use protobuf::{CodedInputStream, Message};
2323
use std::collections::HashMap;
24-
use std::os::unix::io::RawFd;
24+
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
2525
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
2626
use std::sync::mpsc::{channel, sync_channel, Receiver, Sender, SyncSender};
2727
use std::sync::{Arc, Mutex};
@@ -279,8 +279,9 @@ impl Server {
279279
}
280280

281281
let (fd, _) = common::do_bind(host)?;
282-
self.listeners.push(fd);
282+
common::do_listen(fd)?;
283283

284+
self.listeners.push(fd);
284285
Ok(self)
285286
}
286287

@@ -341,8 +342,6 @@ impl Server {
341342
let service_quit = self.quit.clone();
342343
let monitor_fd = self.monitor_fd.0;
343344

344-
common::do_listen(listener)?;
345-
346345
let handler = thread::Builder::new()
347346
.name("listener_loop".into())
348347
.spawn(move || {
@@ -508,3 +507,15 @@ impl Server {
508507
}
509508
}
510509
}
510+
511+
impl FromRawFd for Server {
512+
unsafe fn from_raw_fd(fd: RawFd) -> Self {
513+
Self::default().add_listener(fd).unwrap()
514+
}
515+
}
516+
517+
impl AsRawFd for Server {
518+
fn as_raw_fd(&self) -> RawFd {
519+
self.listeners[0]
520+
}
521+
}

0 commit comments

Comments
 (0)