Intra-process Communication
Last updated
Last updated
The ROS2 Design - Intra-process Communication is a design doc but it captures the main ideas in the actual implementation. Note that for ROS Humble, the intra-process communication is still an experimental feature.
The key of the intra-processs is to keep the message object inside the memory and avoid copy. In this article, we will take a look at the source code and see how the code determine when to make a copy and when to use a pointer.
Let's start with the publisher.publish method:
The control is then passed to another publish method with different signature. It determins if we need to perform inter-process publication. Let's focus on the intra-process communication and take a look at the do_intra_process_ros_message_publish method.
So jump to do_intra_process_ros_message_publish method:
Not too much info here. Move on to do_intra_process_publish method:
Things getting interesting. In this function, we can see many key concepts in the design:
The sub_ids are related to the subscriptions. And we can see there are two types of subscriptions. The ones that own the message and the ones that share (or observe) the message.
We see the word buffers in some of the method names.
Note that for subscriptions that take the ownship of the message, we pass the original unique_ptr of the message to them; for subscriptions that share (or observe) the message, a shared pointer is constructed. Depending on the scenario, we can either convert the unique_ptr to the shared_ptr or we need to construct a copy.
The next question is how to control the type of the subscription?
The buffer type is resolved in the resolve_intra_process_buffer_type.hpp
file:
We can either specify the buffer type explicitly or ask the code to determine the buffer type based on the parameter type of the callback function of the subscriber.