Skip to content

Commit 7499d35

Browse files
committed
more
1 parent 7a2ff84 commit 7499d35

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

worker/include/RTC/Serializable.hpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ namespace RTC
8181
* Update the buffer length of the Serializable.
8282
**
8383
* @remarks
84-
* The child class must invoke this method after parsing completes in case
85-
* it couldn't anticipate its expected exact length. Specially useful when
86-
* parsing variable-length items within a packet.
84+
* - The child class must invoke this method after parsing completes in
85+
* case it couldn't anticipate its expected exact length. Specially
86+
* useful when parsing variable-length items within a packet.
87+
* - The application can also invoke this method on the Serializable if
88+
* it has expanded or reduced the length of the buffer currently assigned
89+
* to the Serializable.
8790
*
8891
* @throw MediaSoupError - If given `bufferLength` is lower than the
8992
* current exact length of the Serializable.
@@ -100,7 +103,8 @@ namespace RTC
100103
*
101104
* @remarks
102105
* - Subclasses must override this method if they hold pointers or allocated
103-
* memory.
106+
* memory. In that case, the subclass should manually invoke `SetBuffer()`
107+
* and `SetBufferLenght()` methods.
104108
* - Anyway, if subclasses override this method they still need to invoke
105109
* it in the parent too.
106110
*
@@ -109,14 +113,28 @@ namespace RTC
109113
*/
110114
virtual void Serialize(const uint8_t* buffer, size_t bufferLength);
111115

116+
/**
117+
* TODO
118+
*/
119+
// virtual std::unique_ptr<Serializable> Clone(const uint8_t* buffer, size_t bufferLength) const;
120+
112121
/**
113122
* Methods to be used by classes inheriting from Serializable.
114123
*/
115124
protected:
125+
/**
126+
* Method to be called by the child class in case it overrides the
127+
* `Serialize()` method.
128+
*/
129+
virtual void SetBuffer(const uint8_t* buffer) final
130+
{
131+
this->buffer = const_cast<uint8_t*>(buffer);
132+
}
133+
116134
/**
117135
* Method to be called by the child class to update the current exact
118136
* length of the Serializable.
119-
**
137+
*
120138
* @remarks
121139
* The child class must invoke this method after parsing completes and
122140
* after every change in the Serializable content that affects its current

worker/test/src/RTC/TestSerializable.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,30 @@ class Foo : public Serializable
525525

526526
void Serialize(const uint8_t* buffer, size_t bufferLength) override
527527
{
528-
Serializable::Serialize(buffer, bufferLength);
528+
size_t itemsOffset = GetItemsPointer() - GetBuffer();
529+
size_t paddingOffset = GetPaddingPointer() - GetBuffer();
530+
size_t padding = GetLength() - (GetPaddingPointer() - GetBuffer());
529531

530-
// Serialize each Item into the new buffer.
532+
// Copy all bytes from beginning of the buffer until the position of the
533+
// Items.
534+
std::memcpy(const_cast<uint8_t*>(buffer), GetBuffer(), itemsOffset);
531535

532-
size_t itemsOffset = GetItemsPointer() - GetBuffer();
533-
uint8_t* ptr = const_cast<uint8_t*>(buffer) + itemsOffset;
536+
// Serialize each Item into the new buffer.
537+
uint8_t* ptr = const_cast<uint8_t*>(buffer) + itemsOffset;
534538

535539
for (auto& item : this->items)
536540
{
537541
item->Serialize(ptr, item->GetLength());
538542

539543
ptr += item->GetLength();
540544
}
545+
546+
// Copy padding bytes.
547+
std::memcpy(const_cast<uint8_t*>(buffer) + paddingOffset, GetPaddingPointer(), padding);
548+
549+
// Manually update buffer and buffer length.
550+
SetBuffer(buffer);
551+
SetBufferLength(bufferLength);
541552
}
542553

543554
uint8_t GetType() const

0 commit comments

Comments
 (0)