-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathRTPSParticipantImpl.cpp
More file actions
3309 lines (2916 loc) · 114 KB
/
Copy pathRTPSParticipantImpl.cpp
File metadata and controls
3309 lines (2916 loc) · 114 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/core/ReturnCode.hpp>
#include <fastdds/dds/log/Log.hpp>
#include <fastdds/LibrarySettings.hpp>
#include <fastdds/rtps/attributes/BuiltinTransports.hpp>
#include <fastdds/rtps/attributes/ServerAttributes.h>
#include <fastdds/rtps/builtin/data/ParticipantProxyData.h>
#include <fastdds/rtps/common/EntityId_t.hpp>
#include <fastdds/rtps/common/LocatorList.hpp>
#include <fastdds/rtps/history/WriterHistory.h>
#include <fastdds/rtps/participant/ParticipantDiscoveryInfo.h>
#include <fastdds/rtps/participant/RTPSParticipant.h>
#include <fastdds/rtps/RTPSDomain.h>
#include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
#include <fastdds/rtps/transport/TCPv6TransportDescriptor.h>
#include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
#include <fastdds/rtps/writer/StatefulPersistentWriter.h>
#include <fastdds/rtps/writer/StatefulWriter.h>
#include <fastdds/rtps/writer/StatelessPersistentWriter.h>
#include <fastdds/rtps/writer/StatelessWriter.h>
#include <fastdds/utils/IPFinder.h>
#include <rtps/builtin/BuiltinProtocols.h>
#include <rtps/builtin/discovery/endpoint/EDP.h>
#include <rtps/builtin/discovery/participant/PDP.h>
#include <rtps/builtin/discovery/participant/PDPClient.h>
#include <rtps/builtin/discovery/participant/PDPServer.hpp>
#include <rtps/builtin/discovery/participant/PDPSimple.h>
#include <rtps/builtin/liveliness/WLP.h>
#include <rtps/history/BasicPayloadPool.hpp>
#include <rtps/messages/MessageReceiver.h>
#include <rtps/network/utils/external_locators.hpp>
#include <rtps/network/utils/netmask_filter.hpp>
#include <rtps/participant/RTPSParticipantImpl.h>
#include <rtps/persistence/PersistenceService.h>
#include <rtps/reader/StatefulPersistentReader.hpp>
#include <rtps/reader/StatefulReader.hpp>
#include <rtps/reader/StatelessPersistentReader.hpp>
#include <rtps/reader/StatelessReader.hpp>
#include <statistics/rtps/GuidUtils.hpp>
#include <utils/Semaphore.hpp>
#include <utils/string_utilities.hpp>
#include <utils/SystemInfo.hpp>
#include <utils/UnitsParser.hpp>
#include <xmlparser/XMLProfileManager.h>
#ifdef FASTDDS_STATISTICS
#include <statistics/rtps/monitor-service/MonitorService.hpp>
#endif // ifdef FASTDDS_STATISTICS
#if HAVE_SECURITY
#include <security/logging/LogTopic.h>
#endif // HAVE_SECURITY
namespace eprosima {
namespace fastrtps {
namespace rtps {
using UDPv4TransportDescriptor = fastdds::rtps::UDPv4TransportDescriptor;
using TCPTransportDescriptor = fastdds::rtps::TCPTransportDescriptor;
using SharedMemTransportDescriptor = fastdds::rtps::SharedMemTransportDescriptor;
using BuiltinTransports = fastdds::rtps::BuiltinTransports;
/**
* Parse the environment variable specifying the transports to instantiate and optional configuration options
* if the transport selected is LARGE_DATA.
*/
static void set_builtin_transports_from_env_var(
RTPSParticipantAttributes& attr)
{
static constexpr const char* env_var_name = "FASTDDS_BUILTIN_TRANSPORTS";
BuiltinTransports ret_val = BuiltinTransports::DEFAULT;
std::string env_value;
if (SystemInfo::get_env(env_var_name, env_value) == fastdds::dds::RETCODE_OK)
{
std::regex COMMON_REGEX(R"((\w+))");
std::regex OPTIONS_REGEX(
R"((\w+)\?(((max_msg_size|sockets_size)=(\d+)(\w*)&?)|(non_blocking=(\w+)&?)|(tcp_negotiation_timeout=(\d+)&?)){0,4})");
std::smatch mr;
if (std::regex_match(env_value, COMMON_REGEX, std::regex_constants::match_not_null))
{
// Only transport mode is specified
if (!get_element_enum_value(env_value.c_str(), ret_val,
"NONE", BuiltinTransports::NONE,
"DEFAULT", BuiltinTransports::DEFAULT,
"DEFAULTv6", BuiltinTransports::DEFAULTv6,
"SHM", BuiltinTransports::SHM,
"UDPv4", BuiltinTransports::UDPv4,
"UDPv6", BuiltinTransports::UDPv6,
"LARGE_DATA", BuiltinTransports::LARGE_DATA,
"LARGE_DATAv6", BuiltinTransports::LARGE_DATAv6))
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, "Wrong value '" << env_value << "' for environment variable '" <<
env_var_name << "'. Leaving as DEFAULT");
}
}
else if (std::regex_match(env_value, mr, OPTIONS_REGEX, std::regex_constants::match_not_null))
{
// Transport mode AND options are specified
std::regex msg_size_regex(R"((max_msg_size)=(\d+)(\w*))");
std::regex sockets_size_regex(R"((sockets_size)=(\d+)(\w*))");
std::regex non_blocking_regex(R"((non_blocking)=(true|false))");
std::regex tcp_timeout_regex(R"((tcp_negotiation_timeout)=(\d+))");
fastdds::rtps::BuiltinTransportsOptions options;
try
{
if (!get_element_enum_value(mr[1].str().c_str(), ret_val,
"NONE", BuiltinTransports::NONE,
"DEFAULT", BuiltinTransports::DEFAULT,
"DEFAULTv6", BuiltinTransports::DEFAULTv6,
"SHM", BuiltinTransports::SHM,
"UDPv4", BuiltinTransports::UDPv4,
"UDPv6", BuiltinTransports::UDPv6,
"LARGE_DATA", BuiltinTransports::LARGE_DATA,
"LARGE_DATAv6", BuiltinTransports::LARGE_DATAv6))
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, "Wrong value '" << env_value << "' for environment variable '" <<
env_var_name << "'. Leaving as DEFAULT");
}
// Max_msg_size parser
if (std::regex_search(env_value, mr, msg_size_regex, std::regex_constants::match_not_null))
{
std::string value = mr[2];
std::string unit = mr[3].str();
options.maxMessageSize = eprosima::fastdds::dds::utils::parse_value_and_units(value, unit);
}
// Sockets_size parser
if (std::regex_search(env_value, mr, sockets_size_regex, std::regex_constants::match_not_null))
{
std::string value = mr[2];
std::string unit = mr[3].str();
options.sockets_buffer_size = eprosima::fastdds::dds::utils::parse_value_and_units(value, unit);
}
// Non-blocking-send parser
if (std::regex_search(env_value, mr, non_blocking_regex, std::regex_constants::match_not_null))
{
options.non_blocking_send = mr[2] == "true";
}
// TCP_negotiation_timeout parser
if (std::regex_search(env_value, mr, tcp_timeout_regex, std::regex_constants::match_not_null))
{
options.tcp_negotiation_timeout = static_cast<uint32_t>(std::stoul(mr[2]));
}
attr.setup_transports(ret_val, options);
return;
}
catch (std::exception& e)
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
"Exception parsing environment variable: " << e.what() <<
" Leaving LARGE_DATA with default options.");
attr.setup_transports(ret_val);
return;
}
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, "Wrong value '" << env_value << "' for environment variable '" <<
env_var_name << "'. Leaving as DEFAULT");
}
}
attr.setup_transports(ret_val);
}
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 == fastdds::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 responsibility to interpret this new port.
uint16_t delta = m_att.port.participantIDGain;
if (metatraffic_unicast_port_ == loc.port)
{
metatraffic_unicast_port_ += delta;
}
loc.port += delta;
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)
, IdCounter(0)
, m_network_Factory(PParam)
, type_check_fn_(nullptr)
, client_override_(false)
, internal_metatraffic_locators_(false)
, internal_default_locators_(false)
#if HAVE_SECURITY
, m_security_manager(this, *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))
#ifdef FASTDDS_STATISTICS
, monitor_server_(nullptr)
, conns_observer_(nullptr)
#endif // if FASTDDS_STATISTICS
, has_shm_transport_(false)
, match_local_endpoints_(should_match_local_endpoints(PParam))
{
if (c_GuidPrefix_Unknown != persistence_guid)
{
m_persistence_guid = GUID_t(persistence_guid, c_EntityId_RTPSParticipant);
}
// Setup builtin transports
if (m_att.useBuiltinTransports)
{
set_builtin_transports_from_env_var(m_att);
}
// BACKUP servers guid is its persistence one
if (m_att.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 (m_att.builtin.discovery_config.discoveryProtocol)
{
case DiscoveryProtocol::BACKUP:
case DiscoveryProtocol::SERVER:
// Verify if listening ports are provided
for (auto& transportDescriptor : m_att.userTransports)
{
TCPTransportDescriptor* pT = dynamic_cast<TCPTransportDescriptor*>(transportDescriptor.get());
if (pT)
{
if (pT->listening_ports.empty())
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
"Participant " << m_att.getName() << " with GUID " << m_guid <<
" tries to create a TCP server for discovery server without providing a proper listening port.");
break;
}
if (!m_att.builtin.metatrafficUnicastLocatorList.empty())
{
std::for_each(m_att.builtin.metatrafficUnicastLocatorList.begin(),
m_att.builtin.metatrafficUnicastLocatorList.end(), [&](Locator_t& locator)
{
// TCP DS default logical port is the same as the physical one
if (locator.kind == LOCATOR_KIND_TCPv4 || locator.kind == LOCATOR_KIND_TCPv6)
{
if (IPLocator::getLogicalPort(locator) == 0)
{
IPLocator::setLogicalPort(locator, IPLocator::getPhysicalPort(locator));
}
}
});
}
}
}
break;
case DiscoveryProtocol::CLIENT:
case DiscoveryProtocol::SUPER_CLIENT:
// Verify if listening ports are provided
for (auto& transportDescriptor : m_att.userTransports)
{
TCPTransportDescriptor* pT = dynamic_cast<TCPTransportDescriptor*>(transportDescriptor.get());
if (pT)
{
if (pT->listening_ports.empty())
{
EPROSIMA_LOG_INFO(RTPS_PARTICIPANT,
"Participant " << m_att.getName() << " with GUID " << m_guid <<
" tries to create a TCP client for discovery server without providing a proper listening port." <<
" No TCP participants will be able to connect to this participant, but it will be able make connections.");
}
for (fastdds::rtps::RemoteServerAttributes& it : m_att.builtin.discovery_config.m_DiscoveryServers)
{
std::for_each(it.metatrafficUnicastLocatorList.begin(),
it.metatrafficUnicastLocatorList.end(), [&](Locator_t& locator)
{
// TCP DS default logical port is the same as the physical one
if (locator.kind == LOCATOR_KIND_TCPv4 || locator.kind == LOCATOR_KIND_TCPv6)
{
if (IPLocator::getLogicalPort(locator) == 0)
{
IPLocator::setLogicalPort(locator, IPLocator::getPhysicalPort(locator));
}
}
});
}
}
}
default:
break;
}
// User defined transports
for (const auto& transportDescriptor : m_att.userTransports)
{
bool register_transport = true;
// Lock user's transport descriptor since it could be modified during registration
transportDescriptor->lock();
auto socket_descriptor =
std::dynamic_pointer_cast<fastdds::rtps::SocketTransportDescriptor>(transportDescriptor);
fastdds::rtps::NetmaskFilterKind socket_descriptor_netmask_filter{};
if (socket_descriptor != nullptr)
{
// Copy original netmask filter value to restore it after registration
socket_descriptor_netmask_filter = socket_descriptor->netmask_filter;
if (!fastdds::rtps::network::netmask_filter::validate_and_transform(socket_descriptor->netmask_filter,
m_att.netmaskFilter))
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
"User transport failed to register. Provided descriptor's netmask filter ("
<< socket_descriptor->netmask_filter << ") is incompatible with participant's ("
<< m_att.netmaskFilter << ").");
register_transport = false;
}
}
bool transport_registered = register_transport && m_network_Factory.RegisterTransport(
transportDescriptor.get(), &m_att.properties, m_att.max_msg_size_no_frag);
if (socket_descriptor != nullptr)
{
// Restore original netmask filter value prior to unlock
socket_descriptor->netmask_filter = socket_descriptor_netmask_filter;
}
transportDescriptor->unlock();
if (transport_registered)
{
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))
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
"Unable to Register SHM Transport. SHM Transport is not supported in"
" the current platform.");
}
else
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
"User transport failed to register.");
}
}
}
mp_userParticipant->mp_impl = this;
uint32_t id_for_thread = static_cast<uint32_t>(m_att.participantID);
const fastdds::rtps::ThreadSettings& thr_config = m_att.timed_events_thread;
mp_event_thr.init_thread(thr_config, "dds.ev.%u", id_for_thread);
if (!networkFactoryHasRegisteredTransports())
{
return;
}
// Check netmask filtering preconditions
std::vector<fastdds::rtps::TransportNetmaskFilterInfo> netmask_filter_info =
m_network_Factory.netmask_filter_info();
std::string error_msg;
if (!fastdds::rtps::network::netmask_filter::check_preconditions(netmask_filter_info,
m_att.ignore_non_matching_locators,
error_msg) ||
!fastdds::rtps::network::netmask_filter::check_preconditions(netmask_filter_info,
m_att.builtin.metatraffic_external_unicast_locators,
error_msg) ||
!fastdds::rtps::network::netmask_filter::check_preconditions(netmask_filter_info,
m_att.default_external_unicast_locators, error_msg))
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, error_msg);
return;
}
#if HAVE_SECURITY
// Start security
if (!m_security_manager.init(
security_attributes_,
m_att.properties))
{
// Participant will be deleted, no need to allocate buffers or create builtin endpoints
return;
}
#endif // if HAVE_SECURITY
setup_meta_traffic();
setup_user_traffic();
setup_initial_peers();
setup_output_traffic();
#if HAVE_SECURITY
if (m_security_manager.is_security_active())
{
if (!m_security_manager.create_entities())
{
return;
}
}
#endif // if HAVE_SECURITY
// Copy NetworkFactory network_configuration to participant attributes prior to proxy creation
// NOTE: all transports already registered before
m_att.builtin.network_configuration = m_network_Factory.network_configuration();
mp_builtinProtocols = new BuiltinProtocols();
// Initialize builtin protocols
if (!mp_builtinProtocols->initBuiltinProtocols(this, m_att.builtin))
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, "The builtin protocols were not correctly initialized");
return;
}
if (c_GuidPrefix_Unknown != persistence_guid)
{
EPROSIMA_LOG_INFO(RTPS_PARTICIPANT,
"RTPSParticipant \"" << m_att.getName() << "\" with guidPrefix: " << m_guid.guidPrefix
<< " and persistence guid: " << persistence_guid);
}
else
{
EPROSIMA_LOG_INFO(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::setup_meta_traffic()
{
/* 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_);
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);
}
if (is_intraprocess_only())
{
m_att.builtin.metatrafficUnicastLocatorList.clear();
}
createReceiverResources(m_att.builtin.metatrafficUnicastLocatorList, true, false, true);
createReceiverResources(m_att.builtin.metatrafficMulticastLocatorList, false, false, true);
// Check metatraffic multicast port
if (0 < m_att.builtin.metatrafficMulticastLocatorList.size() &&
m_att.builtin.metatrafficMulticastLocatorList.begin()->port != meta_multicast_port_for_check)
{
EPROSIMA_LOG_WARNING(RTPS_PARTICIPANT,
"Metatraffic multicast port " << meta_multicast_port_for_check << " cannot be opened."
" It may is opened by another application. Discovery may fail.");
}
namespace external_locators = fastdds::rtps::network::external_locators;
external_locators::set_listening_locators(m_att.builtin.metatraffic_external_unicast_locators,
m_att.builtin.metatrafficUnicastLocatorList);
}
void RTPSParticipantImpl::setup_user_traffic()
{
// 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;
EPROSIMA_LOG_INFO(RTPS_PARTICIPANT,
m_att.getName() << " Created with NO default Unicast Locator List, adding Locators:"
<< m_att.defaultUnicastLocatorList);
}
else
{
// Locator with port 0, calculate port.
uint32_t unicast_port = metatraffic_unicast_port_ + m_att.port.offsetd3 - m_att.port.offsetd1;
std::for_each(m_att.defaultUnicastLocatorList.begin(), m_att.defaultUnicastLocatorList.end(),
[&](Locator_t& loc)
{
m_network_Factory.fill_default_locator_port(loc, unicast_port);
});
m_network_Factory.NormalizeLocators(m_att.defaultUnicastLocatorList);
// Locator with port 0, calculate port.
uint32_t multicast_port = m_network_Factory.calculate_well_known_port(domain_id_, m_att, true);
std::for_each(m_att.defaultMulticastLocatorList.begin(), m_att.defaultMulticastLocatorList.end(),
[&](Locator_t& loc)
{
m_network_Factory.fill_default_locator_port(loc, multicast_port);
});
}
if (is_intraprocess_only())
{
m_att.defaultUnicastLocatorList.clear();
m_att.defaultMulticastLocatorList.clear();
}
createReceiverResources(m_att.defaultUnicastLocatorList, true, false, true);
createReceiverResources(m_att.defaultMulticastLocatorList, false, false, true);
namespace external_locators = fastdds::rtps::network::external_locators;
external_locators::set_listening_locators(m_att.default_external_unicast_locators,
m_att.defaultUnicastLocatorList);
}
void RTPSParticipantImpl::setup_initial_peers()
{
// 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);
});
}
}
void RTPSParticipantImpl::setup_output_traffic()
{
{
const std::string* max_size_property =
PropertyPolicyHelper::find_property(m_att.properties, "fastdds.max_message_size");
if (max_size_property != nullptr)
{
try
{
max_output_message_size_ = std::stoul(*max_size_property);
}
catch (const std::exception& e)
{
EPROSIMA_LOG_ERROR(RTPS_WRITER, "Error parsing max_message_size property: " << e.what());
}
}
}
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 (m_att.throughputController.bytesPerPeriod != UINT32_MAX && m_att.throughputController.periodMillisecs != 0)
{
fastdds::rtps::FlowControllerDescriptor old_descriptor;
old_descriptor.name = guid_str_.c_str();
old_descriptor.max_bytes_per_period = m_att.throughputController.bytesPerPeriod;
old_descriptor.period_ms = m_att.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());
}
}
void RTPSParticipantImpl::enable()
{
mp_builtinProtocols->enable();
//Start reception
for (auto& receiver : m_receiverResourcelist)
{
receiver.Receiver->RegisterReceiver(receiver.mp_receiver);
}
}
void RTPSParticipantImpl::disable()
{
// Disabling event thread also disables participant announcement, so there is no need to call
// stopRTPSParticipantAnnouncement()
mp_event_thr.stop_thread();
// Disable Retries on Transports
m_network_Factory.Shutdown();
// Safely abort threads.
for (auto& block : m_receiverResourcelist)
{
block.Receiver->UnregisterReceiver(block.mp_receiver);
block.disable();
}
deleteAllUserEndpoints();
if (nullptr != mp_builtinProtocols)
{
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_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,
std::atomic<uint32_t>& id_counter,
EndpointAttributes& att,
EntityId_t& entId)
{
const char* debug_label = (att.endpointKind == WRITER ? "writer" : "reader");
if (!att.unicastLocatorList.isValid())
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, "Unicast Locator List for " << debug_label << " contains invalid Locator");
return false;
}
if (!att.multicastLocatorList.isValid())
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT,
"Multicast Locator List for " << debug_label << " contains invalid Locator");
return false;
}
if (!att.remoteLocatorList.isValid())
{
EPROSIMA_LOG_ERROR(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
EPROSIMA_LOG_ERROR(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";
EPROSIMA_LOG_INFO(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))
{
EPROSIMA_LOG_ERROR(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
{
EPROSIMA_LOG_WARNING(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
{
EPROSIMA_LOG_WARNING(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)
{
EPROSIMA_LOG_ERROR(RTPS_PARTICIPANT, "Cannot use a flow controller in synchronously publication mode.");
}
else
{
EPROSIMA_LOG_ERROR(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"))
{
EPROSIMA_LOG_ERROR(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;
}
// Use participant's external locators if writer has none
// WARNING: call before createAndAssociateReceiverswithEndpoint, as the latter intentionally clears external
// locators list when using unique_flows feature
setup_external_locators(SWriter);
#if HAVE_SECURITY
if (!is_builtin)