1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
| | // Copyright (C) 2019 Alibaba Cloud Computing. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//! Traits and Structs for vhost-user slave.
use std::sync::{Arc, Mutex};
use super::connection::{Endpoint, Listener};
use super::message::*;
use super::{Result, SlaveReqHandler, VhostUserSlaveReqHandler};
/// Vhost-user slave side connection listener.
pub struct SlaveListener<S: VhostUserSlaveReqHandler> {
listener: Listener,
backend: Option<Arc<Mutex<S>>>,
}
/// Sets up a listener for incoming master connections, and handles construction
/// of a Slave on success.
impl<S: VhostUserSlaveReqHandler> SlaveListener<S> {
/// Create a unix domain socket for incoming master connections.
///
/// Be careful, the file at `path` will be unlinked if unlink is true
pub fn new(path: &str, unlink: bool, backend: Arc<Mutex<S>>) -> Result<Self> {
Ok(SlaveListener {
listener: Listener::new(path, unlink)?,
backend: Some(backend),
})
}
/// Accept an incoming connection from the master, returning Some(Slave) on
/// success, or None if the socket is nonblocking and no incoming connection
/// was detected
pub fn accept(&mut self) -> Result<Option<SlaveReqHandler<S>>> {
if let Some(fd) = self.listener.accept()? {
return Ok(Some(SlaveReqHandler::new(
Endpoint::<MasterReq>::from_stream(fd),
self.backend.take().unwrap(),
)));
}
Ok(None)
}
/// Change blocking status on the listener.
pub fn set_nonblocking(&self, block: bool) -> Result<()> {
self.listener.set_nonblocking(block)
}
}
|