Skip to content

Commit

Permalink
Revert Fix decoding of multiple packets
Browse files Browse the repository at this point in the history
I need to verify if the existing implementation was correct, meaning
if it's ok to join OSC packets with an OSC bundles if the bundle comes
first.
  • Loading branch information
klingtnet committed Jun 10, 2023
1 parent 5bf00e7 commit a87a05e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
18 changes: 7 additions & 11 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,13 @@ pub fn decode_tcp(msg: &[u8]) -> Result<(&[u8], Option<OscPacket>), OscError> {
return Ok((msg, None));
}

// TODO(2023-06-10): Check why decode_packet is not returning a remainder even though msg contains a subsequent packet, e.g. in case of a TCP stream message that contains multiple packets.
// As a workaround we split the message after the first OSC packet and use the remaining bytes as the remainder.
// Ideally decode_packet should return this remainder, so we don't need to manually split the message here.
let (msg, remainder) = msg.split_at(4 + (osc_packet_length as usize));
let (_, osc_packet) =
decode_packet(&input[..osc_packet_length as usize], msg).map_err(|e| match e {
Err::Incomplete(_) => OscError::BadPacket("Incomplete data"),
Err::Error(e) | Err::Failure(e) => e,
})?;

Ok((remainder, Some(osc_packet)))
match decode_packet(input, msg).map(|(remainder, osc_packet)| (remainder, Some(osc_packet))) {
Ok((remainder, osc_packet)) => Ok((remainder, osc_packet)),
Err(e) => match e {
Err::Incomplete(_) => Err(OscError::BadPacket("Incomplete data")),
Err::Error(e) | Err::Failure(e) => Err(e),
},
}
}

/// Takes a bytes slice from a TCP stream (or any stream-based protocol) and returns a vec of all
Expand Down
10 changes: 5 additions & 5 deletions tests/decode_encode_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const GOLDEN_MESSAGE_WO_ARGS: &str = "2f736f6d652f6164647200002c000000";
const GOLDEN_MESSAGE_WITH_ALL_TYPES: &str = "2f616e6f746865722f616464726573732f3100002c69686664737362746346544e496d725b695b64645d735d0000000000000004000000000000002a40490fda400921fb54442eea54686973206973206120737472696e672e00000054686973206973206120737472696e6720746f6f2e00000000000003010203000000007b000001c80000006304292a81ffc02a0d0000002a3ff3ae147ae147ae4009ae147ae147ae59617900";
const GOLDEN_EMPTY_BUNDLE: &str = "2362756e646c65000000000400000002";
const GOLDEN_BUNDLE: &str = "2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd";
const GOLDEN_MULTI_PACKET: &str = "0000008c2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd000000102f736f6d652f6164647200002c000000";
const GOLDEN_MULTI_PACKET: &str = "000000102f736f6d652f6164647200002c0000000000008c2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd";

fn prefix_with_size(size: u32, golden: &str) -> String {
[hex::encode(size.to_be_bytes()), golden.into()].concat()
Expand Down Expand Up @@ -184,6 +184,10 @@ fn test_bundle() {
#[test]
fn test_multi_packet_message() {
let packets = vec![
OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: vec![],
}),
OscPacket::Bundle(OscBundle {
timetag: (1234, 4321).into(),
content: vec![
Expand All @@ -210,10 +214,6 @@ fn test_multi_packet_message() {
}),
],
}),
OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: vec![],
}),
];

// Stream encoding and decoding.
Expand Down

0 comments on commit a87a05e

Please sign in to comment.