Skip to content

Commit

Permalink
v0.4 API 2 with frame acks
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Jun 17, 2024
1 parent a4d5294 commit 8ea9b69
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rvideo"
version = "0.3.4"
version = "0.4.0"
edition = "2021"
authors = ["Serhij S. <[email protected]>"]
license = "Apache-2.0"
Expand Down
8 changes: 7 additions & 1 deletion protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ to communicate with each other.

* Server-to-client: STREAM-INFO

* Server: starts sending frames
* Server: starts sending frames. To avoid flooding, each frame must be
acknowledged by the client before the next one is sent.

## Structures

Expand Down Expand Up @@ -102,3 +103,8 @@ For raw formats, the picture length is always the same, however the protocol
sends the length for each picture data block to allow compressed formats.

The max picture size is `u32::MAX` bytes.

### Acknowledgment

After receiving the frame, the client must send an acknowledgment to the
server. The acknowledgment is a single byte 0x00.
3 changes: 3 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ impl Iterator for Client {
if let Err(e) = self.stream.read_exact(&mut data) {
return Some(Err(e.into()));
}
if let Err(e) = self.stream.write_all(&[0u8; 1]) {
return Some(Err(e.into()));
}
Some(Ok(Frame {
metadata: metadata.map(Into::into),
data: data.into(),
Expand Down
1 change: 1 addition & 0 deletions src/client_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl ClientAsync {
usize::try_from(u32::from_le_bytes(len_buf)).map_err(|_| Error::FrameDataTooLarge)?;
let mut data = vec![0u8; len];
tokio::time::timeout(self.timeout, self.stream.read_exact(&mut data)).await??;
tokio::time::timeout(self.timeout, self.stream.write_all(&[0u8; 1])).await??;
Ok(Frame {
metadata: metadata.map(Into::into),
data: data.into(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Frame {
}

/// Server API version
pub const API_VERSION: u8 = 1;
pub const API_VERSION: u8 = 2;

/// Error type
#[derive(thiserror::Error, Debug)]
Expand Down
5 changes: 5 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ impl StreamServerInner {
let data_len = u32::try_from(frame.data.len()).unwrap();
socket.write_all(&data_len.to_le_bytes())?;
socket.write_all(&frame.data)?;
let mut buf = [0u8; 1];
socket.read_exact(&mut buf)?;
if buf[0] != 0 {
return Err(Error::NotReady);
}
Ok(())
}
}

0 comments on commit 8ea9b69

Please sign in to comment.