common/Message.hpp | 31 +++++++++++++++++++++++-------- wsd/ClientSession.cpp | 22 ++++------------------ wsd/ClientSession.hpp | 8 ++------ wsd/TileCache.cpp | 10 ++++------ 4 files changed, 33 insertions(+), 38 deletions(-)
New commits: commit acc029a4113349ccc88bf77accd264f4584574e0 Author: Ashod Nakashian <ashod.nakash...@collabora.co.uk> Date: Sat Jan 21 23:50:08 2017 -0500 wsd: autodetect message type Change-Id: I0f3ab61867ea067f24050acb15660fa93fc7bbb5 Reviewed-on: https://gerrit.libreoffice.org/33437 Reviewed-by: Ashod Nakashian <ashnak...@gmail.com> Tested-by: Ashod Nakashian <ashnak...@gmail.com> diff --git a/common/Message.hpp b/common/Message.hpp index cc7063e..5be6186 100644 --- a/common/Message.hpp +++ b/common/Message.hpp @@ -27,14 +27,13 @@ public: /// Construct a text message. /// message must include the full first-line. Message(const std::string& message, - const enum Dir dir, - const enum Type type = Type::Text) : + const enum Dir dir) : _data(message.data(), message.data() + message.size()), _tokens(LOOLProtocol::tokenize(_data.data(), _data.size())), _id(makeId(dir)), _firstLine(LOOLProtocol::getFirstLine(_data.data(), _data.size())), _abbr(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size())), - _type(type) + _type(detectType()) { } @@ -43,14 +42,13 @@ public: /// message must include the full first-line. Message(const std::string& message, const enum Dir dir, - const enum Type type, const size_t reserve) : _data(std::max(reserve, message.size())), _tokens(LOOLProtocol::tokenize(message)), _id(makeId(dir)), _firstLine(LOOLProtocol::getFirstLine(message)), _abbr(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(message)), - _type(type) + _type(detectType()) { _data.resize(message.size()); std::memcpy(_data.data(), message.data(), message.size()); @@ -60,14 +58,13 @@ public: /// Note: p must include the full first-line. Message(const char* p, const size_t len, - const enum Dir dir, - const enum Type type) : + const enum Dir dir) : _data(p, p + len), _tokens(LOOLProtocol::tokenize(_data.data(), _data.size())), _id(makeId(dir)), _firstLine(LOOLProtocol::getFirstLine(_data.data(), _data.size())), _abbr(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size())), - _type(type) + _type(detectType()) { } @@ -114,6 +111,24 @@ private: return (dir == Dir::In ? 'i' : 'o') + std::to_string(++Counter); } + Type detectType() const + { + if (_tokens[0] == "tile:" || + _tokens[0] == "tilecombine:" || + _tokens[0] == "renderfont:") + { + return Type::Binary; + } + + if (_data[_data.size() - 1] == '}') + { + return Type::JSON; + } + + // All others are plain text. + return Type::Text; + } + private: std::vector<char> _data; const std::vector<std::string> _tokens; diff --git a/wsd/ClientSession.cpp b/wsd/ClientSession.cpp index 12b8fb8..6437d00 100644 --- a/wsd/ClientSession.cpp +++ b/wsd/ClientSession.cpp @@ -536,9 +536,7 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt errorKind == "passwordrequired:to-modify" || errorKind == "wrongpassword") { - const auto payload = std::make_shared<Message>(buffer, length, - Message::Dir::Out, - Message::Type::Text); + const auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out); forwardToClient(payload); LOG_WRN("Document load failed: " << errorKind); return false; @@ -606,9 +604,7 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt docBroker->setLoaded(); // Forward the status response to the client. - const auto payload = std::make_shared<Message>(buffer, length, - Message::Dir::Out, - Message::Type::Text); + const auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out); return forwardToClient(payload); } else if (tokens[0] == "commandvalues:") @@ -676,9 +672,7 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt getTokenString(tokens[2], "char", text); assert(firstLine.size() < static_cast<std::string::size_type>(length)); docBroker->tileCache().saveRendering(font+text, "font", buffer + firstLine.size() + 1, length - firstLine.size() - 1); - const auto payload = std::make_shared<Message>(buffer, length, - Message::Dir::Out, - Message::Type::Binary); + const auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out); return forwardToClient(payload); } } @@ -687,16 +681,8 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt LOG_INF("Ignoring notification on password protected document: " << firstLine); } - // Detect json messages, since we must send those as text even though they are multiline. - // If not, the UI will read the first line of a binary payload, assuming that's the only - // text part and the rest is binary. - const bool isBinary = buffer[length - 1] != '}' && firstLine.find('{') == std::string::npos; - // Forward everything else. - const auto payload = std::make_shared<Message>(buffer, length, - Message::Dir::Out, - isBinary ? Message::Type::Binary - : Message::Type::Text); + const auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out); return forwardToClient(payload); } diff --git a/wsd/ClientSession.hpp b/wsd/ClientSession.hpp index 706fae1..234b8fd 100644 --- a/wsd/ClientSession.hpp +++ b/wsd/ClientSession.hpp @@ -50,18 +50,14 @@ public: bool sendBinaryFrame(const char* buffer, int length) override { - auto payload = std::make_shared<Message>(buffer, length, - Message::Dir::Out, - Message::Type::Binary); + auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out); enqueueSendMessage(payload); return true; } bool sendTextFrame(const char* buffer, const int length) override { - auto payload = std::make_shared<Message>(buffer, length, - Message::Dir::Out, - Message::Type::Text); + auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out); enqueueSendMessage(payload); return true; } diff --git a/wsd/TileCache.cpp b/wsd/TileCache.cpp index 9c4b6f4..270031c 100644 --- a/wsd/TileCache.cpp +++ b/wsd/TileCache.cpp @@ -169,9 +169,8 @@ void TileCache::saveTileAndNotify(const TileDesc& tile, const char *data, const // Send to first subscriber as-is (without cache marker). auto payload = std::make_shared<Message>(response, - Message::Dir::Out, - Message::Type::Binary, - response.size() + 1 + size); + Message::Dir::Out, + response.size() + 1 + size); payload->append("\n", 1); payload->append(data, size); @@ -190,9 +189,8 @@ void TileCache::saveTileAndNotify(const TileDesc& tile, const char *data, const // Create a new Payload. payload.reset(); payload = std::make_shared<Message>(response, - Message::Dir::Out, - Message::Type::Binary, - response.size() + size); + Message::Dir::Out, + response.size() + size); payload->append(data, size); for (size_t i = 1; i < subscriberCount; ++i) _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits