-
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
56b385f
commit 0fa3096
Showing
4 changed files
with
106 additions
and
26 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,77 @@ | ||
//! async io module of the plugin io. | ||
//! | ||
//! Vincenzo Palazzo <[email protected]> | ||
use std::io; | ||
use std::io::{Read, Write}; | ||
use std::os::fd::AsRawFd; | ||
|
||
const IN: mio::Token = mio::Token(0); | ||
|
||
pub(crate) struct AsyncIO { | ||
poll: mio::Poll, | ||
} | ||
|
||
impl AsyncIO { | ||
/// Create a new instance of an AsyncIO | ||
pub fn new() -> io::Result<Self> { | ||
Ok(Self { | ||
poll: mio::Poll::new()?, | ||
}) | ||
} | ||
|
||
pub fn register(&mut self) -> io::Result<()> { | ||
let stdin = std::io::stdin().as_raw_fd(); | ||
let mut stdin = mio::unix::SourceFd(&stdin); | ||
|
||
self.poll.registry().register( | ||
&mut stdin, | ||
IN, | ||
mio::Interest::READABLE | mio::Interest::WRITABLE, | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn into_loop<F: FnMut(String) -> Option<String>>( | ||
&mut self, | ||
mut async_callback: F, | ||
) -> io::Result<()> { | ||
let mut events = mio::Events::with_capacity(1024); | ||
loop { | ||
self.poll.poll(&mut events, None)?; | ||
for event in events.iter() { | ||
#[cfg(feature = "log")] | ||
log::info!("getting the event: {:?}", event); | ||
match event.token() { | ||
IN => { | ||
if event.is_readable() { | ||
let mut reader = io::stdin().lock(); | ||
let mut buffer = String::new(); | ||
loop { | ||
let mut byte = [0; 1]; | ||
reader.read_exact(&mut byte).unwrap(); | ||
|
||
// Append the byte to the buffer | ||
buffer.push(byte[0] as char); | ||
|
||
// Check if the buffer ends with double newline | ||
if buffer.ends_with("\n\n") { | ||
drop(reader); | ||
break; // Exit the loop | ||
} | ||
} | ||
let Some(resp) = async_callback(buffer.clone()) else { | ||
continue; | ||
}; | ||
let mut writer = io::stdout().lock(); | ||
writer.write_all(resp.as_bytes())?; | ||
writer.flush()?; | ||
} | ||
} | ||
_ => unreachable!(), | ||
} | ||
#[cfg(feature = "log")] | ||
log::info!("event handled: {:?}", event); | ||
} | ||
} | ||
} | ||
} |
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