diff --git a/msg/templates/urtps/Publisher.cpp.em b/msg/templates/urtps/Publisher.cpp.em index d783428b1a1..36daf5288f3 100644 --- a/msg/templates/urtps/Publisher.cpp.em +++ b/msg/templates/urtps/Publisher.cpp.em @@ -62,16 +62,20 @@ except AttributeError: * This file was adapted from the fastcdrgen tool. */ +#include "@(topic)_Publisher.h" +#include #include #include #include #include #include +@[if version.parse(fastrtps_version) >= version.parse('2.0')]@ +#include -#include +using SharedMemTransportDescriptor = eprosima::fastdds::rtps::SharedMemTransportDescriptor; +@[end if]@ -#include "@(topic)_Publisher.h" @(topic)_Publisher::@(topic)_Publisher() : mp_participant(nullptr), @@ -102,20 +106,27 @@ bool @(topic)_Publisher::init(const std::string &ns) PParam.rtps.setName(nodeName.c_str()); // Check if ROS_LOCALHOST_ONLY is set. This means that one wants to use - // only the localhost network for data sharing - const char *localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); - + // only the localhost network for data sharing. If FastRTPS/DDS >= 2.0 + // then the Shared Memory transport is used + const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); if (localhost_only && strcmp(localhost_only, "1") == 0) { - // Create a custom network transport descriptor to whitelist the localhost - auto localhostDescriptor = std::make_shared(); - localhostDescriptor->interfaceWhiteList.emplace_back("127.0.0.1"); + // Create a custom network UDPv4 transport descriptor + // to whitelist the localhost + auto localhostUdpTransport = std::make_shared(); + localhostUdpTransport->interfaceWhiteList.emplace_back("127.0.0.1"); // Disable the built-in Transport Layer PParam.rtps.useBuiltinTransports = false; // Add the descriptor as a custom user transport - PParam.rtps.userTransports.push_back(localhostDescriptor); - } + PParam.rtps.userTransports.push_back(localhostUdpTransport); + +@[if version.parse(fastrtps_version) >= version.parse('2.0')]@ + // Add shared memory transport when available + auto shmTransport = std::make_shared(); + PParam.rtps.userTransports.push_back(shmTransport); +@[end if]@ + } mp_participant = Domain::createParticipant(PParam); diff --git a/msg/templates/urtps/Subscriber.cpp.em b/msg/templates/urtps/Subscriber.cpp.em index b280774a149..26af57eb1fe 100644 --- a/msg/templates/urtps/Subscriber.cpp.em +++ b/msg/templates/urtps/Subscriber.cpp.em @@ -62,15 +62,20 @@ except AttributeError: * This file was adapted from the fastcdrgen tool. */ +#include "@(topic)_Subscriber.h" + +#include #include #include #include #include #include +@[if version.parse(fastrtps_version) >= version.parse('2.0')]@ +#include -#include +using SharedMemTransportDescriptor = eprosima::fastdds::rtps::SharedMemTransportDescriptor; +@[end if]@ -#include "@(topic)_Subscriber.h" @(topic)_Subscriber::@(topic)_Subscriber() : mp_participant(nullptr), @@ -107,19 +112,26 @@ bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable *t_send PParam.rtps.setName(nodeName.c_str()); // Check if ROS_LOCALHOST_ONLY is set. This means that one wants to use - // only the localhost network for data sharing - const char *localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); - + // only the localhost network for data sharing. If FastRTPS/DDS >= 2.0 + // then the Shared Memory transport is used + const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); if (localhost_only && strcmp(localhost_only, "1") == 0) { - // Create a custom network transport descriptor to whitelist the localhost - auto localhostDescriptor = std::make_shared(); - localhostDescriptor->interfaceWhiteList.emplace_back("127.0.0.1"); + // Create a custom network UDPv4 transport descriptor + // to whitelist the localhost + auto localhostUdpTransport = std::make_shared(); + localhostUdpTransport->interfaceWhiteList.emplace_back("127.0.0.1"); // Disable the built-in Transport Layer PParam.rtps.useBuiltinTransports = false; // Add the descriptor as a custom user transport - PParam.rtps.userTransports.push_back(localhostDescriptor); + PParam.rtps.userTransports.push_back(localhostUdpTransport); + +@[if version.parse(fastrtps_version) >= version.parse('2.0')]@ + // Add shared memory transport when available + auto shmTransport = std::make_shared(); + PParam.rtps.userTransports.push_back(shmTransport); +@[end if]@ } mp_participant = Domain::createParticipant(PParam); diff --git a/msg/templates/urtps/microRTPS_agent.cpp.em b/msg/templates/urtps/microRTPS_agent.cpp.em index e8af3c2cc03..a45562cf096 100644 --- a/msg/templates/urtps/microRTPS_agent.cpp.em +++ b/msg/templates/urtps/microRTPS_agent.cpp.em @@ -237,10 +237,10 @@ int main(int argc, char **argv) printf("\033[0;37m--- MicroRTPS Agent ---\033[0m\n"); printf("[ micrortps_agent ]\tStarting link...\n"); - const char *localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); + const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); if (localhost_only && strcmp(localhost_only, "1") == 0) { - printf("[ micrortps_agent ]\tUsing only the localhost network for data sharing...\n"); + printf("[ micrortps_agent ]\tUsing only the localhost network...\n"); } switch (_options.transport) {