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