-
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 <vincenzopalazzodev@gmail.com>
- Loading branch information
1 parent
56b385f
commit 1c04dbe
Showing
4 changed files
with
85 additions
and
18 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,70 @@ | ||
//! async io module of the plugin io. | ||
//! | ||
//! Vincenzo Palazzo <vincenzopalazzo@member.fsf.org> | ||
use std::io; | ||
use std::io::{Read, Write}; | ||
use std::os::fd::AsRawFd; | ||
|
||
const SERVER: 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); | ||
let stdout = std::io::stdout().as_raw_fd(); | ||
let mut stdout = mio::unix::SourceFd(&stdout); | ||
|
||
self.poll | ||
.registry() | ||
.register(&mut stdin, SERVER, mio::Interest::READABLE)?; | ||
self.poll | ||
.registry() | ||
.register(&mut stdout, SERVER, mio::Interest::WRITABLE)?; | ||
Ok(()) | ||
} | ||
|
||
pub fn into_loop<F: FnMut(String) -> String>(&mut self, mut async_callback: F) { | ||
Check warning on line 37 in plugin/src/io.rs GitHub Actions / clippymethods called `into_*` usually take `self` by value
|
||
let mut events = mio::Events::with_capacity(1024); | ||
loop { | ||
self.poll.poll(&mut events, None).unwrap(); | ||
|
||
for event in events.iter() { | ||
match event.token() { | ||
SERVER => { | ||
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") { | ||
break; // Exit the loop | ||
} | ||
} | ||
let resp = async_callback(buffer.clone()); | ||
io::stdout().write_all(resp.as_bytes()).unwrap(); | ||
io::stdout().flush().unwrap(); | ||
} | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
} | ||
} | ||
} |
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