diff --git a/roslibrust/src/ros1/tcpros.rs b/roslibrust/src/ros1/tcpros.rs index 1609619..ce5b810 100644 --- a/roslibrust/src/ros1/tcpros.rs +++ b/roslibrust/src/ros1/tcpros.rs @@ -13,31 +13,22 @@ use super::names::Name; pub struct ConnectionHeader { pub caller_id: String, - pub latching: bool, - pub msg_definition: String, + pub latching: bool, // TODO this field should be optional and None for service clients and servers + pub msg_definition: String, // TODO this should be optional and None for service clients and servers pub md5sum: Option, // TODO we may want to distinguish between service and topic headers with different types? pub service: Option, pub topic: Option, pub topic_type: String, - pub tcp_nodelay: bool, - // TODO service client may include "persistent" here - // TODO service server only has to respond with caller_id (all other fields optional) + pub tcp_nodelay: bool, // TODO this field should be optional and None for service clients and servers + // TODO service client may include "persistent" here + // TODO service server only has to respond with caller_id (all other fields optional) } impl ConnectionHeader { - /// First reads a length field for overall length of the header - /// Then reads the header from the following bytes - pub fn from_bytes_with_length(header_data: &[u8]) -> std::io::Result { - let mut cursor = Cursor::new(header_data); - let header_len = cursor.read_u32::()? as usize; - ConnectionHeader::from_bytes(&header_data[4..header_len + 4]) - } - /// Parses a connection header from a byte array /// This specifically expects to start at the first byte of the first field /// of the header and bypass the bytes representing the length of the header - /// if present pub fn from_bytes(header_data: &[u8]) -> std::io::Result { let mut cursor = Cursor::new(header_data); @@ -156,6 +147,7 @@ impl ConnectionHeader { header_data.write_u32::(topic_type.len() as u32)?; header_data.write(topic_type.as_bytes())?; + // Now that we know the length, stick its value in the first 4 bytes let total_length = (header_data.len() - 4) as u32; for (idx, byte) in total_length.to_le_bytes().iter().enumerate() { header_data[idx] = *byte; @@ -233,23 +225,22 @@ mod test { #[test_log::test] fn ros_example_header() { let bytes: Vec = vec![ - 0xb0, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, - 0x65, 0x5f, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3d, 0x73, - 0x74, 0x72, 0x69, 0x6e, 0x67, 0x20, 0x64, 0x61, 0x74, 0x61, 0x0a, 0x0a, 0x25, 0x00, - 0x00, 0x00, 0x63, 0x61, 0x6c, 0x6c, 0x65, 0x72, 0x69, 0x64, 0x3d, 0x2f, 0x72, 0x6f, - 0x73, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x5f, 0x34, 0x37, 0x36, 0x37, 0x5f, 0x31, 0x33, - 0x31, 0x36, 0x39, 0x31, 0x32, 0x37, 0x34, 0x31, 0x35, 0x35, 0x37, 0x0a, 0x00, 0x00, - 0x00, 0x6c, 0x61, 0x74, 0x63, 0x68, 0x69, 0x6e, 0x67, 0x3d, 0x31, 0x27, 0x00, 0x00, - 0x00, 0x6d, 0x64, 0x35, 0x73, 0x75, 0x6d, 0x3d, 0x39, 0x39, 0x32, 0x63, 0x65, 0x38, - 0x61, 0x31, 0x36, 0x38, 0x37, 0x63, 0x65, 0x63, 0x38, 0x63, 0x38, 0x62, 0x64, 0x38, - 0x38, 0x33, 0x65, 0x63, 0x37, 0x33, 0x63, 0x61, 0x34, 0x31, 0x64, 0x31, 0x0e, 0x00, - 0x00, 0x00, 0x74, 0x6f, 0x70, 0x69, 0x63, 0x3d, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x74, - 0x65, 0x72, 0x14, 0x00, 0x00, 0x00, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x73, 0x74, 0x64, - 0x5f, 0x6d, 0x73, 0x67, 0x73, 0x2f, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x09, 0x00, - 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, + 0x20, 0x00, 0x00, 0x00, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x64, 0x65, + 0x66, 0x69, 0x6e, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3d, 0x73, 0x74, 0x72, 0x69, 0x6e, + 0x67, 0x20, 0x64, 0x61, 0x74, 0x61, 0x0a, 0x0a, 0x25, 0x00, 0x00, 0x00, 0x63, 0x61, + 0x6c, 0x6c, 0x65, 0x72, 0x69, 0x64, 0x3d, 0x2f, 0x72, 0x6f, 0x73, 0x74, 0x6f, 0x70, + 0x69, 0x63, 0x5f, 0x34, 0x37, 0x36, 0x37, 0x5f, 0x31, 0x33, 0x31, 0x36, 0x39, 0x31, + 0x32, 0x37, 0x34, 0x31, 0x35, 0x35, 0x37, 0x0a, 0x00, 0x00, 0x00, 0x6c, 0x61, 0x74, + 0x63, 0x68, 0x69, 0x6e, 0x67, 0x3d, 0x31, 0x27, 0x00, 0x00, 0x00, 0x6d, 0x64, 0x35, + 0x73, 0x75, 0x6d, 0x3d, 0x39, 0x39, 0x32, 0x63, 0x65, 0x38, 0x61, 0x31, 0x36, 0x38, + 0x37, 0x63, 0x65, 0x63, 0x38, 0x63, 0x38, 0x62, 0x64, 0x38, 0x38, 0x33, 0x65, 0x63, + 0x37, 0x33, 0x63, 0x61, 0x34, 0x31, 0x64, 0x31, 0x0e, 0x00, 0x00, 0x00, 0x74, 0x6f, + 0x70, 0x69, 0x63, 0x3d, 0x2f, 0x63, 0x68, 0x61, 0x74, 0x74, 0x65, 0x72, 0x14, 0x00, + 0x00, 0x00, 0x74, 0x79, 0x70, 0x65, 0x3d, 0x73, 0x74, 0x64, 0x5f, 0x6d, 0x73, 0x67, + 0x73, 0x2f, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, ]; - let header = ConnectionHeader::from_bytes_with_length(&bytes).unwrap(); + let header = ConnectionHeader::from_bytes(&bytes).unwrap(); assert_eq!(header.msg_definition, "string data\n\n"); assert_eq!(header.caller_id, "/rostopic_4767_1316912741557"); assert_eq!(header.latching, true);