/src/Fast-DDS/include/fastdds/rtps/common/CacheChange.hpp
Line | Count | Source |
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 | | |
15 | | /** |
16 | | * @file CacheChange.hpp |
17 | | */ |
18 | | |
19 | | #ifndef FASTDDS_RTPS_COMMON__CACHECHANGE_HPP |
20 | | #define FASTDDS_RTPS_COMMON__CACHECHANGE_HPP |
21 | | |
22 | | #include <atomic> |
23 | | #include <cassert> |
24 | | #include <limits> |
25 | | |
26 | | #include <fastdds/rtps/common/ChangeKind_t.hpp> |
27 | | #include <fastdds/rtps/common/FragmentNumber.hpp> |
28 | | #include <fastdds/rtps/common/InstanceHandle.hpp> |
29 | | #include <fastdds/rtps/common/SerializedPayload.hpp> |
30 | | #include <fastdds/rtps/common/Time_t.hpp> |
31 | | #include <fastdds/rtps/common/Types.hpp> |
32 | | #include <fastdds/rtps/common/VendorId_t.hpp> |
33 | | #include <fastdds/rtps/common/WriteParams.hpp> |
34 | | #include <fastdds/rtps/history/IPayloadPool.hpp> |
35 | | |
36 | | namespace eprosima { |
37 | | namespace fastdds { |
38 | | namespace rtps { |
39 | | |
40 | | struct CacheChange_t; |
41 | | |
42 | | /*! |
43 | | * Specific information for a writer. |
44 | | */ |
45 | | struct CacheChangeWriterInfo_t |
46 | | { |
47 | | //!Number of DATA / DATA_FRAG submessages sent to the transport (only used in Writers) |
48 | | size_t num_sent_submessages = 0; |
49 | | //! Used to link with previous node in a list. Used by FlowControllerImpl. |
50 | | //! Cannot be cached because there are several comparisons without locking. |
51 | | CacheChange_t* volatile previous = nullptr; |
52 | | //! Used to link with next node in a list. Used by FlowControllerImpl. |
53 | | //! Cannot be cached because there are several comparisons without locking. |
54 | | CacheChange_t* volatile next = nullptr; |
55 | | //! Used to know if the object is already in a list. |
56 | | std::atomic_bool is_linked {false}; |
57 | | }; |
58 | | |
59 | | /*! |
60 | | * Specific information for a reader. |
61 | | */ |
62 | | struct CacheChangeReaderInfo_t |
63 | | { |
64 | | //!Reception TimeStamp (only used in Readers) |
65 | | Time_t receptionTimestamp; |
66 | | //! Disposed generation of the instance when this entry was added to it |
67 | | int32_t disposed_generation_count; |
68 | | //! No-writers generation of the instance when this entry was added to it |
69 | | int32_t no_writers_generation_count; |
70 | | //! Ownership stregth of its writer when the sample was received. |
71 | | uint32_t writer_ownership_strength; |
72 | | }; |
73 | | |
74 | | /** |
75 | | * Structure CacheChange_t, contains information on a specific CacheChange. |
76 | | * @ingroup COMMON_MODULE |
77 | | */ |
78 | | struct FASTDDS_EXPORTED_API CacheChange_t |
79 | | { |
80 | | //!Kind of change, default value ALIVE. |
81 | | ChangeKind_t kind = ALIVE; |
82 | | //!GUID_t of the writer that generated this change. |
83 | | GUID_t writerGUID{}; |
84 | | //!Handle of the data associated with this change. |
85 | | InstanceHandle_t instanceHandle{}; |
86 | | //!SequenceNumber of the change |
87 | | SequenceNumber_t sequenceNumber{}; |
88 | | //!Serialized Payload associated with the change. |
89 | | SerializedPayload_t serializedPayload{}; |
90 | | //!CDR serialization of inlined QoS for this change. |
91 | | SerializedPayload_t inline_qos{}; |
92 | | //!Indicates if the cache has been read (only used in READERS) |
93 | | bool isRead = false; |
94 | | //!Source TimeStamp |
95 | | Time_t sourceTimestamp{}; |
96 | | //! Vendor Id of the writer that generated this change. |
97 | | fastdds::rtps::VendorId_t vendor_id = c_VendorId_Unknown; |
98 | | |
99 | | union |
100 | | { |
101 | | CacheChangeReaderInfo_t reader_info; |
102 | | CacheChangeWriterInfo_t writer_info; |
103 | | }; |
104 | | |
105 | | WriteParams write_params{}; |
106 | | bool is_untyped_ = true; |
107 | | |
108 | | /*! |
109 | | * @brief Default constructor. |
110 | | * Creates an empty CacheChange_t. |
111 | | */ |
112 | | CacheChange_t() |
113 | 0 | : writer_info() |
114 | 0 | { |
115 | 0 | inline_qos.encapsulation = DEFAULT_ENDIAN == LITTLEEND ? PL_CDR_LE : PL_CDR_BE; |
116 | 0 | } |
117 | | |
118 | | CacheChange_t( |
119 | | const CacheChange_t&) = delete; |
120 | | const CacheChange_t& operator =( |
121 | | const CacheChange_t&) = delete; |
122 | | |
123 | | /** |
124 | | * Constructor with payload size |
125 | | * @param payload_size Serialized payload size |
126 | | * @param is_untyped Flag to mark the change as untyped. |
127 | | */ |
128 | | CacheChange_t( |
129 | | uint32_t payload_size, |
130 | | bool is_untyped = false) |
131 | | : serializedPayload(payload_size) |
132 | | , is_untyped_(is_untyped) |
133 | 0 | { |
134 | 0 | } |
135 | | |
136 | | /*! |
137 | | * Copy a different change into this one. All the elements are copied, included the data, allocating new memory. |
138 | | * @param [in] ch_ptr Pointer to the change. |
139 | | * @return True if correct. |
140 | | */ |
141 | | bool copy( |
142 | | const CacheChange_t* ch_ptr) |
143 | 0 | { |
144 | 0 | kind = ch_ptr->kind; |
145 | 0 | writerGUID = ch_ptr->writerGUID; |
146 | 0 | instanceHandle = ch_ptr->instanceHandle; |
147 | 0 | sequenceNumber = ch_ptr->sequenceNumber; |
148 | 0 | sourceTimestamp = ch_ptr->sourceTimestamp; |
149 | 0 | reader_info.receptionTimestamp = ch_ptr->reader_info.receptionTimestamp; |
150 | 0 | write_params = ch_ptr->write_params; |
151 | 0 | isRead = ch_ptr->isRead; |
152 | 0 | vendor_id = ch_ptr->vendor_id; |
153 | 0 | fragment_size_ = ch_ptr->fragment_size_; |
154 | 0 | fragment_count_ = ch_ptr->fragment_count_; |
155 | 0 | first_missing_fragment_ = ch_ptr->first_missing_fragment_; |
156 | |
|
157 | 0 | return serializedPayload.copy(&ch_ptr->serializedPayload, !ch_ptr->is_untyped_); |
158 | 0 | } |
159 | | |
160 | | /*! |
161 | | * Copy information form a different change into this one. |
162 | | * All the elements are copied except data. |
163 | | * @param [in] ch_ptr Pointer to the change. |
164 | | */ |
165 | | void copy_not_memcpy( |
166 | | const CacheChange_t* ch_ptr) |
167 | 0 | { |
168 | 0 | kind = ch_ptr->kind; |
169 | 0 | writerGUID = ch_ptr->writerGUID; |
170 | 0 | instanceHandle = ch_ptr->instanceHandle; |
171 | 0 | sequenceNumber = ch_ptr->sequenceNumber; |
172 | 0 | sourceTimestamp = ch_ptr->sourceTimestamp; |
173 | 0 | reader_info.receptionTimestamp = ch_ptr->reader_info.receptionTimestamp; |
174 | 0 | write_params = ch_ptr->write_params; |
175 | 0 | isRead = ch_ptr->isRead; |
176 | 0 | vendor_id = ch_ptr->vendor_id; |
177 | | |
178 | | // Copy certain values from serializedPayload |
179 | 0 | serializedPayload.encapsulation = ch_ptr->serializedPayload.encapsulation; |
180 | 0 | serializedPayload.is_serialized_key = ch_ptr->serializedPayload.is_serialized_key; |
181 | | |
182 | | // Copy fragment size and calculate fragment count |
183 | 0 | setFragmentSize(ch_ptr->fragment_size_, false); |
184 | 0 | } |
185 | | |
186 | 0 | virtual ~CacheChange_t() = default; |
187 | | |
188 | | /*! |
189 | | * Get the number of fragments this change is split into. |
190 | | * @return number of fragments. |
191 | | */ |
192 | | uint32_t getFragmentCount() const |
193 | 0 | { |
194 | 0 | return fragment_count_; |
195 | 0 | } |
196 | | |
197 | | /*! |
198 | | * Get the size of each fragment this change is split into. |
199 | | * @return size of fragment (0 means change is not fragmented). |
200 | | */ |
201 | | uint16_t getFragmentSize() const |
202 | 0 | { |
203 | 0 | return fragment_size_; |
204 | 0 | } |
205 | | |
206 | | /*! |
207 | | * Checks if all fragments have been received. |
208 | | * @return true when change is fully assembled (i.e. no missing fragments). |
209 | | */ |
210 | | bool is_fully_assembled() |
211 | 0 | { |
212 | 0 | return first_missing_fragment_ >= fragment_count_; |
213 | 0 | } |
214 | | |
215 | | /*! Checks if the first fragment is present. |
216 | | * @return true when it contains the first fragment. In other case, false. |
217 | | */ |
218 | | bool contains_first_fragment() |
219 | 0 | { |
220 | 0 | return 0 < first_missing_fragment_; |
221 | 0 | } |
222 | | |
223 | | /*! |
224 | | * Fills a FragmentNumberSet_t with the list of missing fragments. |
225 | | * @param [out] frag_sns FragmentNumberSet_t where result is stored. |
226 | | */ |
227 | | void get_missing_fragments( |
228 | | FragmentNumberSet_t& frag_sns) |
229 | 0 | { |
230 | | // Note: Fragment numbers are 1-based but we keep them 0 based. |
231 | 0 | frag_sns.base(first_missing_fragment_ + 1); |
232 | | |
233 | | // Traverse list of missing fragments, adding them to frag_sns |
234 | 0 | uint32_t current_frag = first_missing_fragment_; |
235 | 0 | while (current_frag < fragment_count_) |
236 | 0 | { |
237 | 0 | frag_sns.add(current_frag + 1); |
238 | 0 | current_frag = get_next_missing_fragment(current_frag); |
239 | 0 | } |
240 | 0 | } |
241 | | |
242 | | /*! |
243 | | * Set fragment size for this change. |
244 | | * |
245 | | * @param fragment_size Size of fragments. |
246 | | * @param create_fragment_list Whether to create missing fragments list or not. |
247 | | * |
248 | | * @remarks Parameter create_fragment_list should only be true when receiving the first |
249 | | * fragment of a change. |
250 | | */ |
251 | | void setFragmentSize( |
252 | | uint16_t fragment_size, |
253 | | bool create_fragment_list = false) |
254 | 0 | { |
255 | 0 | fragment_size_ = fragment_size; |
256 | 0 | fragment_count_ = 0; |
257 | 0 | first_missing_fragment_ = 0; |
258 | |
|
259 | 0 | if (fragment_size > 0) |
260 | 0 | { |
261 | | // This follows RTPS 8.3.7.3.5 |
262 | 0 | fragment_count_ = (serializedPayload.length + fragment_size - 1) / fragment_size; |
263 | |
|
264 | 0 | if (create_fragment_list) |
265 | 0 | { |
266 | | // Keep index of next fragment on the payload portion at the beginning of each fragment. Last |
267 | | // fragment will have fragment_count_ as 'next fragment index' |
268 | 0 | size_t offset = 0; |
269 | 0 | for (uint32_t i = 1; i <= fragment_count_; i++, offset += fragment_size_) |
270 | 0 | { |
271 | 0 | set_next_missing_fragment(i - 1, i); // index to next fragment in missing list |
272 | 0 | } |
273 | 0 | } |
274 | 0 | else |
275 | 0 | { |
276 | | // List not created. This means we are going to send this change fragmented, so it is already |
277 | | // assembled, and the missing list is empty (i.e. first missing points to fragment count) |
278 | 0 | first_missing_fragment_ = fragment_count_; |
279 | 0 | } |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | | bool add_fragments( |
284 | | const SerializedPayload_t& incoming_data, |
285 | | uint32_t fragment_starting_num, |
286 | | uint32_t fragments_in_submessage) |
287 | 0 | { |
288 | 0 | uint32_t original_offset = (fragment_starting_num - 1) * fragment_size_; |
289 | 0 | uint32_t incoming_length = fragment_size_ * fragments_in_submessage; |
290 | 0 | uint32_t last_fragment_index = fragment_starting_num + fragments_in_submessage - 1; |
291 | | |
292 | | // Validate payload types |
293 | 0 | if (serializedPayload.is_serialized_key != incoming_data.is_serialized_key) |
294 | 0 | { |
295 | 0 | return false; |
296 | 0 | } |
297 | | |
298 | | // Validate fragment indexes |
299 | 0 | if (last_fragment_index > fragment_count_) |
300 | 0 | { |
301 | 0 | return false; |
302 | 0 | } |
303 | | |
304 | | // Update incoming length for last fragment |
305 | 0 | if (last_fragment_index == fragment_count_) |
306 | 0 | { |
307 | 0 | incoming_length = serializedPayload.length - original_offset; |
308 | 0 | } |
309 | | |
310 | | // Validate lengths |
311 | 0 | if (incoming_data.length < incoming_length) |
312 | 0 | { |
313 | 0 | return false; |
314 | 0 | } |
315 | | |
316 | 0 | if (original_offset + incoming_length > serializedPayload.length) |
317 | 0 | { |
318 | 0 | return false; |
319 | 0 | } |
320 | | |
321 | 0 | if (received_fragments(fragment_starting_num - 1, fragments_in_submessage)) |
322 | 0 | { |
323 | 0 | memcpy( |
324 | 0 | &serializedPayload.data[original_offset], |
325 | 0 | incoming_data.data, incoming_length); |
326 | 0 | } |
327 | |
|
328 | 0 | return is_fully_assembled(); |
329 | 0 | } |
330 | | |
331 | | /** |
332 | | * @brief Calculate the minimum required payload size to store a fragmented change. |
333 | | * |
334 | | * @param[in] payload_size Size of the full payload. |
335 | | * @param[in] fragment_size Size of each fragment. |
336 | | * @param[out] min_required_size Minimum required size to store the fragmented payload. |
337 | | */ |
338 | | static bool calculate_required_fragmented_payload_size( |
339 | | uint32_t payload_size, |
340 | | uint16_t fragment_size, |
341 | | uint32_t& min_required_size) |
342 | 0 | { |
343 | 0 | if ((0 == fragment_size) || (payload_size <= fragment_size)) |
344 | 0 | { |
345 | 0 | min_required_size = payload_size; |
346 | 0 | return true; |
347 | 0 | } |
348 | | |
349 | | // In order to avoid overflow on the calculations, we limit the maximum payload size |
350 | 0 | constexpr uint32_t MAX_PAYLOAD_SIZE = std::numeric_limits<uint32_t>::max() - 4u - 3u; |
351 | 0 | if (payload_size > MAX_PAYLOAD_SIZE) |
352 | 0 | { |
353 | 0 | return false; |
354 | 0 | } |
355 | | |
356 | | // Ensure fragment size is at least 4 bytes to store fragment index |
357 | 0 | if (fragment_size < 4u) |
358 | 0 | { |
359 | 0 | return false; |
360 | 0 | } |
361 | | |
362 | | // Calculate number of fragments without risk of overflow |
363 | 0 | uint32_t fragment_count = payload_size / fragment_size; |
364 | 0 | if (0 != (payload_size % fragment_size)) |
365 | 0 | { |
366 | 0 | ++fragment_count; |
367 | 0 | } |
368 | | |
369 | | // This cannot overflow as the result will always be <= payload_size |
370 | 0 | uint32_t last_fragment_offset = (fragment_count - 1) * fragment_size; |
371 | | |
372 | | // Since we will write a fragment index at the beginning of each fragment, |
373 | | // we need to ensure there is space for it in the last fragment. |
374 | | // Note: we already imposed limits to ensure no overflow occurs. |
375 | 0 | min_required_size = (last_fragment_offset + 3u) & ~3u; // Align last fragment size to 4 bytes |
376 | 0 | min_required_size += 4u; // Add fragment index size |
377 | | |
378 | | // Ensure minimum size is at least payload size |
379 | 0 | if (min_required_size < payload_size) |
380 | 0 | { |
381 | 0 | min_required_size = payload_size; |
382 | 0 | } |
383 | |
|
384 | 0 | return true; |
385 | 0 | } |
386 | | |
387 | | private: |
388 | | |
389 | | // Fragment size |
390 | | uint16_t fragment_size_ = 0; |
391 | | |
392 | | // Number of fragments |
393 | | uint32_t fragment_count_ = 0; |
394 | | |
395 | | // First fragment in missing list |
396 | | uint32_t first_missing_fragment_ = 0; |
397 | | |
398 | | uint32_t get_next_missing_fragment( |
399 | | uint32_t fragment_index) |
400 | 0 | { |
401 | 0 | uint32_t* ptr = next_fragment_pointer(fragment_index); |
402 | 0 | return *ptr; |
403 | 0 | } |
404 | | |
405 | | void set_next_missing_fragment( |
406 | | uint32_t fragment_index, |
407 | | uint32_t next_fragment_index) |
408 | 0 | { |
409 | 0 | uint32_t* ptr = next_fragment_pointer(fragment_index); |
410 | 0 | *ptr = next_fragment_index; |
411 | 0 | } |
412 | | |
413 | | uint32_t* next_fragment_pointer( |
414 | | uint32_t fragment_index) |
415 | 0 | { |
416 | 0 | size_t offset = fragment_size_; |
417 | 0 | offset *= fragment_index; |
418 | 0 | offset = (offset + 3u) & ~3u; |
419 | 0 | return reinterpret_cast<uint32_t*>(&serializedPayload.data[offset]); |
420 | 0 | } |
421 | | |
422 | | /*! |
423 | | * Mark a set of consecutive fragments as received. |
424 | | * This will remove a set of consecutive fragments from the missing list. |
425 | | * Should be called BEFORE copying the received data into the serialized payload. |
426 | | * |
427 | | * @param initial_fragment Index (0-based) of first received fragment. |
428 | | * @param num_of_fragments Number of received fragments. Should be strictly positive. |
429 | | * @return true if the list of missing fragments was modified, false otherwise. |
430 | | */ |
431 | | bool received_fragments( |
432 | | uint32_t initial_fragment, |
433 | | uint32_t num_of_fragments) |
434 | 0 | { |
435 | 0 | bool at_least_one_changed = false; |
436 | |
|
437 | 0 | if ((fragment_size_ > 0) && (initial_fragment < fragment_count_)) |
438 | 0 | { |
439 | 0 | uint32_t last_fragment = initial_fragment + num_of_fragments; |
440 | 0 | if (last_fragment > fragment_count_) |
441 | 0 | { |
442 | 0 | last_fragment = fragment_count_; |
443 | 0 | } |
444 | |
|
445 | 0 | if (initial_fragment <= first_missing_fragment_) |
446 | 0 | { |
447 | | // Perform first = *first until first >= last_received |
448 | 0 | while (first_missing_fragment_ < last_fragment) |
449 | 0 | { |
450 | 0 | first_missing_fragment_ = get_next_missing_fragment(first_missing_fragment_); |
451 | 0 | at_least_one_changed = true; |
452 | 0 | } |
453 | 0 | } |
454 | 0 | else |
455 | 0 | { |
456 | | // Find prev in missing list |
457 | 0 | uint32_t current_frag = first_missing_fragment_; |
458 | 0 | while (current_frag < initial_fragment) |
459 | 0 | { |
460 | 0 | uint32_t next_frag = get_next_missing_fragment(current_frag); |
461 | 0 | if (next_frag >= initial_fragment) |
462 | 0 | { |
463 | | // This is the fragment previous to initial_fragment. |
464 | | // Find future value for next by repeating next = *next until next >= last_fragment. |
465 | 0 | uint32_t next_missing_fragment = next_frag; |
466 | 0 | while (next_missing_fragment < last_fragment) |
467 | 0 | { |
468 | 0 | next_missing_fragment = get_next_missing_fragment(next_missing_fragment); |
469 | 0 | at_least_one_changed = true; |
470 | 0 | } |
471 | | |
472 | | // Update next and finish loop |
473 | 0 | if (at_least_one_changed) |
474 | 0 | { |
475 | 0 | set_next_missing_fragment(current_frag, next_missing_fragment); |
476 | 0 | } |
477 | 0 | break; |
478 | 0 | } |
479 | 0 | current_frag = next_frag; |
480 | 0 | } |
481 | 0 | } |
482 | 0 | } |
483 | |
|
484 | 0 | return at_least_one_changed; |
485 | 0 | } |
486 | | |
487 | | }; |
488 | | |
489 | | } // namespace rtps |
490 | | } // namespace fastdds |
491 | | } // namespace eprosima |
492 | | |
493 | | #endif // FASTDDS_RTPS_COMMON__CACHECHANGE_HPP |