From 670be0598230c39da18aaa5899d83baa056023f8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 16 Jun 2020 20:47:15 +0200 Subject: Split message strings to clients into (multiple) max ~128 byte transfers. --- src/munia_proto.cc | 57 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/munia_proto.cc b/src/munia_proto.cc index 3349087..2230a0a 100644 --- a/src/munia_proto.cc +++ b/src/munia_proto.cc @@ -153,39 +153,48 @@ int callback_lws_node(struct lws *wsi, i++; } - std::string msgstr; - msgstr.append((size_t)LWS_SEND_BUFFER_PRE_PADDING, ' '); + { + std::string msgstr; + msgstr.append((size_t)LWS_SEND_BUFFER_PRE_PADDING, ' '); - if(msgqueue[wsi].size() == 0 && wsi == current_client) msgstr += ";"; + if(msgqueue[wsi].size() == 0 && wsi == current_client) msgstr += ";"; - while(msgqueue[wsi].size() > 0) - { - message_t msg = msgqueue[wsi].front(); - msgqueue[wsi].pop_front(); - if(msgstr.size() > LWS_SEND_BUFFER_PRE_PADDING) + // Split messages as "soon as possible" after they have exceeded 512 bytes. + while(msgqueue[wsi].size() > 0 && msgstr.length() < 512) { - msgstr += " "; + message_t msg = msgqueue[wsi].front(); + msgqueue[wsi].pop_front(); + if(msgstr.size() > LWS_SEND_BUFFER_PRE_PADDING) + { + msgstr += " "; + } + auto msgs = msg_tostring(msg); + for(const auto& msg_string : msgs) + { + msgstr += std::to_string(msg.tid) + " " + msg_string; + } } - auto msgs = msg_tostring(msg); - for(const auto& msg_string : msgs) + + msgstr.append((size_t)LWS_SEND_BUFFER_POST_PADDING, ' '); + + int n = lws_write(wsi, (unsigned char *) + msgstr.data() + + LWS_SEND_BUFFER_PRE_PADDING, + msgstr.length() - + LWS_SEND_BUFFER_POST_PADDING - + LWS_SEND_BUFFER_PRE_PADDING, + LWS_WRITE_TEXT); + if(n < 0) { - msgstr += std::to_string(msg.tid) + " " + msg_string; + DEBUG(proto,"ERROR writing to socket"); + exit(1); } } - msgstr.append((size_t)LWS_SEND_BUFFER_POST_PADDING, ' '); - - int n = lws_write(wsi, (unsigned char *) - msgstr.c_str() + - LWS_SEND_BUFFER_PRE_PADDING, - msgstr.length() - - LWS_SEND_BUFFER_POST_PADDING - - LWS_SEND_BUFFER_PRE_PADDING, - LWS_WRITE_TEXT); - if(n < 0) + if(msgqueue[wsi].size() > 0) { - DEBUG(proto,"ERROR writing to socket"); - exit(1); + lws_callback_on_writable_all_protocol(lws_get_context(wsi), + lws_get_protocol(wsi)); } } -- cgit v1.2.3