Skip to content

Commit

Permalink
Test usrsctp with usrsctp_get_timeout()
Browse files Browse the repository at this point in the history
### Details

- Related task: #1011. Note that usrsctp author never cared about the existing PR so we are on our own.
- So I've forked usrctp, added the `usrsctp_get_timeout()` feature and released version 0.9.6.0: versatica/usrsctp#1
- And I've made `usrsctp.wrap` point to it.

### Notes

- It would be great to have a way to pass `sctp_debug=true` when building usrsctp Meson subproject so it defines `SCTP_DEBUG` (needed in `DepUsrSCTP.cpp` to show SCTP debug). An manual alternative is to edit `meson.build` of usrsctp and add it unconditionally.
- I'm calling `HandleUsrSctpTimers()` everytime the timer fires (of course), also in `onSendSctpData` callback and also when SCTP data is received (in `SctpAssociation::ProcessSctpData()`. Not sure if correct.
  • Loading branch information
ibc committed Mar 5, 2024
1 parent af7fb8c commit 58384dc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
2 changes: 2 additions & 0 deletions worker/include/DepUsrSCTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class DepUsrSCTP
public:
void Start();
void Stop();
void HandleUsrSctpTimers();

/* Pure virtual methods inherited from TimerHandle::Listener. */
public:
Expand All @@ -37,6 +38,7 @@ class DepUsrSCTP
static void RegisterSctpAssociation(RTC::SctpAssociation* sctpAssociation);
static void DeregisterSctpAssociation(RTC::SctpAssociation* sctpAssociation);
static RTC::SctpAssociation* RetrieveSctpAssociation(uintptr_t id);
static void HandleUsrSctpTimers();

private:
thread_local static Checker* checker;
Expand Down
39 changes: 35 additions & 4 deletions worker/src/DepUsrSCTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,42 @@

/* Static. */

static constexpr size_t CheckerInterval{ 10u }; // In ms.
static std::mutex GlobalSyncMutex;
static size_t GlobalInstances{ 0u };

/* Static methods for usrsctp global callbacks. */

inline static int onSendSctpData(void* addr, void* data, size_t len, uint8_t /*tos*/, uint8_t /*setDf*/)
static int onSendSctpData(void* addr, void* data, size_t len, uint8_t /*tos*/, uint8_t /*setDf*/)
{
MS_TRACE();

auto* sctpAssociation = DepUsrSCTP::RetrieveSctpAssociation(reinterpret_cast<uintptr_t>(addr));

if (!sctpAssociation)
{
MS_WARN_TAG(sctp, "no SctpAssociation found");

DepUsrSCTP::HandleUsrSctpTimers();

// TODO: Why are we returning -1 here? What does it tell usrsctp?
// If the SctpAssociation doesn't exist anymore we don't want that usrscp
// insists.
return -1;
}

sctpAssociation->OnUsrSctpSendSctpData(data, len);

// NOTE: Must not free data, usrsctp lib does it.

DepUsrSCTP::HandleUsrSctpTimers();

return 0;
}

// Static method for printing usrsctp debug.
inline static void sctpDebug(const char* format, ...)
static void sctpDebug(const char* format, ...)
{
MS_TRACE();
char buffer[10000];
va_list ap;

Expand Down Expand Up @@ -207,6 +216,13 @@ RTC::SctpAssociation* DepUsrSCTP::RetrieveSctpAssociation(uintptr_t id)
return it->second;
}

void DepUsrSCTP::HandleUsrSctpTimers()
{
MS_TRACE();

DepUsrSCTP::checker->HandleUsrSctpTimers();
}

/* DepUsrSCTP::Checker instance methods. */

DepUsrSCTP::Checker::Checker() : timer(new TimerHandle(this))
Expand All @@ -229,7 +245,7 @@ void DepUsrSCTP::Checker::Start()

this->lastCalledAtMs = 0u;

this->timer->Start(CheckerInterval, CheckerInterval);
HandleUsrSctpTimers();
}

void DepUsrSCTP::Checker::Stop()
Expand All @@ -243,10 +259,23 @@ void DepUsrSCTP::Checker::Stop()
this->timer->Stop();
}

void DepUsrSCTP::Checker::HandleUsrSctpTimers()
{
MS_TRACE();

auto timeout = static_cast<uint64_t>(usrsctp_get_timeout());

MS_DUMP("------ starting timer with usrsctp timeout: %" PRIu64 " ms", timeout);

this->timer->Start(timeout);
}

void DepUsrSCTP::Checker::OnTimer(TimerHandle* /*timer*/)
{
MS_TRACE();

MS_DUMP("------ onTimer!");

auto nowMs = DepLibUV::GetTimeMs();
const int elapsedMs = this->lastCalledAtMs ? static_cast<int>(nowMs - this->lastCalledAtMs) : 0;

Expand All @@ -267,4 +296,6 @@ void DepUsrSCTP::Checker::OnTimer(TimerHandle* /*timer*/)
#endif

this->lastCalledAtMs = nowMs;

HandleUsrSctpTimers();
}
4 changes: 4 additions & 0 deletions worker/src/RTC/SctpAssociation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ namespace RTC
#endif

usrsctp_conninput(reinterpret_cast<void*>(this->id), data, len, 0);

// TODO: IMHO it makes sense that we handle/refresh usrsctp timers when
// SCTP data is received.
DepUsrSCTP::HandleUsrSctpTimers();
}

void SctpAssociation::SendSctpMessage(
Expand Down
8 changes: 4 additions & 4 deletions worker/subprojects/usrsctp.wrap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[wrap-file]
directory = usrsctp-ebb18adac6501bad4501b1f6dccb67a1c85cc299
source_url = https://github.com/sctplab/usrsctp/archive/ebb18adac6501bad4501b1f6dccb67a1c85cc299.zip
source_filename = ebb18adac6501bad4501b1f6dccb67a1c85cc299.zip
source_hash = e77a855ce395b877e9f2aa7ebe070dfaf5cb11cfecdeb424cc876fc0d98e5d11
directory = usrsctp-0.9.6.0
source_url = https://github.com/versatica/usrsctp/archive/refs/tags/0.9.6.0.zip
source_filename = usrsctp-0.9.6.0.zip
source_hash = 13518f1912631c796da167ba3c3e4fd7f80886de4afa75abc6fcf843160b65f8

[provide]
usrsctp = usrsctp_dep

0 comments on commit 58384dc

Please sign in to comment.