diff --git a/flake.nix b/flake.nix index 330032da9..3f9fa72d8 100644 --- a/flake.nix +++ b/flake.nix @@ -133,6 +133,7 @@ boxfort clang-tools criterion + gdb jq libffi libgit2 diff --git a/include/villas/nodes/webrtc/signaling_client.hpp b/include/villas/nodes/webrtc/signaling_client.hpp index edfa8853b..14ce0efaf 100644 --- a/include/villas/nodes/webrtc/signaling_client.hpp +++ b/include/villas/nodes/webrtc/signaling_client.hpp @@ -73,9 +73,6 @@ class SignalingClient { Logger logger; - int protocolCallback(struct lws *wsi, enum lws_callback_reasons reason, - void *in, size_t len); - static void connectStatic(struct lws_sorted_usec_list *sul); int writable(); @@ -85,9 +82,8 @@ class SignalingClient { const std::string &peer, Web *w); ~SignalingClient(); - static int protocolCallbackStatic(struct lws *wsi, - enum lws_callback_reasons reason, - void *user, void *in, size_t len); + static int protocolCallback(struct lws *wsi, enum lws_callback_reasons reason, + void *user, void *in, size_t len); void connect(); void disconnect(); diff --git a/lib/nodes/webrtc/signaling_client.cpp b/lib/nodes/webrtc/signaling_client.cpp index b00b9f5bc..9d1a7b41e 100644 --- a/lib/nodes/webrtc/signaling_client.cpp +++ b/lib/nodes/webrtc/signaling_client.cpp @@ -99,58 +99,63 @@ void SignalingClient::connectStatic(struct lws_sorted_usec_list *sul) { } } -int SignalingClient::protocolCallbackStatic(struct lws *wsi, - enum lws_callback_reasons reason, - void *user, void *in, size_t len) { - auto *c = reinterpret_cast(user); - - return c->protocolCallback(wsi, reason, in, len); -} - int SignalingClient::protocolCallback(struct lws *wsi, enum lws_callback_reasons reason, - void *in, size_t len) { + void *user, void *in, size_t len) { + auto *c = reinterpret_cast(user); + int ret; switch (reason) { - case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: - cbError(in ? (char *)in : "unknown error"); + case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: { + assert(c); + c->cbError(in ? (char *)in : "unknown error"); goto do_retry; + } + + case LWS_CALLBACK_CLIENT_RECEIVE: { + assert(c); + auto &buffer = c->buffer; - case LWS_CALLBACK_CLIENT_RECEIVE: if (lws_is_first_fragment(wsi)) buffer.clear(); buffer.append((char *)in, len); if (lws_is_final_fragment(wsi)) { - logger->trace("Signaling message received: {:.{}}", buffer.data(), - buffer.size()); + c->logger->trace("Signaling message received: {:.{}}", buffer.data(), + buffer.size()); auto *json = buffer.decode(); if (json == nullptr) { - logger->error("Failed to decode JSON"); + c->logger->error("Failed to decode JSON"); goto do_retry; } - cbMessage(SignalingMessage::fromJson(json)); + c->cbMessage(SignalingMessage::fromJson(json)); json_decref(json); } break; + } - case LWS_CALLBACK_CLIENT_ESTABLISHED: - retry_count = 0; - cbConnected(); + case LWS_CALLBACK_CLIENT_ESTABLISHED: { + assert(c); + c->retry_count = 0; + c->cbConnected(); break; + } - case LWS_CALLBACK_CLIENT_CLOSED: - cbDisconnected(); + case LWS_CALLBACK_CLIENT_CLOSED: { + assert(c); + c->cbDisconnected(); goto do_retry; + } case LWS_CALLBACK_CLIENT_WRITEABLE: { - ret = writable(); + assert(c); + ret = c->writable(); if (ret) goto do_retry; @@ -161,10 +166,10 @@ int SignalingClient::protocolCallback(struct lws *wsi, break; } - return lws_callback_http_dummy(wsi, reason, this, in, len); + return lws_callback_http_dummy(wsi, reason, user, in, len); do_retry: - logger->info("Attempting to reconnect..."); + c->logger->info("Attempting to reconnect..."); /* Retry the connection to keep it nailed up * @@ -175,9 +180,9 @@ int SignalingClient::protocolCallback(struct lws *wsi, * elements in the backoff table, it will never give up and keep * retrying at the last backoff delay plus the random jitter amount. */ - if (lws_retry_sul_schedule_retry_wsi(wsi, &sul_helper.sul, connectStatic, - &retry_count)) - logger->error("Signaling connection attempts exhausted"); + if (lws_retry_sul_schedule_retry_wsi(wsi, &c->sul_helper.sul, connectStatic, + &c->retry_count)) + c->logger->error("Signaling connection attempts exhausted"); return -1; } diff --git a/lib/web.cpp b/lib/web.cpp index b3b2be629..e96ae849d 100644 --- a/lib/web.cpp +++ b/lib/web.cpp @@ -46,8 +46,7 @@ lws_protocols protocols[] = { #endif // WITH_NODE_WEBSOCKET #ifdef WITH_NODE_WEBRTC {.name = "webrtc-signaling", - .callback = webrtc::SignalingClient::protocolCallbackStatic, - .per_session_data_size = sizeof(webrtc::SignalingClient), + .callback = webrtc::SignalingClient::protocolCallback, .rx_buffer_size = 0}, #endif {