RTCPMessageManager.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /**
  15. * @file RTCPMessageManager.h
  16. */
  17. #ifndef _FASTDDS_RTCP_MESSAGEMANAGER_H_
  18. #define _FASTDDS_RTCP_MESSAGEMANAGER_H_
  19. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  20. #include <fastdds/rtps/common/all_common.h>
  21. #include <fastdds/rtps/writer/StatelessWriter.h>
  22. #include <fastdds/rtps/writer/StatefulWriter.h>
  23. #include <fastdds/rtps/transport/tcp/TCPControlMessage.h>
  24. #include <fastdds/rtps/transport/tcp/RTCPHeader.h>
  25. #include<memory>
  26. #include <atomic>
  27. namespace eprosima {
  28. namespace fastdds{
  29. namespace rtps {
  30. class TCPChannelResource;
  31. class TCPTransportInterface;
  32. const fastrtps::rtps::ProtocolVersion_t c_rtcpProtocolVersion = {1, 0};
  33. /**
  34. * Class RTCPMessageManager, process the received TCP messages.
  35. * @ingroup MANAGEMENT_MODULE
  36. */
  37. class RTCPMessageManager
  38. {
  39. std::atomic<bool> alive_;
  40. public:
  41. RTCPMessageManager(TCPTransportInterface* pTransport) : alive_(true), mTransport(pTransport) {}
  42. virtual ~RTCPMessageManager();
  43. /** @name Send RTCP Message Methods.
  44. * These methods create RTPS messages for different types
  45. */
  46. TCPTransactionId sendConnectionRequest(std::shared_ptr<TCPChannelResource>& channel);
  47. TCPTransactionId sendOpenLogicalPortRequest(
  48. TCPChannelResource* channel,
  49. OpenLogicalPortRequest_t &request);
  50. TCPTransactionId sendOpenLogicalPortRequest(
  51. TCPChannelResource* channel,
  52. uint16_t port);
  53. TCPTransactionId sendCheckLogicalPortsRequest(
  54. TCPChannelResource* channel,
  55. CheckLogicalPortsRequest_t &request);
  56. TCPTransactionId sendCheckLogicalPortsRequest(
  57. TCPChannelResource* channel,
  58. std::vector<uint16_t> &ports);
  59. TCPTransactionId sendKeepAliveRequest(
  60. std::shared_ptr<TCPChannelResource>& channel,
  61. KeepAliveRequest_t &request);
  62. TCPTransactionId sendKeepAliveRequest(std::shared_ptr<TCPChannelResource>& channel);
  63. TCPTransactionId sendLogicalPortIsClosedRequest(
  64. std::shared_ptr<TCPChannelResource>& channel,
  65. LogicalPortIsClosedRequest_t& request);
  66. TCPTransactionId sendLogicalPortIsClosedRequest(
  67. std::shared_ptr<TCPChannelResource>& channel,
  68. uint16_t port);
  69. TCPTransactionId sendUnbindConnectionRequest(std::shared_ptr<TCPChannelResource>& channel);
  70. /** @name Process RTCP Message Methods.
  71. * These methods create RTPS messages for different types
  72. */
  73. ResponseCode processBindConnectionRequest(
  74. std::shared_ptr<TCPChannelResource>& channel,
  75. const ConnectionRequest_t &request,
  76. const TCPTransactionId &transactionId,
  77. fastrtps::rtps::Locator_t &localLocator);
  78. virtual ResponseCode processOpenLogicalPortRequest(
  79. std::shared_ptr<TCPChannelResource>& channel,
  80. const OpenLogicalPortRequest_t &request,
  81. const TCPTransactionId &transactionId);
  82. void processCheckLogicalPortsRequest(
  83. std::shared_ptr<TCPChannelResource>& channel,
  84. const CheckLogicalPortsRequest_t &request,
  85. const TCPTransactionId &transactionId);
  86. ResponseCode processKeepAliveRequest(
  87. std::shared_ptr<TCPChannelResource>& channel,
  88. const KeepAliveRequest_t &request,
  89. const TCPTransactionId &transactionId);
  90. void processLogicalPortIsClosedRequest(
  91. std::shared_ptr<TCPChannelResource>& channel,
  92. const LogicalPortIsClosedRequest_t &request,
  93. const TCPTransactionId &transactionId);
  94. ResponseCode processBindConnectionResponse(
  95. std::shared_ptr<TCPChannelResource>& channel,
  96. const BindConnectionResponse_t &response,
  97. const TCPTransactionId &transactionId);
  98. ResponseCode processCheckLogicalPortsResponse(
  99. std::shared_ptr<TCPChannelResource>& channel,
  100. const CheckLogicalPortsResponse_t &response,
  101. const TCPTransactionId &transactionId);
  102. ResponseCode processOpenLogicalPortResponse(
  103. std::shared_ptr<TCPChannelResource>& channel,
  104. ResponseCode respCode,
  105. const TCPTransactionId &transactionId);
  106. ResponseCode processKeepAliveResponse(
  107. std::shared_ptr<TCPChannelResource>& channel,
  108. ResponseCode respCode,
  109. const TCPTransactionId &transactionId);
  110. ResponseCode processRTCPMessage(
  111. std::shared_ptr<TCPChannelResource>& channel,
  112. fastrtps::rtps::octet* receive_buffer,
  113. size_t receivedSize);
  114. static uint32_t& addToCRC(uint32_t &crc, fastrtps::rtps::octet data);
  115. void dispose()
  116. {
  117. alive_.store(false);
  118. }
  119. protected:
  120. TCPTransportInterface* mTransport;
  121. std::set<TCPTransactionId> mUnconfirmedTransactions;
  122. TCPTransactionId myTransId;
  123. std::recursive_mutex mutex;
  124. TCPTransactionId getTransactionId()
  125. {
  126. std::unique_lock<std::recursive_mutex> scopedLock(mutex);
  127. return myTransId++;
  128. }
  129. bool findTransactionId(const TCPTransactionId& transactionId)
  130. {
  131. std::unique_lock<std::recursive_mutex> scopedLock(mutex);
  132. auto it = mUnconfirmedTransactions.find(transactionId);
  133. return it != mUnconfirmedTransactions.end();
  134. }
  135. void addTransactionId(const TCPTransactionId& transactionId)
  136. {
  137. std::unique_lock<std::recursive_mutex> scopedLock(mutex);
  138. mUnconfirmedTransactions.emplace(transactionId);
  139. }
  140. bool removeTransactionId(const TCPTransactionId& transactionId)
  141. {
  142. std::unique_lock<std::recursive_mutex> scopedLock(mutex);
  143. auto it = mUnconfirmedTransactions.find(transactionId);
  144. if (it != mUnconfirmedTransactions.end())
  145. {
  146. mUnconfirmedTransactions.erase(it);
  147. return true;
  148. }
  149. else
  150. {
  151. return false;
  152. }
  153. }
  154. //void prepareAndSendCheckLogicalPortsRequest(TCPChannelResource *p_channel_resource);
  155. size_t sendMessage(
  156. TCPChannelResource* channel,
  157. const fastrtps::rtps::CDRMessage_t &msg) const;
  158. bool sendData(
  159. std::shared_ptr<TCPChannelResource>& channel,
  160. TCPCPMKind kind,
  161. const TCPTransactionId &transactionId,
  162. const fastrtps::rtps::SerializedPayload_t *payload = nullptr,
  163. const ResponseCode respCode = RETCODE_VOID);
  164. bool sendData(
  165. TCPChannelResource* channel,
  166. TCPCPMKind kind,
  167. const TCPTransactionId &transactionId,
  168. const fastrtps::rtps::SerializedPayload_t *payload = nullptr,
  169. const ResponseCode respCode = RETCODE_VOID);
  170. void fillHeaders(TCPCPMKind kind, const TCPTransactionId &transactionId, TCPControlMsgHeader &retCtrlHeader,
  171. TCPHeader &header, const fastrtps::rtps::SerializedPayload_t *payload = nullptr, const ResponseCode *respCode = nullptr);
  172. bool isCompatibleProtocol(const fastrtps::rtps::ProtocolVersion_t &protocol) const;
  173. inline bool alive() const
  174. {
  175. return alive_.load();
  176. }
  177. };
  178. } /* namespace rtps */
  179. } /* namespace fastdds */
  180. } /* namespace eprosima */
  181. #endif
  182. #endif /* _FASTDDS_RTCP_MESSAGEMANAGER_H_ */