-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): add async io for the plugin
Adding the support of the async io for reading to the std io. Link: #98 Signed-off-by: Vincenzo Palazzo <[email protected]>
- Loading branch information
1 parent
020cc5f
commit 90f17b3
Showing
4 changed files
with
151 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
//! async io module of the plugin io. | ||
//! | ||
//! Vincenzo Palazzo <[email protected]> | ||
use std::{ | ||
fs::File, | ||
io::{self, Read}, | ||
os::{ | ||
fd::{AsRawFd, FromRawFd}, | ||
unix::prelude::FileExt, | ||
}, | ||
sync::Arc, | ||
}; | ||
|
||
const STDOUT: mio::Token = mio::Token(0); | ||
const STDIN: mio::Token = mio::Token(1); | ||
|
||
pub(crate) struct AsyncIO { | ||
poll: mio::Poll, | ||
write_ready: bool, | ||
read_ready: bool, | ||
stdin: Option<Arc<File>>, | ||
stdout: Option<Arc<File>>, | ||
} | ||
|
||
impl AsyncIO { | ||
/// Create a new instance of an AsyncIO | ||
pub fn new() -> io::Result<Self> { | ||
Ok(Self { | ||
poll: mio::Poll::new()?, | ||
write_ready: false, | ||
read_ready: false, | ||
stdin: None, | ||
stdout: None, | ||
}) | ||
} | ||
|
||
pub fn register(&mut self) -> io::Result<()> { | ||
let stdin = std::io::stdin().as_raw_fd(); | ||
let mut stdin = mio::unix::SourceFd(&stdin); | ||
let stdin_file = unsafe { File::from_raw_fd(*stdin.0) }; | ||
|
||
let stdout = std::io::stdout().as_raw_fd(); | ||
let mut stdout = mio::unix::SourceFd(&stdout); | ||
let stdout_file = unsafe { File::from_raw_fd(*stdout.0) }; | ||
|
||
self.poll | ||
.registry() | ||
.register(&mut stdin, STDIN, mio::Interest::READABLE)?; | ||
self.poll | ||
.registry() | ||
.register(&mut stdout, STDOUT, mio::Interest::WRITABLE)?; | ||
self.stdout = Some(Arc::new(stdout_file)); | ||
self.stdin = Some(Arc::new(stdin_file)); | ||
Ok(()) | ||
} | ||
|
||
fn stdin(&self) -> Arc<File> { | ||
self.stdin.clone().unwrap() | ||
} | ||
|
||
fn stdout(&self) -> Arc<File> { | ||
self.stdout.clone().unwrap() | ||
} | ||
|
||
pub fn into_loop<F: FnMut(String) -> String>(&mut self, mut async_callback: F) { | ||
Check warning on line 65 in plugin/src/io.rs GitHub Actions / clippymethods called `into_*` usually take `self` by value
|
||
let mut events = mio::Events::with_capacity(1024); | ||
let mut buf = vec![]; | ||
let mut cursor = 0; | ||
'outer: loop { | ||
let ret = self.poll.poll(&mut events, None); | ||
if let Err(x) = ret { | ||
if x.kind() == io::ErrorKind::Interrupted { | ||
continue; | ||
} | ||
break; | ||
} | ||
|
||
for event in events.iter() { | ||
match event.token() { | ||
STDIN => { | ||
self.read_ready = true; | ||
} | ||
STDOUT => { | ||
self.write_ready = true; | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
loop { | ||
if self.read_ready { | ||
#[allow(unused_assignments)] | ||
match self.stdin().read_to_end(&mut buf) { | ||
Ok(0) => { | ||
buf = async_callback(String::from_utf8(buf).unwrap()) | ||
.as_bytes() | ||
.to_vec(); | ||
break 'outer; | ||
} | ||
Ok(x) => { | ||
cursor += x; | ||
} | ||
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => { | ||
continue; | ||
} | ||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { | ||
self.read_ready = false; | ||
} | ||
Err(_) => { | ||
break 'outer; | ||
} | ||
} | ||
} else if self.write_ready && cursor > 0 { | ||
match self.stdout().write_at(&buf, cursor as u64) { | ||
Ok(x) => { | ||
unsafe { | ||
buf.set_len(cursor); | ||
} | ||
let _ = buf.drain(0..x); | ||
cursor -= x; | ||
} | ||
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => { | ||
continue; | ||
} | ||
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { | ||
self.write_ready = false; | ||
} | ||
Err(_) => { | ||
break 'outer; | ||
} | ||
} | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters