# Intra-process Communication

## Related Readings

* [ROS2 Design - Intra-process Communication](https://design.ros2.org/articles/intraprocess_communications.html)
* [C++ Smart Pointers Lecture](https://websites.umich.edu/~eecs381/handouts/C++11_smart_ptrs.pdf)

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.

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FD0VwiKpjgcpOUVJbSEIH%2Fimage.png?alt=media&#x26;token=b84b25f4-b8b1-4a08-ace1-79557ffff64e" alt="" width="552"><figcaption></figcaption></figure>

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:

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FUEZJWGLI8penV39QVA62%2Fimage.png?alt=media&#x26;token=fb13cd28-22aa-4bec-b83f-acb2ab303877" alt=""><figcaption></figcaption></figure>

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.

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FDV8huNLUarU5AKEQgd4m%2Fimage.png?alt=media&#x26;token=4e211d81-b6d1-44b6-a933-ae206d424168" alt=""><figcaption></figcaption></figure>

So jump to do\_intra\_process\_ros\_message\_publish method:

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FVZkK8TYrMR9XnfsMNjcm%2Fimage.png?alt=media&#x26;token=8096f772-5bfc-4b58-96d3-37bbaa6d22ad" alt=""><figcaption></figcaption></figure>

Not too much info here. Move on to do\_intra\_process\_publish method:

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FXOLFgTXZ9QdUFe5ibdAN%2Fimage.png?alt=media&#x26;token=053955e2-32aa-4d98-9e2e-43d2fbb2bc0b" alt=""><figcaption></figcaption></figure>

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?

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FWgsnpPaT990e9lI2YacO%2Fimage.png?alt=media&#x26;token=290ac10b-82ef-4c91-a6af-63d3461862fd" alt=""><figcaption></figcaption></figure>

The buffer type is resolved in the `resolve_intra_process_buffer_type.hpp` file:

<figure><img src="https://442453138-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FWs42vVsGF012EH3SD2WG%2Fuploads%2FYzkFlIWUfYJuJA9FPpjk%2Fimage.png?alt=media&#x26;token=58660f7e-1fed-4b8e-9056-5baf25129518" alt=""><figcaption></figcaption></figure>

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.\ <br>
