PortParameters.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @file PortParameters.h
  16. */
  17. #ifndef _FASTDDS_RTPS_PORT_PARAMETERS_H_
  18. #define _FASTDDS_RTPS_PORT_PARAMETERS_H_
  19. #include <fastdds/rtps/common/Types.h>
  20. #include <fastdds/dds/log/Log.hpp>
  21. namespace eprosima {
  22. namespace fastrtps{
  23. namespace rtps {
  24. /**
  25. * Class PortParameters, to define the port parameters and gains related with the RTPS protocol.
  26. * @ingroup RTPS_ATTRIBUTES_MODULE
  27. */
  28. class PortParameters
  29. {
  30. public:
  31. PortParameters()
  32. : portBase(7400),
  33. domainIDGain(250),
  34. participantIDGain(2),
  35. offsetd0(0),
  36. offsetd1(10),
  37. offsetd2(1),
  38. offsetd3(11)
  39. {}
  40. virtual ~PortParameters(){}
  41. bool operator==(const PortParameters& b) const
  42. {
  43. return (this->portBase == b.portBase) &&
  44. (this->domainIDGain == b.domainIDGain) &&
  45. (this->participantIDGain == b.participantIDGain) &&
  46. (this->offsetd0 == b.offsetd0) &&
  47. (this->offsetd1 == b.offsetd1) &&
  48. (this->offsetd2 == b.offsetd2) &&
  49. (this->offsetd3 == b.offsetd3);
  50. }
  51. /**
  52. * Get a multicast port based on the domain ID.
  53. *
  54. * @param domainId Domain ID.
  55. * @return Multicast port
  56. */
  57. inline uint32_t getMulticastPort(uint32_t domainId) const
  58. {
  59. uint32_t port = portBase + domainIDGain * domainId + offsetd0;
  60. if (port > 65535)
  61. {
  62. logError(RTPS, "Calculated port number is too high. Probably the domainId is over 232 "
  63. << "or portBase is too high.");
  64. std::cout << "Calculated port number is too high. Probably the domainId is over 232 "
  65. << "or portBase is too high." << std::endl;
  66. std::cout.flush();
  67. exit(EXIT_FAILURE);
  68. }
  69. return port;
  70. }
  71. /**
  72. * Get a unicast port baes on the domain ID and the participant ID.
  73. *
  74. * @param domainId Domain ID.
  75. * @param RTPSParticipantID Participant ID.
  76. * @return Unicast port
  77. */
  78. inline uint32_t getUnicastPort(uint32_t domainId,uint32_t RTPSParticipantID) const
  79. {
  80. uint32_t port = portBase + domainIDGain * domainId + offsetd1 + participantIDGain * RTPSParticipantID;
  81. if (port > 65535)
  82. {
  83. logError(RTPS, "Calculated port number is too high. Probably the domainId is over 232, there are "
  84. << "too much participants created or portBase is too high.");
  85. std::cout << "Calculated port number is too high. Probably the domainId is over 232, there are "
  86. << "too much participants created or portBase is too high." << std::endl;
  87. std::cout.flush();
  88. exit(EXIT_FAILURE);
  89. }
  90. return port;
  91. }
  92. public:
  93. //!PortBase, default value 7400.
  94. uint16_t portBase;
  95. //!DomainID gain, default value 250.
  96. uint16_t domainIDGain;
  97. //!ParticipantID gain, default value 2.
  98. uint16_t participantIDGain;
  99. //!Offset d0, default value 0.
  100. uint16_t offsetd0;
  101. //!Offset d1, default value 10.
  102. uint16_t offsetd1;
  103. //!Offset d2, default value 1.
  104. uint16_t offsetd2;
  105. //!Offset d3, default value 11.
  106. uint16_t offsetd3;
  107. };
  108. }
  109. } /* namespace rtps */
  110. } /* namespace eprosima */
  111. #endif /* _FASTDDS_RTPS_PORT_PARAMETERS_H_ */