create_publisher过程
应用层代码如下:
PublisherQos pubqos = PUBLISHER_QOS_DEFAULT;
//创建publisher
publisher_ = participant_->create_publisher(pubqos,nullptr);
Publisher* DomainParticipant::create_publisher(
const PublisherQos& qos,
PublisherListener* listener,
const StatusMask& mask)
{
return impl_->create_publisher(qos, listener, mask);
}
会调用DomainParticipantImpl::create_publisher:
Publisher* DomainParticipantImpl::create_publisher(
const PublisherQos& qos,
PublisherImpl** impl,
PublisherListener* listener,
const StatusMask& mask)
{
//这里listener为nullptr,new一个PublisherImpl
PublisherImpl* pubimpl = create_publisher_impl(qos, listener);
//new一个Publisher
Publisher* pub = new Publisher(pubimpl, mask);
pubimpl->user_publisher_ = pub;
pubimpl->rtps_participant_ = get_rtps_participant();
bool enabled = get_rtps_participant() != nullptr;
// Create InstanceHandle for the new publisher
InstanceHandle_t pub_handle;
//设置guid 01.0f.65.91.8c.0d.fb.19.00.00.00.00|0.0.1.1
//这里guid_.entityId还未设置,在create_writer中设置
create_instance_handle(pub_handle);
pubimpl->handle_ = pub_handle;
//SAVE THE PUBLISHER INTO MAPS
std::lock_guard<std::mutex> lock(mtx_pubs_);
publishers_by_handle_[pub_handle] = pub;
//键值对关系
publishers_[pub] = pubimpl;
// Enable publisher if appropriate
if (enabled && qos_.entity_factory().autoenable_created_entities)
{
//enable()调用writer->enable(),但还未创建writer
ReturnCode_t ret_publisher_enable = pub->enable();
assert(ReturnCode_t::RETCODE_OK == ret_publisher_enable);
(void)ret_publisher_enable;
}
if (impl)
{
*impl = pubimpl;
}
return pub;
}
create_topic过程
Topic* DomainParticipantImpl::create_topic(
const std::string& topic_name,
const std::string& type_name,
const TopicQos& qos,
TopicListener* listener,
const StatusMask& mask)
{
//Look for the correct type registration
TypeSupport type_support = find_type(type_name);
if (type_support.empty())
{
EPROSIMA_LOG_ERROR(PARTICIPANT, "Type : " << type_name << " Not Registered");
return nullptr;
}
if (!TopicImpl::check_qos_including_resource_limits(qos, type_support))
{
return nullptr;
}
bool enabled = get_rtps_participant() != nullptr;
std::lock_guard<std::mutex> lock(mtx_topics_);
// Check there is no Topic with the same name
if ((topics_.find(topic_name) != topics_.end()) ||
(filtered_topics_.find(topic_name) != filtered_topics_.end()))
{
return nullptr;
}
InstanceHandle_t topic_handle;
//设置guid
create_instance_handle(topic_handle);
//new 一个 TopicProxyFactory
TopicProxyFactory* factory = new TopicProxyFactory(this, topic_name, type_name, mask, type_support, qos, listener);
//创建topic
TopicProxy* proxy = factory->create_topic();
Topic* topic = proxy->get_topic();
topic->set_instance_handle(topic_handle);
//SAVE THE TOPIC INTO MAPS
topics_by_handle_[topic_handle] = topic;
topics_[topic_name] = factory;
// Enable topic if appropriate
if (enabled && qos_.entity_factory().autoenable_created_entities)
{
ReturnCode_t ret_topic_enable = topic->enable();
assert(ReturnCode_t::RETCODE_OK == ret_topic_enable);
(void)ret_topic_enable;
}
cond_topics_.notify_all();
return topic;
}
create_datawriter过程
DataWriter* create_datawriter(
Topic* topic,
const DataWriterQos& qos,
DataWriterListener* listener = nullptr,
const StatusMask& mask = StatusMask::all())
{
if (create_datawriter_mock())
{
return nullptr;
}
//PublisherImpl::create_datawriter
return impl_->create_datawriter(topic, qos, listener, mask);
}
调用PublisherImpl::create_datawriter:
DataWriter* PublisherImpl::create_datawriter(
Topic* topic,
const DataWriterQos& qos,
DataWriterListener* listener,
const StatusMask& mask)
{
EPROSIMA_LOG_INFO(PUBLISHER, "CREATING WRITER IN TOPIC: " << topic->get_name());
//Look for the correct type registration
//在APP init()::type_.register_type时注册
TypeSupport type_support = participant_->find_type(topic->get_type_name());
/// Preconditions
// Check the type was registered.
if (type_support.empty())
{
EPROSIMA_LOG_ERROR(PUBLISHER, "Type: " << topic->get_type_name() << " Not Registered");
return nullptr;
}
if (!DataWriterImpl::check_qos_including_resource_limits(qos, type_support))
{
return nullptr;
}
//new 一个 DataWriterImpl
DataWriterImpl* impl = create_datawriter_impl(type_support, topic, qos, listener);
return create_datawriter(topic, impl, mask);
}
create_datawriter_impl创建new DataWriterImpl中计算guid_:
[0.0.1.3]
{
//guid_.entityId = 0.0.1.3
fastrtps::rtps::RTPSParticipantImpl::preprocess_endpoint_attributes<WRITER, 0x03, 0x02>(EntityId_t::unknown(), publisher_->get_participant_impl()->id_counter(), endpoint_attributes, guid_.entityId);
guid_.guidPrefix = publisher_->get_participant_impl()->guid().guidPrefix;
}
DataWriter* PublisherImpl::create_datawriter(
Topic* topic,
DataWriterImpl* impl,
const StatusMask& mask)
{
topic->get_impl()->reference();
DataWriter* writer = new DataWriter(impl, mask);
impl->user_datawriter_ = writer;
{
std::lock_guard<std::mutex> lock(mtx_writers_);
//DataWriterImpl与 topic建立关联
writers_[topic->get_name()].push_back(impl);
}
if (user_publisher_->is_enabled() && qos_.entity_factory().autoenable_created_entities)
{
//执行DataWriter enable()
if (ReturnCode_t::RETCODE_OK != writer->enable())
{
delete_datawriter(writer);
return nullptr;
}
}
return writer;
}
执行重点函数DataWriterImpl::enable()