Skip to content

Commit d156faf

Browse files
authored
Handle fragmented message. (#6)
1 parent 217d598 commit d156faf

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

chime-sdk-signaling-cpp/src/websocket/libwebsockets_websocket.cc

+12-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,15 @@ int LibwebsocketsWebsocket::Callback(struct lws* wsi, enum lws_callback_reasons
8383
case LWS_CALLBACK_CLIENT_RECEIVE: {
8484
lwsl_debug("Data received.\n");
8585
lwsl_hexdump_debug(in, len);
86+
const size_t remaining = lws_remaining_packet_payload(wsi);
8687
auto* uint8_ptr = static_cast<uint8_t*>(in);
87-
std::vector<uint8_t> data;
88-
data.assign(uint8_ptr, uint8_ptr + len);
89-
self->observer_->OnWebsocketBinaryReceived(data);
88+
// Messages can be fragmented if the size exceeds max bytes
89+
// Therefore, it needs to handle fragmented message.
90+
self->received_data_buffer_.insert(self->received_data_buffer_.end(), uint8_ptr, uint8_ptr + len);
91+
if (!remaining && lws_is_final_fragment(wsi)) {
92+
self->observer_->OnWebsocketBinaryReceived(self->received_data_buffer_);
93+
self->received_data_buffer_.clear();
94+
}
9095
break;
9196
}
9297

@@ -136,6 +141,7 @@ int LibwebsocketsWebsocket::Callback(struct lws* wsi, enum lws_callback_reasons
136141
break;
137142
}
138143
case LWS_CALLBACK_CLIENT_WRITEABLE: {
144+
// TODO: Check if this can be also fragmented
139145
if (!self->message_queue_.empty()) {
140146
std::vector<uint8_t> data = self->message_queue_.front();
141147
self->message_queue_.pop();
@@ -238,6 +244,7 @@ void LibwebsocketsWebsocket::Poll() { lws_service(context_, 0); }
238244

239245
void LibwebsocketsWebsocket::Close() {
240246
if (!context_) return;
247+
241248
lws_context_destroy(context_);
242249
lwsl_user("Closed\n");
243250
}
@@ -248,6 +255,7 @@ void LibwebsocketsWebsocket::SendBinary(const std::vector<uint8_t>& data) {
248255
// Triggers LWS_CALLBACK_CLIENT_WRITEABLE event in Callback when socket is ready to accept data.
249256
lws_callback_on_writable(wsi_);
250257
}
258+
251259
int LibwebsocketsWebsocket::ConvertLogLevel(LogLevel level) {
252260
int lws_level = 0;
253261
switch (level) {
@@ -267,7 +275,7 @@ int LibwebsocketsWebsocket::ConvertLogLevel(LogLevel level) {
267275
return lws_level;
268276
}
269277

270-
void LibwebsocketsWebsocket::HandleError(std::string error_description) {
278+
void LibwebsocketsWebsocket::HandleError(const std::string& error_description) {
271279
lwsl_err("%s", error_description.c_str());
272280
WebsocketErrorStatus error_status;
273281
error_status.description = error_description;

chime-sdk-signaling-cpp/src/websocket/libwebsockets_websocket.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct LibwebsocketsWebsocketConfiguration : public WebsocketConfiguration {
2727
class LibwebsocketsWebsocket : public Websocket {
2828
public:
2929
LibwebsocketsWebsocket(LibwebsocketsWebsocketConfiguration configuration, WebsocketObserver* observer);
30-
30+
~LibwebsocketsWebsocket() = default;
3131
// chime::signaling::Websocket overrides
3232
void Connect() override;
3333
void Close() override;
@@ -41,6 +41,9 @@ class LibwebsocketsWebsocket : public Websocket {
4141
// Messages are buffered here first until the socket will accept packets without blocking.
4242
std::queue<std::vector<uint8_t>> message_queue_;
4343

44+
// Internal data
45+
std::vector<uint8_t> received_data_buffer_;
46+
4447
// This policy sets both connection attempt retry parameters and ping/pong parameters.
4548
// The retry parameters apply to failures that happen before a websocket connection is established.
4649
lws_retry_bo_t retry_and_idle_policy_;
@@ -83,7 +86,7 @@ class LibwebsocketsWebsocket : public Websocket {
8386
int ConvertLogLevel(LogLevel level);
8487

8588
// Log error and notify observer.
86-
void HandleError(std::string error_description);
89+
void HandleError(const std::string& error_description);
8790
};
8891

8992
} // namespace chime

0 commit comments

Comments
 (0)