From 88dbba1d3c7587703ed016c99ea787ef047d4885 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Wed, 12 Aug 2026 13:23:54 -0500 Subject: [PATCH] refactor(rtps_embedded): De-template the engine (Phase 3) Co-Authored-By: Claude Opus 4.8 (1M context) --- components/rtps_embedded/CMakeLists.txt | 3 ++ .../include/rtps/entities/StatefulReader.hpp | 14 ++--- .../include/rtps/entities/StatefulWriter.hpp | 13 ++--- .../include/rtps/entities/StatelessWriter.hpp | 14 ++--- .../entities/StatefulReader.cpp} | 26 ++++------ .../entities/StatefulWriter.cpp} | 52 ++++++++----------- .../entities/StatelessWriter.cpp} | 38 ++++++-------- lib/espp.cmake | 3 ++ 8 files changed, 69 insertions(+), 94 deletions(-) rename components/rtps_embedded/{include/rtps/entities/StatefulReader.tpp => src/entities/StatefulReader.cpp} (90%) rename components/rtps_embedded/{include/rtps/entities/StatefulWriter.tpp => src/entities/StatefulWriter.cpp} (89%) rename components/rtps_embedded/{include/rtps/entities/StatelessWriter.tpp => src/entities/StatelessWriter.cpp} (82%) diff --git a/components/rtps_embedded/CMakeLists.txt b/components/rtps_embedded/CMakeLists.txt index c4d0b3156..bc784c654 100644 --- a/components/rtps_embedded/CMakeLists.txt +++ b/components/rtps_embedded/CMakeLists.txt @@ -9,7 +9,10 @@ idf_component_register( "src/entities/Domain.cpp" "src/entities/Participant.cpp" "src/entities/Reader.cpp" + "src/entities/StatefulReader.cpp" + "src/entities/StatefulWriter.cpp" "src/entities/StatelessReader.cpp" + "src/entities/StatelessWriter.cpp" "src/entities/Writer.cpp" "src/messages/MessageReceiver.cpp" "src/messages/MessageTypes.cpp" diff --git a/components/rtps_embedded/include/rtps/entities/StatefulReader.hpp b/components/rtps_embedded/include/rtps/entities/StatefulReader.hpp index cef1c9479..800715bb7 100644 --- a/components/rtps_embedded/include/rtps/entities/StatefulReader.hpp +++ b/components/rtps_embedded/include/rtps/entities/StatefulReader.hpp @@ -37,13 +37,13 @@ namespace rtps { class EsppTransport; struct SubmessageHeartbeat; -template class StatefulReaderT final : public Reader { +class StatefulReader final : public Reader { public: - StatefulReaderT() + StatefulReader() : m_srcPort(0) , m_transport(nullptr) {} - ~StatefulReaderT() override; - bool init(const TopicData &attributes, NetworkDriver &driver); + ~StatefulReader() override; + bool init(const TopicData &attributes, EsppTransport &driver); void newChange(const ReaderCacheChange &cacheChange) override; bool addNewMatchedWriter(const WriterProxy &newProxy) override; bool onNewHeartbeat(const SubmessageHeartbeat &msg, const GuidPrefix_t &remotePrefix) override; @@ -53,13 +53,9 @@ template class StatefulReaderT final : public Reader { private: Ip4Port_t m_srcPort; // TODO intended for reuse but buffer not used as such - NetworkDriver *m_transport; + EsppTransport *m_transport; }; -using StatefulReader = StatefulReaderT; - } // namespace rtps -#include "StatefulReader.tpp" - #endif // RTPS_STATEFULREADER_H diff --git a/components/rtps_embedded/include/rtps/entities/StatefulWriter.hpp b/components/rtps_embedded/include/rtps/entities/StatefulWriter.hpp index c99be1586..e972127ce 100644 --- a/components/rtps_embedded/include/rtps/entities/StatefulWriter.hpp +++ b/components/rtps_embedded/include/rtps/entities/StatefulWriter.hpp @@ -41,12 +41,12 @@ namespace rtps { class EsppTransport; -template class StatefulWriterT final : public Writer { +class StatefulWriter final : public Writer { public: - StatefulWriterT() + StatefulWriter() : m_transport(nullptr) {} - ~StatefulWriterT() override; - bool init(TopicData attributes, TopicKind_t topicKind, NetworkDriver &driver, + ~StatefulWriter() override; + bool init(TopicData attributes, TopicKind_t topicKind, EsppTransport &driver, bool enfUnicast = false); //! Executes required steps like sending packets. Intended to be called by @@ -76,7 +76,7 @@ template class StatefulWriterT final : public Writer { void updateChangeKind(SequenceNumber_t &sequence_number); private: - NetworkDriver *m_transport = nullptr; + EsppTransport *m_transport = nullptr; HistoryCacheWithDeletion m_history; @@ -103,9 +103,6 @@ template class StatefulWriterT final : public Writer { const SequenceNumber_t &nextValid); }; -using StatefulWriter = StatefulWriterT; } // namespace rtps -#include "StatefulWriter.tpp" - #endif // RTPS_STATEFULWRITER_H diff --git a/components/rtps_embedded/include/rtps/entities/StatelessWriter.hpp b/components/rtps_embedded/include/rtps/entities/StatelessWriter.hpp index abf86c567..ba1b1d344 100644 --- a/components/rtps_embedded/include/rtps/entities/StatelessWriter.hpp +++ b/components/rtps_embedded/include/rtps/entities/StatelessWriter.hpp @@ -36,12 +36,12 @@ namespace rtps { class EsppTransport; -template class StatelessWriterT : public Writer { +class StatelessWriter : public Writer { public: - StatelessWriterT() + StatelessWriter() : m_transport(nullptr) {} - ~StatelessWriterT() override; - bool init(TopicData attributes, TopicKind_t topicKind, NetworkDriver &driver, + ~StatelessWriter() override; + bool init(TopicData attributes, TopicKind_t topicKind, EsppTransport &driver, bool enfUnicast = false); void progress() override; @@ -55,15 +55,11 @@ template class StatelessWriterT : public Writer { void reset() override; private: - NetworkDriver *m_transport; + EsppTransport *m_transport; SimpleHistoryCache m_history; }; -using StatelessWriter = StatelessWriterT; - } // namespace rtps -#include "StatelessWriter.tpp" - #endif // RTPS_RTPSWRITER_H diff --git a/components/rtps_embedded/include/rtps/entities/StatefulReader.tpp b/components/rtps_embedded/src/entities/StatefulReader.cpp similarity index 90% rename from components/rtps_embedded/include/rtps/entities/StatefulReader.tpp rename to components/rtps_embedded/src/entities/StatefulReader.cpp index 96c573419..f88e18a73 100644 --- a/components/rtps_embedded/include/rtps/entities/StatefulReader.tpp +++ b/components/rtps_embedded/src/entities/StatefulReader.cpp @@ -24,6 +24,7 @@ Author: i11 - Embedded Software, RWTH Aachen University */ #include "rtps/entities/StatefulReader.hpp" +#include "rtps/communication/EsppTransport.hpp" #include "rtps/messages/MessageFactory.hpp" #include "rtps/storages/PayloadBuffer.hpp" #include "rtps/utils/Diagnostics.hpp" @@ -45,16 +46,15 @@ using rtps::PacketInfo; using rtps::ReaderCacheChange; using rtps::SequenceNumber_t; using rtps::SequenceNumberSet; -using rtps::StatefulReaderT; +using rtps::StatefulReader; using rtps::SubmessageGap; using rtps::SubmessageHeartbeat; using rtps::TopicData; using rtps::WriterProxy; -template StatefulReaderT::~StatefulReaderT() {} +StatefulReader::~StatefulReader() {} -template -bool StatefulReaderT::init(const TopicData &attributes, NetworkDriver &driver) { +bool StatefulReader::init(const TopicData &attributes, EsppTransport &driver) { if (!initMutex()) { return false; } @@ -67,8 +67,7 @@ bool StatefulReaderT::init(const TopicData &attributes, NetworkDr return true; } -template -void StatefulReaderT::newChange(const ReaderCacheChange &cacheChange) { +void StatefulReader::newChange(const ReaderCacheChange &cacheChange) { if (m_callback_count == 0 || !m_is_initialized_) { return; } @@ -96,17 +95,14 @@ void StatefulReaderT::newChange(const ReaderCacheChange &cacheCha } } -template -bool StatefulReaderT::addNewMatchedWriter(const WriterProxy &newProxy) { +bool StatefulReader::addNewMatchedWriter(const WriterProxy &newProxy) { #if SFR_VERBOSE && RTPS_GLOBAL_VERBOSE SFR_LOG("New writer added"); #endif return m_proxies.add(newProxy); } -template -bool StatefulReaderT::onNewGapMessage(const SubmessageGap &msg, - const GuidPrefix_t &remotePrefix) { +bool StatefulReader::onNewGapMessage(const SubmessageGap &msg, const GuidPrefix_t &remotePrefix) { std::lock_guard lock(m_proxies_mutex); if (!m_is_initialized_) { return false; @@ -204,9 +200,8 @@ bool StatefulReaderT::onNewGapMessage(const SubmessageGap &msg, } } -template -bool StatefulReaderT::onNewHeartbeat(const SubmessageHeartbeat &msg, - const GuidPrefix_t &sourceGuidPrefix) { +bool StatefulReader::onNewHeartbeat(const SubmessageHeartbeat &msg, + const GuidPrefix_t &sourceGuidPrefix) { std::lock_guard lock(m_proxies_mutex); if (!m_is_initialized_) { return false; @@ -251,8 +246,7 @@ bool StatefulReaderT::onNewHeartbeat(const SubmessageHeartbeat &m return true; } -template -bool StatefulReaderT::sendPreemptiveAckNack(const WriterProxy &writer) { +bool StatefulReader::sendPreemptiveAckNack(const WriterProxy &writer) { std::lock_guard lock(m_proxies_mutex); if (!m_is_initialized_) { return false; diff --git a/components/rtps_embedded/include/rtps/entities/StatefulWriter.tpp b/components/rtps_embedded/src/entities/StatefulWriter.cpp similarity index 89% rename from components/rtps_embedded/include/rtps/entities/StatefulWriter.tpp rename to components/rtps_embedded/src/entities/StatefulWriter.cpp index 45513cabc..b77f7cd7f 100644 --- a/components/rtps_embedded/include/rtps/entities/StatefulWriter.tpp +++ b/components/rtps_embedded/src/entities/StatefulWriter.cpp @@ -24,6 +24,7 @@ Author: i11 - Embedded Software, RWTH Aachen University */ #include "rtps/entities/StatefulWriter.hpp" +#include "rtps/communication/EsppTransport.hpp" #include "rtps/messages/MessageFactory.hpp" #include "rtps/messages/MessageTypes.hpp" #include "rtps/storages/PayloadBuffer.hpp" @@ -39,7 +40,7 @@ using rtps::CacheChange; using rtps::GuidPrefix_t; using rtps::ReaderProxy; using rtps::SequenceNumber_t; -using rtps::StatefulWriterT; +using rtps::StatefulWriter; using rtps::SubmessageAckNack; #if SFW_VERBOSE && RTPS_GLOBAL_VERBOSE @@ -51,12 +52,10 @@ using rtps::SubmessageAckNack; } while (0) #endif -template StatefulWriterT::~StatefulWriterT() = default; +StatefulWriter::~StatefulWriter() = default; -template -bool StatefulWriterT::init(TopicData attributes, TopicKind_t topicKind, - NetworkDriver &driver, - bool enfUnicast) { +bool StatefulWriter::init(TopicData attributes, TopicKind_t topicKind, EsppTransport &driver, + bool enfUnicast) { m_attributes = attributes; @@ -81,15 +80,14 @@ bool StatefulWriterT::init(TopicData attributes, TopicKind_t topi return true; } -template void StatefulWriterT::reset() { +void StatefulWriter::reset() { m_is_initialized_ = false; // TODO } -template -const rtps::CacheChange * -StatefulWriterT::newChange(ChangeKind_t kind, const uint8_t *data, DataSize_t size, - bool inLineQoS, bool markDisposedAfterWrite) { +const rtps::CacheChange *StatefulWriter::newChange(ChangeKind_t kind, const uint8_t *data, + DataSize_t size, bool inLineQoS, + bool markDisposedAfterWrite) { INIT_GUARD() if (isIrrelevant(kind)) { return nullptr; @@ -129,7 +127,7 @@ StatefulWriterT::newChange(ChangeKind_t kind, const uint8_t *data return result; } -template void StatefulWriterT::progress() { +void StatefulWriter::progress() { INIT_GUARD() std::lock_guard lock(m_mutex); CacheChange *next = m_history.getChangeBySN(m_nextSequenceNumberToSend); @@ -177,7 +175,7 @@ template void StatefulWriterT::progress() { } } -template void StatefulWriterT::setAllChangesToUnsent() { +void StatefulWriter::setAllChangesToUnsent() { INIT_GUARD() std::lock_guard lock(m_mutex); @@ -197,9 +195,8 @@ template void StatefulWriterT::setAllChange } } -template -void StatefulWriterT::onNewAckNack(const SubmessageAckNack &msg, - const GuidPrefix_t &sourceGuidPrefix) { +void StatefulWriter::onNewAckNack(const SubmessageAckNack &msg, + const GuidPrefix_t &sourceGuidPrefix) { INIT_GUARD() std::lock_guard lock(m_mutex); if (!m_is_initialized_) { @@ -301,14 +298,12 @@ void StatefulWriterT::onNewAckNack(const SubmessageAckNack &msg, } } -template -bool rtps::StatefulWriterT::removeFromHistory(const SequenceNumber_t &s) { +bool rtps::StatefulWriter::removeFromHistory(const SequenceNumber_t &s) { std::lock_guard lock(m_mutex); return m_history.dropChange(s); } -template -bool StatefulWriterT::sendData(const ReaderProxy &reader, const CacheChange *next) { +bool StatefulWriter::sendData(const ReaderProxy &reader, const CacheChange *next) { INIT_GUARD() // TODO smarter packaging, e.g. create a message struct and serialize once. @@ -337,10 +332,8 @@ bool StatefulWriterT::sendData(const ReaderProxy &reader, const C return true; } -template -void StatefulWriterT::sendGap(const ReaderProxy &reader, - const SequenceNumber_t &firstMissing, - const SequenceNumber_t &nextValid) { +void StatefulWriter::sendGap(const ReaderProxy &reader, const SequenceNumber_t &firstMissing, + const SequenceNumber_t &nextValid) { INIT_GUARD() // TODO smarter packaging, e.g. create a message struct and serialize once. @@ -366,9 +359,7 @@ void StatefulWriterT::sendGap(const ReaderProxy &reader, m_transport->sendPacket(info); } -template -bool StatefulWriterT::sendDataWRMulticast(const ReaderProxy &reader, - const CacheChange *next) { +bool StatefulWriter::sendDataWRMulticast(const ReaderProxy &reader, const CacheChange *next) { INIT_GUARD() if (reader.useMulticast || reader.suppressUnicast == false) { @@ -409,9 +400,8 @@ bool StatefulWriterT::sendDataWRMulticast(const ReaderProxy &read return true; } -template std::chrono::steady_clock::time_point -StatefulWriterT::heartbeatTick(std::chrono::steady_clock::time_point now) { +StatefulWriter::heartbeatTick(std::chrono::steady_clock::time_point now) { if (!m_is_initialized_) { // Not ticking: report a far-future deadline so the scheduler ignores us. return now + std::chrono::hours(24); @@ -439,7 +429,7 @@ StatefulWriterT::heartbeatTick(std::chrono::steady_clock::time_po return m_nextHeartbeat; } -template void StatefulWriterT::dropDisposeAfterWriteChanges() { +void StatefulWriter::dropDisposeAfterWriteChanges() { SequenceNumber_t oldest_retained; while (m_disposeWithDelay.peakFirst(oldest_retained)) { @@ -472,7 +462,7 @@ template void StatefulWriterT::dropDisposeA } } -template void StatefulWriterT::sendHeartBeat() { +void StatefulWriter::sendHeartBeat() { INIT_GUARD() if (m_proxies.isEmpty() || !m_is_initialized_) { diff --git a/components/rtps_embedded/include/rtps/entities/StatelessWriter.tpp b/components/rtps_embedded/src/entities/StatelessWriter.cpp similarity index 82% rename from components/rtps_embedded/include/rtps/entities/StatelessWriter.tpp rename to components/rtps_embedded/src/entities/StatelessWriter.cpp index 63eafc9b2..26bfa9bf0 100644 --- a/components/rtps_embedded/include/rtps/entities/StatelessWriter.tpp +++ b/components/rtps_embedded/src/entities/StatelessWriter.cpp @@ -23,9 +23,13 @@ This file is part of embeddedRTPS. Author: i11 - Embedded Software, RWTH Aachen University */ +#include "rtps/entities/StatelessWriter.hpp" + #include #include +#include "rtps/communication/EsppTransport.hpp" +#include "rtps/communication/PacketInfo.hpp" #include "rtps/messages/MessageFactory.hpp" #include "rtps/storages/PayloadBuffer.hpp" #include "rtps/utils/Log.hpp" @@ -35,7 +39,7 @@ Author: i11 - Embedded Software, RWTH Aachen University using rtps::CacheChange; using rtps::GuidPrefix_t; using rtps::SequenceNumber_t; -using rtps::StatelessWriterT; +using rtps::StatelessWriter; using rtps::SubmessageAckNack; #if SLW_VERBOSE && RTPS_GLOBAL_VERBOSE @@ -47,16 +51,14 @@ using rtps::SubmessageAckNack; } while (0) #endif -template StatelessWriterT::~StatelessWriterT() { +StatelessWriter::~StatelessWriter() { // if(sys_mutex_valid(&m_mutex)){ // sys_mutex_free(&m_mutex); // } } -template -bool StatelessWriterT::init(TopicData attributes, TopicKind_t topicKind, - NetworkDriver &driver, - bool enfUnicast) { +bool StatelessWriter::init(TopicData attributes, TopicKind_t topicKind, EsppTransport &driver, + bool enfUnicast) { m_attributes = attributes; @@ -75,15 +77,11 @@ bool StatelessWriterT::init(TopicData attributes, TopicKind_t top return true; } -template void StatelessWriterT::reset() { - m_is_initialized_ = false; -} +void StatelessWriter::reset() { m_is_initialized_ = false; } -template -const CacheChange *StatelessWriterT::newChange(rtps::ChangeKind_t kind, - const uint8_t *data, DataSize_t size, - bool inLineQoS, - bool markDisposedAfterWrite) { +const CacheChange *StatelessWriter::newChange(rtps::ChangeKind_t kind, const uint8_t *data, + DataSize_t size, bool inLineQoS, + bool markDisposedAfterWrite) { INIT_GUARD(); if (isIrrelevant(kind)) { return nullptr; @@ -112,13 +110,12 @@ const CacheChange *StatelessWriterT::newChange(rtps::ChangeKind_t return result; } -template -bool StatelessWriterT::removeFromHistory(const SequenceNumber_t &s) { +bool StatelessWriter::removeFromHistory(const SequenceNumber_t &s) { return false; // Stateless Writers currently do not support deletion from // history } -template void StatelessWriterT::setAllChangesToUnsent() { +void StatelessWriter::setAllChangesToUnsent() { INIT_GUARD(); std::lock_guard lock(m_mutex); @@ -131,14 +128,13 @@ template void StatelessWriterT::setAllCh } } -template -void StatelessWriterT::onNewAckNack(const SubmessageAckNack & /*msg*/, - const GuidPrefix_t &sourceGuidPrefix) { +void StatelessWriter::onNewAckNack(const SubmessageAckNack & /*msg*/, + const GuidPrefix_t &sourceGuidPrefix) { INIT_GUARD(); // Too lazy to respond } -template void StatelessWriterT::progress() { +void StatelessWriter::progress() { INIT_GUARD(); // TODO smarter packaging e.g. by creating MessageStruct and serializing // after adjusting values. diff --git a/lib/espp.cmake b/lib/espp.cmake index e66e38d77..3e4c1f73c 100644 --- a/lib/espp.cmake +++ b/lib/espp.cmake @@ -68,7 +68,10 @@ set(ESPP_SOURCES ${ESPP_COMPONENTS}/rtps_embedded/src/entities/Domain.cpp ${ESPP_COMPONENTS}/rtps_embedded/src/entities/Participant.cpp ${ESPP_COMPONENTS}/rtps_embedded/src/entities/Reader.cpp + ${ESPP_COMPONENTS}/rtps_embedded/src/entities/StatefulReader.cpp + ${ESPP_COMPONENTS}/rtps_embedded/src/entities/StatefulWriter.cpp ${ESPP_COMPONENTS}/rtps_embedded/src/entities/StatelessReader.cpp + ${ESPP_COMPONENTS}/rtps_embedded/src/entities/StatelessWriter.cpp ${ESPP_COMPONENTS}/rtps_embedded/src/entities/Writer.cpp ${ESPP_COMPONENTS}/rtps_embedded/src/messages/MessageReceiver.cpp ${ESPP_COMPONENTS}/rtps_embedded/src/messages/MessageTypes.cpp