Avoid duplicate wait_events methods

This commit is contained in:
Ruihan Li
2024-06-30 22:38:28 +08:00
committed by Tate, Hongliang Tian
parent 6e8896165d
commit ab9941263b
11 changed files with 126 additions and 134 deletions

View File

@ -14,7 +14,7 @@ use crate::{
MessageHeader, SendRecvFlags, SockShutdownCmd, Socket, SocketAddr,
},
prelude::*,
process::signal::Poller,
process::signal::{Pollable, Poller},
util::IoVec,
};
@ -53,39 +53,6 @@ impl VsockStreamSocket {
self.is_nonblocking.store(nonblocking, Ordering::Relaxed);
}
// TODO: Support timeout
fn wait_events<F, R>(&self, mask: IoEvents, mut cond: F) -> Result<R>
where
F: FnMut() -> Result<R>,
{
let poller = Poller::new();
loop {
match cond() {
Err(err) if err.error() == Errno::EAGAIN => (),
result => {
if let Err(e) = result {
debug!("The result of cond() is Error: {:?}", e);
}
return result;
}
};
let events = match &*self.status.read() {
Status::Init(init) => init.poll(mask, Some(&poller)),
Status::Listen(listen) => listen.poll(mask, Some(&poller)),
Status::Connected(connected) => connected.poll(mask, Some(&poller)),
};
debug!("events: {:?}", events);
if !events.is_empty() {
continue;
}
poller.wait()?;
}
}
fn try_accept(&self) -> Result<(Arc<dyn FileLike>, SocketAddr)> {
let listen = match &*self.status.read() {
Status::Listen(listen) => listen.clone(),
@ -155,6 +122,16 @@ impl VsockStreamSocket {
}
}
impl Pollable for VsockStreamSocket {
fn poll(&self, mask: IoEvents, poller: Option<&Poller>) -> IoEvents {
match &*self.status.read() {
Status::Init(init) => init.poll(mask, poller),
Status::Listen(listen) => listen.poll(mask, poller),
Status::Connected(connected) => connected.poll(mask, poller),
}
}
}
impl FileLike for VsockStreamSocket {
fn as_socket(self: Arc<Self>) -> Option<Arc<dyn Socket>> {
Some(self)
@ -170,14 +147,6 @@ impl FileLike for VsockStreamSocket {
self.send(buf, SendRecvFlags::empty())
}
fn poll(&self, mask: IoEvents, poller: Option<&Poller>) -> IoEvents {
match &*self.status.read() {
Status::Init(init) => init.poll(mask, poller),
Status::Listen(listen) => listen.poll(mask, poller),
Status::Connected(connected) => connected.poll(mask, poller),
}
}
fn status_flags(&self) -> StatusFlags {
if self.is_nonblocking() {
StatusFlags::O_NONBLOCK