创建RTPSWriter[0.1.0.c2]
RTPSWriter* wout = nullptr;
//创建writer
if (mp_RTPSParticipant->createWriter(&wout, watt,//mp_history->mp_writer = this;
endpoints->writer.payload_pool_, endpoints->writer.history_.get(),nullptr,
c_EntityId_SPDPWriter, true))
{
//存入容器
endpoints->writer.writer_ = dynamic_cast<StatelessWriter*>(wout);
}
与创建RTPSReader过程基本一致:
生成guid 0.1.0.c2
构建RTPSReader,实际是StatelessWriter
存入RTPSParticipantImpl::vector<RTPSWriter*> m_allWriterList
创建SendResources
writer并不存入associated_writers_
WriterProxy
initEDP
mp_EDP = new EDPSimple(this, mp_RTPSParticipant);
if (!mp_EDP->initEDP(m_discovery))
{
delete mp_EDP;
mp_EDP = nullptr;
return false;
}
主要做了一件事,创建EDP endpoint:
bool EDPSimple::initEDP(
BuiltinAttributes& attributes)
{
m_discovery = attributes;
if (!createSEDPEndpoints())
{
return false;
}
return true;
}
重点是createSEDPEndpoints(),创建了2个RTPSReader , 2个RTPSWriter,并且设置mp_listener为publications_listener_ , subscriptions_listener_。
Publications writer[0.0.3.c2]:pub端发送给0.0.3.c7,会发送pub端writer[0.0.1.3]信息
Publications reader[0.0.3.c7]:sub端监听0.0.3.c2
Subscriptions writer[0.0.4.c2]:sub端发送端口,发给0.0.4.c7, 会发送sub端reader[0.0.1.4]信息
Subscriptions reader[0.0.4.c7]:pub端监听端口,监听0.0.4.c2
createSEDPEndpoints()
创建RTPSReader , RTPSWriter
bool EDPSimple::createSEDPEndpoints()
{
WriterAttributes watt;
ReaderAttributes ratt;
HistoryAttributes reader_history_att;
HistoryAttributes writer_history_att;
set_builtin_reader_history_attributes(reader_history_att);
set_builtin_writer_history_attributes(writer_history_att);
set_builtin_reader_attributes(ratt);
set_builtin_writer_attributes(watt);
publications_listener_ = new EDPSimplePUBListener(this);
subscriptions_listener_ = new EDPSimpleSUBListener(this);
if (m_discovery.discovery_config.m_simpleEDP.use_PublicationWriterANDSubscriptionReader)
{
//ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER
//创建一个 DCPS Publications writer 0.0.3.c2 存入publications_writer_
if (!EDPUtils::create_edp_writer(mp_RTPSParticipant, "DCPSPublications", c_EntityId_SEDPPubWriter,writer_history_att, watt, publications_listener_, pub_writer_payload_pool_, publications_writer_))
{
return false;
}
//ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER
//创建一个 DCPS Subscriptions reader 0.0.4.c7 存入subscriptions_readers_
if (!EDPUtils::create_edp_reader(mp_RTPSParticipant, "DCPSSubscriptions", c_EntityId_SEDPSubReader,reader_history_att, ratt, subscriptions_listener_, sub_reader_payload_pool_, subscriptions_reader_))
{
return false;
}
}
if (m_discovery.discovery_config.m_simpleEDP.use_PublicationReaderANDSubscriptionWriter)
{
//ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER
//创建一个 DCPS Publications reader 0.0.3.c7 存入associated_readers_
if (!EDPUtils::create_edp_reader(mp_RTPSParticipant, "DCPSPublications", c_EntityId_SEDPPubReader,reader_history_att, ratt, publications_listener_, pub_reader_payload_pool_, publications_reader_))
{
return false;
}
//ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER
//创建一个 DCPS Subscriptions writer 0.0.4.c2 存入associated_writers_
if (!EDPUtils::create_edp_writer(mp_RTPSParticipant, "DCPSSubscriptions", c_EntityId_SEDPSubWriter,writer_history_att, watt, subscriptions_listener_, sub_writer_payload_pool_, subscriptions_writer_))
{
return false;
}
}
return true;
以创建 DCPS Publications writer为例:
static bool create_edp_writer(
RTPSParticipantImpl* participant,
const std::string& topic_name,
const EntityId_t& entity_id,
const HistoryAttributes& history_att,
WriterAttributes& watt,
WriterListener* listener,
std::shared_ptr<ITopicPayloadPool>& payload_pool,
WriterHistoryPair& edp_writer)
{
RTPSWriter* waux = nullptr;
//创建 payload_pool
payload_pool = create_payload_pool(topic_name, history_att, false);
//构建 WriterHistory ,(*reader)->history_ = hist;
edp_writer.second = new WriterHistory(history_att);
//创建 StatefulWriter
bool created =
participant->createWriter(&waux, watt, payload_pool, edp_writer.second, listener, entity_id,true);
if (created)
{
// StatefulWriter 作为 publications_writer_ 的 first 成员
edp_writer.first = dynamic_cast<StatefulWriter*>(waux);
}
return created;
}
};
RTPSParticipantImpl->enable()
void RTPSParticipantImpl::enable()
{
mp_builtinProtocols->enable();
//从列表中取出receiver
for (auto& receiver : m_receiverResourcelist)
{
//进行注册
receiver.Receiver->RegisterReceiver(receiver.mp_receiver);
}
}
mp_builtinProtocols->enable();
void BuiltinProtocols::enable()
{
if (nullptr != mp_PDP)
{
mp_PDP->enable();
//广播participant这一层的信息
mp_PDP->announceParticipantState(true);
mp_PDP->resetParticipantAnnouncement();
}
}
announceParticipantState分析:
该方法会进行一次广播,下面分析具体过程:
void PDPSimple::announceParticipantState(
bool new_change,
bool dispose,
WriteParams& wp)
{
if (enabled_)
{
auto endpoints = //包含 RTPSreader RTPSwriter static_cast<fastdds::rtps::SimplePDPEndpoints*>(builtin_endpoints_.get());
//获取PDP的writer
StatelessWriter& writer = *(endpoints->writer.writer_);
//获取PDP的history
WriterHistory& history = *(endpoints->writer.history_);
//广播PDP信息
PDP::announceParticipantState(writer, history, new_change, dispose, wp);
//循环广播
if (!(dispose || new_change))
{
writer.unsent_changes_reset();
}
}
}
广播的具体过程:
void PDP::announceParticipantState(
RTPSWriter& writer,
WriterHistory& history,
bool new_change,
bool dispose,
WriteParams& wparams)
{
if (enabled_)
{
CacheChange_t* change = nullptr;
//dispose = false 走if
if (!dispose)
{
if (m_hasChangedLocalPDP.exchange(false) || new_change)
{
this->mp_mutex->lock();
ParticipantProxyData* local_participant_data = getLocalParticipantProxyData();
InstanceHandle_t key = local_participant_data->m_key;
ParticipantProxyData proxy_data_copy(*local_participant_data);
this->mp_mutex->unlock();
if (history.getHistorySize() > 0)
{
history.remove_min_change();
}
uint32_t cdr_size = proxy_data_copy.get_serialized_size(true);
//创建 new change->writerGUID =
01.0f.65.91.0d.11.62.6e.00.00.00.00|0.1.0.c2
change = writer.new_change(
[cdr_size]() -> uint32_t
{
return cdr_size;
},
ALIVE, key);
if (change != nullptr)
{
CDRMessage_t aux_msg(change->serializedPayload);
change->serializedPayload.encapsulation = (uint16_t)PL_CDR_LE;
//设置大小端
aux_msg.msg_endian = LITTLEEND;
//设置 msg 缓冲区等
if (proxy_data_copy.writeToCDRMessage(&aux_msg, true))
{
//设置payload length = 400
change->serializedPayload.length = (uint16_t)aux_msg.length;
//通过 WriterHistory 通知
history.add_change(change, wparams);
}
else
{
}
}
}
}
else
{
......
}
}
}
add_change分析:
bool WriterHistory::add_change_(
CacheChange_t* a_change,
WriteParams& wparams,
std::chrono::time_point<std::chrono::steady_clock> max_blocking_time)
{
std::lock_guard<RecursiveTimedMutex> guard(*mp_mutex);
//填充 change
if (!prepare_and_add_change(a_change, wparams))
{
return false;
}
//通过RTPSWriter发送
notify_writer(a_change, max_blocking_time);
return true;
}
填充change
bool WriterHistory::prepare_and_add_change(
CacheChange_t* a_change,
WriteParams& wparams)
{
//如果 GUID不一致,则返回false
if (a_change->writerGUID != mp_writer->getGuid())
{
return false;
}
++m_lastCacheChangeSeqNum;
a_change->sequenceNumber = m_lastCacheChangeSeqNum;
if (wparams.source_timestamp().seconds() < 0)
{
Time_t::now(a_change->sourceTimestamp);
}
else
{
//设置时间戳
a_change->sourceTimestamp = wparams.source_timestamp();
}
a_change->writer_info.num_sent_submessages = 0;
a_change->write_params = wparams;
// Updated sample and related sample identities on the user's write params
//设置writerGUID
wparams.sample_identity().writer_guid(a_change->writerGUID);
//设置序列号
wparams.sample_identity().sequence_number(a_change->sequenceNumber);
//设置 sample_identity_
wparams.related_sample_identity(wparams.sample_identity());
set_fragments(a_change);
//存入容器m_changes
m_changes.push_back(a_change);
if (static_cast<int32_t>(m_changes.size()) == m_att.maximumReservedCaches)
{
m_isHistoryFull = true;
}
return true;
}
通知writer发送:
void WriterHistory::notify_writer(CacheChange_t* a_change,
const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time)
{
//调用StatelessWriter发送
mp_writer->unsent_change_added_to_history(a_change, max_blocking_time);
}