TCPChannelResource.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_TCP_CHANNEL_RESOURCE_BASE_
  15. #define _FASTDDS_TCP_CHANNEL_RESOURCE_BASE_
  16. #include <fastdds/rtps/transport/TCPTransportDescriptor.h>
  17. #include <fastdds/rtps/transport/TransportReceiverInterface.h>
  18. #include <fastdds/rtps/transport/ChannelResource.h>
  19. #include <fastdds/rtps/transport/tcp/RTCPMessageManager.h>
  20. #include <fastdds/rtps/common/Locator.h>
  21. #include <asio.hpp>
  22. namespace eprosima {
  23. namespace fastdds {
  24. namespace rtps {
  25. class TCPConnector;
  26. class TCPTransportInterface;
  27. enum eSocketErrorCodes
  28. {
  29. eNoError,
  30. eBrokenPipe,
  31. eAsioError,
  32. eSystemError,
  33. eException,
  34. eConnectionAborted = 125
  35. };
  36. class TCPChannelResource : public ChannelResource
  37. {
  38. protected:
  39. enum TCPConnectionType
  40. {
  41. TCP_ACCEPT_TYPE = 0,
  42. TCP_CONNECT_TYPE = 1
  43. };
  44. enum eConnectionStatus
  45. {
  46. eDisconnected = 0,
  47. eConnecting, // Output -> Trying connection.
  48. eConnected, // Output -> Send bind message.
  49. eWaitingForBind, // Input -> Waiting for the bind message.
  50. eWaitingForBindResponse, // Output -> Waiting for the bind response message.
  51. eEstablished,
  52. eUnbinding
  53. };
  54. TCPTransportInterface* parent_;
  55. fastrtps::rtps::Locator_t locator_;
  56. bool waiting_for_keep_alive_;
  57. // Must be accessed after lock pending_logical_mutex_
  58. std::map<TCPTransactionId, uint16_t> negotiating_logical_ports_;
  59. std::map<TCPTransactionId, uint16_t> last_checked_logical_port_;
  60. std::vector<uint16_t> pending_logical_output_ports_; // Must be accessed after lock pending_logical_mutex_
  61. std::vector<uint16_t> logical_output_ports_;
  62. std::mutex read_mutex_;
  63. std::recursive_mutex pending_logical_mutex_;
  64. std::atomic<eConnectionStatus> connection_status_;
  65. public:
  66. void add_logical_port(
  67. uint16_t port,
  68. RTCPMessageManager* rtcp_manager);
  69. void set_logical_port_pending(
  70. uint16_t port);
  71. bool remove_logical_port(
  72. uint16_t port);
  73. virtual void disable() override;
  74. bool is_logical_port_opened(
  75. uint16_t port);
  76. bool is_logical_port_added(
  77. uint16_t port);
  78. bool connection_established()
  79. {
  80. return connection_status_ == eConnectionStatus::eEstablished;
  81. }
  82. eConnectionStatus connection_status()
  83. {
  84. return connection_status_;
  85. }
  86. inline const fastrtps::rtps::Locator_t& locator() const
  87. {
  88. return locator_;
  89. }
  90. ResponseCode process_bind_request(
  91. const fastrtps::rtps::Locator_t& locator);
  92. // Socket related methods
  93. virtual void connect(
  94. const std::shared_ptr<TCPChannelResource>& myself) = 0;
  95. virtual void disconnect() = 0;
  96. virtual uint32_t read(
  97. fastrtps::rtps::octet* buffer,
  98. std::size_t size,
  99. asio::error_code& ec) = 0;
  100. virtual size_t send(
  101. const fastrtps::rtps::octet* header,
  102. size_t header_size,
  103. const fastrtps::rtps::octet* buffer,
  104. size_t size,
  105. asio::error_code& ec) = 0;
  106. virtual asio::ip::tcp::endpoint remote_endpoint() const = 0;
  107. virtual asio::ip::tcp::endpoint local_endpoint() const = 0;
  108. virtual void set_options(
  109. const TCPTransportDescriptor* options) = 0;
  110. virtual void cancel() = 0;
  111. virtual void close() = 0;
  112. virtual void shutdown(
  113. asio::socket_base::shutdown_type what) = 0;
  114. TCPConnectionType tcp_connection_type() const
  115. {
  116. return tcp_connection_type_;
  117. }
  118. protected:
  119. // Constructor called when trying to connect to a remote server
  120. TCPChannelResource(
  121. TCPTransportInterface* parent,
  122. const fastrtps::rtps::Locator_t& locator,
  123. uint32_t maxMsgSize);
  124. // Constructor called when local server accepted connection
  125. TCPChannelResource(
  126. TCPTransportInterface* parent,
  127. uint32_t maxMsgSize);
  128. inline eConnectionStatus change_status(
  129. eConnectionStatus s,
  130. RTCPMessageManager* rtcp_manager = nullptr)
  131. {
  132. eConnectionStatus old = connection_status_.exchange(s);
  133. if (old != s)
  134. {
  135. if (s == eEstablished)
  136. {
  137. assert(rtcp_manager != nullptr);
  138. send_pending_open_logical_ports(rtcp_manager);
  139. }
  140. }
  141. return old;
  142. }
  143. void add_logical_port_response(
  144. const TCPTransactionId& id,
  145. bool success,
  146. RTCPMessageManager* rtcp_manager);
  147. void process_check_logical_ports_response(
  148. const TCPTransactionId& transactionId,
  149. const std::vector<uint16_t>& availablePorts,
  150. RTCPMessageManager* rtcp_manager);
  151. TCPConnectionType tcp_connection_type_;
  152. friend class TCPTransportInterface;
  153. friend class RTCPMessageManager;
  154. private:
  155. void prepare_send_check_logical_ports_req(
  156. uint16_t closedPort,
  157. RTCPMessageManager* rtcp_manager);
  158. void send_pending_open_logical_ports(
  159. RTCPMessageManager* rtcp_manager);
  160. void set_all_ports_pending();
  161. TCPChannelResource(
  162. const TCPChannelResource&) = delete;
  163. TCPChannelResource& operator =(
  164. const TCPChannelResource&) = delete;
  165. };
  166. } // namespace rtps
  167. } // namespace fastdds
  168. } // namespace eprosima
  169. #endif // _FASTDDS_TCP_CHANNEL_RESOURCE_BASE_