mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
webrtc: Support signaling messages spread over multiple WebSocket fragments
Signed-off-by: Steffen Vogel <steffen.vogel@opal-rt.com>
This commit is contained in:
parent
14e819568a
commit
43bab87be6
2 changed files with 19 additions and 22 deletions
|
@ -16,6 +16,7 @@
|
|||
#include <libwebsockets.h>
|
||||
|
||||
#include <villas/queue.hpp>
|
||||
#include <villas/buffer.hpp>
|
||||
#include <villas/web.hpp>
|
||||
#include <villas/log.hpp>
|
||||
#include <villas/nodes/webrtc/signaling_message.hpp>
|
||||
|
@ -68,6 +69,8 @@ protected:
|
|||
|
||||
std::atomic<bool> running;
|
||||
|
||||
Buffer buffer; // A buffer for received fragments before JSON decoding.
|
||||
|
||||
Logger logger;
|
||||
|
||||
int protocolCallback(struct lws *wsi, enum lws_callback_reasons reason, void *in, size_t len);
|
||||
|
@ -75,7 +78,6 @@ protected:
|
|||
static
|
||||
void connectStatic(struct lws_sorted_usec_list *sul);
|
||||
|
||||
int receive(void *in, size_t len);
|
||||
int writable();
|
||||
|
||||
public:
|
||||
|
|
|
@ -114,9 +114,22 @@ int SignalingClient::protocolCallback(struct lws *wsi, enum lws_callback_reasons
|
|||
goto do_retry;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
ret = receive(in, len);
|
||||
if (ret)
|
||||
goto do_retry;
|
||||
if (lws_is_first_fragment(wsi))
|
||||
buffer.clear();
|
||||
|
||||
buffer.append((char *) in, len);
|
||||
|
||||
if (lws_is_final_fragment(wsi)) {
|
||||
auto *json = buffer.decode();
|
||||
if (json == nullptr) {
|
||||
logger->error("Failed to decode JSON");
|
||||
goto do_retry;
|
||||
}
|
||||
|
||||
cbMessage(SignalingMessage::fromJSON(json));
|
||||
|
||||
json_decref(json);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
|
@ -197,24 +210,6 @@ int SignalingClient::writable()
|
|||
return 0;
|
||||
}
|
||||
|
||||
int SignalingClient::receive(void *in, size_t len)
|
||||
{
|
||||
json_error_t err;
|
||||
json_t *json = json_loadb((char *) in, len, 0, &err);
|
||||
if (!json) {
|
||||
logger->error("Failed to decode json: {} at ({}:{})", err.text, err.line, err.column);
|
||||
return -1;
|
||||
}
|
||||
|
||||
logger->debug("Signaling message received: {:.{}}", (char *)in, len);
|
||||
|
||||
cbMessage(SignalingMessage::fromJSON(json));
|
||||
|
||||
json_decref(json);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void SignalingClient::sendMessage(SignalingMessage msg)
|
||||
{
|
||||
outgoingMessages.push(msg);
|
||||
|
|
Loading…
Add table
Reference in a new issue