poll loop

This commit is contained in:
ssrlive 2023-09-27 23:29:08 +08:00
parent 7216793088
commit 91b2737989

View file

@ -112,7 +112,7 @@ impl WinTunInterface {
match result { match result {
Ok(_) => {} Ok(_) => {}
Err(err) if err.kind() == io::ErrorKind::WouldBlock => { Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
log::trace!("reader_thread phy: tx failed due to WouldBlock") log::trace!("Wintun reader_thread: tx failed due to WouldBlock")
} }
Err(err) => log::error!("{}", err), Err(err) => log::error!("{}", err),
} }
@ -139,25 +139,27 @@ impl WinTunInterface {
} }
pub fn pipe_client_event(&self) -> Result<(), io::Error> { pub fn pipe_client_event(&self) -> Result<(), io::Error> {
let mut buffer = vec![0; self.mtu]; let mut reader = self
match self
.pipe_client .pipe_client
.lock() .lock()
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))? .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
.read(&mut buffer) let mut buffer = vec![0; self.mtu];
{ loop {
Ok(len) => { match reader.read(&mut buffer[..]) {
let write_pack = self.wintun_session.allocate_send_packet(len as u16); Ok(len) => match self.wintun_session.allocate_send_packet(len as u16) {
if let Ok(mut write_pack) = write_pack { Ok(mut write_pack) => {
write_pack.bytes_mut().copy_from_slice(&buffer[..len]); write_pack.bytes_mut().copy_from_slice(&buffer[..len]);
self.wintun_session.send_packet(write_pack); self.wintun_session.send_packet(write_pack);
} else if let Err(err) = write_pack {
log::error!("phy: failed to allocate send packet: {}", err);
} }
Err(err) => {
log::error!("Wintun: failed to allocate send packet: {}", err);
} }
Err(err) if err.kind() == io::ErrorKind::WouldBlock => {} },
Err(err) if err.kind() == io::ErrorKind::WouldBlock => break,
Err(err) if err.kind() == io::ErrorKind::Interrupted => continue,
Err(err) => return Err(err), Err(err) => return Err(err),
} }
}
Ok(()) Ok(())
} }
@ -307,7 +309,7 @@ impl phy::TxToken for TxToken {
match self.pipe_server.borrow_mut().write(&buffer[..]) { match self.pipe_server.borrow_mut().write(&buffer[..]) {
Ok(_) => {} Ok(_) => {}
Err(err) if err.kind() == io::ErrorKind::WouldBlock => { Err(err) if err.kind() == io::ErrorKind::WouldBlock => {
log::trace!("phy: tx failed due to WouldBlock") log::trace!("Wintun TxToken: tx failed due to WouldBlock")
} }
Err(err) => log::error!("{}", err), Err(err) => log::error!("{}", err),
} }