-
Notifications
You must be signed in to change notification settings - Fork 189
feat: add serialized pub/sub APIs #592
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
bresilla
wants to merge
2
commits into
ros2-rust:main
Choose a base branch
from
bresilla:serialized_transport
base: main
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,40 @@ | ||
| use crate::{rcl_bindings::*, RclrsError, ToResult}; | ||
|
|
||
| /// A growable serialized message buffer. | ||
| /// | ||
| /// This wraps `rcl_serialized_message_t` (aka `rmw_serialized_message_t`). | ||
| pub struct SerializedMessage { | ||
| pub(crate) msg: rcl_serialized_message_t, | ||
| } | ||
|
|
||
| unsafe impl Send for SerializedMessage {} | ||
|
|
||
| impl SerializedMessage { | ||
| /// Create a new serialized message buffer with the given capacity in bytes. | ||
| pub fn new(capacity: usize) -> Result<Self, RclrsError> { | ||
| unsafe { | ||
| let mut msg = rcutils_get_zero_initialized_uint8_array(); | ||
| let allocator = rcutils_get_default_allocator(); | ||
| rcutils_uint8_array_init(&mut msg, capacity, &allocator).ok()?; | ||
| Ok(Self { msg }) | ||
| } | ||
| } | ||
|
|
||
| /// Return the current serialized payload. | ||
| pub fn as_bytes(&self) -> &[u8] { | ||
| unsafe { std::slice::from_raw_parts(self.msg.buffer, self.msg.buffer_length) } | ||
| } | ||
|
|
||
| /// Reset the length to 0 without changing capacity. | ||
| pub fn clear(&mut self) { | ||
| self.msg.buffer_length = 0; | ||
| } | ||
| } | ||
|
|
||
| impl Drop for SerializedMessage { | ||
| fn drop(&mut self) { | ||
| unsafe { | ||
| let _ = rcutils_uint8_array_fini(&mut self.msg); | ||
| } | ||
| } | ||
| } |
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,34 @@ | ||
| use crate::{node::NodeHandle, rcl_bindings::*, RclrsError, ToResult, ENTITY_LIFECYCLE_MUTEX}; | ||
| use std::{ptr, sync::Arc}; | ||
|
|
||
| use crate::serialized_message::SerializedMessage; | ||
|
|
||
| /// A publisher which publishes serialized ROS messages. | ||
| pub struct SerializedPublisher { | ||
| pub(crate) handle: Arc<NodeHandle>, | ||
| pub(crate) pub_: rcl_publisher_t, | ||
| } | ||
|
|
||
| unsafe impl Send for SerializedPublisher {} | ||
| unsafe impl Sync for SerializedPublisher {} | ||
|
|
||
| impl Drop for SerializedPublisher { | ||
| fn drop(&mut self) { | ||
| let _context_lock = self.handle.context_handle.rcl_context.lock().unwrap(); | ||
| let mut node = self.handle.rcl_node.lock().unwrap(); | ||
| let _lifecycle_lock = ENTITY_LIFECYCLE_MUTEX.lock().unwrap(); | ||
| unsafe { | ||
| let _ = rcl_publisher_fini(&mut self.pub_, &mut *node); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SerializedPublisher { | ||
| /// Publish a serialized (CDR) message. | ||
| pub fn publish(&self, msg: &SerializedMessage) -> Result<(), RclrsError> { | ||
| unsafe { | ||
| rcl_publish_serialized_message(&self.pub_, &msg.msg, ptr::null_mut()).ok()?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } |
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,42 @@ | ||
| use crate::{node::NodeHandle, rcl_bindings::*, MessageInfo, RclrsError, ENTITY_LIFECYCLE_MUTEX}; | ||
| use std::{ptr, sync::Arc}; | ||
|
|
||
| use crate::serialized_message::SerializedMessage; | ||
|
|
||
| /// A subscription which receives serialized ROS messages. | ||
| pub struct SerializedSubscription { | ||
| pub(crate) handle: Arc<NodeHandle>, | ||
| pub(crate) sub: rcl_subscription_t, | ||
| } | ||
|
|
||
| unsafe impl Send for SerializedSubscription {} | ||
| unsafe impl Sync for SerializedSubscription {} | ||
|
|
||
| impl Drop for SerializedSubscription { | ||
| fn drop(&mut self) { | ||
| let _context_lock = self.handle.context_handle.rcl_context.lock().unwrap(); | ||
| let mut node = self.handle.rcl_node.lock().unwrap(); | ||
| let _lifecycle_lock = ENTITY_LIFECYCLE_MUTEX.lock().unwrap(); | ||
| unsafe { | ||
| let _ = rcl_subscription_fini(&mut self.sub, &mut *node); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SerializedSubscription { | ||
| /// Take a serialized (CDR) message. | ||
| /// | ||
| /// Returns `Ok(None)` when no message is available. | ||
| pub fn take(&self, buf: &mut SerializedMessage) -> Result<Option<MessageInfo>, RclrsError> { | ||
| unsafe { | ||
| let mut info: rmw_message_info_t = std::mem::zeroed(); | ||
| let rc = | ||
| rcl_take_serialized_message(&self.sub, &mut buf.msg, &mut info, ptr::null_mut()); | ||
| if rc != 0 { | ||
| // No message available or error. The rmw/rcl API uses negative codes for "take failed". | ||
| return Ok(None); | ||
| } | ||
| Ok(Some(MessageInfo::from_rmw_message_info(&info))) | ||
| } | ||
| } | ||
| } |
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.
lib.rshas#![warn(missing_docs)], and CI runscargo rustdoc -- -D warnings/cargo clippy ... -D warnings. Makingrcl_bindingspublic without a doc comment will emit amissing_docswarning for this module item and fail CI. Add a///doc comment forpub mod rcl_bindings;(or annotate this item with#[allow(missing_docs)]/#[doc(hidden)]if you don't want it documented).