Skip to content
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

refactor framing_sv2 example #1468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ jobs:
run: |
cargo run --manifest-path=protocols/v1/Cargo.toml --example client_and_server 30

- name: Run framing-sv2 example
run: |
cargo run --manifest-path=protocols/v2/framing-sv2/Cargo.toml --example sv2_frame

- name: interop-test
run: |
if [ ${{ matrix.os }} == "ubuntu-latest" ]; then
Expand Down
29 changes: 17 additions & 12 deletions protocols/v2/framing-sv2/examples/sv2_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,29 @@
// cargo run --example sv2_frame
// ```

use binary_sv2::{binary_codec_sv2, Serialize};
use binary_sv2::{binary_codec_sv2, Deserialize, Serialize};
use framing_sv2::framing::Sv2Frame;
use std::convert::TryInto;

// Example message type (e.g., SetupConnection)
const MSG_TYPE: u8 = 1;
// Example extension type (e.g., a standard Sv2 message)
const EXT_TYPE: u16 = 0x0001;

#[derive(Serialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CustomMessage {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking out loud here, wouldn't it be more beneficial to actually have an SV2 message here? for the MSG_TYPE, SETUP_CONNECTION is used, maybe here as well?

pub data: u32,
pub nonce: u32,
}

fn main() {
// Create the message payload
let message = CustomMessage { data: 42 };
let message = CustomMessage { nonce: 42 };

// Create the frame from the message
let frame: Sv2Frame<CustomMessage, Vec<u8>> =
Sv2Frame::from_message(message, MSG_TYPE, EXT_TYPE, false)
Sv2Frame::from_message(message.clone(), MSG_TYPE, EXT_TYPE, false)
.expect("Failed to frame the message");

// How header information is accessed
let header = frame.get_header().expect("Frame has no header");
assert_eq!(header.msg_type(), MSG_TYPE);
assert_eq!(header.ext_type(), EXT_TYPE);

// Serialize the frame into a byte array for transmission
let mut serialized_frame = vec![0u8; frame.encoded_length()];
frame
Expand All @@ -54,6 +50,15 @@ fn main() {
let mut deserialized_frame = Sv2Frame::<CustomMessage, Vec<u8>>::from_bytes(serialized_frame)
.expect("Failed to deserialize frame");

assert_eq!(deserialized_frame.encoded_length(), 10); // 6 header bytes + 4 payload bytes
assert_eq!(deserialized_frame.payload(), [42, 0, 0, 0]);
// Assert that deserialized header has the original content
let deserialized_header = deserialized_frame
.get_header()
.expect("Frame has no header");
assert_eq!(deserialized_header.msg_type(), MSG_TYPE);
assert_eq!(deserialized_header.ext_type(), EXT_TYPE);

// Assert that deserialized message has the original content
let deserialized_message: CustomMessage = binary_sv2::from_bytes(deserialized_frame.payload())
.expect("Failed to extract the message from the payload");
assert_eq!(deserialized_message.nonce, message.nonce);
}
Loading