Skip to content

Commit f50b9f8

Browse files
authored
SCTP part 3 (#1742)
1 parent 92f4d4b commit f50b9f8

41 files changed

Lines changed: 1551 additions & 490 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎worker/.clang-format‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Language: Cpp
22
AccessModifierOffset: -2
33
AlignAfterOpenBracket: AlwaysBreak
4+
AlignArrayOfStructures: Left
45
AlignConsecutiveAssignments: Consecutive
56
AlignConsecutiveDeclarations: None
67
AlignOperands: true
@@ -10,12 +11,14 @@ AllowShortBlocksOnASingleLine: Never
1011
AllowShortCaseLabelsOnASingleLine: false
1112
AllowShortFunctionsOnASingleLine: None
1213
AllowShortIfStatementsOnASingleLine: false
14+
AllowShortLambdasOnASingleLine: None
1315
AllowShortLoopsOnASingleLine: false
14-
AlwaysBreakAfterReturnType: None
1516
AlwaysBreakBeforeMultilineStrings: true
1617
AlwaysBreakTemplateDeclarations: Yes
1718
BinPackArguments: false
1819
BinPackParameters: false
20+
# TODO: It requires clang-format 22.
21+
# BreakAfterOpenBracketBracedList: true
1922
BraceWrapping:
2023
AfterClass: true
2124
AfterControlStatement: Always
@@ -29,6 +32,7 @@ BraceWrapping:
2932
BeforeCatch: true
3033
BeforeElse: true
3134
IndentBraces: false
35+
BreakAfterReturnType: Automatic
3236
BreakBeforeBraces: Allman
3337
BreakBeforeBinaryOperators: None
3438
BreakBeforeInheritanceComma: false

worker/fuzzer/include/RTC/SCTP/association/FuzzerStateCookie.hpp renamed to worker/fuzzer/include/RTC/SCTP/FuzzerStateCookie.hpp

File renamed without changes.

worker/fuzzer/src/RTC/SCTP/association/FuzzerStateCookie.cpp renamed to worker/fuzzer/src/RTC/SCTP/FuzzerStateCookie.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "RTC/SCTP/association/FuzzerStateCookie.hpp"
1+
#include "RTC/SCTP/FuzzerStateCookie.hpp"
22
#include "Utils.hpp"
3-
#include "RTC/SCTP/association/StateCookie.hpp"
3+
#include "RTC/SCTP/StateCookie.hpp"
44
#include <cstdlib> // std::malloc(), std::free()
55
#include <cstring> // std::memcpy()
66

‎worker/fuzzer/src/fuzzer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "RTC/RTP/FuzzerProbationGenerator.hpp"
2727
#include "RTC/RTP/FuzzerRetransmissionBuffer.hpp"
2828
#include "RTC/RTP/FuzzerRtpStreamSend.hpp"
29-
#include "RTC/SCTP/association/FuzzerStateCookie.hpp"
29+
#include "RTC/SCTP/FuzzerStateCookie.hpp"
3030
#include "RTC/SCTP/packet/FuzzerPacket.hpp"
3131
#include <cstdlib> // std::getenv()
3232
#include <iostream>

‎worker/include/RTC/RTCP/CompoundPacket.hpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,19 @@ namespace RTC
6464
this->xrPacket.Begin(),
6565
this->xrPacket.End(),
6666
[](const ExtendedReportBlock* report)
67-
{ return report->GetType() == ExtendedReportBlock::Type::RRT; });
67+
{
68+
return report->GetType() == ExtendedReportBlock::Type::RRT;
69+
});
6870
}
6971
bool HasDelaySinceLastRr()
7072
{
7173
return std::any_of(
7274
this->xrPacket.Begin(),
7375
this->xrPacket.End(),
7476
[](const ExtendedReportBlock* report)
75-
{ return report->GetType() == ExtendedReportBlock::Type::DLRR; });
77+
{
78+
return report->GetType() == ExtendedReportBlock::Type::DLRR;
79+
});
7680
}
7781
void Serialize(uint8_t* data);
7882

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#ifndef MS_RTC_SCTP_MESSAGE_HPP
2+
#define MS_RTC_SCTP_MESSAGE_HPP
3+
4+
#include "common.hpp"
5+
#include "RTC/SCTP/packet/Packet.hpp"
6+
7+
namespace RTC
8+
{
9+
namespace SCTP
10+
{
11+
/**
12+
* An SCTP message is a group of bytes sent or received as a whole on a
13+
* specified stream identifier (`streamId`) and with a payload protocol
14+
* identifier (`ppid`).
15+
*/
16+
class Message
17+
{
18+
public:
19+
Message(uint16_t streamId, uint32_t ppid, std::vector<uint8_t> payload);
20+
21+
// Move constructor. No need to do anything special since std::vector
22+
// already implements move.
23+
Message(Message&& other) = default;
24+
25+
// Move assignment. No need to do anything special since std::vector
26+
// already implements move.
27+
Message& operator=(Message&& other) = default;
28+
29+
// Disable copy constructor.
30+
Message(const Message&) = delete;
31+
32+
// Disable copy assignment.
33+
Message& operator=(const Message&) = delete;
34+
35+
~Message();
36+
37+
public:
38+
void Dump(int indentation = 0) const;
39+
40+
uint16_t GetStreamId() const
41+
{
42+
return this->streamId;
43+
}
44+
45+
uint32_t GetPayloadProtocolIdentifier() const
46+
{
47+
return this->ppid;
48+
}
49+
50+
const uint8_t* GetPayload() const
51+
{
52+
return this->payload.data();
53+
}
54+
55+
size_t GetPayloadLength() const
56+
{
57+
return this->payload.size();
58+
}
59+
60+
/**
61+
* Useful to extract the payload and its ownership When destructing the
62+
* Message.
63+
*/
64+
std::vector<uint8_t> ReleasePayload() &&
65+
{
66+
return std::move(this->payload);
67+
}
68+
69+
private:
70+
uint16_t streamId{ 0 };
71+
uint32_t ppid{ 0 };
72+
std::vector<uint8_t> payload;
73+
};
74+
} // namespace SCTP
75+
} // namespace RTC
76+
77+
#endif

