|
| 1 | +// Copyright 2023 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 | +#pragma once |
| 16 | + |
| 17 | +#include <atomic> |
| 18 | +#include <map> |
| 19 | +#include <mutex> |
| 20 | + |
| 21 | +#include <cpp_utils/ReturnCode.hpp> |
| 22 | + |
| 23 | +#include <fastdds/dds/publisher/DataWriter.hpp> |
| 24 | +#include <fastdds/rtps/common/InstanceHandle.h> |
| 25 | +#include <fastdds/rtps/common/SerializedPayload.h> |
| 26 | +#include <fastdds/rtps/history/IPayloadPool.h> |
| 27 | + |
| 28 | +#include <ddspipe_core/efficiency/payload/PayloadPool.hpp> |
| 29 | +#include <ddspipe_core/types/data/RtpsPayloadData.hpp> |
| 30 | + |
| 31 | +namespace eprosima { |
| 32 | +namespace ddspipe { |
| 33 | +namespace core { |
| 34 | + |
| 35 | +/** |
| 36 | + * DataWriters always allocate memory for a payload when writing it to the \c PayloadPool. In the DdsPipe, however, the |
| 37 | + * \c payload is already in the \c PayloadPool (saved by the Reader), so we can reuse it to avoid an extra copy. |
| 38 | + * |
| 39 | + * This class works as a mediator between the \c FastPayloadPool and the DataWriter's write functions, to avoid making |
| 40 | + * the extra copy. |
| 41 | + */ |
| 42 | +class PayloadPoolMediator : public fastrtps::rtps::IPayloadPool |
| 43 | +{ |
| 44 | +public: |
| 45 | + |
| 46 | + /** |
| 47 | + * @brief construct a \c PayloadPoolMediator to mediate for a \c PayloadPool. |
| 48 | + * |
| 49 | + * @param payload_pool the \c PayloadPool to mediate for. |
| 50 | + */ |
| 51 | + DDSPIPE_CORE_DllAPI |
| 52 | + PayloadPoolMediator( |
| 53 | + const std::shared_ptr<PayloadPool>& payload_pool); |
| 54 | + |
| 55 | + /** |
| 56 | + * @brief write \c data with a \c writer but avoiding copying the \c data from and to the same \c PayloadPool. |
| 57 | + * |
| 58 | + * Save the \c data and call the \c write in the \c writer with \c data as an argument. |
| 59 | + * The \c write in the \c writer will call \c get_payload which, instead of saving a new chunk of memory for data it |
| 60 | + * already contains, will return the \c data it had saved at the time of writing. |
| 61 | + * |
| 62 | + * @warning This method locks the \c mutex to avoid retrieving the \c data saved in another call to \c write. |
| 63 | + * |
| 64 | + * Thread safe. |
| 65 | + * |
| 66 | + * @param writer the writer who has to write the \c data. |
| 67 | + * @param data the data to be written by the \c writer. |
| 68 | + * |
| 69 | + * @return true if everything OK |
| 70 | + * @return false if something went wrong |
| 71 | + */ |
| 72 | + DDSPIPE_CORE_DllAPI |
| 73 | + bool write( |
| 74 | + fastdds::dds::DataWriter* writer, |
| 75 | + types::RtpsPayloadData* data); |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief write \c data with a \c writer with \c params but avoiding copying the \c data from and to the same |
| 79 | + * \c PayloadPool. |
| 80 | + * |
| 81 | + * Save the \c data and call the \c write in the \c writer with \c data and \c params as arguments. |
| 82 | + * The \c write in the \c writer will call \c get_payload which, instead of saving a new chunk of memory for data it |
| 83 | + * already contains, will return the \c data it had saved at the time of writing. |
| 84 | + * |
| 85 | + * @warning This method locks the \c mutex to avoid retrieving the \c data saved in another call to \c write. |
| 86 | + * |
| 87 | + * Thread safe. |
| 88 | + * |
| 89 | + * @param writer the writer who has to write the \c data. |
| 90 | + * @param data the data to be written by the \c writer. |
| 91 | + * @param params the data to be written by the \c writer. |
| 92 | + * |
| 93 | + * @return true if everything OK |
| 94 | + * @return false if something went wrong |
| 95 | + */ |
| 96 | + DDSPIPE_CORE_DllAPI |
| 97 | + bool write( |
| 98 | + fastdds::dds::DataWriter* writer, |
| 99 | + types::RtpsPayloadData* data, |
| 100 | + fastrtps::rtps::WriteParams& params); |
| 101 | + |
| 102 | + /** |
| 103 | + * @brief write \c data with a \c writer with a \c handle but avoiding copying the \c data from and to the same |
| 104 | + * \c PayloadPool. |
| 105 | + * |
| 106 | + * Save the \c data and call the \c write in the \c writer with \c data and \c params as arguments. |
| 107 | + * The \c write in the \c writer will call \c get_payload which, instead of saving a new chunk of memory for data it |
| 108 | + * already contains, will return the \c data it had saved at the time of writing. |
| 109 | + * |
| 110 | + * @warning This method locks the \c mutex to avoid retrieving the \c data saved in another call to \c write. |
| 111 | + * |
| 112 | + * Thread safe. |
| 113 | + * |
| 114 | + * @param writer the writer who has to write the \c data. |
| 115 | + * @param data the data to be written by the \c writer. |
| 116 | + * @param handle the data to be used by the \c writer. |
| 117 | + * |
| 118 | + * @return true if everything OK |
| 119 | + * @return false if something went wrong |
| 120 | + */ |
| 121 | + DDSPIPE_CORE_DllAPI |
| 122 | + utils::ReturnCode write( |
| 123 | + fastdds::dds::DataWriter* writer, |
| 124 | + types::RtpsPayloadData* data, |
| 125 | + const fastrtps::rtps::InstanceHandle_t& handle); |
| 126 | + |
| 127 | + /** |
| 128 | + * Instead of reserving a block of memory of \c size in the \c payload_pool, we can redirect the call to |
| 129 | + * \c get_payload providing the \c payload (that we saved in the call to \c write) and the \c payload_pool. |
| 130 | + * |
| 131 | + * @param size size of the new chunk of data to allocate in the \c payload_pool. |
| 132 | + * @param cache_change object to store the new data in. |
| 133 | + * |
| 134 | + * @return true if everything OK |
| 135 | + * @return false if something went wrong |
| 136 | + */ |
| 137 | + DDSPIPE_CORE_DllAPI |
| 138 | + virtual bool get_payload( |
| 139 | + uint32_t size, |
| 140 | + fastrtps::rtps::CacheChange_t& cache_change) override; |
| 141 | + |
| 142 | + /** |
| 143 | + * @brief redirect the call to the \c release_payload in the \c payload_pool. |
| 144 | + * |
| 145 | + * @param data the data to get from the \c PayloadPool. |
| 146 | + * @param data_owner the \c PayloadPool to get the \c data from. |
| 147 | + * @param cache_change object to store the \c data in. |
| 148 | + * |
| 149 | + * @return true if everything OK |
| 150 | + * @return false if something went wrong |
| 151 | + */ |
| 152 | + DDSPIPE_CORE_DllAPI |
| 153 | + virtual bool get_payload( |
| 154 | + fastrtps::rtps::SerializedPayload_t& data, |
| 155 | + fastrtps::rtps::IPayloadPool*& data_owner, |
| 156 | + fastrtps::rtps::CacheChange_t& cache_change) override; |
| 157 | + |
| 158 | + /** |
| 159 | + * @brief redirect the call to the \c release_payload in the \c payload_pool. |
| 160 | + * @param cache_change object to release. |
| 161 | + * |
| 162 | + * @return true if everything OK |
| 163 | + * @return false if something went wrong |
| 164 | + */ |
| 165 | + DDSPIPE_CORE_DllAPI |
| 166 | + virtual bool release_payload( |
| 167 | + fastrtps::rtps::CacheChange_t& cache_change) override; |
| 168 | + |
| 169 | +protected: |
| 170 | + |
| 171 | + //! Mutex to prevent simultaneous calls to write |
| 172 | + std::mutex mutex_; |
| 173 | + |
| 174 | + //! A pointer to the payload_ we want to write down in the PayloadPool. |
| 175 | + types::Payload* payload_; |
| 176 | + |
| 177 | + //! The \c PayloadPool the \c PayloadPoolMediator is mediating for. |
| 178 | + const std::shared_ptr<PayloadPool>& payload_pool_; |
| 179 | +}; |
| 180 | + |
| 181 | +} /* namespace core */ |
| 182 | +} /* namespace ddspipe */ |
| 183 | +} /* namespace eprosima */ |
0 commit comments