Skip to content

Commit 86fa775

Browse files
committed
async: implement FromRawFd and AsRawFd
Provide a way for users to manage fd by themselves, especially for hot upgrade functions. Signed-off-by: Tim Zhang <[email protected]>
1 parent 0b5abea commit 86fa775

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/asynchronous/server.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ impl Server {
7373
Ok(self)
7474
}
7575

76+
pub fn set_domain_unix(mut self) -> Self {
77+
self.domain = Some(Domain::Unix);
78+
self
79+
}
80+
81+
pub fn set_domain_vsock(mut self) -> Self {
82+
self.domain = Some(Domain::Vsock);
83+
self
84+
}
85+
7686
pub fn add_listener(mut self, fd: RawFd) -> Result<Server> {
7787
self.listeners.push(fd);
7888

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

103-
match self.domain.as_ref().unwrap() {
104-
Domain::Unix => {
113+
match self.domain.as_ref() {
114+
Some(Domain::Unix) => {
105115
let sys_unix_listener;
106116
unsafe {
107117
sys_unix_listener = SysUnixListener::from_raw_fd(listenfd);
@@ -110,14 +120,15 @@ impl Server {
110120

111121
self.do_start(listenfd, unix_listener).await
112122
}
113-
Domain::Vsock => {
123+
Some(Domain::Vsock) => {
114124
let incoming;
115125
unsafe {
116126
incoming = VsockListener::from_raw_fd(listenfd).incoming();
117127
}
118128

119129
self.do_start(listenfd, incoming).await
120130
}
131+
_ => Err(Error::Others("Domain is not set".to_string())),
121132
}
122133
}
123134

@@ -312,3 +323,15 @@ async fn handle_request(
312323
}
313324
}
314325
}
326+
327+
impl FromRawFd for Server {
328+
unsafe fn from_raw_fd(fd: RawFd) -> Self {
329+
Self::default().add_listener(fd).unwrap()
330+
}
331+
}
332+
333+
impl AsRawFd for Server {
334+
fn as_raw_fd(&self) -> RawFd {
335+
self.listeners[0]
336+
}
337+
}

0 commit comments

Comments
 (0)