worker/include/RTC/SCTP/association/NegotiatedCapabilities.hpp renamed to worker/include/RTC/SCTP/NegotiatedCapabilities.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define MS_RTC_SCTP_NEGOTIATED_CAPABILITIES_HPP
33

44
#include "common.hpp"
5-
#include "RTC/SCTP/association/SocketOptions.hpp"
5+
#include "RTC/SCTP/SocketOptions.hpp"
66
#include "RTC/SCTP/packet/chunks/InitAckChunk.hpp"
77
#include "RTC/SCTP/packet/chunks/InitChunk.hpp"
88
#include <variant> // std::variant, std::visit()

worker/include/RTC/SCTP/association/Socket.hpp renamed to worker/include/RTC/SCTP/Socket.hpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#define MS_RTC_SCTP_SOCKET_HPP
33

44
#include "common.hpp"
5-
#include "RTC/SCTP/association/NegotiatedCapabilities.hpp"
6-
#include "RTC/SCTP/association/SocketMetrics.hpp"
7-
#include "RTC/SCTP/association/SocketOptions.hpp"
8-
#include "RTC/SCTP/association/TransmissionControlBlock.hpp"
5+
#include "RTC/SCTP/NegotiatedCapabilities.hpp"
6+
#include "RTC/SCTP/SocketDeferredListener.hpp"
7+
#include "RTC/SCTP/SocketListener.hpp"
8+
#include "RTC/SCTP/SocketMetrics.hpp"
9+
#include "RTC/SCTP/SocketOptions.hpp"
10+
#include "RTC/SCTP/TransmissionControlBlock.hpp"
911
#include "RTC/SCTP/packet/Chunk.hpp"
1012
#include "RTC/SCTP/packet/Packet.hpp"
1113
#include "RTC/SCTP/packet/chunks/AbortAssociationChunk.hpp"
@@ -34,16 +36,6 @@ namespace RTC
3436
*/
3537
class Socket : public BackoffTimerHandle::Listener
3638
{
37-
public:
38-
class Listener
39-
{
40-
public:
41-
virtual ~Listener() = default;
42-
43-
public:
44-
virtual void OnSocketSendSctpPacket(const Socket* socket, Packet* packet) const = 0;
45-
};
46-
4739
public:
4840
/**
4941
* SCTP association state.
@@ -77,10 +69,10 @@ namespace RTC
7769
};
7870

7971
public:
80-
static constexpr std::string_view State2String(State state);
72+
static constexpr std::string_view StateToString(State state);
8173

8274
public:
83-
explicit Socket(SocketOptions options, Listener* listener);
75+
explicit Socket(SocketOptions options, SocketListener* listener);
8476

8577
~Socket();
8678

@@ -93,7 +85,7 @@ namespace RTC
9385
* @remarks
9486
* The Socket must be in Closed state.
9587
*/
96-
void Associate();
88+
void Connect();
9789

9890
/**
9991
* Receive a Packet received from the peer.
@@ -155,11 +147,11 @@ namespace RTC
155147
bool ProcessReceivedUnknownChunk(
156148
const Packet* receivedPacket, const UnknownChunk* receivedUnknownChunk);
157149

158-
void OnT1InitTimer(uint64_t& baseTimeout, bool& stop);
150+
void OnT1InitTimer(uint64_t& baseTimeoutMs, bool& stop);
159151

160-
void OnT1CookieTimer(uint64_t& baseTimeout, bool& stop);
152+
void OnT1CookieTimer(uint64_t& baseTimeoutMs, bool& stop);
161153

162-
void OnT2ShutdownTimer(uint64_t& baseTimeout, bool& stop);
154+
void OnT2ShutdownTimer(uint64_t& baseTimeoutMs, bool& stop);
163155

164156
template<typename... States>
165157
void AssertState(States... expectedStates) const;
@@ -171,13 +163,14 @@ namespace RTC
171163

172164
/* Pure virtual methods inherited from BackoffTimerHandle::Listener. */
173165
public:
174-
void OnTimer(BackoffTimerHandle* backoffTimer, uint64_t& baseTimeout, bool& stop) override;
166+
void OnTimer(BackoffTimerHandle* backoffTimer, uint64_t& baseTimeoutMs, bool& stop) override;
175167

176168
private:
177169
// Socket options given in th econstructor.
178170
const SocketOptions options;
179-
// Listener.
180-
const Listener* listener{ nullptr };
171+
// Listener. It's not a SocketListener but a SocketDeferredListener which
172+
// inherits from SocketListener.
173+
SocketDeferredListener listener;
181174
// SCTP association state.
182175
State state{ State::CLOSED };
183176
// Metrics.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#ifndef MS_RTC_SCTP_SOCKET_DEFERRED_LISTENER_HPP
2+
#define MS_RTC_SCTP_SOCKET_DEFERRED_LISTENER_HPP
3+
4+
#include "common.hpp"
5+
#include "RTC/SCTP/Message.hpp"
6+
#include "RTC/SCTP/SocketListener.hpp"
7+
#include "RTC/SCTP/Types.hpp"
8+
#include "RTC/SCTP/packet/Packet.hpp"
9+
#include <span>
10+
#include <string>
11+
#include <string_view>
12+
#include <variant>
13+
#include <vector>
14+
15+
namespace RTC
16+
{
17+
namespace SCTP
18+
{
19+
class SocketDeferredListener : public SocketListener
20+
{
21+
public:
22+
class ScopedDeferred
23+
{
24+
public:
25+
explicit ScopedDeferred(SocketDeferredListener* deferredListener);
26+
27+
~ScopedDeferred();
28+
29+
private:
30+
SocketDeferredListener* deferredListener;
31+
};
32+
33+
private:
34+
struct Error
35+
{
36+
Types::ErrorKind errorKind;
37+
std::string message;
38+
};
39+
40+
struct StreamReset
41+
{
42+
std::vector<uint16_t> streamIds;
43+
std::string errorMessage;
44+
};
45+
46+
// Use a pre-sized variant for storage to avoid double heap allocation. This
47+
// variant can hold all cases of stored data.
48+
using CallbackData = std::variant<std::monostate, Message, Error, StreamReset, uint16_t>;
49+
50+
using Callback = std::function<void(CallbackData, SocketListener*)>;
51+
52+
public:
53+
explicit SocketDeferredListener(SocketListener* innerListener);
54+
55+
private:
56+
void SetReady();
57+
58+
void TriggerDeferredCallbacks();
59+
60+
public:
61+
/* Pure virtual methods inherited from Socket::Listener. */
62+
bool OnSocketSendSctpPacket(const Socket* socket, Packet* packet) override;
63+
64+
void OnSocketConnected(const Socket* socket) override;
65+
66+
void OnSocketClosed(const Socket* socket) override;
67+
68+
void OnSocketConnectionRestarted(const Socket* socket) override;
69+
70+
void OnSocketError(
71+
const Socket* socket, Types::ErrorKind errorKind, std::string_view errorMessage) override;
72+
73+
void OnSocketAborted(
74+
const Socket* socket, Types::ErrorKind errorKind, std::string_view errorMessage) override;
75+
76+
void OnSocketMessageReceived(const Socket* socket, Message message) override;
77+
78+
void OnSocketStreamsResetPerformed(
79+
const Socket* socket, std::span<const uint16_t> outboundStreamIds) override;
80+
81+
void OnSocketStreamsResetFailed(
82+
const Socket* socket,
83+
std::span<const uint16_t> outboundStreamIds,
84+
std::string_view errorMessage) override;
85+
86+
void OnSocketInboundStreamsReset(
87+
const Socket* socket, std::span<const uint16_t> inboundStreamIds) override;
88+
89+
void OnSocketBufferedAmountLow(const Socket* socket, uint16_t streamId) override;
90+
91+
void OnSocketTotalBufferedAmountLow(const Socket* socket) override;
92+
93+
private:
94+
SocketListener* innerListener{ nullptr };
95+
bool ready{ false };
96+
std::vector<std::pair<Callback, CallbackData>> deferredCallbacks;
97+
};
98+
} // namespace SCTP
99+
} // namespace RTC
100+
101+
#endif

0 commit comments

Comments
 (0)