Skip to content

Commit 704ab4b

Browse files
committed
more
1 parent 3cdd09e commit 704ab4b

File tree

9 files changed

+84
-64
lines changed

9 files changed

+84
-64
lines changed

TODO_SCTP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ NO! Instead:
7070
7171
- If we have a FooDataItem, the app should not be able to change its id via `SetId()`, AKA make `SetId()` protected since app should use `FooXxxxItem::Factory()` static methods.
7272
- `FooItem` constructor: It doesn't make sense that it fills things since we want to rely on subclasses' `Parse()`, `Factory()` and constructors (that will override these calls anyway).
73+
- If `FooItem` has valid but unknown id, that's correct, but let's remove the `SetValue()` method so those unknown items can only be parsed but not created.

worker/include/RTC/Serializable.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ namespace RTC
110110
* @throw MediaSoupError - If given `bufferLength` is lower than the
111111
* current exact length of the Serializable.
112112
*/
113-
virtual void Serialize(const uint8_t* buffer, size_t bufferLength);
113+
virtual void Serialize(uint8_t* buffer, size_t bufferLength);
114114

115115
/**
116116
* Clone the Serializable into a new buffer. This method returns a new
@@ -129,7 +129,7 @@ namespace RTC
129129
* @throw MediaSoupError - If given `bufferLength` is lower than the
130130
* current exact length of the Serializable.
131131
*/
132-
virtual std::unique_ptr<Serializable> Clone(const uint8_t* buffer, size_t bufferLength) const = 0;
132+
virtual std::unique_ptr<Serializable> Clone(uint8_t* buffer, size_t bufferLength) const = 0;
133133

134134
/**
135135
* Methods to be used by classes inheriting from Serializable.
@@ -139,9 +139,9 @@ namespace RTC
139139
* Method to be called by the child class in case it overrides the
140140
* `Serialize()` method.
141141
*/
142-
virtual void SetBuffer(const uint8_t* buffer) final
142+
virtual void SetBuffer(uint8_t* buffer) final
143143
{
144-
this->buffer = const_cast<uint8_t*>(buffer);
144+
this->buffer = buffer;
145145
}
146146

