-
Notifications
You must be signed in to change notification settings - Fork 811
/
Copy pathStatefulReader.cpp
1552 lines (1369 loc) · 51.2 KB
/
StatefulReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @file StatefulReader.cpp
*
*/
#include <fastdds/rtps/reader/StatefulReader.h>
#include <fastdds/rtps/reader/ReaderListener.h>
#include <fastdds/rtps/history/ReaderHistory.h>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/messages/RTPSMessageCreator.h>
#include <rtps/participant/RTPSParticipantImpl.h>
#include <rtps/reader/WriterProxy.h>
#include <fastrtps/utils/TimeConversion.h>
#include <rtps/history/HistoryAttributesExtension.hpp>
#include <rtps/DataSharing/DataSharingListener.hpp>
#include <rtps/DataSharing/ReaderPool.hpp>
#include <fastdds/rtps/builtin/BuiltinProtocols.h>
#include <fastdds/rtps/builtin/liveliness/WLP.h>
#include <fastdds/rtps/writer/LivelinessManager.h>
#include "rtps/RTPSDomainImpl.hpp"
#include <mutex>
#include <thread>
#include <cassert>
#define IDSTRING "(ID:" << std::this_thread::get_id() << ") " <<
using namespace eprosima::fastrtps::rtps;
static void send_ack_if_datasharing(
StatefulReader* reader,
ReaderHistory* history,
WriterProxy* writer,
const SequenceNumber_t& sequence_number)
{
// If not datasharing, we are done
if (!writer || !writer->is_datasharing_writer())
{
return;
}
// This may not be the change read with highest SN,
// need to find largest SN to ACK
for (std::vector<CacheChange_t*>::iterator it = history->changesBegin(); it != history->changesEnd(); ++it)
{
if (!(*it)->isRead)
{
if ((*it)->writerGUID == writer->guid())
{
if ((*it)->sequenceNumber < sequence_number)
{
//There are earlier changes not read yet. Do not send ACK.
return;
}
SequenceNumberSet_t sns((*it)->sequenceNumber);
reader->send_acknack(writer, sns, writer, false);
return;
}
}
}
// Must ACK all in the writer
SequenceNumberSet_t sns(writer->available_changes_max() + 1);
reader->send_acknack(writer, sns, writer, false);
}
StatefulReader::~StatefulReader()
{
EPROSIMA_LOG_INFO(RTPS_READER, "StatefulReader destructor.");
// Only is_alive_ assignment needs to be protected, as
// matched_writers_ and matched_writers_pool_ are only used
// when is_alive_ is true
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
is_alive_ = false;
}
// Datasharing listener must be stopped to avoid processing notifications
// while the reader is being destroyed
if (is_datasharing_compatible_)
{
datasharing_listener_->stop();
}
for (WriterProxy* writer : matched_writers_)
{
delete(writer);
}
for (WriterProxy* writer : matched_writers_pool_)
{
delete(writer);
}
}
StatefulReader::StatefulReader(
RTPSParticipantImpl* pimpl,
const GUID_t& guid,
const ReaderAttributes& att,
ReaderHistory* hist,
ReaderListener* listen)
: RTPSReader(pimpl, guid, att, hist, listen)
, acknack_count_(0)
, nackfrag_count_(0)
, times_(att.times)
, matched_writers_(att.matched_writers_allocation)
, matched_writers_pool_(att.matched_writers_allocation)
, proxy_changes_config_(resource_limits_from_history(hist->m_att, 0))
, disable_positive_acks_(att.disable_positive_acks)
, is_alive_(true)
{
init(pimpl, att);
}
StatefulReader::StatefulReader(
RTPSParticipantImpl* pimpl,
const GUID_t& guid,
const ReaderAttributes& att,
const std::shared_ptr<IPayloadPool>& payload_pool,
ReaderHistory* hist,
ReaderListener* listen)
: RTPSReader(pimpl, guid, att, payload_pool, hist, listen)
, acknack_count_(0)
, nackfrag_count_(0)
, times_(att.times)
, matched_writers_(att.matched_writers_allocation)
, matched_writers_pool_(att.matched_writers_allocation)
, proxy_changes_config_(resource_limits_from_history(hist->m_att, 0))
, disable_positive_acks_(att.disable_positive_acks)
, is_alive_(true)
{
init(pimpl, att);
}
StatefulReader::StatefulReader(
RTPSParticipantImpl* pimpl,
const GUID_t& guid,
const ReaderAttributes& att,
const std::shared_ptr<IPayloadPool>& payload_pool,
const std::shared_ptr<IChangePool>& change_pool,
ReaderHistory* hist,
ReaderListener* listen)
: RTPSReader(pimpl, guid, att, payload_pool, change_pool, hist, listen)
, acknack_count_(0)
, nackfrag_count_(0)
, times_(att.times)
, matched_writers_(att.matched_writers_allocation)
, matched_writers_pool_(att.matched_writers_allocation)
, proxy_changes_config_(resource_limits_from_history(hist->m_att, 0))
, disable_positive_acks_(att.disable_positive_acks)
, is_alive_(true)
{
init(pimpl, att);
}
void StatefulReader::init(
RTPSParticipantImpl* pimpl,
const ReaderAttributes& att)
{
const RTPSParticipantAttributes& part_att = pimpl->getRTPSParticipantAttributes();
for (size_t n = 0; n < att.matched_writers_allocation.initial; ++n)
{
matched_writers_pool_.push_back(new WriterProxy(this, part_att.allocation.locators, proxy_changes_config_));
}
}
bool StatefulReader::matched_writer_add(
const WriterProxyData& wdata)
{
assert(wdata.guid() != c_Guid_Unknown);
ReaderListener* listener = nullptr;
{
std::unique_lock<RecursiveTimedMutex> guard(mp_mutex);
if (!is_alive_)
{
return false;
}
listener = mp_listener;
bool is_same_process = RTPSDomainImpl::should_intraprocess_between(m_guid, wdata.guid());
bool is_datasharing = !is_same_process && is_datasharing_compatible_with(wdata);
for (WriterProxy* it : matched_writers_)
{
if (it->guid() == wdata.guid())
{
EPROSIMA_LOG_INFO(RTPS_READER, "Attempting to add existing writer, updating information");
// If Ownership strength changes then update all history instances.
if (EXCLUSIVE_OWNERSHIP_QOS == m_att.ownershipKind &&
it->ownership_strength() != wdata.m_qos.m_ownershipStrength.value)
{
mp_history->writer_update_its_ownership_strength_nts(
it->guid(), wdata.m_qos.m_ownershipStrength.value);
}
it->update(wdata);
if (!is_same_process)
{
for (const Locator_t& locator : it->remote_locators_shrinked())
{
getRTPSParticipant()->createSenderResources(locator);
}
}
if (nullptr != listener)
{
// call the listener without the lock taken
guard.unlock();
listener->on_writer_discovery(this, WriterDiscoveryInfo::CHANGED_QOS_WRITER, wdata.guid(), &wdata);
}
return false;
}
}
// Get a writer proxy from the inactive pool (or create a new one if necessary and allowed)
WriterProxy* wp = nullptr;
if (matched_writers_pool_.empty())
{
size_t max_readers = matched_writers_pool_.max_size();
if (getMatchedWritersSize() + matched_writers_pool_.size() < max_readers)
{
const RTPSParticipantAttributes& part_att = mp_RTPSParticipant->getRTPSParticipantAttributes();
wp = new WriterProxy(this, part_att.allocation.locators, proxy_changes_config_);
}
else
{
EPROSIMA_LOG_WARNING(RTPS_READER, "Maximum number of reader proxies (" << max_readers << \
") reached for writer " << m_guid);
return false;
}
}
else
{
wp = matched_writers_pool_.back();
matched_writers_pool_.pop_back();
}
SequenceNumber_t initial_sequence;
add_persistence_guid(wdata.guid(), wdata.persistence_guid());
initial_sequence = get_last_notified(wdata.guid());
wp->start(wdata, initial_sequence, is_datasharing);
if (!is_same_process)
{
for (const Locator_t& locator : wp->remote_locators_shrinked())
{
getRTPSParticipant()->createSenderResources(locator);
}
}
if (is_datasharing)
{
if (datasharing_listener_->add_datasharing_writer(wdata.guid(),
m_att.durabilityKind == VOLATILE,
mp_history->m_att.maximumReservedCaches))
{
matched_writers_.push_back(wp);
EPROSIMA_LOG_INFO(RTPS_READER, "Writer Proxy " << wdata.guid() << " added to " << this->m_guid.entityId
<< " with data sharing");
}
else
{
EPROSIMA_LOG_ERROR(RTPS_READER, "Failed to add Writer Proxy " << wdata.guid()
<< " to " << this->m_guid.entityId
<< " with data sharing.");
{
// Release reader's lock to avoid deadlock when waiting for event (requiring mutex) to finish
guard.unlock();
assert(!guard.owns_lock());
wp->stop();
guard.lock();
}
matched_writers_pool_.push_back(wp);
return false;
}
// Intraprocess manages durability itself
if (VOLATILE == m_att.durabilityKind)
{
std::shared_ptr<ReaderPool> pool = datasharing_listener_->get_pool_for_writer(wp->guid());
SequenceNumber_t last_seq = pool->get_last_read_sequence_number();
if (SequenceNumber_t::unknown() != last_seq)
{
SequenceNumberSet_t sns(last_seq + 1);
send_acknack(wp, sns, wp, false);
wp->lost_changes_update(last_seq + 1);
}
}
else if (!is_same_process)
{
// simulate a notification to force reading of transient changes
datasharing_listener_->notify(false);
}
}
else
{
matched_writers_.push_back(wp);
EPROSIMA_LOG_INFO(RTPS_READER, "Writer Proxy " << wp->guid() << " added to " << m_guid.entityId);
}
}
if (liveliness_lease_duration_ < c_TimeInfinite)
{
auto wlp = this->mp_RTPSParticipant->wlp();
if ( wlp != nullptr)
{
wlp->sub_liveliness_manager_->add_writer(
wdata.guid(),
liveliness_kind_,
liveliness_lease_duration_);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_LIVELINESS,
"Finite liveliness lease duration but WLP not enabled, cannot add writer");
}
}
if (nullptr != listener)
{
listener->on_writer_discovery(this, WriterDiscoveryInfo::DISCOVERED_WRITER, wdata.guid(), &wdata);
}
return true;
}
bool StatefulReader::matched_writer_remove(
const GUID_t& writer_guid,
bool removed_by_lease)
{
if (is_alive_ && liveliness_lease_duration_ < c_TimeInfinite)
{
auto wlp = this->mp_RTPSParticipant->wlp();
if ( wlp != nullptr)
{
wlp->sub_liveliness_manager_->remove_writer(
writer_guid,
liveliness_kind_,
liveliness_lease_duration_);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_LIVELINESS,
"Finite liveliness lease duration but WLP not enabled, cannot remove writer");
}
}
std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
WriterProxy* wproxy = nullptr;
if (is_alive_)
{
//Remove cachechanges belonging to the unmatched writer
mp_history->writer_unmatched(writer_guid, get_last_notified(writer_guid));
for (ResourceLimitedVector<WriterProxy*>::iterator it = matched_writers_.begin();
it != matched_writers_.end();
++it)
{
if ((*it)->guid() == writer_guid)
{
EPROSIMA_LOG_INFO(RTPS_READER, "Writer proxy " << writer_guid << " removed from " << m_guid.entityId);
wproxy = *it;
matched_writers_.erase(it);
break;
}
}
if (wproxy != nullptr)
{
remove_persistence_guid(wproxy->guid(), wproxy->persistence_guid(), removed_by_lease);
if (wproxy->is_datasharing_writer())
{
// If it is datasharing, it must be in the listener
bool removed_from_listener = datasharing_listener_->remove_datasharing_writer(writer_guid);
assert(removed_from_listener);
(void)removed_from_listener;
remove_changes_from(writer_guid, true);
}
{
// Release reader's lock to avoid deadlock when waiting for event (requiring mutex) to finish
lock.unlock();
assert(!lock.owns_lock());
wproxy->stop();
lock.lock();
}
matched_writers_pool_.push_back(wproxy);
if (nullptr != mp_listener)
{
// call the listener without the lock taken
ReaderListener* listener = mp_listener;
lock.unlock();
listener->on_writer_discovery(this, WriterDiscoveryInfo::REMOVED_WRITER, writer_guid, nullptr);
}
}
else
{
EPROSIMA_LOG_INFO(RTPS_READER,
"Writer Proxy " << writer_guid << " doesn't exist in reader " << this->getGuid().entityId);
}
}
return (wproxy != nullptr);
}
bool StatefulReader::matched_writer_is_matched(
const GUID_t& writer_guid)
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
if (is_alive_)
{
for (WriterProxy* it : matched_writers_)
{
if (it->guid() == writer_guid && it->is_alive())
{
return true;
}
}
}
return false;
}
bool StatefulReader::matched_writer_lookup(
const GUID_t& writerGUID,
WriterProxy** WP)
{
assert(WP);
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
if (!is_alive_)
{
return false;
}
bool returnedValue = findWriterProxy(writerGUID, WP);
if (returnedValue)
{
EPROSIMA_LOG_INFO(RTPS_READER, this->getGuid().entityId << " FINDS writerProxy " << writerGUID << " from "
<< getMatchedWritersSize());
}
else
{
EPROSIMA_LOG_INFO(RTPS_READER, this->getGuid().entityId << " NOT FINDS writerProxy " << writerGUID << " from "
<< getMatchedWritersSize());
}
return returnedValue;
}
bool StatefulReader::findWriterProxy(
const GUID_t& writerGUID,
WriterProxy** WP) const
{
assert(WP);
for (WriterProxy* it : matched_writers_)
{
if (it->guid() == writerGUID && it->is_alive())
{
*WP = it;
return true;
}
}
return false;
}
void StatefulReader::assert_writer_liveliness(
const GUID_t& writer)
{
if (liveliness_lease_duration_ < c_TimeInfinite)
{
auto wlp = this->mp_RTPSParticipant->wlp();
if (wlp != nullptr)
{
wlp->sub_liveliness_manager_->assert_liveliness(
writer,
liveliness_kind_,
liveliness_lease_duration_);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_LIVELINESS, "Finite liveliness lease duration but WLP not enabled");
}
}
}
bool StatefulReader::processDataMsg(
CacheChange_t* change)
{
WriterProxy* pWP = nullptr;
assert(change);
std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
if (!is_alive_)
{
return false;
}
if (acceptMsgFrom(change->writerGUID, &pWP))
{
// Check if CacheChange was received or is framework data
if (!pWP || !pWP->change_was_received(change->sequenceNumber))
{
// Always assert liveliness on scope exit
auto assert_liveliness_lambda = [&lock, this, change](void*)
{
lock.unlock(); // Avoid deadlock with LivelinessManager.
assert_writer_liveliness(change->writerGUID);
};
std::unique_ptr<void, decltype(assert_liveliness_lambda)> p{ this, assert_liveliness_lambda };
EPROSIMA_LOG_INFO(RTPS_MSG_IN,
IDSTRING "Trying to add change " << change->sequenceNumber << " TO reader: " << getGuid().entityId);
size_t unknown_missing_changes_up_to = pWP ? pWP->unknown_missing_changes_up_to(change->sequenceNumber) : 0;
bool will_never_be_accepted = false;
if (!mp_history->can_change_be_added_nts(change->writerGUID, change->serializedPayload.length,
unknown_missing_changes_up_to, will_never_be_accepted))
{
if (will_never_be_accepted && pWP)
{
pWP->irrelevant_change_set(change->sequenceNumber);
NotifyChanges(pWP);
send_ack_if_datasharing(this, mp_history, pWP, change->sequenceNumber);
}
return false;
}
if (data_filter_ && !data_filter_->is_relevant(*change, m_guid))
{
if (pWP)
{
pWP->irrelevant_change_set(change->sequenceNumber);
NotifyChanges(pWP);
send_ack_if_datasharing(this, mp_history, pWP, change->sequenceNumber);
}
return true;
}
// Ask the pool for a cache change
CacheChange_t* change_to_add = nullptr;
if (!change_pool_->reserve_cache(change_to_add))
{
EPROSIMA_LOG_WARNING(RTPS_MSG_IN,
IDSTRING "Reached the maximum number of samples allowed by this reader's QoS. Rejecting change for reader: " <<
m_guid );
return false;
}
// Copy metadata to reserved change
change_to_add->copy_not_memcpy(change);
// Ask payload pool to copy the payload
IPayloadPool* payload_owner = change->payload_owner();
if (is_datasharing_compatible_ && datasharing_listener_->writer_is_matched(change->writerGUID))
{
// We may receive the change from the listener (with owner a ReaderPool) or intraprocess (with owner a WriterPool)
ReaderPool* datasharing_pool = dynamic_cast<ReaderPool*>(payload_owner);
if (!datasharing_pool)
{
datasharing_pool = datasharing_listener_->get_pool_for_writer(change->writerGUID).get();
}
if (!datasharing_pool)
{
EPROSIMA_LOG_WARNING(RTPS_MSG_IN, IDSTRING "Problem copying DataSharing CacheChange from writer "
<< change->writerGUID);
change_pool_->release_cache(change_to_add);
return false;
}
datasharing_pool->get_payload(change->serializedPayload, payload_owner, *change_to_add);
}
else if (payload_pool_->get_payload(change->serializedPayload, payload_owner, *change_to_add))
{
change->payload_owner(payload_owner);
}
else
{
EPROSIMA_LOG_WARNING(RTPS_MSG_IN, IDSTRING "Problem copying CacheChange, received data is: "
<< change->serializedPayload.length << " bytes and max size in reader "
<< m_guid << " is "
<< (fixed_payload_size_ > 0 ? fixed_payload_size_ : std::numeric_limits<uint32_t>::max()));
change_pool_->release_cache(change_to_add);
return false;
}
// Perform reception of cache change
if (!change_received(change_to_add, pWP, unknown_missing_changes_up_to))
{
EPROSIMA_LOG_INFO(RTPS_MSG_IN,
IDSTRING "Change " << change_to_add->sequenceNumber << " not added to history");
change_to_add->payload_owner()->release_payload(*change_to_add);
change_pool_->release_cache(change_to_add);
return false;
}
}
return true;
}
return false;
}
bool StatefulReader::processDataFragMsg(
CacheChange_t* incomingChange,
uint32_t sampleSize,
uint32_t fragmentStartingNum,
uint16_t fragmentsInSubmessage)
{
WriterProxy* pWP = nullptr;
assert(incomingChange);
std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
if (!is_alive_)
{
return false;
}
// TODO: see if we need manage framework fragmented DATA message
if (acceptMsgFrom(incomingChange->writerGUID, &pWP) && pWP)
{
// Always assert liveliness on scope exit
auto assert_liveliness_lambda = [&lock, this, incomingChange](void*)
{
lock.unlock(); // Avoid deadlock with LivelinessManager.
assert_writer_liveliness(incomingChange->writerGUID);
};
std::unique_ptr<void, decltype(assert_liveliness_lambda)> p{ this, assert_liveliness_lambda };
// Check if CacheChange was received.
if (!pWP->change_was_received(incomingChange->sequenceNumber))
{
EPROSIMA_LOG_INFO(RTPS_MSG_IN,
IDSTRING "Trying to add fragment " << incomingChange->sequenceNumber.to64long() << " TO reader: " <<
getGuid().entityId);
size_t changes_up_to = pWP->unknown_missing_changes_up_to(incomingChange->sequenceNumber);
bool will_never_be_accepted = false;
if (!mp_history->can_change_be_added_nts(incomingChange->writerGUID, sampleSize, changes_up_to,
will_never_be_accepted))
{
if (will_never_be_accepted)
{
pWP->irrelevant_change_set(incomingChange->sequenceNumber);
NotifyChanges(pWP);
send_ack_if_datasharing(this, mp_history, pWP, incomingChange->sequenceNumber);
}
return false;
}
CacheChange_t* change_to_add = incomingChange;
CacheChange_t* change_created = nullptr;
CacheChange_t* work_change = nullptr;
if (!mp_history->get_change(change_to_add->sequenceNumber, change_to_add->writerGUID, &work_change))
{
// A new change should be reserved
if (reserveCache(&work_change, sampleSize))
{
if (work_change->serializedPayload.max_size < sampleSize)
{
releaseCache(work_change);
work_change = nullptr;
}
else
{
work_change->copy_not_memcpy(change_to_add);
work_change->serializedPayload.length = sampleSize;
work_change->setFragmentSize(change_to_add->getFragmentSize(), true);
change_created = work_change;
}
}
}
if (work_change != nullptr)
{
work_change->add_fragments(change_to_add->serializedPayload, fragmentStartingNum,
fragmentsInSubmessage);
}
// If this is the first time we have received fragments for this change, add it to history
if (change_created != nullptr)
{
if (!change_received(change_created, pWP, changes_up_to))
{
EPROSIMA_LOG_INFO(RTPS_MSG_IN,
IDSTRING "MessageReceiver not add change " << change_created->sequenceNumber.to64long());
releaseCache(change_created);
work_change = nullptr;
}
}
// If change has been fully reassembled, mark as received and add notify user
if (work_change != nullptr && work_change->is_fully_assembled())
{
fastdds::dds::SampleRejectedStatusKind rejection_reason;
if (mp_history->completed_change(work_change, changes_up_to, rejection_reason))
{
pWP->received_change_set(work_change->sequenceNumber);
// Temporarilly assign the inline qos while evaluating the data filter
work_change->inline_qos = incomingChange->inline_qos;
bool filtered_out = data_filter_ && !data_filter_->is_relevant(*work_change, m_guid);
work_change->inline_qos = SerializedPayload_t();
if (filtered_out)
{
mp_history->remove_change(work_change);
}
NotifyChanges(pWP);
}
else
{
bool has_to_notify = false;
if (fastdds::dds::NOT_REJECTED != rejection_reason)
{
if (getListener())
{
getListener()->on_sample_rejected((RTPSReader*)this, rejection_reason, work_change);
}
/* Special case: rejected by REJECTED_BY_INSTANCES_LIMIT should never be received again.
*/
if (fastdds::dds::REJECTED_BY_INSTANCES_LIMIT == rejection_reason)
{
pWP->irrelevant_change_set(work_change->sequenceNumber);
has_to_notify = true;
}
}
History::const_iterator chit = mp_history->find_change_nts(work_change);
if (chit != mp_history->changesEnd())
{
mp_history->remove_change_nts(chit);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_READER, "Change should exist but didn't find it");
}
if (has_to_notify)
{
NotifyChanges(pWP);
}
}
}
}
}
return true;
}
bool StatefulReader::processHeartbeatMsg(
const GUID_t& writerGUID,
uint32_t hbCount,
const SequenceNumber_t& firstSN,
const SequenceNumber_t& lastSN,
bool finalFlag,
bool livelinessFlag)
{
WriterProxy* writer = nullptr;
std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
if (!is_alive_)
{
return false;
}
if (acceptMsgFrom(writerGUID, &writer) && writer)
{
bool assert_liveliness = false;
int32_t current_sample_lost = 0;
if (writer->process_heartbeat(
hbCount, firstSN, lastSN, finalFlag, livelinessFlag, disable_positive_acks_, assert_liveliness,
current_sample_lost))
{
mp_history->remove_fragmented_changes_until(firstSN, writerGUID);
if (0 < current_sample_lost)
{
if (getListener() != nullptr)
{
getListener()->on_sample_lost((RTPSReader*)this, current_sample_lost);
}
}
// Maybe now we have to notify user from new CacheChanges.
NotifyChanges(writer);
// Try to assert liveliness if requested by proxy's logic
if (assert_liveliness)
{
if (liveliness_lease_duration_ < c_TimeInfinite)
{
if (liveliness_kind_ == MANUAL_BY_TOPIC_LIVELINESS_QOS ||
writer->liveliness_kind() == MANUAL_BY_TOPIC_LIVELINESS_QOS)
{
auto wlp = this->mp_RTPSParticipant->wlp();
if ( wlp != nullptr)
{
lock.unlock(); // Avoid deadlock with LivelinessManager.
wlp->sub_liveliness_manager_->assert_liveliness(
writerGUID,
liveliness_kind_,
liveliness_lease_duration_);
}
else
{
EPROSIMA_LOG_ERROR(RTPS_LIVELINESS, "Finite liveliness lease duration but WLP not enabled");
}
}
}
}
}
return true;
}
return false;
}
bool StatefulReader::processGapMsg(
const GUID_t& writerGUID,
const SequenceNumber_t& gapStart,
const SequenceNumberSet_t& gapList)
{
WriterProxy* pWP = nullptr;
std::unique_lock<RecursiveTimedMutex> lock(mp_mutex);
if (!is_alive_)
{
return false;
}
if (acceptMsgFrom(writerGUID, &pWP) && pWP)
{
// TODO (Miguel C): Refactor this inside WriterProxy
SequenceNumber_t auxSN;
SequenceNumber_t finalSN = gapList.base() - 1;
History::const_iterator history_iterator = mp_history->changesBegin();
for (auxSN = gapStart; auxSN <= finalSN; auxSN++)
{
if (pWP->irrelevant_change_set(auxSN))
{
CacheChange_t* to_remove = nullptr;
auto ret_iterator = findCacheInFragmentedProcess(auxSN, pWP->guid(), &to_remove, history_iterator);
if (to_remove != nullptr)
{
// we called the History version to avoid callbacks
history_iterator = mp_history->History::remove_change_nts(ret_iterator);
}
else if (ret_iterator != mp_history->changesEnd())
{
history_iterator = ret_iterator;
}
}
}
gapList.for_each(
[&](SequenceNumber_t it)
{
if (pWP->irrelevant_change_set(it))
{
CacheChange_t* to_remove = nullptr;
auto ret_iterator =
findCacheInFragmentedProcess(auxSN, pWP->guid(), &to_remove, history_iterator);
if (to_remove != nullptr)
{
// we called the History version to avoid callbacks
history_iterator = mp_history->History::remove_change_nts(ret_iterator);
}
else if (ret_iterator != mp_history->changesEnd())
{
history_iterator = ret_iterator;
}
}
});
// Maybe now we have to notify user from new CacheChanges.
NotifyChanges(pWP);
return true;
}
return false;
}
bool StatefulReader::acceptMsgFrom(
const GUID_t& writerId,
WriterProxy** wp) const
{
assert(wp != nullptr);
for (WriterProxy* it : matched_writers_)
{
if (it->guid() == writerId && it->is_alive())
{
*wp = it;
return true;
}
}
// Check if it's a framework's one. In this case, m_acceptMessagesFromUnkownWriters
// is an enabler for the trusted entity comparison
if (m_acceptMessagesFromUnkownWriters
&& (writerId.entityId == m_trustedWriterEntityId))
{
*wp = nullptr;
return true;
}
return false;
}
bool StatefulReader::change_removed_by_history(
CacheChange_t* a_change,
WriterProxy* wp)
{
std::lock_guard<RecursiveTimedMutex> guard(mp_mutex);
if (is_alive_)
{
if (a_change->is_fully_assembled())
{
if (!a_change->isRead &&
get_last_notified(a_change->writerGUID) >= a_change->sequenceNumber)
{
if (0 < total_unread_)
{
--total_unread_;
}
}
WriterProxy* proxy = wp;
if (nullptr == proxy)
{
if (!findWriterProxy(a_change->writerGUID, &proxy))
{
return false;
}
}
if (nullptr != proxy)
{
send_ack_if_datasharing(this, mp_history, proxy, a_change->sequenceNumber);
}
}
else
{
/* A not fully assembled fragmented sample may be removed when receiving a newer sample and KEEP_LAST
* policy. The WriterProxy should consider it as irrelevant to avoid an infinite loop asking for it.
*/
WriterProxy* proxy = wp;
if (nullptr == proxy)
{
if (!findWriterProxy(a_change->writerGUID, &proxy))
{
return false;
}
proxy->irrelevant_change_set(a_change->sequenceNumber);
send_ack_if_datasharing(this, mp_history, proxy, a_change->sequenceNumber);
}
}
return true;
}
//Simulate a datasharing notification to process any pending payloads that were waiting due to full history
if (is_datasharing_compatible_)
{
datasharing_listener_->notify(false);
}