/// Initialization context structure which is used to store init specific information.
typedef struct RMW_PUBLIC_TYPE rmw_context_t
{
/// Locally (process local) unique ID that represents this init/shutdown cycle.
uint64_t instance_id;
/// Implementation identifier, used to ensure two different implementations are not being mixed.
const char * implementation_identifier;
/// Options used to initialize the context.
rmw_init_options_t options;
/// Domain id that is being used.
size_t actual_domain_id;
/// Implementation defined context information.
/** May be NULL if there is no implementation defined context information. */
rmw_context_impl_t * impl;
} rmw_context_t;
/// Base data structure that a Context will need in any DDS-based RMW implementation
/// mapping one Participant to Multiple Nodes.
struct Context
{
/// Global ID of the Participant that the Context uses.
rmw_gid_t gid;
/// Publisher used to publish ParticipantEntitiesInfo discovery data.
rmw_publisher_t * pub;
/// Subscriber used to listen to ParticipantEntitiesInfo discovery data.
rmw_subscription_t * sub;
/// Cached graph from discovery data.
GraphCache graph_cache;
/// Mutex that should be locked when updating graph cache and publishing a graph message.
std::mutex node_update_mutex;
/// Thread to listen to discovery data.
std::thread listener_thread;
/// Indicates if the listener thread is running.
std::atomic_bool thread_is_running;
/// Awakes listener thread when finishing the context.
rmw_guard_condition_t * listener_thread_gc;
/// Guard condition that should be triggered when the graph changes.
rmw_guard_condition_t * graph_guard_condition;
};
RMW_LOCALHOST_ONLY_ENABLED
是一个枚举值或宏定义,通常用于配置ROS 2(Robot Operating System 2)中的RMW(ROS Middleware)实现,以限制通信仅在本地主机上进行。在ROS 2中,RMW层是负责底层通信的中间件接口,它允许ROS 2节点在不同的进程或机器之间发送和接收消息。
当RMW_LOCALHOST_ONLY_ENABLED
被设置为启用(例如,其值为非零或true
)时,ROS 2节点只能与运行在同一台机器上的其他节点通信。这通常用于开发和调试目的,以确保节点之间的通信不会受到网络延迟或其他网络问题的影响。
相反,如果RMW_LOCALHOST_ONLY_ENABLED
被禁用(例如,其值为零或false
),则ROS 2节点可以通过网络与其他机器上的节点进行通信。
在配置ROS 2环境时,你可以通过设置环境变量或在启动文件中指定参数来启用或禁用此选项。具体的实现和名称可能会因ROS 2的不同发行版或RMW实现(如Fast-RTPS、DDS等)而略有不同。
注意:RMW_LOCALHOST_ONLY_ENABLED
的确切含义和用法可能会随着ROS 2的不同版本和配置而有所变化。因此,最好查阅你正在使用的ROS 2发行版的官方文档或源代码,以获取最准确的信息。
struct rmw_context_impl_t
{
/// Pointer to `rmw_dds_common::Context`.
void * common;
/// Pointer to `rmw_fastrtps_shared_cpp::CustomParticipantInfo`.
void * participant_info;
/// Mutex used to protect initialization/destruction.
std::mutex mutex;
/// Reference count.
uint64_t count;
/// Shutdown flag.
bool is_shutdown;
};
Fast DDS 利用DomainParticipant来GuidPrefix_t识别在同一主机中运行的对等点。两个具有相同前 4 个字节的参与者GuidPrefix_t被认为在同一主机上运行。 is_on_same_host_as()提供 API 来检查此情况。请同时考虑进程内交付的 GUID 前缀注意事项中包含的警告。
Fast DDS 利用DomainParticipantGuidPrefix_t来识别在同一进程中运行的对等点。两个具有相同前 8 个字节的参与者GuidPrefix_t被认为在同一进程中运行,因此使用进程内传递。 is_on_same_process_as()提供 API 来检查此情况。当让 Fast DDS 为创建的 DomainParticipants 设置 GUID 前缀时,此机制可以开箱即用。但是,GuidPrefix_t以编程方式或使用 XML 手动设置时需要特别考虑。
guid是在fast dds层生成的,通过下面的函数:
bool RTPSDomainImpl::create_participant_guid(
int32_t& participant_id,
GUID_t& guid)
{
bool ret_value = get_instance()->reserve_participant_id(participant_id);
if (ret_value)
{
guid_prefix_create(participant_id, guid.guidPrefix);
guid.entityId = c_EntityId_RTPSParticipant;
}
return ret_value;
}
在RTPS层又会生成一个新的guid
// Generate a new GuidPrefix_t
GuidPrefix_t guidP;
guid_prefix_create(instance->get_id_for_prefix(ID), guidP);
if (!PParam.builtin.metatraffic_external_unicast_locators.empty())
{
fastdds::rtps::LocatorList locators;
fastrtps::rtps::IPFinder::getIP4Address(&locators);
fastdds::rtps::ExternalLocatorsProcessor::add_external_locators(locators,
PParam.builtin.metatraffic_external_unicast_locators);
uint16_t host_id = Host::compute_id(locators);
guidP.value[2] = static_cast<octet>(host_id & 0xFF);
guidP.value[3] = static_cast<octet>((host_id >> 8) & 0xFF);
}