common/Common.hpp | 10 ++++++++-- common/IoUtil.cpp | 9 ++++----- test/helpers.hpp | 4 ++-- wsd/LOOLWebSocket.hpp | 12 +++--------- 4 files changed, 17 insertions(+), 18 deletions(-)
New commits: commit 6ab32e0aa9e5c6a292d5722ef8c943d5d510af53 Author: Ashod Nakashian <[email protected]> Date: Sat Nov 26 22:28:48 2016 -0500 loolwsd: use larger threshold to large messages Messages larger than a certain size are preambled with a 'nextmessage' message that hold the size of the subsequent message. This is a workaround to a limitation the Poco WebSocket API where if the buffer size is smaller than the received frame the socket ends up in a bad state and must be closed. Unfortunately the new API that avoids this workaround is not yet released by Poco. Here we minimize the need for 'nextmessage' to truely large messages. The limit is now raised from above 1KB to over 63KB. We may raise this limit further, but that will cost each socket that much dedicated buffer size. Change-Id: I01e4c68cdbe67e413c04a9725152224a87ab8267 Reviewed-on: https://gerrit.libreoffice.org/31286 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/common/Common.hpp b/common/Common.hpp index 0223be8..5a002a1 100644 --- a/common/Common.hpp +++ b/common/Common.hpp @@ -28,11 +28,17 @@ constexpr int WS_JITTER_MS = 5; /// Pipe and Socket read buffer size. /// Should be large enough for ethernet packets /// which can be 1500 bytes long. -constexpr int READ_BUFFER_SIZE = 2048; +constexpr int READ_BUFFER_SIZE = 64 * 1024; + +/// Size beyond which messages will be sent preceded with +/// 'nextmessage' frame to let the receiver know in advance +/// the size of the larger coming message. All messages up to, +/// but not including, this size are considered small messages. +constexpr int LARGE_MESSAGE_SIZE = READ_BUFFER_SIZE - 512; /// Message larger than this will be dropped as invalid /// or as intentionally flooding the server. -constexpr int MAX_MESSAGE_SIZE = 100 * 1024 * READ_BUFFER_SIZE; +constexpr int MAX_MESSAGE_SIZE = 2 * 1024 * READ_BUFFER_SIZE; constexpr auto JAILED_DOCUMENT_ROOT = "/user/docs/"; constexpr auto CHILD_URI = "/loolws/child?"; diff --git a/common/IoUtil.cpp b/common/IoUtil.cpp index 6ae507c..768ab95 100644 --- a/common/IoUtil.cpp +++ b/common/IoUtil.cpp @@ -50,11 +50,10 @@ void SocketProcessor(const std::shared_ptr<LOOLWebSocket>& ws, // Timeout given is in microseconds. static const Poco::Timespan waitTime(POLL_TIMEOUT_MS * 1000); - const auto bufferSize = READ_BUFFER_SIZE * 100; int flags = 0; int n = -1; bool stop = false; - std::vector<char> payload(bufferSize); + std::vector<char> payload(READ_BUFFER_SIZE); try { ws->setReceiveTimeout(0); @@ -81,8 +80,8 @@ void SocketProcessor(const std::shared_ptr<LOOLWebSocket>& ws, { payload.resize(payload.capacity()); n = -1; - n = ws->receiveFrame(payload.data(), payload.capacity(), flags); - payload.resize(n > 0 ? n : 0); + n = ws->receiveFrame(payload.data(), payload.size(), flags); + payload.resize(std::max(n, 0)); } catch (const Poco::TimeoutException&) { @@ -107,7 +106,7 @@ void SocketProcessor(const std::shared_ptr<LOOLWebSocket>& ws, LOG_WRN("SocketProcessor [" << name << "]: Receiving multi-parm frame."); while (true) { - char buffer[READ_BUFFER_SIZE * 10]; + char buffer[READ_BUFFER_SIZE]; n = ws->receiveFrame(buffer, sizeof(buffer), flags); if (n <= 0 || (flags & WebSocket::FRAME_OP_BITMASK) == WebSocket::FRAME_OP_CLOSE) { diff --git a/test/helpers.hpp b/test/helpers.hpp index b33dcb4..9ab7855 100644 --- a/test/helpers.hpp +++ b/test/helpers.hpp @@ -212,7 +212,7 @@ std::vector<char> getResponseMessage(LOOLWebSocket& ws, const std::string& prefi response.resize(READ_BUFFER_SIZE); int bytes = ws.receiveFrame(response.data(), response.size(), flags); - response.resize(bytes >= 0 ? bytes : 0); + response.resize(std::max(bytes, 0)); std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(response.data(), bytes, flags) << std::endl; const auto message = LOOLProtocol::getFirstLine(response); if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE) @@ -228,7 +228,7 @@ std::vector<char> getResponseMessage(LOOLWebSocket& ws, const std::string& prefi { response.resize(size); bytes = ws.receiveFrame(response.data(), response.size(), flags); - response.resize(bytes >= 0 ? bytes : 0); + response.resize(std::max(bytes, 0)); std::cerr << name << "Got " << LOOLProtocol::getAbbreviatedFrameDump(response.data(), bytes, flags) << std::endl; if (bytes > 0 && LOOLProtocol::matchPrefix(prefix, LOOLProtocol::getFirstLine(response))) diff --git a/wsd/LOOLWebSocket.hpp b/wsd/LOOLWebSocket.hpp index e744cf7..3bca89d 100644 --- a/wsd/LOOLWebSocket.hpp +++ b/wsd/LOOLWebSocket.hpp @@ -117,17 +117,11 @@ public: #endif std::unique_lock<std::mutex> lock(_mutex); - // Size after which messages will be sent preceded with - // 'nextmessage' frame to let the receiver know in advance - // the size of larger coming message. All messages up to this - // size are considered small messages. - constexpr int SMALL_MESSAGE_SIZE = READ_BUFFER_SIZE / 2; - - if (length > SMALL_MESSAGE_SIZE) + if (length >= LARGE_MESSAGE_SIZE) { const std::string nextmessage = "nextmessage: size=" + std::to_string(length); Poco::Net::WebSocket::sendFrame(nextmessage.data(), nextmessage.size()); - Log::debug("Message is long, sent " + nextmessage); + LOG_TRC("Message is long, sent " + nextmessage); } const int result = Poco::Net::WebSocket::sendFrame(buffer, length, flags); @@ -141,7 +135,7 @@ public: } else { - LOG_DBG("Sent frame: " << LOOLProtocol::getAbbreviatedMessage(buffer, length)); + LOG_TRC("Sent frame: " << LOOLProtocol::getAbbreviatedMessage(buffer, length)); } return result; _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
