-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add synchronous stream implementation #35
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
Open
yhdengh
wants to merge
1
commit into
arca-networking-pr
Choose a base branch
from
arca-networking-pr-1
base: arca-networking-pr
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,155 @@ | ||
| use crate::pipe::{BidirectionalPipe, PipeError, Read, Write}; | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum StreamError { | ||
| WriteClosed, | ||
| } | ||
|
|
||
| pub struct SyncStream<'a> { | ||
| /// BuddyAllocator offset of the SHM region backing this pipe. | ||
| pub shm_offset: u64, | ||
| pipe: BidirectionalPipe<'a>, | ||
| } | ||
|
|
||
| impl<'a> SyncStream<'a> { | ||
| pub fn from_pipe(shm_offset: u64, pipe: BidirectionalPipe<'a>) -> Self { | ||
| Self { shm_offset, pipe } | ||
| } | ||
|
|
||
| /// Write all of `buf` into the pipe, spinning if the ring is full; returns `Err(WriteClosed)` if the peer closed their read side. | ||
| pub fn send(&mut self, buf: &[u8]) -> Result<usize, StreamError> { | ||
| if self.pipe.is_peer_read_closed() { | ||
| self.pipe.close_write(); | ||
| return Err(StreamError::WriteClosed); | ||
| } | ||
| if buf.is_empty() { | ||
| return Ok(0); | ||
| } | ||
| self.pipe.write_all(buf); | ||
| Ok(buf.len()) | ||
| } | ||
|
|
||
| /// Read exactly `buf.len()` bytes, spinning until full; returns `Ok(n < buf.len())` only on EOF when the peer closed their write side. | ||
| pub fn recv(&mut self, buf: &mut [u8]) -> Result<usize, StreamError> { | ||
| let n = read_exact(&mut self.pipe, buf); | ||
| if n < buf.len() { | ||
| self.pipe.close_read(); | ||
| } | ||
| Ok(n) | ||
| } | ||
|
|
||
| pub fn close_write(&mut self) { | ||
| self.pipe.close_write(); | ||
| } | ||
|
|
||
| pub fn close_read(&mut self) { | ||
| self.pipe.close_read(); | ||
| } | ||
|
|
||
| pub fn is_closed(&self) -> bool { | ||
| self.pipe.is_closed() | ||
| } | ||
| } | ||
|
|
||
| fn read_exact(pipe: &mut crate::pipe::BidirectionalPipe, buf: &mut [u8]) -> usize { | ||
| let mut filled = 0; | ||
| while filled < buf.len() { | ||
| match pipe.read(&mut buf[filled..]) { | ||
| Ok(n) => filled += n, | ||
| Err(PipeError::WouldBlock) => { | ||
| if pipe.is_peer_write_closed() { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| filled | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use arca_pipe::{BidirectionalPipe, SharedMemoryRegion, Side}; | ||
|
Check failure on line 72 in common/src/sync_stream.rs
|
||
|
|
||
| #[repr(align(8))] | ||
| struct Aligned<const N: usize>([u8; N]); | ||
|
|
||
| macro_rules! stream_pair { | ||
| ($ring:expr, $mem:ident, $a:ident, $b:ident) => { | ||
| let mut $mem = Aligned([0u8; BidirectionalPipe::required_size($ring as u64) as usize]); | ||
| let region = | ||
| unsafe { SharedMemoryRegion::from_raw($mem.0.as_mut_ptr(), $mem.0.len() as u64) }; | ||
| let pipe_a = BidirectionalPipe::new(®ion, $ring, Side::A); | ||
| let pipe_b = BidirectionalPipe::new(®ion, $ring, Side::B); | ||
| let mut $a = SyncStream::from_pipe(0, pipe_a); | ||
| let mut $b = SyncStream::from_pipe(0, pipe_b); | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn send_recv_data() { | ||
| stream_pair!(128, mem, a, b); | ||
| assert_eq!(a.send(b"hello").unwrap(), 5); | ||
| let mut buf = [0u8; 5]; | ||
| assert_eq!(b.recv(&mut buf).unwrap(), 5); | ||
| assert_eq!(&buf, b"hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn close_write_signals_eof_to_peer() { | ||
| stream_pair!(64, mem, a, b); | ||
| a.close_write(); | ||
| let mut buf = [0u8; 8]; | ||
| assert_eq!(b.recv(&mut buf).unwrap(), 0); | ||
| assert!(!b.is_closed()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn close_both_sides_blocks_peer_ops() { | ||
| stream_pair!(64, mem, a, b); | ||
| b.close_write(); | ||
| b.close_read(); | ||
| // b has closed its own ends but a hasn't yet — pipe not fully closed | ||
| assert!(!b.is_closed()); | ||
| let mut buf = [0u8; 8]; | ||
| // a sees EOF because b closed write, and WriteClosed because b closed read | ||
| assert_eq!(a.recv(&mut buf).unwrap(), 0); | ||
| assert!(matches!(a.send(b"x"), Err(StreamError::WriteClosed))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn send_after_peer_closes_read_errors() { | ||
| stream_pair!(64, mem, a, b); | ||
| b.close_read(); | ||
| assert!(matches!(a.send(b"x"), Err(StreamError::WriteClosed))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recv_after_eof_returns_zero() { | ||
| stream_pair!(64, mem, a, b); | ||
| a.close_write(); | ||
| let mut buf = [0u8; 8]; | ||
| b.recv(&mut buf).unwrap(); | ||
| assert_eq!(b.recv(&mut buf).unwrap(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recv_fills_exact_buffer_size() { | ||
| stream_pair!(128, mem, a, b); | ||
| assert_eq!(a.send(b"hello").unwrap(), 5); | ||
| let mut buf = [0u8; 5]; | ||
| assert_eq!(b.recv(&mut buf).unwrap(), 5); | ||
| assert_eq!(&buf, b"hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn pipe_closed_after_both_sides_close() { | ||
| stream_pair!(128, mem, a, b); | ||
| a.close_write(); | ||
| let mut buf = [0u8; 8]; | ||
| b.recv(&mut buf).unwrap(); | ||
| b.close_write(); | ||
| a.recv(&mut buf).unwrap(); | ||
| assert!(a.is_closed()); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
shm_offsetused anywhere?