TransportInterface.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2019 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. #ifndef _FASTDDS_TRANSPORT_INTERFACE_H
  15. #define _FASTDDS_TRANSPORT_INTERFACE_H
  16. #include <memory>
  17. #include <vector>
  18. #include <fastdds/rtps/common/Locator.h>
  19. #include <fastdds/rtps/common/LocatorSelector.hpp>
  20. #include <fastdds/rtps/common/PortParameters.h>
  21. #include <fastdds/rtps/transport/TransportDescriptorInterface.h>
  22. #include <fastdds/rtps/transport/TransportReceiverInterface.h>
  23. #include <fastdds/rtps/network/SenderResource.h>
  24. namespace eprosima{
  25. namespace fastdds{
  26. namespace rtps{
  27. static const uint32_t s_maximumMessageSize = 65500;
  28. static const uint32_t s_maximumInitialPeersRange = 4;
  29. static const uint32_t s_minimumSocketBuffer = 65536;
  30. static const std::string s_IPv4AddressAny = "0.0.0.0";
  31. static const std::string s_IPv6AddressAny = "::";
  32. using SendResourceList = std::vector<std::unique_ptr<fastrtps::rtps::SenderResource>>;
  33. class ChannelResource;
  34. /**
  35. * Interface against which to implement a transport layer, decoupled from FastRTPS internals.
  36. * TransportInterface expects the user to implement a logical equivalence between Locators and protocol-specific "channels".
  37. * This equivalence can be narrowing: For instance in UDP/IP, a port would take the role of channel, and several different
  38. * locators can map to the same port, and hence the same channel.
  39. * @ingroup TRANSPORT_MODULE
  40. * */
  41. class TransportInterface
  42. {
  43. public:
  44. /**
  45. * Aside from the API defined here, an user-defined Transport must define a descriptor data type and a constructor that
  46. * expects a constant reference to such descriptor. e.g:
  47. *
  48. * class MyTransport:
  49. * public:
  50. * typedef struct { ... } MyTransportDescriptor;
  51. * MyTransport(const MyTransportDescriptor&);
  52. * ...
  53. */
  54. virtual ~TransportInterface() = default;
  55. /**
  56. * Initialize this transport. This method will prepare all the internals of the transport.
  57. * @return True when the transport was correctly intialized.
  58. */
  59. virtual bool init() = 0;
  60. /**
  61. * Must report whether the input channel associated to this locator is open. Channels must either be
  62. * fully closed or fully open, so that "open" and "close" operations are whole and definitive.
  63. */
  64. virtual bool IsInputChannelOpen(const fastrtps::rtps::Locator_t&) const = 0;
  65. //! Must report whether the given locator is supported by this transport (typically inspecting its "kind" value).
  66. virtual bool IsLocatorSupported(const fastrtps::rtps::Locator_t&) const = 0;
  67. //! Must report whether the given locator is allowed by this transport.
  68. virtual bool is_locator_allowed(const fastrtps::rtps::Locator_t&) const = 0;
  69. //! Returns the locator describing the main (most general) channel that can write to the provided remote locator.
  70. virtual fastrtps::rtps::Locator_t RemoteToMainLocal(const fastrtps::rtps::Locator_t& remote) const = 0;
  71. /**
  72. * Transforms a remote locator into a locator optimized for local communications.
  73. *
  74. * If the remote locator corresponds to one of the local interfaces, it is converted
  75. * to the corresponding local address.
  76. *
  77. * @param [in] remote_locator Locator to be converted.
  78. * @param [out] result_locator Converted locator.
  79. *
  80. * @return false if the input locator is not supported/allowed by this transport, true otherwise.
  81. */
  82. virtual bool transform_remote_locator(
  83. const fastrtps::rtps::Locator_t& remote_locator,
  84. fastrtps::rtps::Locator_t& result_locator) const
  85. {
  86. (void)remote_locator;
  87. (void)result_locator;
  88. return false;
  89. }
  90. //! Must open the channel that maps to/from the given locator. This method must allocate, reserve and mark
  91. //! any resources that are needed for said channel.
  92. virtual bool OpenOutputChannel(
  93. SendResourceList& sender_resource_list,
  94. const fastrtps::rtps::Locator_t&) = 0;
  95. virtual bool OpenInputChannel(
  96. const fastrtps::rtps::Locator_t&,
  97. TransportReceiverInterface*, uint32_t) = 0;
  98. /**
  99. * Must close the channel that maps to/from the given locator.
  100. * IMPORTANT: It MUST be safe to call this method even during a Receive operation on another thread. You must implement
  101. * any necessary mutual exclusion and timeout mechanisms to make sure the channel can be closed without damage.
  102. */
  103. virtual bool CloseInputChannel(const fastrtps::rtps::Locator_t&) = 0;
  104. //! Must report whether two locators map to the same internal channel.
  105. virtual bool DoInputLocatorsMatch(const fastrtps::rtps::Locator_t&, const fastrtps::rtps::Locator_t&) const = 0;
  106. virtual fastrtps::rtps::LocatorList_t NormalizeLocator(const fastrtps::rtps::Locator_t& locator) = 0;
  107. /**
  108. * Performs the locator selection algorithm for this transport.
  109. *
  110. * It basically consists of the following steps
  111. * - selector.transport_starts is called
  112. * - transport handles the selection state of each locator
  113. * - if a locator from an entry is selected, selector.select is called for that entry
  114. *
  115. * @param [in, out] selector Locator selector.
  116. */
  117. virtual void select_locators(fastrtps::rtps::LocatorSelector& selector) const = 0;
  118. virtual bool is_local_locator(const fastrtps::rtps::Locator_t& locator) const = 0;
  119. virtual TransportDescriptorInterface* get_configuration() = 0;
  120. virtual void AddDefaultOutputLocator(fastrtps::rtps::LocatorList_t &defaultList) = 0;
  121. virtual bool getDefaultMetatrafficMulticastLocators(
  122. fastrtps::rtps::LocatorList_t& locators,
  123. uint32_t metatraffic_multicast_port) const = 0;
  124. virtual bool getDefaultMetatrafficUnicastLocators(
  125. fastrtps::rtps::LocatorList_t& locators,
  126. uint32_t metatraffic_unicast_port) const = 0;
  127. virtual bool getDefaultUnicastLocators(
  128. fastrtps::rtps::LocatorList_t& locators,
  129. uint32_t unicast_port) const = 0;
  130. virtual bool fillMetatrafficMulticastLocator(
  131. fastrtps::rtps::Locator_t& locator,
  132. uint32_t metatraffic_multicast_port) const = 0;
  133. virtual bool fillMetatrafficUnicastLocator(
  134. fastrtps::rtps::Locator_t& locator,
  135. uint32_t metatraffic_unicast_port) const = 0;
  136. virtual bool configureInitialPeerLocator(
  137. fastrtps::rtps::Locator_t& locator,
  138. const fastrtps::rtps::PortParameters &port_params,
  139. uint32_t domainId,
  140. fastrtps::rtps::LocatorList_t& list) const = 0;
  141. virtual bool fillUnicastLocator(
  142. fastrtps::rtps::Locator_t& locator,
  143. uint32_t well_known_port) const = 0;
  144. /**
  145. * @return The maximum datagram size for reception supported by the transport
  146. */
  147. virtual uint32_t max_recv_buffer_size() const = 0;
  148. /**
  149. * Shutdown method to close the connections of the transports.
  150. */
  151. virtual void shutdown() {};
  152. int32_t kind() const { return transport_kind_; }
  153. protected:
  154. TransportInterface(int32_t transport_kind)
  155. : transport_kind_(transport_kind) {}
  156. int32_t transport_kind_;
  157. };
  158. } // namespace rtps
  159. } // namespace fastdds
  160. } // namespace eprosima
  161. #endif // _FASTDDS_TRANSPORT_INTERFACE_H