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