147147
/**

worker/src/RTC/Serializable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace RTC
2323
this->bufferLength = bufferLength;
2424
}
2525

26-
void Serializable::Serialize(const uint8_t* buffer, size_t bufferLength)
26+
void Serializable::Serialize(uint8_t* buffer, size_t bufferLength)
2727
{
2828
MS_TRACE();
2929

@@ -35,9 +35,9 @@ namespace RTC
3535
this->length);
3636
}
3737

38-
std::memcpy(const_cast<uint8_t*>(buffer), this->buffer, this->length);
38+
std::memcpy(buffer, this->buffer, this->length);
3939

40-
this->buffer = const_cast<uint8_t*>(buffer);
40+
this->buffer = buffer;
4141
this->bufferLength = bufferLength;
4242
}
4343

worker/test/include/RTC/TestSerializable/FooDataItem.hpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#define MS_RTC_SERIALIZABLE_FOO_DATA_ITEM_HPP
33

44
#include "common.hpp"
5-
#include "Utils.hpp"
6-
#include "RTC/Serializable.hpp"
75
#include "RTC/TestSerializable/FooItem.hpp"
86

97
using namespace RTC;
@@ -44,7 +42,7 @@ class FooDataItem : public FooItem
4442
static std::unique_ptr<FooDataItem> Parse(const uint8_t* buffer, size_t bufferLength);
4543

4644
static std::unique_ptr<FooDataItem> Factory(
47-
const uint8_t* buffer, size_t bufferLength, uint8_t flags, uint16_t number);
45+
uint8_t* buffer, size_t bufferLength, uint8_t flags, uint16_t number);
4846

4947
private:
5048
/**
@@ -58,18 +56,9 @@ class FooDataItem : public FooItem
5856

5957
virtual void Dump() const override final;
6058

61-
virtual uint16_t GetNumber() const final
62-
{
63-
return Utils::Byte::Get2Bytes(GetValuePointer(), 0);
64-
}
59+
virtual uint16_t GetNumber() const final;
6560

66-
virtual void SetNumber(uint16_t number) final
67-
{
68-
// Convert number to network byte order.
69-
number = uint16_t{ htons(number) };
70-
71-
SetValue(reinterpret_cast<const uint8_t*>(std::addressof(number)), NumberLength);
72-
}
61+
virtual void SetNumber(uint16_t number) final;
7362
};
7463

7564
#endif

worker/test/include/RTC/TestSerializable/FooItem.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class FooItem : public Serializable
4040
* @remarks
4141
* - NONE is not a valid value, so a FooItem with id NONE should be
4242
* considered invalid and parsing should fail.
43-
* - Values other than the defined must be accepted as unknown FooItem.
43+
* - Values other than the defined must be accepted as FooUnknownItem.
4444
*/
4545
enum class ItemId : uint8_t
4646
{
@@ -92,7 +92,7 @@ class FooItem : public Serializable
9292
static std::unique_ptr<FooItem> Parse(const uint8_t* buffer, size_t bufferLength);
9393

9494
static std::unique_ptr<FooItem> Factory(
95-
const uint8_t* buffer,
95+
uint8_t* buffer,
9696
size_t bufferLength,
9797
ItemId id,
9898
uint8_t flags,
@@ -122,7 +122,7 @@ class FooItem : public Serializable
122122
/**
123123
* Could be overridden by each subclass.
124124
*/
125-
std::unique_ptr<Serializable> Clone(const uint8_t* buffer, size_t bufferLength) const override;
125+
std::unique_ptr<Serializable> Clone(uint8_t* buffer, size_t bufferLength) const override;
126126

127127
virtual ItemId GetId() const final
128128
{
@@ -176,13 +176,19 @@ class FooItem : public Serializable
176176
virtual void SetValue(const uint8_t* value, uint8_t valueLength) final;
177177

178178
protected:
179+
/**
180+
* NOTE: Return ItemHeader* instead of const ItemHeader* since we may
181+
* want to modify its fields.
182+
*/
179183
ItemHeader* GetHeaderPointer() const
180184
{
181185
return reinterpret_cast<ItemHeader*>(const_cast<uint8_t*>(GetBuffer()));
182186
}
183187

184-
// We make this method private because it returns the value of the Value
185-
// Length field, which is not useful for the application.
188+
/**
189+
* Private private because it returns the value of the Value Length field,
190+
* which is not useful for the application.
191+
*/
186192
uint8_t GetValueLengthField() const
187193
{
188194
return GetHeaderPointer()->valueLength;
@@ -193,9 +199,13 @@ class FooItem : public Serializable
193199
GetHeaderPointer()->valueLength = valueLength;
194200
}
195201

196-
const uint8_t* GetValuePointer() const
202+
/**
203+
* NOTE: Return uint8_t* instead of const uint8_t* since we may want to
204+
* modify its value.
205+
*/
206+
uint8_t* GetValuePointer() const
197207
{
198-
return GetBuffer() + ItemHeaderLength;
208+
return const_cast<uint8_t*>(GetBuffer()) + ItemHeaderLength;
199209
}
200210

201211
const uint8_t* GetEndPointer() const

worker/test/include/RTC/TestSerializable/FooPacket.hpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FooPacket : public Serializable
6767
*/
6868
static std::unique_ptr<FooPacket> Parse(const uint8_t* buffer, size_t length);
6969

70-
static std::unique_ptr<FooPacket> Factory(const uint8_t* buffer, size_t bufferLength, uint8_t type);
70+
static std::unique_ptr<FooPacket> Factory(uint8_t* buffer, size_t bufferLength, uint8_t type);
7171

7272
private:
7373
/**
@@ -81,9 +81,9 @@ class FooPacket : public Serializable
8181

8282
void Dump() const override;
8383

84-
void Serialize(const uint8_t* buffer, size_t bufferLength) override;
84+
void Serialize(uint8_t* buffer, size_t bufferLength) override;
8585

86-
std::unique_ptr<Serializable> Clone(const uint8_t* buffer, size_t bufferLength) const override;
86+
std::unique_ptr<Serializable> Clone(uint8_t* buffer, size_t bufferLength) const override;
8787

8888
uint8_t GetType() const
8989
{
@@ -143,13 +143,19 @@ class FooPacket : public Serializable
143143
void AddItem(std::unique_ptr<FooItem> item);
144144

145145
private:
146+
/**
147+
* NOTE: Return Header* instead of const Header* since we may want to
148+
* modify its fields.
149+
*/
146150
Header* GetHeaderPointer() const
147151
{
148152
return reinterpret_cast<Header*>(const_cast<uint8_t*>(GetBuffer()));
149153
}
150154

151-
// We make this method private because it returns the value of the Length
152-
// field, which is not useful for the application.
155+
/**
156+
* NOTE: Private because it returns the value of the Length field, which is
157+
* not useful for the application.
158+
*/
153159
uint16_t GetLengthField() const
154160
{
155161
return uint16_t{ ntohs(GetHeaderPointer()->length) };
@@ -170,9 +176,13 @@ class FooPacket : public Serializable
170176
GetHeaderPointer()->length = uint16_t{ htons(length) };
171177
}
172178

173-
const uint8_t* GetAppendixPointer() const
179+
/**
180+
* NOTE: Return uint8_t* instead of const uint8_t* since we may want to
181+
* modify its value.
182+
*/
183+
uint8_t* GetAppendixPointer() const
174184
{
175-
return GetBuffer() + HeaderLength;
185+
return const_cast<uint8_t*>(GetBuffer()) + HeaderLength;
176186
}
177187

178188
const uint8_t* GetItemsPointer() const
@@ -187,9 +197,9 @@ class FooPacket : public Serializable
187197
return ptr;
188198
}
189199

190-
const uint8_t* GetPaddingPointer() const
200+
uint8_t* GetPaddingPointer() const
191201
{
192-
return GetBuffer() + GetLengthField();
202+
return const_cast<uint8_t*>(GetBuffer()) + GetLengthField();
193203
}
194204

195205
/**

worker/test/src/RTC/TestSerializable/FooDataItem.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "RTC/TestSerializable/FooDataItem.hpp"
55
#include "Logger.hpp"
66
#include "MediaSoupErrors.hpp"
7+
#include "Utils.hpp"
78

89
using namespace RTC;
910

@@ -41,7 +42,7 @@ std::unique_ptr<FooDataItem> FooDataItem::Parse(const uint8_t* buffer, size_t bu
4142
}
4243

4344
std::unique_ptr<FooDataItem> FooDataItem::Factory(
44-
const uint8_t* buffer, size_t bufferLength, uint8_t flags, uint16_t number)
45+
uint8_t* buffer, size_t bufferLength, uint8_t flags, uint16_t number)
4546
{
4647
MS_TRACE();
4748

@@ -99,3 +100,17 @@ void FooDataItem::Dump() const
99100
MS_DUMP(" number: %" PRIu16, GetNumber());
100101
MS_DUMP("</FooDataItem>");
101102
}
103+
104+
uint16_t FooDataItem::GetNumber() const
105+
{
106+
MS_TRACE();
107+
108+
return Utils::Byte::Get2Bytes(GetValuePointer(), 0);
109+
}
110+
111+
void FooDataItem::SetNumber(uint16_t number)
112+
{
113+
MS_TRACE();
114+
115+
Utils::Byte::Set2Bytes(GetValuePointer(), 0, number);
116+
}

worker/test/src/RTC/TestSerializable/FooItem.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ std::unique_ptr<FooItem> FooItem::Parse(const uint8_t* buffer, size_t bufferLeng
7373

7474
// Pointer that starts at the beginning of the buffer and it's incremented
7575
// to point to different parts of the item.
76-
const uint8_t* ptr = buffer;
76+
auto* ptr = buffer;
7777

7878
// Pointer that points to the end of the buffer.
79-
const uint8_t* end = buffer + bufferLength;
79+
auto* end = buffer + bufferLength;
8080

8181
// NOTE: We are parsing so we don't want to initialize the header.
8282
auto item = std::unique_ptr<FooItem>(new FooItem(buffer, bufferLength, /*initializeHeader*/ false));
@@ -108,12 +108,7 @@ std::unique_ptr<FooItem> FooItem::Parse(const uint8_t* buffer, size_t bufferLeng
108108
}
109109

