diff --git a/msg/templates/urtps/Publisher.cpp.em b/msg/templates/urtps/Publisher.cpp.em index f04acbe0ae..5805286d61 100644 --- a/msg/templates/urtps/Publisher.cpp.em +++ b/msg/templates/urtps/Publisher.cpp.em @@ -109,11 +109,17 @@ bool @(topic)_Publisher::init(const std::string &ns) nodeName.append("@(topic)_publisher"); PParam.rtps.setName(nodeName.c_str()); +@[if ros2_distro]@ // Check if ROS_LOCALHOST_ONLY is set. This means that one wants to use only // the localhost network for data sharing. If FastRTPS/DDS >= 2.0 and // RMW_IMPLEMENTATION is FastDDS then the Shared Memory transport is used const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); - if (localhost_only && strcmp(localhost_only, "1") == 0) { + const char* rmw_implementation = std::getenv("RMW_IMPLEMENTATION"); + const char* ros_distro = std::getenv("ROS_DISTRO"); + if (localhost_only && strcmp(localhost_only, "1") == 0 + && ((rmw_implementation && ((strcmp(rmw_implementation, "rmw_fastrtps_cpp") == 0) + || (strcmp(rmw_implementation, "rmw_fastrtps_dynamic_cpp") == 0))) + || (!rmw_implementation && ros_distro && strcmp(ros_distro, "foxy") == 0))) { // Create a custom network UDPv4 transport descriptor // to whitelist the localhost auto localhostUdpTransport = std::make_shared(); @@ -125,17 +131,13 @@ bool @(topic)_Publisher::init(const std::string &ns) // Add the descriptor as a custom user transport PParam.rtps.userTransports.push_back(localhostUdpTransport); -@[if version.parse(fastrtps_version) >= version.parse('2.0')]@ - const char* rmw_implementation = std::getenv("RMW_IMPLEMENTATION"); - const char* ros_distro = std::getenv("RMW_IMPLEMENTATION"); - if (((rmw_implementation && (strcmp(rmw_implementation, "rmw_fastrtps_cpp") == 0)) || (strcmp(rmw_implementation, "rmw_fastrtps_dynamic_cpp") == 0)) || - (!rmw_implementation && strcmp(ros_distro, "foxy") == 0)) { - // Add shared memory transport when available - auto shmTransport = std::make_shared(); - PParam.rtps.userTransports.push_back(shmTransport); - } -@[end if]@ +@[ 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]@ } +@[end if]@ mp_participant = Domain::createParticipant(PParam); diff --git a/msg/templates/urtps/Subscriber.cpp.em b/msg/templates/urtps/Subscriber.cpp.em index 18374d0b30..aba50ce346 100644 --- a/msg/templates/urtps/Subscriber.cpp.em +++ b/msg/templates/urtps/Subscriber.cpp.em @@ -115,11 +115,17 @@ bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable *t_send nodeName.append("@(topic)_subscriber"); PParam.rtps.setName(nodeName.c_str()); +@[if ros2_distro]@ // Check if ROS_LOCALHOST_ONLY is set. This means that one wants to use only // the localhost network for data sharing. If FastRTPS/DDS >= 2.0 and // RMW_IMPLEMENTATION is FastDDS then the Shared Memory transport is used const char* localhost_only = std::getenv("ROS_LOCALHOST_ONLY"); - if (localhost_only && strcmp(localhost_only, "1") == 0) { + const char* rmw_implementation = std::getenv("RMW_IMPLEMENTATION"); + const char* ros_distro = std::getenv("ROS_DISTRO"); + if (localhost_only && strcmp(localhost_only, "1") == 0 + && ((rmw_implementation && ((strcmp(rmw_implementation, "rmw_fastrtps_cpp") == 0) + || (strcmp(rmw_implementation, "rmw_fastrtps_dynamic_cpp") == 0))) + || (!rmw_implementation && ros_distro && strcmp(ros_distro, "foxy") == 0))) { // Create a custom network UDPv4 transport descriptor // to whitelist the localhost auto localhostUdpTransport = std::make_shared(); @@ -131,17 +137,13 @@ bool @(topic)_Subscriber::init(uint8_t topic_ID, std::condition_variable *t_send // Add the descriptor as a custom user transport PParam.rtps.userTransports.push_back(localhostUdpTransport); -@[if version.parse(fastrtps_version) >= version.parse('2.0')]@ - const char* rmw_implementation = std::getenv("RMW_IMPLEMENTATION"); - const char* ros_distro = std::getenv("RMW_IMPLEMENTATION"); - if (((rmw_implementation && (strcmp(rmw_implementation, "rmw_fastrtps_cpp") == 0)) || (strcmp(rmw_implementation, "rmw_fastrtps_dynamic_cpp") == 0)) || - (!rmw_implementation && strcmp(ros_distro, "foxy") == 0)) { - // Add shared memory transport when available - auto shmTransport = std::make_shared(); - PParam.rtps.userTransports.push_back(shmTransport); - } -@[end if]@ +@[ 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]@ } +@[end if]@ mp_participant = Domain::createParticipant(PParam);