SenderResource.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_SENDER_RESOURCE_H
  15. #define _FASTDDS_RTPS_SENDER_RESOURCE_H
  16. #include <functional>
  17. #include <vector>
  18. #include <chrono>
  19. namespace eprosima{
  20. namespace fastrtps{
  21. namespace rtps{
  22. class RTPSParticipantImpl;
  23. class MessageReceiver;
  24. class Locator_t;
  25. /**
  26. * RAII object that encapsulates the Send operation over one chanel in an unknown transport.
  27. * A Sender resource is always univocally associated to a transport channel; the
  28. * act of constructing a Sender Resource opens the channel and its destruction
  29. * closes it.
  30. * @ingroup NETWORK_MODULE
  31. */
  32. class SenderResource
  33. {
  34. public:
  35. /**
  36. * Sends to a destination locator, through the channel managed by this resource.
  37. * @param data Raw data slice to be sent.
  38. * @param dataLength Length of the data to be sent. Will be used as a boundary for
  39. * the previous parameter.
  40. * @param destination_locators_begin destination endpoint Locators iterator begin.
  41. * @param destination_locators_end destination endpoint Locators iterator end.
  42. * @param max_blocking_time_point If transport supports it then it will use it as maximum blocking time.
  43. * @return Success of the send operation.
  44. */
  45. bool send(
  46. const octet* data,
  47. uint32_t dataLength,
  48. LocatorsIterator* destination_locators_begin,
  49. LocatorsIterator* destination_locators_end,
  50. const std::chrono::steady_clock::time_point& max_blocking_time_point)
  51. {
  52. bool returned_value = false;
  53. if (send_lambda_)
  54. {
  55. returned_value = send_lambda_(data, dataLength, destination_locators_begin, destination_locators_end, max_blocking_time_point);
  56. }
  57. return returned_value;
  58. }
  59. /**
  60. * Resources can only be transfered through move semantics. Copy, assignment, and
  61. * construction outside of the factory are forbidden.
  62. */
  63. SenderResource(SenderResource&& rValueResource)
  64. {
  65. clean_up.swap(rValueResource.clean_up);
  66. send_lambda_.swap(rValueResource.send_lambda_);
  67. }
  68. virtual ~SenderResource() = default;
  69. int32_t kind() const { return transport_kind_; }
  70. protected:
  71. SenderResource(int32_t transport_kind) : transport_kind_(transport_kind) {}
  72. int32_t transport_kind_;
  73. std::function<void()> clean_up;
  74. std::function<bool(
  75. const octet*,
  76. uint32_t,
  77. LocatorsIterator* destination_locators_begin,
  78. LocatorsIterator* destination_locators_end,
  79. const std::chrono::steady_clock::time_point&)> send_lambda_;
  80. private:
  81. SenderResource() = delete;
  82. SenderResource(const SenderResource&) = delete;
  83. SenderResource& operator=(const SenderResource&) = delete;
  84. };
  85. } // namespace rtps
  86. } // namespace fastrtps
  87. } // namespace eprosima
  88. #endif /* _FASTDDS_RTPS_SENDER_RESOURCE_H */