@@ -59,7 +59,7 @@ namespace RTC
5959 {
6060 // The remaining length in the buffer is the potential buffer length
6161 // of the chunk.
62- size_t chunkBufferLength = packet-> GetEndPointer () - ptr ;
62+ size_t chunkBufferLength = bufferLength - (ptr - buffer) ;
6363
6464 // Here we must anticipate the type of each chunk to use its appropriate
6565 // parser.
@@ -128,7 +128,7 @@ namespace RTC
128128 // Ensure computed length matches the total given buffer length.
129129 if (computedLength != bufferLength)
130130 {
131- MS_WARN_DEV (" computed padded length != buffer length" );
131+ MS_WARN_DEV (" computed length != buffer length" );
132132
133133 delete packet;
134134 return nullptr ;
@@ -148,9 +148,22 @@ namespace RTC
148148 {
149149 MS_TRACE ();
150150
151- // TODO
151+ size_t computedLength = Packet::CommonHeaderLength;
152152
153- return nullptr ;
153+ // No space for common header.
154+ if (bufferLength < computedLength)
155+ {
156+ MS_THROW_TYPE_ERROR (" no space for common header" );
157+ }
158+
159+ auto * packet = new Packet (buffer, bufferLength);
160+
161+ packet->InitializeHeader ();
162+
163+ // Must always invoke SetLength() after constructing a Serializable.
164+ packet->SetLength (computedLength);
165+
166+ return packet;
154167 }
155168
156169 /* Instance methods. */
@@ -164,11 +177,10 @@ namespace RTC
164177 {
165178 MS_TRACE ();
166179
167- // TODO
168- // for (auto* chunk : this->chunks)
169- // {
170- // delete chunk;
171- // }
180+ for (auto * chunk : this ->chunks )
181+ {
182+ delete chunk;
183+ }
172184 }
173185
174186 void Packet::Dump () const
@@ -211,18 +223,17 @@ namespace RTC
211223 // Serialize each chunk into the new buffer.
212224 auto * ptr = buffer + chunksOffset;
213225
214- // TODO
215- // for (auto* chunk : this->chunks)
216- // {
217- // chunk->Serialize(ptr, chunk->GetLength());
226+ for (auto * chunk : this ->chunks )
227+ {
228+ chunk->Serialize (ptr, chunk->GetLength ());
218229
219- // // After calling `Serialize()` on the chunk, its `frozen` flag is
220- // // reverted to false, but we want it to remain set because it's a
221- // // chunk within the packet.
222- // chunk->Freeze();
230+ // After calling `Serialize()` on the chunk, its `frozen` flag is
231+ // reverted to false, but we want it to remain set because it's a
232+ // chunk within the packet.
233+ chunk->Freeze ();
223234
224- // ptr += chunk->GetLength();
225- // }
235+ ptr += chunk->GetLength ();
236+ }
226237
227238 // Manually update buffer and buffer length.
228239 SetBuffer (buffer);
@@ -236,9 +247,40 @@ namespace RTC
236247 {
237248 MS_TRACE ();
238249
239- // TODO
250+ if (bufferLength < GetLength ())
251+ {
252+ MS_THROW_TYPE_ERROR (
253+ " bufferLength (%zu bytes) is lower than current length (%zu bytes)" ,
254+ bufferLength,
255+ GetLength ());
256+ }
257+
258+ size_t chunksOffset = GetChunksPointer () - GetBuffer ();
259+
260+ // Copy all bytes from beginning of the buffer until the position of the
261+ // chunks.
262+ std::memcpy (buffer, GetBuffer (), chunksOffset);
263+
264+ auto * clonedPacket = new Packet (buffer, bufferLength);
265+
266+ // Clone each Chunk into the new buffer.
267+ auto * ptr = buffer + chunksOffset;
268+
269+ for (const auto * chunk : this ->chunks )
270+ {
271+ auto * clonedChunk = chunk->Clone (ptr, chunk->GetLength ());
272+
273+ clonedPacket->AddParsedChunk (clonedChunk);
274+
275+ ptr += chunk->GetLength ();
276+ }
277+
278+ // Need to manually set Serializable length.
279+ clonedPacket->SetLength (GetLength ());
240280
241- return nullptr ;
281+ // NOTE: The `frozen` flag will be false in the cloned packet by default.
282+
283+ return clonedPacket;
242284 }
243285
244286 void Packet::AddChunk (const Chunk* chunk)
@@ -247,7 +289,18 @@ namespace RTC
247289
248290 AssertNotFrozen ();
249291
250- // TODO
292+ size_t length = GetLength () + chunk->GetLength ();
293+
294+ // Let's append the chunk at the end of existing chunks.
295+ auto * clonedChunk = chunk->Clone (GetEndPointer (), chunk->GetLength ());
296+
297+ // Freeze the cloned chunk.
298+ clonedChunk->Freeze ();
299+
300+ this ->chunks .push_back (clonedChunk);
301+
302+ // Update Serializable length.
303+ SetLength (length);
251304 }
252305
253306 void Packet::InitializeHeader ()
0 commit comments