110110
std::unique_ptr<FooItem> FooItem::Factory(
111-
const uint8_t* buffer,
112-
size_t bufferLength,
113-
ItemId id,
114-
uint8_t flags,
115-
const uint8_t* value,
116-
uint8_t valueLength)
111+
uint8_t* buffer, size_t bufferLength, ItemId id, uint8_t flags, const uint8_t* value, uint8_t valueLength)
117112
{
118113
MS_TRACE();
119114

@@ -197,7 +192,7 @@ void FooItem::Dump() const
197192
MS_DUMP("</FooItem>");
198193
}
199194

200-
std::unique_ptr<Serializable> FooItem::Clone(const uint8_t* buffer, size_t bufferLength) const
195+
std::unique_ptr<Serializable> FooItem::Clone(uint8_t* buffer, size_t bufferLength) const
201196
{
202197
MS_TRACE();
203198

@@ -207,7 +202,7 @@ std::unique_ptr<Serializable> FooItem::Clone(const uint8_t* buffer, size_t buffe
207202
"bufferLength (%zu bytes) is lower than current length (%zu bytes)", bufferLength, GetLength());
208203
}
209204

210-
std::memcpy(const_cast<uint8_t*>(buffer), GetBuffer(), GetLength());
205+
std::memcpy(buffer, GetBuffer(), GetLength());
211206

212207
auto clonedFooItem =
213208
std::unique_ptr<FooItem>(new FooItem(buffer, bufferLength, /*initializeHeader*/ false));
@@ -228,7 +223,7 @@ void FooItem::SetValue(const uint8_t* value, uint8_t valueLength)
228223
SetValueLengthField(valueLength);
229224

230225
// Copy the given value into the buffer.
231-
std::memcpy(const_cast<uint8_t*>(GetValuePointer()), value, valueLength);
226+
std::memcpy(GetValuePointer(), value, valueLength);
232227

233228
// Update Serializable length.
234229
SetLength(GetLength() - previousValueLength + valueLength);

0 commit comments

Comments
 (0)