Skip to content

Commit

Permalink
async: implement FromRawFd and AsRawFd
Browse files Browse the repository at this point in the history
Provide a way for users to manage fd by themselves,
especially for hot upgrade functions.

Signed-off-by: Tim Zhang <[email protected]>
  • Loading branch information
Tim-Zhang committed Sep 28, 2020
1 parent 0b5abea commit 86fa775
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/asynchronous/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ impl Server {
Ok(self)
}

pub fn set_domain_unix(mut self) -> Self {
self.domain = Some(Domain::Unix);
self
}

pub fn set_domain_vsock(mut self) -> Self {
self.domain = Some(Domain::Vsock);
self
}

pub fn add_listener(mut self, fd: RawFd) -> Result<Server> {
self.listeners.push(fd);

Expand Down Expand Up @@ -100,8 +110,8 @@ impl Server {
pub async fn start(&mut self) -> Result<()> {
let listenfd = self.get_listenfd()?;

match self.domain.as_ref().unwrap() {
Domain::Unix => {
match self.domain.as_ref() {
Some(Domain::Unix) => {
let sys_unix_listener;
unsafe {
sys_unix_listener = SysUnixListener::from_raw_fd(listenfd);
Expand All @@ -110,14 +120,15 @@ impl Server {

self.do_start(listenfd, unix_listener).await
}
Domain::Vsock => {
Some(Domain::Vsock) => {
let incoming;
unsafe {
incoming = VsockListener::from_raw_fd(listenfd).incoming();
}

self.do_start(listenfd, incoming).await
}
_ => Err(Error::Others("Domain is not set".to_string())),
}
}

Expand Down Expand Up @@ -312,3 +323,15 @@ async fn handle_request(
}
}
}

impl FromRawFd for Server {
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self::default().add_listener(fd).unwrap()
}
}

impl AsRawFd for Server {
fn as_raw_fd(&self) -> RawFd {
self.listeners[0]
}
}

0 comments on commit 86fa775

Please sign in to comment.