ReceiverResource.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef _FASTDDS_RTPS_RECEIVER_RESOURCE_H
  15. #define _FASTDDS_RTPS_RECEIVER_RESOURCE_H
  16. #include <functional>
  17. #include <vector>
  18. #include <memory>
  19. #include <fastdds/rtps/messages/MessageReceiver.h>
  20. #include <fastdds/rtps/transport/TransportInterface.h>
  21. namespace eprosima {
  22. namespace fastrtps {
  23. namespace rtps {
  24. /**
  25. * RAII object that encapsulates the Receive operation over one channel in an unknown transport.
  26. * A Receiver resource is always univocally associated to a transport channel; the
  27. * act of constructing a Receiver Resource opens the channel and its destruction
  28. * closes it.
  29. * @ingroup NETWORK_MODULE
  30. */
  31. class ReceiverResource : public fastdds::rtps::TransportReceiverInterface
  32. {
  33. //! Only NetworkFactory is ever allowed to construct a ReceiverResource from scratch.
  34. //! In doing so, it guarantees the transport and channel are in a valid state for
  35. //! this resource to exist.
  36. friend class NetworkFactory;
  37. public:
  38. /**
  39. * Method called by the transport when receiving data.
  40. * @param data Pointer to the received data.
  41. * @param size Number of bytes received.
  42. * @param localLocator Locator identifying the local endpoint.
  43. * @param remoteLocator Locator identifying the remote endpoint.
  44. */
  45. virtual void OnDataReceived(const octet* data, const uint32_t size,
  46. const Locator_t& localLocator, const Locator_t& remoteLocator) override;
  47. /**
  48. * Reports whether this resource supports the given local locator (i.e., said locator
  49. * maps to the transport channel managed by this resource).
  50. */
  51. bool SupportsLocator(const Locator_t& localLocator);
  52. /**
  53. * Register a MessageReceiver object to be called upon reception of data.
  54. * @param receiver The message receiver to register.
  55. */
  56. void RegisterReceiver(MessageReceiver* receiver);
  57. /**
  58. * Unregister a MessageReceiver object to be called upon reception of data.
  59. * @param receiver The message receiver to unregister.
  60. */
  61. void UnregisterReceiver(MessageReceiver* receiver);
  62. /**
  63. * Closes related ChannelResources.
  64. */
  65. void disable();
  66. inline uint32_t max_message_size() const
  67. {
  68. return max_message_size_;
  69. }
  70. /**
  71. * Resources can only be transfered through move semantics. Copy, assignment, and
  72. * construction outside of the factory are forbidden.
  73. */
  74. ReceiverResource(ReceiverResource&&);
  75. ~ReceiverResource() override;
  76. private:
  77. ReceiverResource() = delete;
  78. ReceiverResource(const ReceiverResource&) = delete;
  79. ReceiverResource& operator=(const ReceiverResource&) = delete;
  80. ReceiverResource(fastdds::rtps::TransportInterface&, const Locator_t&, uint32_t);
  81. std::function<void()> Cleanup;
  82. std::function<bool(const Locator_t&)> LocatorMapsToManagedChannel;
  83. bool mValid; // Post-construction validity check for the NetworkFactory
  84. std::mutex mtx;
  85. MessageReceiver* receiver;
  86. uint32_t max_message_size_;
  87. };
  88. } // namespace rtps
  89. } // namespace fastrtps
  90. } // namespace eprosima
  91. #endif /* _FASTDDS_RTPS_RECEIVER_RESOURCE_H */