diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2020-06-16 20:47:15 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2020-06-19 21:24:03 +0200 |
commit | 670be0598230c39da18aaa5899d83baa056023f8 (patch) | |
tree | 2f885ff01c0540ff56b7605dc9a320920d7ab817 | |
parent | a8c2cada5206621f2eb2894258851d7d3a411e40 (diff) |
Split message strings to clients into (multiple) max ~128 byte transfers.
-rw-r--r-- | src/munia_proto.cc | 57 |
1 files 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)); } } |