-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathRTPSParticipantImpl.cpp
More file actions
2413 lines (2135 loc) · 81.5 KB
/
Copy pathRTPSParticipantImpl.cpp
File metadata and controls
2413 lines (2135 loc) · 81.5 KB
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 RTPSParticipant.cpp
*
*/
#include <rtps/participant/RTPSParticipantImpl.h>
#include <algorithm>
#include <functional>
#include <memory>
#include <mutex>
#include <sstream>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/rtps/attributes/ServerAttributes.h>
#include <fastdds/rtps/builtin/BuiltinProtocols.h>
#include <fastdds/rtps/builtin/discovery/endpoint/EDP.h>
#include <fastdds/rtps/builtin/discovery/participant/PDPSimple.h>
#include <fastdds/rtps/builtin/data/ParticipantProxyData.h>
#include <fastdds/rtps/builtin/liveliness/WLP.h>
#include <fastdds/rtps/history/WriterHistory.h>
#include <fastdds/rtps/messages/MessageReceiver.h>
#include <fastdds/rtps/participant/RTPSParticipant.h>
#include <fastdds/rtps/reader/StatelessReader.h>
#include <fastdds/rtps/reader/StatefulReader.h>
#include <fastdds/rtps/reader/StatelessPersistentReader.h>
#include <fastdds/rtps/reader/StatefulPersistentReader.h>
#include <fastdds/rtps/RTPSDomain.h>
#include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv6TransportDescriptor.h>
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastdds/rtps/writer/StatelessWriter.h>
#include <fastdds/rtps/writer/StatefulWriter.h>
#include <fastdds/rtps/writer/StatelessPersistentWriter.h>
#include <fastdds/rtps/writer/StatefulPersistentWriter.h>
#include <fastrtps/utils/IPFinder.h>
#include <fastrtps/utils/Semaphore.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <rtps/builtin/discovery/participant/PDPServer.hpp>
#include <rtps/builtin/discovery/participant/PDPClient.h>
#include <rtps/history/BasicPayloadPool.hpp>
#include <rtps/persistence/PersistenceService.h>
#include <statistics/rtps/GuidUtils.hpp>
namespace eprosima {
namespace fastrtps {
namespace rtps {
using UDPv4TransportDescriptor = fastdds::rtps::UDPv4TransportDescriptor;
using TCPTransportDescriptor = fastdds::rtps::TCPTransportDescriptor;
using SharedMemTransportDescriptor = fastdds::rtps::SharedMemTransportDescriptor;
static EntityId_t TrustedWriter(
const EntityId_t& reader)
{
return
(reader == c_EntityId_SPDPReader) ? c_EntityId_SPDPWriter :
(reader == c_EntityId_SEDPPubReader) ? c_EntityId_SEDPPubWriter :
(reader == c_EntityId_SEDPSubReader) ? c_EntityId_SEDPSubWriter :
(reader == c_EntityId_ReaderLiveliness) ? c_EntityId_WriterLiveliness :
c_EntityId_Unknown;
}
static bool should_be_intraprocess_only(
const RTPSParticipantAttributes& att)
{
return
xmlparser::XMLProfileManager::library_settings().intraprocess_delivery == INTRAPROCESS_FULL &&
att.builtin.discovery_config.ignoreParticipantFlags ==
(ParticipantFilteringFlags::FILTER_DIFFERENT_HOST | ParticipantFilteringFlags::FILTER_DIFFERENT_PROCESS);
}
static bool get_unique_flows_parameters(
const RTPSParticipantAttributes& part_att,
const EndpointAttributes& att,
bool& unique_flows,
uint16_t& initial_port,
uint16_t& final_port)
{
const std::string* value = PropertyPolicyHelper::find_property(att.properties, "fastdds.unique_network_flows");
unique_flows = (nullptr != value);
if (unique_flows)
{
// TODO (Miguel C): parse value to get port range
final_port = part_att.port.portBase;
initial_port = part_att.port.portBase - 400;
}
return true;
}
Locator_t& RTPSParticipantImpl::applyLocatorAdaptRule(
Locator_t& loc)
{
// This is a completely made up rule
// It is transport responsability to interpret this new port.
loc.port += m_att.port.participantIDGain;
return loc;
}
RTPSParticipantImpl::RTPSParticipantImpl(
uint32_t domain_id,
const RTPSParticipantAttributes& PParam,
const GuidPrefix_t& guidP,
const GuidPrefix_t& persistence_guid,
RTPSParticipant* par,
RTPSParticipantListener* plisten)
: domain_id_(domain_id)
, m_att(PParam)
, m_guid(guidP, c_EntityId_RTPSParticipant)
, mp_builtinProtocols(nullptr)
, mp_ResourceSemaphore(new Semaphore(0))
, IdCounter(0)
, type_check_fn_(nullptr)
, client_override_(false)
, internal_metatraffic_locators_(false)
, internal_default_locators_(false)
#if HAVE_SECURITY
, m_security_manager(this)
#endif // if HAVE_SECURITY
, mp_participantListener(plisten)
, mp_userParticipant(par)
, mp_mutex(new std::recursive_mutex())
, is_intraprocess_only_(should_be_intraprocess_only(PParam))
, has_shm_transport_(false)
{
if (c_GuidPrefix_Unknown != persistence_guid)
{
m_persistence_guid = GUID_t(persistence_guid, c_EntityId_RTPSParticipant);
}
// Builtin transports by default
if (PParam.useBuiltinTransports)
{
UDPv4TransportDescriptor descriptor;
descriptor.sendBufferSize = m_att.sendSocketBufferSize;
descriptor.receiveBufferSize = m_att.listenSocketBufferSize;
m_network_Factory.RegisterTransport(&descriptor, &m_att.properties);
#ifdef SHM_TRANSPORT_BUILTIN
SharedMemTransportDescriptor shm_transport;
// We assume (Linux) UDP doubles the user socket buffer size in kernel, so
// the equivalent segment size in SHM would be socket buffer size x 2
auto segment_size_udp_equivalent =
std::max(m_att.sendSocketBufferSize, m_att.listenSocketBufferSize) * 2;
shm_transport.segment_size(segment_size_udp_equivalent);
// Use same default max_message_size on both UDP and SHM
shm_transport.max_message_size(descriptor.max_message_size());
has_shm_transport_ |= m_network_Factory.RegisterTransport(&shm_transport);
#endif // ifdef SHM_TRANSPORT_BUILTIN
}
// BACKUP servers guid is its persistence one
if (PParam.builtin.discovery_config.discoveryProtocol == DiscoveryProtocol::BACKUP)
{
m_persistence_guid = m_guid;
}
// Store the Guid in string format.
std::stringstream guid_sstr;
guid_sstr << m_guid;
guid_str_ = guid_sstr.str();
// Client-server discovery protocol requires that every TCP transport has a listening port
switch (PParam.builtin.discovery_config.discoveryProtocol)
{
case DiscoveryProtocol::BACKUP:
case DiscoveryProtocol::CLIENT:
case DiscoveryProtocol::SERVER:
case DiscoveryProtocol::SUPER_CLIENT:
// Verify if listening ports are provided
for (auto& transportDescriptor : PParam.userTransports)
{
TCPTransportDescriptor* pT = dynamic_cast<TCPTransportDescriptor*>(transportDescriptor.get());
if (pT && pT->listening_ports.empty())
{
logInfo(RTPS_PARTICIPANT,
"Participant " << m_att.getName() << " with GUID " << m_guid <<
" tries to use discovery server over TCP without providing a proper listening port.");
}
}
default:
break;
}
// User defined transports
for (const auto& transportDescriptor : PParam.userTransports)
{
if (m_network_Factory.RegisterTransport(transportDescriptor.get(), &m_att.properties))
{
has_shm_transport_ |=
(dynamic_cast<fastdds::rtps::SharedMemTransportDescriptor*>(transportDescriptor.get()) != nullptr);
}
else
{
// SHM transport could be disabled
if ((dynamic_cast<fastdds::rtps::SharedMemTransportDescriptor*>(transportDescriptor.get()) != nullptr))
{
logError(RTPS_PARTICIPANT,
"Unable to Register SHM Transport. SHM Transport is not supported in"
" the current platform.");
}
else
{
logError(RTPS_PARTICIPANT,
"User transport failed to register.");
}
}
}
mp_userParticipant->mp_impl = this;
mp_event_thr.init_thread();
if (!networkFactoryHasRegisteredTransports())
{
return;
}
/* If metatrafficMulticastLocatorList is empty, add mandatory default Locators
Else -> Take them */
// Creation of metatraffic locator and receiver resources
uint32_t metatraffic_multicast_port = m_att.port.getMulticastPort(domain_id_);
uint32_t metatraffic_unicast_port = m_att.port.getUnicastPort(domain_id_,
static_cast<uint32_t>(m_att.participantID));
uint32_t meta_multicast_port_for_check = metatraffic_multicast_port;
/* INSERT DEFAULT MANDATORY MULTICAST LOCATORS HERE */
if (m_att.builtin.metatrafficMulticastLocatorList.empty() && m_att.builtin.metatrafficUnicastLocatorList.empty())
{
get_default_metatraffic_locators();
internal_metatraffic_locators_ = true;
}
else
{
if (0 < m_att.builtin.metatrafficMulticastLocatorList.size() &&
0 != m_att.builtin.metatrafficMulticastLocatorList.begin()->port)
{
meta_multicast_port_for_check = m_att.builtin.metatrafficMulticastLocatorList.begin()->port;
}
std::for_each(m_att.builtin.metatrafficMulticastLocatorList.begin(),
m_att.builtin.metatrafficMulticastLocatorList.end(), [&](Locator_t& locator)
{
m_network_Factory.fillMetatrafficMulticastLocator(locator, metatraffic_multicast_port);
});
m_network_Factory.NormalizeLocators(m_att.builtin.metatrafficMulticastLocatorList);
std::for_each(m_att.builtin.metatrafficUnicastLocatorList.begin(),
m_att.builtin.metatrafficUnicastLocatorList.end(), [&](Locator_t& locator)
{
m_network_Factory.fillMetatrafficUnicastLocator(locator, metatraffic_unicast_port);
});
m_network_Factory.NormalizeLocators(m_att.builtin.metatrafficUnicastLocatorList);
}
// Initial peers
if (m_att.builtin.initialPeersList.empty())
{
m_att.builtin.initialPeersList = m_att.builtin.metatrafficMulticastLocatorList;
}
else
{
LocatorList_t initial_peers;
initial_peers.swap(m_att.builtin.initialPeersList);
std::for_each(initial_peers.begin(), initial_peers.end(),
[&](Locator_t& locator)
{
m_network_Factory.configureInitialPeerLocator(domain_id_, locator, m_att);
});
}
// Creation of user locator and receiver resources
//If no default locators are defined we define some.
/* The reasoning here is the following.
If the parameters of the RTPS Participant don't hold default listening locators for the creation
of Endpoints, we make some for Unicast only.
If there is at least one listen locator of any kind, we do not create any default ones.
If there are no sending locators defined, we create default ones for the transports we implement.
*/
if (m_att.defaultUnicastLocatorList.empty() && m_att.defaultMulticastLocatorList.empty())
{
//Default Unicast Locators in case they have not been provided
/* INSERT DEFAULT UNICAST LOCATORS FOR THE PARTICIPANT */
get_default_unicast_locators();
internal_default_locators_ = true;
logInfo(RTPS_PARTICIPANT, m_att.getName() << " Created with NO default Unicast Locator List, adding Locators:"
<< m_att.defaultUnicastLocatorList);
}
else
{
// Locator with port 0, calculate port.
std::for_each(m_att.defaultUnicastLocatorList.begin(), m_att.defaultUnicastLocatorList.end(),
[&](Locator_t& loc)
{
m_network_Factory.fill_default_locator_port(domain_id_, loc, m_att, false);
});
m_network_Factory.NormalizeLocators(m_att.defaultUnicastLocatorList);
std::for_each(m_att.defaultMulticastLocatorList.begin(), m_att.defaultMulticastLocatorList.end(),
[&](Locator_t& loc)
{
m_network_Factory.fill_default_locator_port(domain_id_, loc, m_att, true);
});
}
#if HAVE_SECURITY
// Start security
if (!m_security_manager.init(security_attributes_, PParam.properties,
m_is_security_active))
{
// Participant will be deleted, no need to allocate buffers or create builtin endpoints
return;
}
#endif // if HAVE_SECURITY
if (is_intraprocess_only())
{
m_att.builtin.metatrafficUnicastLocatorList.clear();
m_att.defaultUnicastLocatorList.clear();
m_att.defaultMulticastLocatorList.clear();
}
createReceiverResources(m_att.builtin.metatrafficMulticastLocatorList, true, false);
createReceiverResources(m_att.builtin.metatrafficUnicastLocatorList, true, false);
createReceiverResources(m_att.defaultUnicastLocatorList, true, false);
createReceiverResources(m_att.defaultMulticastLocatorList, true, false);
// Check metatraffic multicast port
if (0 < m_att.builtin.metatrafficMulticastLocatorList.size() &&
m_att.builtin.metatrafficMulticastLocatorList.begin()->port != meta_multicast_port_for_check)
{
logWarning(RTPS_PARTICIPANT,
"Metatraffic multicast port " << meta_multicast_port_for_check << " cannot be opened."
" It may is opened by another application. Discovery may fail.");
}
bool allow_growing_buffers = m_att.allocation.send_buffers.dynamic;
size_t num_send_buffers = m_att.allocation.send_buffers.preallocated_number;
if (num_send_buffers == 0)
{
// Two buffers (user, events)
num_send_buffers = 2;
// Add one buffer per reception thread
num_send_buffers += m_receiverResourcelist.size();
}
// Create buffer pool
send_buffers_.reset(new SendBuffersManager(num_send_buffers, allow_growing_buffers));
send_buffers_->init(this);
// Initialize flow controller factory.
// This must be done after initiate network layer.
flow_controller_factory_.init(this);
// Support old API
if (PParam.throughputController.bytesPerPeriod != UINT32_MAX && PParam.throughputController.periodMillisecs != 0)
{
fastdds::rtps::FlowControllerDescriptor old_descriptor;
old_descriptor.name = guid_str_.c_str();
old_descriptor.max_bytes_per_period = PParam.throughputController.bytesPerPeriod;
old_descriptor.period_ms = PParam.throughputController.periodMillisecs;
flow_controller_factory_.register_flow_controller(old_descriptor);
}
// Register user's flow controllers.
for (auto flow_controller_desc : m_att.flow_controllers)
{
flow_controller_factory_.register_flow_controller(*flow_controller_desc.get());
}
#if HAVE_SECURITY
if (m_is_security_active)
{
m_is_security_active = m_security_manager.create_entities();
if (!m_is_security_active)
{
// Participant will be deleted, no need to create builtin endpoints
return;
}
}
#endif // if HAVE_SECURITY
mp_builtinProtocols = new BuiltinProtocols();
// Initialize builtin protocols
if (!mp_builtinProtocols->initBuiltinProtocols(this, m_att.builtin))
{
logError(RTPS_PARTICIPANT, "The builtin protocols were not correctly initialized");
return;
}
if (c_GuidPrefix_Unknown != persistence_guid)
{
logInfo(RTPS_PARTICIPANT, "RTPSParticipant \"" << m_att.getName() << "\" with guidPrefix: " << m_guid.guidPrefix
<< " and persistence guid: " << persistence_guid);
}
else
{
logInfo(RTPS_PARTICIPANT,
"RTPSParticipant \"" << m_att.getName() << "\" with guidPrefix: " << m_guid.guidPrefix);
}
initialized_ = true;
}
RTPSParticipantImpl::RTPSParticipantImpl(
uint32_t domain_id,
const RTPSParticipantAttributes& PParam,
const GuidPrefix_t& guidP,
RTPSParticipant* par,
RTPSParticipantListener* plisten)
: RTPSParticipantImpl(domain_id, PParam, guidP, c_GuidPrefix_Unknown, par, plisten)
{
}
void RTPSParticipantImpl::enable()
{
mp_builtinProtocols->enable();
//Start reception
for (auto& receiver : m_receiverResourcelist)
{
receiver.Receiver->RegisterReceiver(receiver.mp_receiver);
}
}
void RTPSParticipantImpl::disable()
{
// Ensure that other participants will not accidentally discover this one
if (mp_builtinProtocols && mp_builtinProtocols->mp_PDP)
{
mp_builtinProtocols->stopRTPSParticipantAnnouncement();
}
// Disable Retries on Transports
m_network_Factory.Shutdown();
// Safely abort threads.
for (auto& block : m_receiverResourcelist)
{
block.Receiver->UnregisterReceiver(block.mp_receiver);
block.disable();
}
{
std::lock_guard<std::recursive_mutex> lock(*mp_mutex);
while (m_userReaderList.size() > 0)
{
deleteUserEndpoint(static_cast<Endpoint*>(*m_userReaderList.begin()));
}
while (m_userWriterList.size() > 0)
{
deleteUserEndpoint(static_cast<Endpoint*>(*m_userWriterList.begin()));
}
}
delete(mp_builtinProtocols);
mp_builtinProtocols = nullptr;
}
const std::vector<RTPSWriter*>& RTPSParticipantImpl::getAllWriters() const
{
return m_allWriterList;
}
const std::vector<RTPSReader*>& RTPSParticipantImpl::getAllReaders() const
{
return m_allReaderList;
}
RTPSParticipantImpl::~RTPSParticipantImpl()
{
disable();
#if HAVE_SECURITY
m_security_manager.destroy();
#endif // if HAVE_SECURITY
// Destruct message receivers
for (auto& block : m_receiverResourcelist)
{
delete block.mp_receiver;
}
m_receiverResourcelist.clear();
delete mp_ResourceSemaphore;
delete mp_userParticipant;
mp_userParticipant = nullptr;
send_resource_list_.clear();
delete mp_mutex;
}
template <EndpointKind_t kind, octet no_key, octet with_key>
bool RTPSParticipantImpl::preprocess_endpoint_attributes(
const EntityId_t& entity_id,
uint32_t& id_counter,
EndpointAttributes& att,
EntityId_t& entId)
{
const char* debug_label = (att.endpointKind == WRITER ? "writer" : "reader");
if (!att.unicastLocatorList.isValid())
{
logError(RTPS_PARTICIPANT, "Unicast Locator List for " << debug_label << " contains invalid Locator");
return false;
}
if (!att.multicastLocatorList.isValid())
{
logError(RTPS_PARTICIPANT, "Multicast Locator List for " << debug_label << " contains invalid Locator");
return false;
}
if (!att.remoteLocatorList.isValid())
{
logError(RTPS_PARTICIPANT, "Remote Locator List for " << debug_label << " contains invalid Locator");
return false;
}
if (entity_id == c_EntityId_Unknown)
{
if (att.topicKind == NO_KEY)
{
entId.value[3] = (-2 == att.getUserDefinedID() && 0 < att.getEntityID()) ? (0x60) | no_key : no_key;
}
else if (att.topicKind == WITH_KEY)
{
entId.value[3] = (-2 == att.getUserDefinedID() && 0 < att.getEntityID()) ? (0x60) | with_key : with_key;
}
uint32_t idnum;
if (att.getEntityID() > 0)
{
idnum = static_cast<uint32_t>(att.getEntityID());
}
else
{
idnum = ++id_counter;
}
entId.value[2] = octet(idnum);
entId.value[1] = octet(idnum >> 8);
entId.value[0] = octet(idnum >> 16);
}
else
{
entId = entity_id;
}
if (att.persistence_guid == c_Guid_Unknown)
{
// Try to load persistence_guid from property
const std::string* persistence_guid_property = PropertyPolicyHelper::find_property(
att.properties, "dds.persistence.guid");
if (persistence_guid_property != nullptr)
{
// Load persistence_guid from property
std::istringstream(persistence_guid_property->c_str()) >> att.persistence_guid;
if (att.persistence_guid == c_Guid_Unknown)
{
// Wrongly configured property
logError(RTPS_PARTICIPANT, "Cannot configure " << debug_label << "'s persistence GUID from '"
<< persistence_guid_property->c_str()
<< "'. Wrong input");
return false;
}
}
}
// Error log level can be disable. Avoid unused warning
static_cast<void>(debug_label);
return true;
}
template<typename Functor>
bool RTPSParticipantImpl::create_writer(
RTPSWriter** writer_out,
WriterAttributes& param,
const EntityId_t& entity_id,
bool is_builtin,
const Functor& callback)
{
std::string type = (param.endpoint.reliabilityKind == RELIABLE) ? "RELIABLE" : "BEST_EFFORT";
logInfo(RTPS_PARTICIPANT, "Creating writer of type " << type);
EntityId_t entId;
if (!preprocess_endpoint_attributes<WRITER, 0x03, 0x02>(entity_id, IdCounter, param.endpoint, entId))
{
return false;
}
if (existsEntityId(entId, WRITER))
{
logError(RTPS_PARTICIPANT,
"A writer with the same entityId already exists in this RTPSParticipant");
return false;
}
GUID_t guid(m_guid.guidPrefix, entId);
fastdds::rtps::FlowController* flow_controller = nullptr;
const char* flow_controller_name = param.flow_controller_name;
// Support of old flow controller style.
if (param.throughputController.bytesPerPeriod != UINT32_MAX && param.throughputController.periodMillisecs != 0)
{
flow_controller_name = guid_str_.c_str();
if (ASYNCHRONOUS_WRITER == param.mode)
{
fastdds::rtps::FlowControllerDescriptor old_descriptor;
old_descriptor.name = guid_str_.c_str();
old_descriptor.max_bytes_per_period = param.throughputController.bytesPerPeriod;
old_descriptor.period_ms = param.throughputController.periodMillisecs;
flow_controller_factory_.register_flow_controller(old_descriptor);
flow_controller = flow_controller_factory_.retrieve_flow_controller(guid_str_.c_str(), param);
}
else
{
logWarning(RTPS_PARTICIPANT,
"Throughput flow controller was configured while writer's publish mode is configured as synchronous." \
"Throughput flow controller configuration is not taken into account.")
}
}
if (m_att.throughputController.bytesPerPeriod != UINT32_MAX && m_att.throughputController.periodMillisecs != 0)
{
if (ASYNCHRONOUS_WRITER == param.mode && nullptr == flow_controller)
{
flow_controller_name = guid_str_.c_str();
flow_controller = flow_controller_factory_.retrieve_flow_controller(guid_str_, param);
}
else
{
logWarning(RTPS_PARTICIPANT,
"Throughput flow controller was configured while writer's publish mode is configured as synchronous." \
"Throughput flow controller configuration is not taken into account.")
}
}
// Retrieve flow controller.
// If not default flow controller, publish_mode must be asynchronously.
if (nullptr == flow_controller &&
(fastdds::rtps::FASTDDS_FLOW_CONTROLLER_DEFAULT == flow_controller_name ||
ASYNCHRONOUS_WRITER == param.mode))
{
flow_controller = flow_controller_factory_.retrieve_flow_controller(flow_controller_name, param);
}
if (nullptr == flow_controller)
{
if (fastdds::rtps::FASTDDS_FLOW_CONTROLLER_DEFAULT != flow_controller_name &&
SYNCHRONOUS_WRITER == param.mode)
{
logError(RTPS_PARTICIPANT, "Cannot use a flow controller in synchronously publication mode.");
}
else
{
logError(RTPS_PARTICIPANT, "Cannot create the writer. Couldn't find flow controller "
<< flow_controller_name << " for writer.");
}
return false;
}
// Check for unique_network_flows feature
if (nullptr != PropertyPolicyHelper::find_property(param.endpoint.properties, "fastdds.unique_network_flows"))
{
logError(RTPS_PARTICIPANT, "Unique network flows not supported on writers");
return false;
}
// Special case for DiscoveryProtocol::BACKUP, which abuses persistence guid
GUID_t former_persistence_guid = param.endpoint.persistence_guid;
if (param.endpoint.persistence_guid == c_Guid_Unknown)
{
if (m_persistence_guid != c_Guid_Unknown)
{
// Generate persistence guid from participant persistence guid
param.endpoint.persistence_guid = GUID_t(
m_persistence_guid.guidPrefix,
entity_id);
}
}
// Get persistence service
IPersistenceService* persistence = nullptr;
if (!get_persistence_service(is_builtin, param.endpoint, persistence))
{
return false;
}
normalize_endpoint_locators(param.endpoint);
RTPSWriter* SWriter = nullptr;
SWriter = callback(guid, param, flow_controller, persistence, param.endpoint.reliabilityKind == RELIABLE);
// restore attributes
param.endpoint.persistence_guid = former_persistence_guid;
if (SWriter == nullptr)
{
return false;
}
if (!SWriter->is_pool_initialized())
{
delete(SWriter);
return false;
}
#if HAVE_SECURITY
if (!is_builtin)
{
if (!m_security_manager.register_local_writer(SWriter->getGuid(),
param.endpoint.properties, SWriter->getAttributes().security_attributes()))
{
delete(SWriter);
return false;
}
}
else
{
if (!m_security_manager.register_local_builtin_writer(SWriter->getGuid(),
SWriter->getAttributes().security_attributes()))
{
delete(SWriter);
return false;
}
}
#endif // if HAVE_SECURITY
createSendResources(SWriter);
if (param.endpoint.reliabilityKind == RELIABLE)
{
if (!createAndAssociateReceiverswithEndpoint(SWriter))
{
delete(SWriter);
return false;
}
}
{
std::lock_guard<std::mutex> lock(endpoints_list_mutex);
m_allWriterList.push_back(SWriter);
}
if (!is_builtin)
{
std::lock_guard<std::recursive_mutex> guard(*mp_mutex);
m_userWriterList.push_back(SWriter);
}
*writer_out = SWriter;
#ifdef FASTDDS_STATISTICS
if (!is_builtin)
{
// Register all compatible statistical listeners
for_each_listener([this, &guid](Key listener)
{
if (are_writers_involved(listener->mask()))
{
register_in_writer(listener->get_shared_ptr(), guid);
}
});
}
#endif // FASTDDS_STATISTICS
return true;
}
template <typename Functor>
bool RTPSParticipantImpl::create_reader(
RTPSReader** reader_out,
ReaderAttributes& param,
const EntityId_t& entity_id,
bool is_builtin,
bool enable,
const Functor& callback)
{
std::string type = (param.endpoint.reliabilityKind == RELIABLE) ? "RELIABLE" : "BEST_EFFORT";
logInfo(RTPS_PARTICIPANT, "Creating reader of type " << type);
EntityId_t entId;
if (!preprocess_endpoint_attributes<READER, 0x04, 0x07>(entity_id, IdCounter, param.endpoint, entId))
{
return false;
}
if (existsEntityId(entId, READER))
{
logError(RTPS_PARTICIPANT,
"A reader with the same entityId already exists in this RTPSParticipant");
return false;
}
// Special case for DiscoveryProtocol::BACKUP, which abuses persistence guid
GUID_t former_persistence_guid = param.endpoint.persistence_guid;
if (param.endpoint.persistence_guid == c_Guid_Unknown)
{
if (m_persistence_guid != c_Guid_Unknown)
{
// Generate persistence guid from participant persistence guid
param.endpoint.persistence_guid = GUID_t(
m_persistence_guid.guidPrefix,
entity_id);
}
}
// Get persistence service
IPersistenceService* persistence = nullptr;
if (!get_persistence_service(is_builtin, param.endpoint, persistence))
{
return false;
}
// Check for unique_network_flows feature
bool request_unique_flows = false;
uint16_t initial_port = 0;
uint16_t final_port = 0;
if (!get_unique_flows_parameters(m_att, param.endpoint, request_unique_flows, initial_port, final_port))
{
return false;
}
normalize_endpoint_locators(param.endpoint);
RTPSReader* SReader = nullptr;
GUID_t guid(m_guid.guidPrefix, entId);
SReader = callback(guid, param, persistence, param.endpoint.reliabilityKind == RELIABLE);
// restore attributes
param.endpoint.persistence_guid = former_persistence_guid;
if (SReader == nullptr)
{
return false;
}
#if HAVE_SECURITY
if (!is_builtin)
{
if (!m_security_manager.register_local_reader(SReader->getGuid(),
param.endpoint.properties, SReader->getAttributes().security_attributes()))
{
delete(SReader);
return false;
}
}
else
{
if (!m_security_manager.register_local_builtin_reader(SReader->getGuid(),
SReader->getAttributes().security_attributes()))
{
delete(SReader);
return false;
}
}
#endif // if HAVE_SECURITY
if (param.endpoint.reliabilityKind == RELIABLE)
{
createSendResources(SReader);
}
if (is_builtin)
{
SReader->setTrustedWriter(TrustedWriter(SReader->getGuid().entityId));
}
if (enable)
{
if (!createAndAssociateReceiverswithEndpoint(SReader, request_unique_flows, initial_port, final_port))
{
delete(SReader);
return false;
}
}
{
std::lock_guard<std::mutex> lock(endpoints_list_mutex);
m_allReaderList.push_back(SReader);
}
if (!is_builtin)
{
std::lock_guard<std::recursive_mutex> guard(*mp_mutex);
m_userReaderList.push_back(SReader);
}
*reader_out = SReader;
#ifdef FASTDDS_STATISTICS
if (!is_builtin)
{
// Register all compatible statistical listeners
for_each_listener([this, &guid](Key listener)
{
if (are_readers_involved(listener->mask()))
{
register_in_reader(listener->get_shared_ptr(), guid);
}
});
}
#endif // FASTDDS_STATISTICS
return true;
}
/*
*
* MAIN RTPSParticipant IMPL API
*
*/
bool RTPSParticipantImpl::createWriter(
RTPSWriter** WriterOut,
WriterAttributes& param,
WriterHistory* hist,
WriterListener* listen,
const EntityId_t& entityId,
bool isBuiltin)
{
auto callback = [hist, listen, this]
(const GUID_t& guid, WriterAttributes& param, fastdds::rtps::FlowController* flow_controller,
IPersistenceService* persistence, bool is_reliable) -> RTPSWriter*
{
if (is_reliable)
{
if (persistence != nullptr)
{
return new StatefulPersistentWriter(this, guid, param, flow_controller,
hist, listen, persistence);
}
else
{
return new StatefulWriter(this, guid, param, flow_controller,
hist, listen);
}
}
else
{
if (persistence != nullptr)
{
return new StatelessPersistentWriter(this, guid, param, flow_controller,
hist, listen, persistence);
}
else
{
return new StatelessWriter(this, guid, param, flow_controller,
hist, listen);
}
}
};
return create_writer(WriterOut, param, entityId, isBuiltin, callback);
}
bool RTPSParticipantImpl::createWriter(
RTPSWriter** WriterOut,
WriterAttributes& param,
const std::shared_ptr<IPayloadPool>& payload_pool,
WriterHistory* hist,
WriterListener* listen,
const EntityId_t& entityId,
bool isBuiltin)
{
if (!payload_pool)
{
logError(RTPS_PARTICIPANT, "Trying to create writer with null payload pool");
return false;
}
auto callback = [hist, listen, &payload_pool, this]
(const GUID_t& guid, WriterAttributes& param, fastdds::rtps::FlowController* flow_controller,
IPersistenceService* persistence, bool is_reliable) -> RTPSWriter*
{
if (is_reliable)
{
if (persistence != nullptr)
{
return new StatefulPersistentWriter(this, guid, param, payload_pool, flow_controller,
hist, listen, persistence);
}