Skip to content

Commit 9157c0c

Browse files
committed
fix: support multiple callbacks for different ports
1 parent 311fea0 commit 9157c0c

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

include/rtc/global.hpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ RTC_CPP_EXPORT void Preload();
3535
RTC_CPP_EXPORT std::shared_future<void> Cleanup();
3636

3737
struct UnhandledStunRequest {
38-
optional<std::string> localUfrag;
39-
optional<std::string> remoteUfrag;
40-
std::string address;
41-
uint16_t port;
38+
std::string localUfrag;
39+
std::string remoteUfrag;
40+
std::string remoteHost;
41+
uint16_t remotePort;
4242
};
4343

44-
RTC_CPP_EXPORT typedef std::function<void(UnhandledStunRequest request)> UnhandledStunRequestCallback;
44+
RTC_CPP_EXPORT typedef std::function<void(UnhandledStunRequest request, void* userPtr)> UnhandledStunRequestCallback;
4545

46-
RTC_CPP_EXPORT void OnUnhandledStunRequest(std::string host, int port, UnhandledStunRequestCallback callback = nullptr);
46+
struct UnhandledStunRequestHandler {
47+
UnhandledStunRequestCallback callback;
48+
void *userPtr;
49+
};
50+
51+
RTC_CPP_EXPORT void OnUnhandledStunRequest(std::string host, int port, UnhandledStunRequestCallback callback = nullptr, void *userPtr = nullptr);
4752

4853
struct SctpSettings {
4954
// For the following settings, not set means optimized default

src/global.cpp

+26-19
Original file line numberDiff line numberDiff line change
@@ -93,50 +93,57 @@ std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
9393

9494
void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
9595

96-
#if !USE_NICE
96+
std::map<int, UnhandledStunRequestHandler *> unboundStunCallbacks;
9797

98-
UnhandledStunRequestCallback unboundStunCallback;
98+
#if !USE_NICE
9999

100100
void InvokeUnhandledStunRequestCallback (const juice_mux_binding_request *info, void *user_ptr) {
101-
PLOG_DEBUG << "Invoking Unbind STUN listener";
102-
auto callback = static_cast<UnhandledStunRequestCallback *>(user_ptr);
103-
104-
(*callback)({
105-
std::string(info->local_ufrag),
106-
std::string(info->remote_ufrag),
107-
std::string(info->address),
108-
info->port
109-
});
101+
PLOG_DEBUG << "Invoking unhandled STUN request callback";
102+
103+
UnhandledStunRequestHandler *handler = (struct UnhandledStunRequestHandler*)user_ptr;
104+
105+
if (handler->callback) {
106+
handler->callback({
107+
std::string(info->local_ufrag),
108+
std::string(info->remote_ufrag),
109+
std::string(info->address),
110+
info->port
111+
}, handler->userPtr);
112+
} else {
113+
PLOG_DEBUG << "No unhandled STUN request callback configured for port " << info->port;
114+
}
110115
}
111116

112117
#endif
113118

114-
void OnUnhandledStunRequest ([[maybe_unused]] std::string host, [[maybe_unused]] int port, [[maybe_unused]] UnhandledStunRequestCallback callback) {
119+
void OnUnhandledStunRequest ([[maybe_unused]] std::string host, [[maybe_unused]] int port, [[maybe_unused]] UnhandledStunRequestCallback callback, [[maybe_unused]] void *userPtr) {
115120
#if USE_NICE
116121
PLOG_WARNING << "BindStunListener is not supported with libnice, please use libjuice";
117122
#else
118123
if (callback == NULL) {
119124
PLOG_DEBUG << "Removing unhandled STUN request listener";
120125

126+
free(unboundStunCallbacks.at(port));
127+
unboundStunCallbacks.erase(port);
128+
121129
// call with NULL callback to unbind
122130
if (juice_mux_listen(host.c_str(), port, NULL, NULL) < 0) {
123131
throw std::runtime_error("Could not unbind STUN listener");
124132
}
125-
unboundStunCallback = NULL;
126133

127134
return;
128135
}
129136

130137
PLOG_DEBUG << "Adding listener for unhandled STUN requests";
131138

132-
if (unboundStunCallback != NULL) {
133-
throw std::runtime_error("Unhandled STUN request handler already present");
134-
}
139+
UnhandledStunRequestHandler *handler = (UnhandledStunRequestHandler*)calloc(1, sizeof(UnhandledStunRequestHandler));
140+
handler->callback = std::move(callback);
141+
handler->userPtr = userPtr;
135142

136-
unboundStunCallback = std::move(callback);
143+
unboundStunCallbacks[port] = handler;
137144

138-
if (juice_mux_listen(host.c_str(), port, &InvokeUnhandledStunRequestCallback, &unboundStunCallback) < 0) {
139-
throw std::invalid_argument("Could add listener for unhandled STUN requests");
145+
if (juice_mux_listen(host.c_str(), port, &InvokeUnhandledStunRequestCallback, handler) < 0) {
146+
throw std::invalid_argument("Could not add listener for unhandled STUN requests");
140147
}
141148
#endif
142149
}

0 commit comments

Comments
 (0)