Repository navigation
Isolated Processes 16: Lay out default remote IO and wire format #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,22 @@ | ||
| use core::ffi::c_int; | ||
|
|
||
| #[cfg(feature = "unix_stream")] | ||
| pub use unix_stream::UnixStream; | ||
|
|
||
| #[cfg(feature = "unix_stream")] | ||
| mod unix_stream; | ||
|
|
||
| pub type Result<T> = core::result::Result<T, Error>; | ||
|
|
||
| pub trait Io: core::fmt::Debug { | ||
| fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>; | ||
| fn write_all(&mut self, buf: &[u8]) -> Result<()>; | ||
| } | ||
|
|
||
| #[derive(Debug, serde::Deserialize, serde::Serialize, thiserror::Error)] | ||
| pub enum Error { | ||
| #[error("an OS error occurred. Errno: {0}")] | ||
| OsError(c_int), | ||
| #[error("unexpected end-of-file reached")] | ||
| UnexpectedEof, | ||
| } |
This file contains hidden or 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,71 @@ | ||
| use super as io; | ||
| use crate::wrapper::{OwnedFd, errno}; | ||
| use core::ffi::c_int; | ||
| use io::{Error, Result}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct UnixStream(OwnedFd); | ||
|
|
||
| impl UnixStream { | ||
| pub fn pair() -> Result<(UnixStream, UnixStream)> { | ||
| let mut fds = [0; 2]; | ||
| let rv = unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, fds.as_mut_ptr()) }; | ||
| if rv == -1 { | ||
| return Err(Error::OsError(errno())); | ||
| } | ||
| let stream0 = unsafe { Self::from_raw_fd(fds[0]) }; | ||
| let stream1 = unsafe { Self::from_raw_fd(fds[1]) }; | ||
| Ok((stream0, stream1)) | ||
| } | ||
|
|
||
| /// # Safety | ||
| /// | ||
| /// 'fd' must be a valid UNIX stream socket | ||
| pub unsafe fn from_raw_fd(fd: c_int) -> Self { | ||
| unsafe { Self(OwnedFd::new(fd)) } | ||
| } | ||
|
|
||
| pub fn as_raw_fd(&self) -> c_int { | ||
| self.0.as_raw_fd() | ||
| } | ||
|
|
||
| fn read(&mut self, buf: &mut [u8]) -> Result<usize> { | ||
| let rv = unsafe { libc::read(self.0.as_raw_fd(), buf.as_mut_ptr().cast(), buf.len()) }; | ||
| if rv == -1 { | ||
| return Err(Error::OsError(errno())); | ||
| 8000 | } | |
| let bytes_read = usize::try_from(rv).unwrap(); | ||
| Ok(bytes_read) | ||
| } | ||
|
|
||
| fn write(&mut self, buf: &[u8]) -> Result<usize> { | ||
| let rv = unsafe { libc::write(self.0.as_raw_fd(), buf.as_ptr().cast(), buf.len()) }; | ||
| if rv == -1 { | ||
| return Err(Error::OsError(errno())); | ||
| } | ||
| let bytes_written = usize::try_from(rv).unwrap(); | ||
| Ok(bytes_written) | ||
| } | ||
| } | ||
|
|
||
| impl io::Io for UnixStream { | ||
| fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<()> { | ||
| while !buf.is_empty() { | ||
| let bytes_read = self.read(buf)?; | ||
| if bytes_read == 0 { | ||
| return Err(Error::UnexpectedEof); | ||
| } | ||
| buf = &mut buf[bytes_read..]; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn write_all(&mut self, mut buf: &[u8]) -> Result<()> { | ||
| while !buf.is_empty() { | ||
| let bytes_written = self.write(buf)?; | ||
| assert_ne!(bytes_written, 0); | ||
| buf = &buf[bytes_written..]; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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,4 @@ | ||
| pub mod io; | ||
| pub mod transport; | ||
|
|
||
| mod wire; |
This file contains hidden or 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,55 @@ | ||
| use super as remote; | ||
| use remote::io; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[cfg(feature = "postcard")] | ||
| pub mod postcard; | ||
|
|
||
| pub trait Backend: core::fmt::Debug { | ||
| fn max_response_output_len(&self) -> usize; | ||
| fn send_request<'output, Req: Serialize, Resp: Deserialize<'output>>( | ||
| &'output mut self, | ||
| req: Req, | ||
| ) -> Result<Resp, BackendError>; | ||
| } | ||
|
|
||
| pub trait Executor: core::fmt::Debug { | ||
| fn read_request<'args, D: Deserialize<'args>>(&'args mut self) -> Result<D, ExecutorError>; | ||
| fn write_response<S: Serialize>(&mut self, resp: S) -> Result<(), ExecutorError>; | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, Deserialize, Serialize)] | ||
| pub enum BackendError { | ||
| #[error("an error occurred serializing the request")] | ||
| SerializeRequest(#[source] SerializeError), | ||
| #[error("an error occurred deserializing the response")] | ||
| DeserializeResponse(#[source] DeserializeError), | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, Deserialize, Serialize)] | ||
| pub enum ExecutorError { | ||
| #[error("an error occurred deserializing the request")] | ||
| DeserializeRequest(#[source] DeserializeError), | ||
| #[error("an error occurred serializing the response")] | ||
| SerializeResponse(#[source] SerializeError), | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, serde::Deserialize, serde::Serialize)] | ||
| pub enum SerializeError { | ||
| #[cfg(feature = "postcard")] | ||
| #[error("a Postcard-specific serialize error occurred")] | ||
| Postcard(#[source] postcard::Error), | ||
| #[error("an I/O occurred during serialization")] | ||
| Io(#[source] io::Error), | ||
| } | ||
|
|
||
| #[derive(Debug, thiserror::Error, serde::Deserialize, serde::Serialize)] | ||
| pub enum DeserializeError { | ||
| #[cfg(feature = "postcard")] | ||
| #[error("a Postcard-specific deserialize error occurred")] | ||
| Postcard(#[source] postcard::Error), | ||
| #[error("an I/O occurred during deserialization")] | ||
| Io(#[source] io::Error), | ||
| #[error("the I/O buffer was too small")] | ||
| BufferTooSmall, | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.