UDPChannelResource.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_UDP_CHANNEL_RESOURCE_INFO_
  15. #define _FASTDDS_UDP_CHANNEL_RESOURCE_INFO_
  16. #include <fastdds/rtps/transport/ChannelResource.h>
  17. #include <fastdds/rtps/common/Locator.h>
  18. #include <asio.hpp>
  19. namespace eprosima{
  20. namespace fastdds{
  21. namespace rtps{
  22. class TransportReceiverInterface;
  23. class UDPTransportInterface;
  24. #if defined(ASIO_HAS_MOVE)
  25. // Typedefs
  26. typedef asio::ip::udp::socket eProsimaUDPSocket;
  27. typedef eProsimaUDPSocket& eProsimaUDPSocketRef;
  28. // UDP
  29. inline eProsimaUDPSocket* getSocketPtr(eProsimaUDPSocket &socket)
  30. {
  31. return &socket;
  32. }
  33. inline eProsimaUDPSocket moveSocket(eProsimaUDPSocket &socket)
  34. {
  35. return std::move(socket);
  36. }
  37. inline eProsimaUDPSocket createUDPSocket(asio::io_service& io_service)
  38. {
  39. return asio::ip::udp::socket(io_service);
  40. }
  41. inline eProsimaUDPSocket& getRefFromPtr(eProsimaUDPSocket* socket)
  42. {
  43. return *socket;
  44. }
  45. #else
  46. // Typedefs
  47. typedef std::shared_ptr<asio::ip::udp::socket> eProsimaUDPSocket;
  48. typedef eProsimaUDPSocket eProsimaUDPSocketRef;
  49. // UDP
  50. inline eProsimaUDPSocket getSocketPtr(eProsimaUDPSocket socket)
  51. {
  52. return socket;
  53. }
  54. inline eProsimaUDPSocket moveSocket(eProsimaUDPSocket socket)
  55. {
  56. return socket;
  57. }
  58. inline eProsimaUDPSocket createUDPSocket(asio::io_service& io_service)
  59. {
  60. return std::make_shared<asio::ip::udp::socket>(io_service);
  61. }
  62. inline eProsimaUDPSocket getRefFromPtr(eProsimaUDPSocket socket)
  63. {
  64. return socket;
  65. }
  66. #endif
  67. class UDPChannelResource : public ChannelResource
  68. {
  69. public:
  70. UDPChannelResource(
  71. UDPTransportInterface* transport,
  72. eProsimaUDPSocket& socket,
  73. uint32_t maxMsgSize,
  74. const fastrtps::rtps::Locator_t& locator,
  75. const std::string& sInterface,
  76. TransportReceiverInterface* receiver);
  77. virtual ~UDPChannelResource() override;
  78. UDPChannelResource& operator=(UDPChannelResource&& channelResource)
  79. {
  80. socket_ = moveSocket(channelResource.socket_);
  81. return *this;
  82. }
  83. void only_multicast_purpose(const bool value)
  84. {
  85. only_multicast_purpose_ = value;
  86. }
  87. bool& only_multicast_purpose()
  88. {
  89. return only_multicast_purpose_;
  90. }
  91. bool only_multicast_purpose() const
  92. {
  93. return only_multicast_purpose_;
  94. }
  95. #if defined(ASIO_HAS_MOVE)
  96. inline eProsimaUDPSocket* socket()
  97. #else
  98. inline eProsimaUDPSocket socket()
  99. #endif
  100. {
  101. return getSocketPtr(socket_);
  102. }
  103. inline void interface(const std::string& interface)
  104. {
  105. interface_ = interface;
  106. }
  107. inline const std::string& interface() const
  108. {
  109. return interface_;
  110. }
  111. inline void message_receiver(TransportReceiverInterface* receiver)
  112. {
  113. message_receiver_ = receiver;
  114. }
  115. inline TransportReceiverInterface* message_receiver()
  116. {
  117. return message_receiver_;
  118. }
  119. inline virtual void disable() override
  120. {
  121. ChannelResource::disable();
  122. }
  123. void release();
  124. protected:
  125. /**
  126. * Function to be called from a new thread, which takes cares of performing a blocking receive
  127. * operation on the ReceiveResource
  128. * @param input_locator - Locator that triggered the creation of the resource
  129. */
  130. void perform_listen_operation(
  131. fastrtps::rtps::Locator_t input_locator);
  132. /**
  133. * Blocking Receive from the specified channel.
  134. * @param receive_buffer vector with enough capacity (not size) to accomodate a full receive buffer. That
  135. * capacity must not be less than the receive_buffer_size supplied to this class during construction.
  136. * @param receive_buffer_capacity Maximum size of the receive_buffer.
  137. * @param[out] receive_buffer_size Size of the received buffer.
  138. * @param[out] remote_locator Locator describing the remote restination we received a packet from.
  139. */
  140. bool Receive(
  141. fastrtps::rtps::octet* receive_buffer,
  142. uint32_t receive_buffer_capacity,
  143. uint32_t& receive_buffer_size,
  144. fastrtps::rtps::Locator_t& remote_locator);
  145. private:
  146. TransportReceiverInterface* message_receiver_; //Associated Readers/Writers inside of MessageReceiver
  147. eProsimaUDPSocket socket_;
  148. bool only_multicast_purpose_;
  149. std::string interface_;
  150. UDPTransportInterface* transport_;
  151. UDPChannelResource(const UDPChannelResource&) = delete;
  152. UDPChannelResource& operator=(const UDPChannelResource&) = delete;
  153. };
  154. } // namespace rtps
  155. } // namespace fastdds
  156. } // namespace eprosima
  157. #endif // _FASTDDS_UDP_CHANNEL_RESOURCE_INFO_