Coverage Report

Created: 2026-07-25 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Fast-DDS/src/cpp/rtps/writer/StatelessWriter.cpp
Line
Count
Source
1
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
/*
16
 * @file StatelessWriter.cpp
17
 *
18
 */
19
20
#include "StatelessWriter.hpp"
21
22
#include <algorithm>
23
#include <mutex>
24
#include <set>
25
#include <vector>
26
27
#include <fastdds/dds/log/Log.hpp>
28
#include <fastdds/rtps/builtin/data/SubscriptionBuiltinTopicData.hpp>
29
#include <fastdds/rtps/history/WriterHistory.hpp>
30
#include <fastdds/rtps/reader/ReaderDiscoveryStatus.hpp>
31
#include <fastdds/rtps/reader/RTPSReader.hpp>
32
#include <fastdds/rtps/writer/WriterListener.hpp>
33
34
#include "../flowcontrol/FlowController.hpp"
35
#include <rtps/builtin/BuiltinProtocols.h>
36
#include <rtps/builtin/liveliness/WLP.hpp>
37
#include <rtps/DataSharing/DataSharingNotifier.hpp>
38
#include <rtps/DataSharing/DataSharingPayloadPool.hpp>
39
#include <rtps/DataSharing/WriterPool.hpp>
40
#include <rtps/history/BasicPayloadPool.hpp>
41
#include <rtps/history/CacheChangePool.h>
42
#include <rtps/history/HistoryAttributesExtension.hpp>
43
#include <rtps/messages/RTPSMessageGroup.hpp>
44
#include <rtps/network/utils/external_locators.hpp>
45
#include <rtps/participant/RTPSParticipantImpl.hpp>
46
#include <rtps/reader/BaseReader.hpp>
47
#include <rtps/domain/RTPSDomainImpl.hpp>
48
#include <rtps/writer/BaseWriter.hpp>
49
50
namespace eprosima {
51
namespace fastdds {
52
namespace rtps {
53
54
/**
55
 * Loops over all the readers in the vector, applying the given routine.
56
 * The loop continues until the result of the routine is true for any reader
57
 * or all readers have been processes.
58
 * The returned value is true if the routine returned true at any point,
59
 * or false otherwise.
60
 */
61
bool for_matched_readers(
62
        ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_1,
63
        std::function<bool(ReaderLocator&)> fun)
64
0
{
65
0
    for (auto& remote_reader : reader_vector_1)
66
0
    {
67
0
        if (fun(*remote_reader))
68
0
        {
69
0
            return true;
70
0
        }
71
0
    }
72
73
0
    return false;
74
0
}
75
76
bool for_matched_readers(
77
        ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_1,
78
        ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_2,
79
        std::function<bool(ReaderLocator&)> fun)
80
0
{
81
0
    if (for_matched_readers(reader_vector_1, fun))
82
0
    {
83
0
        return true;
84
0
    }
85
0
    return for_matched_readers(reader_vector_2, fun);
86
0
}
87
88
bool for_matched_readers(
89
        ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_1,
90
        ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_2,
91
        ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_3,
92
        std::function<bool(ReaderLocator&)> fun)
93
0
{
94
0
    if (for_matched_readers(reader_vector_1, reader_vector_2, fun))
95
0
    {
96
0
        return true;
97
0
    }
98
0
    return for_matched_readers(reader_vector_3, fun);
99
0
}
100
101
/**
102
 * Loops over all the readers in the vector, applying the given routine.
103
 * The loop continues until the result of the routine is true for any reader
104
 * or all readers have been processes.
105
 * The returned value is true if the routine returned true at any point,
106
 * or false otherwise.
107
 *
108
 * const version
109
 */
110
bool for_matched_readers(
111
        const ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_1,
112
        std::function<bool(const ReaderLocator)> fun)
113
0
{
114
0
    for (const auto& remote_reader : reader_vector_1)
115
0
    {
116
0
        if (fun(*remote_reader))
117
0
        {
118
0
            return true;
119
0
        }
120
0
    }
121
122
0
    return false;
123
0
}
124
125
bool for_matched_readers(
126
        const ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_1,
127
        const ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_2,
128
        std::function<bool(const ReaderLocator)> fun)
129
0
{
130
0
    if (for_matched_readers(reader_vector_1, fun))
131
0
    {
132
0
        return true;
133
0
    }
134
0
    return for_matched_readers(reader_vector_2, fun);
135
0
}
136
137
bool for_matched_readers(
138
        const ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_1,
139
        const ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_2,
140
        const ResourceLimitedVector<std::unique_ptr<ReaderLocator>>& reader_vector_3,
141
        std::function<bool(const ReaderLocator&)> fun)
142
0
{
143
0
    if (for_matched_readers(reader_vector_1, reader_vector_2, fun))
144
0
    {
145
0
        return true;
146
0
    }
147
0
    return for_matched_readers(reader_vector_3, fun);
148
0
}
149
150
StatelessWriter::StatelessWriter(
151
        RTPSParticipantImpl* impl,
152
        const GUID_t& guid,
153
        const WriterAttributes& attributes,
154
        FlowController* flow_controller,
155
        WriterHistory* history,
156
        WriterListener* listener)
157
0
    : BaseWriter(impl, guid, attributes, flow_controller, history, listener)
158
0
    , matched_remote_readers_(attributes.matched_readers_allocation)
159
0
    , matched_local_readers_(attributes.matched_readers_allocation)
160
0
    , matched_datasharing_readers_(attributes.matched_readers_allocation)
161
0
    , matched_readers_pool_(attributes.matched_readers_allocation)
162
0
    , locator_selector_(*this, attributes.matched_readers_allocation)
163
0
{
164
0
    init(impl, attributes);
165
0
}
166
167
void StatelessWriter::init(
168
        RTPSParticipantImpl* participant,
169
        const WriterAttributes& attributes)
170
0
{
171
0
    get_builtin_guid();
172
173
0
    const RemoteLocatorsAllocationAttributes& loc_alloc =
174
0
            participant->get_attributes().allocation.locators;
175
176
0
    for (size_t i = 0; i < attributes.matched_readers_allocation.initial; ++i)
177
0
    {
178
0
        matched_readers_pool_.emplace_back(new ReaderLocator(
179
0
                    this,
180
0
                    loc_alloc.max_unicast_locators,
181
0
                    loc_alloc.max_multicast_locators));
182
0
    }
183
0
}
184
185
StatelessWriter::~StatelessWriter()
186
0
{
187
0
    EPROSIMA_LOG_INFO(RTPS_WRITER, "StatelessWriter destructor"; );
188
0
}
189
190
void StatelessWriter::local_actions_on_writer_removed()
191
0
{
192
0
    EPROSIMA_LOG_INFO(RTPS_WRITER, "StatelessWriter local_actions_on_writer_removed"; );
193
0
    BaseWriter::local_actions_on_writer_removed();
194
0
}
195
196
void StatelessWriter::get_builtin_guid()
197
0
{
198
0
    if (m_guid.entityId == ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER)
199
0
    {
200
0
        add_guid(locator_selector_, GUID_t { GuidPrefix_t(), c_EntityId_SPDPReader });
201
0
    }
202
#if HAVE_SECURITY
203
    else if (m_guid.entityId == ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER)
204
    {
205
        add_guid(locator_selector_, GUID_t { GuidPrefix_t(), participant_stateless_message_reader_entity_id });
206
    }
207
#endif // if HAVE_SECURITY
208
0
}
209
210
bool StatelessWriter::has_builtin_guid()
211
0
{
212
0
    if (m_guid.entityId == ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER)
213
0
    {
214
0
        return true;
215
0
    }
216
#if HAVE_SECURITY
217
    if (m_guid.entityId == ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER)
218
    {
219
        return true;
220
    }
221
#endif // if HAVE_SECURITY
222
0
    return false;
223
0
}
224
225
void StatelessWriter::update_reader_info(
226
        bool create_sender_resources)
227
0
{
228
0
    bool addGuid = !has_builtin_guid();
229
0
    is_inline_qos_expected_ = false;
230
231
0
    for_matched_readers(matched_local_readers_, matched_datasharing_readers_, matched_remote_readers_,
232
0
            [this](const ReaderLocator& reader)
233
0
            {
234
0
                is_inline_qos_expected_ |= reader.expects_inline_qos();
235
0
                return false;
236
0
            }
237
0
            );
238
239
0
    update_cached_info_nts(locator_selector_);
240
0
    if (addGuid)
241
0
    {
242
0
        compute_selected_guids(locator_selector_);
243
0
    }
244
245
0
    if (create_sender_resources)
246
0
    {
247
0
        RTPSParticipantImpl* part = mp_RTPSParticipant;
248
0
        locator_selector_.locator_selector.for_each([part](const Locator_t& loc)
249
0
                {
250
0
                    part->createSenderResources(loc);
251
0
                });
252
0
    }
253
0
}
254
255
/*
256
 *  CHANGE-RELATED METHODS
257
 */
258
bool StatelessWriter::datasharing_delivery(
259
        CacheChange_t* change)
260
0
{
261
0
    auto pool = std::dynamic_pointer_cast<WriterPool>(history_->get_payload_pool());
262
0
    assert(pool != nullptr);
263
264
0
    pool->add_to_shared_history(change);
265
0
    EPROSIMA_LOG_INFO(RTPS_WRITER, "Notifying readers of cache change with SN " << change->sequenceNumber);
266
0
    for (std::unique_ptr<ReaderLocator>& reader : matched_datasharing_readers_)
267
0
    {
268
0
        if (!reader_data_filter_ || reader_data_filter_->is_relevant(*change, reader->remote_guid()))
269
0
        {
270
0
            reader->datasharing_notify();
271
0
        }
272
0
    }
273
0
    return true;
274
0
}
275
276
void StatelessWriter::unsent_change_added_to_history(
277
        CacheChange_t* change,
278
        const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time)
279
0
{
280
0
    std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
281
0
    auto payload_length = change->serializedPayload.length;
282
283
0
    if (liveliness_lease_duration_ < dds::c_TimeInfinite)
284
0
    {
285
0
        mp_RTPSParticipant->wlp()->assert_liveliness(
286
0
            getGuid(),
287
0
            liveliness_kind_,
288
0
            liveliness_lease_duration_);
289
0
    }
290
291
    // Notify the datasharing readers
292
    // This also prepares the metadata for late-joiners
293
0
    if (is_datasharing_compatible())
294
0
    {
295
0
        datasharing_delivery(change);
296
0
    }
297
298
    // Now for the rest of readers
299
0
    if (!fixed_locators_.empty() || get_matched_readers_size() > 0)
300
0
    {
301
0
        flow_controller_->add_new_sample(this, change, max_blocking_time);
302
0
    }
303
0
    else
304
0
    {
305
0
        EPROSIMA_LOG_INFO(RTPS_WRITER, "No reader to add change.");
306
0
        if (listener_ != nullptr)
307
0
        {
308
0
            listener_->on_writer_change_received_by_all(this, change);
309
0
        }
310
0
    }
311
312
    // Throughput should be notified even if no matches are available
313
0
    on_publish_throughput(payload_length);
314
0
}
315
316
bool StatelessWriter::intraprocess_delivery(
317
        CacheChange_t* change,
318
        ReaderLocator& reader_locator)
319
0
{
320
0
    LocalReaderPointer::Instance local_reader = reader_locator.local_reader();
321
322
0
    if (local_reader &&
323
0
            (!reader_data_filter_ || reader_data_filter_->is_relevant(*change, reader_locator.remote_guid())))
324
0
    {
325
0
        if (change->write_params.related_sample_identity() != SampleIdentity::unknown())
326
0
        {
327
0
            change->write_params.sample_identity(change->write_params.related_sample_identity());
328
0
        }
329
0
        return local_reader->process_data_msg(change);
330
0
    }
331
332
0
    return false;
333
0
}
334
335
bool StatelessWriter::change_removed_by_history(
336
        CacheChange_t* change,
337
        const std::chrono::time_point<std::chrono::steady_clock>& max_blocking_time)
338
0
{
339
0
    bool ret_value = false;
340
0
    std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
341
342
0
    if (flow_controller_->remove_change(change, max_blocking_time))
343
0
    {
344
345
        // remove from datasharing pool history
346
0
        if (is_datasharing_compatible())
347
0
        {
348
0
            auto pool = std::dynamic_pointer_cast<WriterPool>(history_->get_payload_pool());
349
0
            assert (pool != nullptr);
350
351
0
            pool->remove_from_shared_history(change);
352
0
            EPROSIMA_LOG_INFO(RTPS_WRITER, "Removing shared cache change with SN " << change->sequenceNumber);
353
0
        }
354
355
0
        const uint64_t sequence_number = change->sequenceNumber.to64long();
356
0
        if (sequence_number > last_sequence_number_sent_)
357
0
        {
358
0
            unsent_changes_cond_.notify_all();
359
0
        }
360
361
0
        ret_value = true;
362
0
    }
363
364
0
    return ret_value;
365
0
}
366
367
bool StatelessWriter::has_been_fully_delivered(
368
        const SequenceNumber_t& seq_num) const
369
0
{
370
    // Sequence number has not been generated by this WriterHistory
371
0
    {
372
0
        std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
373
0
        if (seq_num >= history_->next_sequence_number())
374
0
        {
375
0
            return false;
376
0
        }
377
0
    }
378
379
0
    if (get_matched_readers_size() > 0)
380
0
    {
381
0
        return is_acked_by_all(seq_num);
382
0
    }
383
0
    return true;
384
0
}
385
386
bool StatelessWriter::is_acked_by_all(
387
        const SequenceNumber_t& seq_num) const
388
0
{
389
0
    std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
390
0
    return seq_num.to64long() <= last_sequence_number_sent_;
391
0
}
392
393
bool StatelessWriter::wait_for_all_acked(
394
        const dds::Duration_t& max_wait)
395
0
{
396
0
    static_cast<void>(max_wait);
397
0
    return true;
398
0
}
399
400
bool StatelessWriter::get_disable_positive_acks() const
401
0
{
402
0
    return false;
403
0
}
404
405
bool StatelessWriter::matched_readers_guids(
406
        std::vector<GUID_t>& guids) const
407
0
{
408
0
    std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
409
0
    guids.clear();
410
0
    guids.reserve(matched_local_readers_.size() + matched_datasharing_readers_.size() + matched_remote_readers_.size());
411
0
    for_matched_readers(matched_local_readers_, matched_datasharing_readers_, matched_remote_readers_,
412
0
            [&guids](const ReaderLocator& reader)
413
0
            {
414
0
                guids.emplace_back(reader.remote_guid());
415
0
                return false;
416
0
            }
417
0
            );
418
0
    return true;
419
0
}
420
421
bool StatelessWriter::try_remove_change(
422
        const std::chrono::steady_clock::time_point&,
423
        std::unique_lock<RecursiveTimedMutex>&)
424
0
{
425
0
    return history_->remove_min_change();
426
0
}
427
428
bool StatelessWriter::wait_for_acknowledgement(
429
        const SequenceNumber_t& seq,
430
        const std::chrono::steady_clock::time_point& max_blocking_time_point,
431
        std::unique_lock<RecursiveTimedMutex>& lock)
432
0
{
433
0
    uint64_t seq_long_64 = seq.to64long();
434
0
    auto change_is_acknowledged = [this, seq, seq_long_64]()
435
0
            {
436
0
                bool ret = false;
437
0
                if (seq_long_64 <= last_sequence_number_sent_)
438
0
                {
439
                    // Stop waiting if the sequence number has been sent
440
0
                    ret = true;
441
0
                }
442
0
                else
443
0
                {
444
                    // If the sequence number has not been sent, stop waiting if it is not present in the history
445
0
                    CacheChange_t* change = nullptr;
446
0
                    ret = !history_->get_change(seq, m_guid, &change);
447
0
                }
448
0
                return ret;
449
0
            };
450
0
    return unsent_changes_cond_.wait_until(lock, max_blocking_time_point, change_is_acknowledged);
451
0
}
452
453
/*
454
 *  MATCHED_READER-RELATED METHODS
455
 */
456
bool StatelessWriter::matched_reader_add_edp(
457
        const ReaderProxyData& data)
458
0
{
459
0
    using network::external_locators::filter_remote_locators;
460
461
0
    std::unique_lock<RecursiveTimedMutex> guard(mp_mutex);
462
0
    std::unique_lock<LocatorSelectorSender> locator_selector_guard(locator_selector_);
463
464
0
    assert(data.guid != c_Guid_Unknown);
465
466
0
    if (for_matched_readers(matched_local_readers_, matched_datasharing_readers_, matched_remote_readers_,
467
0
            [this, &data](ReaderLocator& reader)
468
0
            {
469
0
                if (reader.remote_guid() == data.guid)
470
0
                {
471
0
                    EPROSIMA_LOG_WARNING(RTPS_WRITER, "Attempting to add existing reader, updating information.");
472
0
                    if (reader.update(data.remote_locators.unicast,
473
0
                    data.remote_locators.multicast,
474
0
                    data.expects_inline_qos))
475
0
                    {
476
0
                        filter_remote_locators(*reader.general_locator_selector_entry(),
477
0
                        m_att.external_unicast_locators, m_att.ignore_non_matching_locators);
478
0
                        mp_RTPSParticipant->createSenderResources(data.remote_locators, m_att);
479
0
                        update_reader_info(true);
480
0
                    }
481
0
                    return true;
482
0
                }
483
0
                return false;
484
0
            }
485
0
            ))
486
0
    {
487
0
        if (nullptr != listener_)
488
0
        {
489
            // call the listener without locks taken
490
0
            locator_selector_guard.unlock();
491
0
            guard.unlock();
492
0
            listener_->on_reader_discovery(this, ReaderDiscoveryStatus::CHANGED_QOS_READER, data.guid, &data);
493
0
        }
494
495
0
#ifdef FASTDDS_STATISTICS
496
        // notify monitor service so that the connectionlist for this entity
497
        // could be updated
498
0
        if (nullptr != mp_RTPSParticipant->get_connections_observer() && !m_guid.is_builtin())
499
0
        {
500
0
            mp_RTPSParticipant->get_connections_observer()->on_local_entity_connections_change(m_guid);
501
0
        }
502
0
#endif //FASTDDS_STATISTICS
503
504
0
        return false;
505
0
    }
506
507
    // Get a locator from the inactive pool (or create a new one if necessary and allowed)
508
0
    std::unique_ptr<ReaderLocator> new_reader;
509
0
    if (matched_readers_pool_.empty())
510
0
    {
511
0
        size_t max_readers = matched_readers_pool_.max_size();
512
0
        if (get_matched_readers_size() + matched_readers_pool_.size() < max_readers)
513
0
        {
514
0
            const RemoteLocatorsAllocationAttributes& loc_alloc =
515
0
                    mp_RTPSParticipant->get_attributes().allocation.locators;
516
517
0
            new_reader.reset(new ReaderLocator(
518
0
                        this,
519
0
                        loc_alloc.max_unicast_locators,
520
0
                        loc_alloc.max_multicast_locators));
521
0
        }
522
0
        else
523
0
        {
524
0
            EPROSIMA_LOG_WARNING(RTPS_WRITER, "Couldn't add matched reader due to resource limits");
525
0
            return false;
526
0
        }
527
0
    }
528
0
    else
529
0
    {
530
0
        new_reader = std::move(matched_readers_pool_.back());
531
0
        matched_readers_pool_.pop_back();
532
0
    }
533
534
    // Add info of new datareader.
535
0
    new_reader->start(data.guid,
536
0
            data.remote_locators.unicast,
537
0
            data.remote_locators.multicast,
538
0
            data.expects_inline_qos,
539
0
            is_datasharing_compatible_with(data.data_sharing));
540
0
    filter_remote_locators(*new_reader->general_locator_selector_entry(),
541
0
            m_att.external_unicast_locators, m_att.ignore_non_matching_locators);
542
543
0
    locator_selector_.locator_selector.add_entry(new_reader->general_locator_selector_entry());
544
545
0
    if (new_reader->is_local_reader())
546
0
    {
547
0
        matched_local_readers_.push_back(std::move(new_reader));
548
0
        EPROSIMA_LOG_INFO(RTPS_WRITER, "Adding reader " << data.guid << " to " << this->m_guid.entityId
549
0
                                                        << " as local reader");
550
0
    }
551
0
    else if (new_reader->is_datasharing_reader())
552
0
    {
553
0
        matched_datasharing_readers_.push_back(std::move(new_reader));
554
0
        EPROSIMA_LOG_INFO(RTPS_WRITER, "Adding reader " << data.guid << " to " << this->m_guid.entityId
555
0
                                                        << " as data sharing");
556
0
    }
557
0
    else
558
0
    {
559
0
        matched_remote_readers_.push_back(std::move(new_reader));
560
0
        EPROSIMA_LOG_INFO(RTPS_WRITER, "Adding reader " << data.guid << " to " << this->m_guid.entityId
561
0
                                                        << " as remote reader");
562
0
    }
563
564
    // Create sender resources for the case when we send to a single reader
565
0
    mp_RTPSParticipant->createSenderResources(data.remote_locators, m_att);
566
567
    // Create sender resources for the case when we send to all readers
568
0
    update_reader_info(true);
569
570
0
    if (nullptr != listener_)
571
0
    {
572
        // call the listener without locks taken
573
0
        locator_selector_guard.unlock();
574
0
        guard.unlock();
575
0
        listener_->on_reader_discovery(this, ReaderDiscoveryStatus::DISCOVERED_READER, data.guid, &data);
576
0
    }
577
578
0
#ifdef FASTDDS_STATISTICS
579
    // notify monitor service so that the connectionlist for this entity
580
    // could be updated
581
0
    if (nullptr != mp_RTPSParticipant->get_connections_observer() && !m_guid.is_builtin())
582
0
    {
583
0
        mp_RTPSParticipant->get_connections_observer()->on_local_entity_connections_change(m_guid);
584
0
    }
585
0
#endif //FASTDDS_STATISTICS
586
587
0
    return true;
588
0
}
589
590
bool StatelessWriter::matched_reader_remove(
591
        const GUID_t& reader_guid)
592
0
{
593
0
    std::unique_lock<RecursiveTimedMutex> guard(mp_mutex);
594
0
    std::unique_lock<LocatorSelectorSender> locator_selector_guard(locator_selector_);
595
596
0
    if (locator_selector_.locator_selector.remove_entry(reader_guid))
597
0
    {
598
0
        std::unique_ptr<ReaderLocator> reader;
599
0
        for (auto it = matched_local_readers_.begin();
600
0
                it != matched_local_readers_.end(); ++it)
601
0
        {
602
0
            if ((*it)->remote_guid() == reader_guid)
603
0
            {
604
0
                reader = std::move(*it);
605
0
                matched_local_readers_.erase(it);
606
0
                break;
607
0
            }
608
0
        }
609
610
0
        if (reader == nullptr)
611
0
        {
612
0
            for (auto it = matched_datasharing_readers_.begin();
613
0
                    it != matched_datasharing_readers_.end(); ++it)
614
0
            {
615
0
                if ((*it)->remote_guid() == reader_guid)
616
0
                {
617
0
                    reader = std::move(*it);
618
0
                    matched_datasharing_readers_.erase(it);
619
0
                    break;
620
0
                }
621
0
            }
622
0
        }
623
624
0
        if (reader == nullptr)
625
0
        {
626
0
            for (auto it = matched_remote_readers_.begin();
627
0
                    it != matched_remote_readers_.end(); ++it)
628
0
            {
629
0
                if ((*it)->remote_guid() == reader_guid)
630
0
                {
631
0
                    reader = std::move(*it);
632
0
                    matched_remote_readers_.erase(it);
633
0
                    break;
634
0
                }
635
0
            }
636
0
        }
637
638
        // guid should be both on locator_selector_ and matched_readers_ or in none
639
0
        assert(reader != nullptr);
640
641
0
        reader->stop();
642
0
        matched_readers_pool_.push_back(std::move(reader));
643
0
        update_reader_info(false);
644
0
        EPROSIMA_LOG_INFO(RTPS_WRITER, "Reader Proxy removed: " << reader_guid);
645
0
        if (nullptr != listener_)
646
0
        {
647
            // call the listener without locks taken
648
0
            locator_selector_guard.unlock();
649
0
            guard.unlock();
650
651
0
            listener_->on_reader_discovery(this, ReaderDiscoveryStatus::REMOVED_READER, reader_guid, nullptr);
652
0
        }
653
654
0
#ifdef FASTDDS_STATISTICS
655
        // notify monitor service so that the connectionlist for this entity
656
        // could be updated
657
0
        if (nullptr != mp_RTPSParticipant->get_connections_observer() && !m_guid.is_builtin())
658
0
        {
659
0
            mp_RTPSParticipant->get_connections_observer()->on_local_entity_connections_change(m_guid);
660
0
        }
661
0
#endif //FASTDDS_STATISTICS
662
663
0
        return true;
664
0
    }
665
666
0
    return false;
667
0
}
668
669
bool StatelessWriter::matched_reader_is_matched(
670
        const GUID_t& reader_guid)
671
0
{
672
0
    std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
673
0
    return for_matched_readers(matched_local_readers_, matched_datasharing_readers_, matched_remote_readers_,
674
0
                   [reader_guid](const ReaderLocator& reader)
675
0
                   {
676
0
                       return reader.remote_guid() == reader_guid;
677
0
                   }
678
0
                   );
679
0
}
680
681
bool StatelessWriter::process_acknack(
682
        const GUID_t& writer_guid,
683
        const GUID_t& reader_guid,
684
        uint32_t ack_count,
685
        const SequenceNumberSet_t& sn_set,
686
        bool final_flag,
687
        bool& result,
688
        fastdds::rtps::VendorId_t origin_vendor_id)
689
0
{
690
0
    static_cast<void>(reader_guid);
691
0
    static_cast<void>(ack_count);
692
0
    static_cast<void>(sn_set);
693
0
    static_cast<void>(final_flag);
694
0
    static_cast<void>(origin_vendor_id);
695
696
0
    result = false;
697
0
    return writer_guid == m_guid;
698
0
}
699
700
bool StatelessWriter::process_nack_frag(
701
        const GUID_t& writer_guid,
702
        const GUID_t& reader_guid,
703
        uint32_t ack_count,
704
        const SequenceNumber_t& seq_num,
705
        const FragmentNumberSet_t& fragments_state,
706
        bool& result,
707
        fastdds::rtps::VendorId_t origin_vendor_id)
708
0
{
709
0
    static_cast<void>(reader_guid);
710
0
    static_cast<void>(ack_count);
711
0
    static_cast<void>(seq_num);
712
0
    static_cast<void>(fragments_state);
713
0
    static_cast<void>(origin_vendor_id);
714
715
0
    result = false;
716
0
    return writer_guid == m_guid;
717
0
}
718
719
bool StatelessWriter::send_nts(
720
        const std::vector<eprosima::fastdds::rtps::NetworkBuffer>& buffers,
721
        const uint32_t& total_bytes,
722
        const LocatorSelectorSender& locator_selector,
723
        std::chrono::steady_clock::time_point& max_blocking_time_point) const
724
0
{
725
0
    if (!BaseWriter::send_nts(buffers, total_bytes, locator_selector, max_blocking_time_point))
726
0
    {
727
0
        return false;
728
0
    }
729
730
0
    return send_to_fixed_locators(buffers, total_bytes, max_blocking_time_point);
731
0
}
732
733
bool StatelessWriter::send_to_fixed_locators(
734
        const std::vector<eprosima::fastdds::rtps::NetworkBuffer>& buffers,
735
        const uint32_t& total_bytes,
736
        std::chrono::steady_clock::time_point& max_blocking_time_point) const
737
0
{
738
0
    return fixed_locators_.empty() ||
739
0
           mp_RTPSParticipant->sendSync(buffers, total_bytes, m_guid,
740
0
                   Locators(fixed_locators_.begin()), Locators(fixed_locators_.end()),
741
0
                   max_blocking_time_point, transport_priority_);
742
0
}
743
744
DeliveryRetCode StatelessWriter::deliver_sample_nts(
745
        CacheChange_t* cache_change,
746
        RTPSMessageGroup& group,
747
        LocatorSelectorSender& locator_selector, // Object locked by FlowControllerImpl
748
        const std::chrono::time_point<std::chrono::steady_clock>& /*TODO max_blocking_time*/)
749
0
{
750
0
    uint64_t change_sequence_number = cache_change->sequenceNumber.to64long();
751
0
    NetworkFactory& network = mp_RTPSParticipant->network_factory();
752
0
    DeliveryRetCode ret_code = DeliveryRetCode::DELIVERED;
753
754
0
    if (current_sequence_number_sent_ != change_sequence_number)
755
0
    {
756
0
        current_sequence_number_sent_ = change_sequence_number;
757
0
        current_fragment_sent_ = 0;
758
0
    }
759
760
    // Send the new sample to intra-process readers.
761
0
    if (0 == current_fragment_sent_)
762
0
    {
763
0
        for_matched_readers(matched_local_readers_, [&, cache_change](ReaderLocator& reader)
764
0
                {
765
0
                    intraprocess_delivery(cache_change, reader);
766
0
                    return false;
767
0
                });
768
0
    }
769
770
0
    try
771
0
    {
772
0
        uint32_t n_fragments = cache_change->getFragmentCount();
773
774
0
        if (separate_sending_enabled_)
775
0
        {
776
0
            if (0 < n_fragments)
777
0
            {
778
0
                for (FragmentNumber_t frag = current_fragment_sent_ + 1;
779
0
                        DeliveryRetCode::DELIVERED == ret_code && frag <= n_fragments; ++frag)
780
0
                {
781
0
                    for (std::unique_ptr<ReaderLocator>& it : matched_remote_readers_)
782
0
                    {
783
0
                        if ((nullptr == reader_data_filter_) ||
784
0
                                reader_data_filter_->is_relevant(*cache_change, it->remote_guid()))
785
0
                        {
786
0
                            group.sender(this, &*it);
787
0
                            size_t num_locators = it->locators_size();
788
789
0
                            if (group.add_data_frag(*cache_change, frag, is_inline_qos_expected_))
790
0
                            {
791
0
                                add_statistics_sent_submessage(cache_change, num_locators);
792
0
                            }
793
0
                            else
794
0
                            {
795
0
                                EPROSIMA_LOG_ERROR(RTPS_WRITER,
796
0
                                        "Error sending fragment (" << cache_change->sequenceNumber << ", " << frag
797
0
                                                                   << ")");
798
0
                                ret_code = DeliveryRetCode::NOT_DELIVERED;
799
0
                            }
800
0
                        }
801
0
                    }
802
803
0
                    if (DeliveryRetCode::DELIVERED == ret_code)
804
0
                    {
805
0
                        current_fragment_sent_ = frag;
806
0
                    }
807
0
                }
808
0
            }
809
0
            else
810
0
            {
811
0
                for (std::unique_ptr<ReaderLocator>& it : matched_remote_readers_)
812
0
                {
813
0
                    if ((nullptr == reader_data_filter_) ||
814
0
                            reader_data_filter_->is_relevant(*cache_change, it->remote_guid()))
815
0
                    {
816
0
                        group.sender(this, &*it);
817
0
                        size_t num_locators = it->locators_size();
818
819
0
                        if (group.add_data(*cache_change, is_inline_qos_expected_))
820
0
                        {
821
0
                            add_statistics_sent_submessage(cache_change, num_locators);
822
0
                        }
823
0
                        else
824
0
                        {
825
0
                            EPROSIMA_LOG_ERROR(RTPS_WRITER, "Error sending change " << cache_change->sequenceNumber);
826
0
                            ret_code = DeliveryRetCode::NOT_DELIVERED;
827
0
                        }
828
0
                    }
829
0
                }
830
0
            }
831
0
        }
832
0
        else
833
0
        {
834
0
            if (nullptr != reader_data_filter_)
835
0
            {
836
0
                locator_selector.locator_selector.reset(false);
837
0
                for (std::unique_ptr<ReaderLocator>& it : matched_remote_readers_)
838
0
                {
839
0
                    if (reader_data_filter_->is_relevant(*cache_change, it->remote_guid()))
840
0
                    {
841
0
                        locator_selector.locator_selector.enable(it->remote_guid());
842
0
                    }
843
0
                }
844
0
            }
845
0
            else
846
0
            {
847
0
                locator_selector.locator_selector.reset(true);
848
0
            }
849
850
0
            if (locator_selector.locator_selector.state_has_changed())
851
0
            {
852
0
                network.select_locators(locator_selector.locator_selector);
853
0
                if (!has_builtin_guid())
854
0
                {
855
0
                    compute_selected_guids(locator_selector);
856
0
                }
857
0
            }
858
0
            size_t num_locators = locator_selector.locator_selector.selected_size() + fixed_locators_.size();
859
860
0
            if (0 < num_locators)
861
0
            {
862
0
                if (0 < n_fragments)
863
0
                {
864
0
                    for (FragmentNumber_t frag = current_fragment_sent_ + 1;
865
0
                            DeliveryRetCode::DELIVERED == ret_code && frag <= n_fragments; ++frag)
866
0
                    {
867
0
                        if (group.add_data_frag(*cache_change, frag, is_inline_qos_expected_))
868
0
                        {
869
0
                            current_fragment_sent_ = frag;
870
0
                            add_statistics_sent_submessage(cache_change, num_locators);
871
0
                        }
872
0
                        else
873
0
                        {
874
0
                            EPROSIMA_LOG_ERROR(RTPS_WRITER,
875
0
                                    "Error sending fragment (" << cache_change->sequenceNumber << ", " << frag << ")");
876
0
                            ret_code = DeliveryRetCode::NOT_DELIVERED;
877
0
                        }
878
0
                    }
879
0
                }
880
0
                else
881
0
                {
882
0
                    if (group.add_data(*cache_change, is_inline_qos_expected_))
883
0
                    {
884
0
                        add_statistics_sent_submessage(cache_change, num_locators);
885
0
                    }
886
0
                    else
887
0
                    {
888
0
                        EPROSIMA_LOG_ERROR(RTPS_WRITER, "Error sending change " << cache_change->sequenceNumber);
889
0
                        ret_code = DeliveryRetCode::NOT_DELIVERED;
890
0
                    }
891
0
                }
892
0
            }
893
0
        }
894
895
        // Do not send data without information (submessages)
896
0
        if (cache_change->writer_info.num_sent_submessages)
897
0
        {
898
0
            on_sample_datas(cache_change->write_params.sample_identity(),
899
0
                    cache_change->writer_info.num_sent_submessages);
900
0
            on_data_sent();
901
0
        }
902
903
0
    }
904
0
    catch (const RTPSMessageGroup::timeout&)
905
0
    {
906
0
        EPROSIMA_LOG_ERROR(RTPS_WRITER, "Max blocking time reached");
907
0
        ret_code = DeliveryRetCode::NOT_DELIVERED;
908
0
    }
909
0
    catch (const RTPSMessageGroup::limit_exceeded&)
910
0
    {
911
0
        ret_code = DeliveryRetCode::EXCEEDED_LIMIT;
912
0
    }
913
914
0
    group.sender(this, &locator_selector);
915
916
0
    if (DeliveryRetCode::DELIVERED == ret_code &&
917
0
            change_sequence_number > last_sequence_number_sent_)
918
0
    {
919
        // This update must be done before calling the callback.
920
0
        last_sequence_number_sent_ = change_sequence_number;
921
0
        unsent_changes_cond_.notify_all();
922
923
0
        if (nullptr != listener_)
924
0
        {
925
0
            listener_->on_writer_change_received_by_all(this, cache_change);
926
0
        }
927
0
    }
928
929
0
    return ret_code;
930
0
}
931
932
#ifdef FASTDDS_STATISTICS
933
934
bool StatelessWriter::get_connections(
935
        fastdds::statistics::rtps::ConnectionList& connection_list)
936
0
{
937
0
    connection_list.reserve(matched_local_readers_.size() +
938
0
            matched_datasharing_readers_.size() +
939
0
            matched_remote_readers_.size());
940
941
0
    fastdds::statistics::Connection connection;
942
943
0
    {
944
0
        std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
945
946
        //! intraprocess
947
0
        for_matched_readers(matched_local_readers_, [&connection, &connection_list](ReaderLocator& reader)
948
0
                {
949
0
                    connection.guid(fastdds::statistics::to_statistics_type(reader.remote_guid()));
950
0
                    connection.mode(fastdds::statistics::ConnectionMode::INTRAPROCESS);
951
0
                    connection_list.push_back(connection);
952
953
0
                    return false;
954
0
                });
955
0
    }
956
957
0
    {
958
0
        std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
959
960
        //! datasharing
961
0
        for_matched_readers(matched_datasharing_readers_, [&connection, &connection_list](ReaderLocator& reader)
962
0
                {
963
0
                    connection.guid(fastdds::statistics::to_statistics_type(reader.remote_guid()));
964
0
                    connection.mode(fastdds::statistics::ConnectionMode::DATA_SHARING);
965
0
                    connection_list.push_back(connection);
966
967
0
                    return false;
968
0
                });
969
0
    }
970
971
0
    {
972
0
        std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
973
974
        //! remote
975
0
        for_matched_readers(matched_remote_readers_, [&connection, &connection_list](ReaderLocator& reader)
976
0
                {
977
                    //! Announced locators is, for the moment,
978
                    //! equal to the used_locators
979
0
                    LocatorSelectorEntry* loc_selector_entry = reader.general_locator_selector_entry();
980
981
0
                    connection.announced_locators().reserve(reader.locators_size());
982
0
                    connection.used_locators().reserve(reader.locators_size());
983
984
0
                    std::vector<fastdds::statistics::detail::Locator_s> statistics_locators;
985
0
                    std::for_each(loc_selector_entry->multicast.begin(), loc_selector_entry->multicast.end(),
986
0
                    [&statistics_locators](const Locator_t& locator)
987
0
                    {
988
0
                        statistics_locators.push_back(fastdds::statistics::to_statistics_type(locator));
989
0
                    });
990
991
0
                    std::for_each(loc_selector_entry->unicast.begin(), loc_selector_entry->unicast.end(),
992
0
                    [&statistics_locators](const Locator_t& locator)
993
0
                    {
994
0
                        statistics_locators.push_back(fastdds::statistics::to_statistics_type(locator));
995
0
                    });
996
997
0
                    connection.guid(fastdds::statistics::to_statistics_type(reader.remote_guid()));
998
0
                    connection.mode(fastdds::statistics::ConnectionMode::TRANSPORT);
999
0
                    connection.announced_locators(statistics_locators);
1000
0
                    connection.used_locators(statistics_locators);
1001
0
                    connection_list.push_back(connection);
1002
1003
0
                    return false;
1004
0
                });
1005
0
    }
1006
1007
0
    return true;
1008
0
}
1009
1010
#endif // ifdef FASTDDS_STATISTICS
1011
1012
} /* namespace rtps */
1013
} /* namespace fastdds */
1014
} /* namespace eprosima */