@@ -93,50 +93,57 @@ std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
93
93
94
94
void SetSctpSettings (SctpSettings s) { impl::Init::Instance ().setSctpSettings (std::move (s)); }
95
95
96
- # if !USE_NICE
96
+ std::map< int , UnhandledStunRequestHandler *> unboundStunCallbacks;
97
97
98
- UnhandledStunRequestCallback unboundStunCallback;
98
+ # if !USE_NICE
99
99
100
100
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
+ }
110
115
}
111
116
112
117
#endif
113
118
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 ) {
115
120
#if USE_NICE
116
121
PLOG_WARNING << " BindStunListener is not supported with libnice, please use libjuice" ;
117
122
#else
118
123
if (callback == NULL ) {
119
124
PLOG_DEBUG << " Removing unhandled STUN request listener" ;
120
125
126
+ free (unboundStunCallbacks.at (port));
127
+ unboundStunCallbacks.erase (port);
128
+
121
129
// call with NULL callback to unbind
122
130
if (juice_mux_listen (host.c_str (), port, NULL , NULL ) < 0 ) {
123
131
throw std::runtime_error (" Could not unbind STUN listener" );
124
132
}
125
- unboundStunCallback = NULL ;
126
133
127
134
return ;
128
135
}
129
136
130
137
PLOG_DEBUG << " Adding listener for unhandled STUN requests" ;
131
138
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;
135
142
136
- unboundStunCallback = std::move (callback) ;
143
+ unboundStunCallbacks[port] = handler ;
137
144
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" );
140
147
}
141
148
#endif
142
149
}
0 commit comments