[Libreoffice-commits] online.git: loolwsd/ClientSession.cpp loolwsd/LOOLSession.cpp loolwsd/LOOLSession.hpp loolwsd/PrisonerSession.cpp
Ashod Nakashian
ashod.nakashian at collabora.co.uk
Wed Jul 27 16:00:44 UTC 2016
loolwsd/ClientSession.cpp | 18 +++++++++---------
loolwsd/LOOLSession.cpp | 9 ++++-----
loolwsd/LOOLSession.hpp | 11 ++++++++---
loolwsd/PrisonerSession.cpp | 14 ++++++++++----
4 files changed, 31 insertions(+), 21 deletions(-)
New commits:
commit ecd33e1b07793d83396f22fad38ef70cb33e4612
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Wed Jul 27 10:56:28 2016 -0400
loolwsd: send json messages to the UI as text
Change-Id: Ie5712b4ebf4f01a6650f5f7c0df5197b5b9f9208
Reviewed-on: https://gerrit.libreoffice.org/27588
Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
Tested-by: Ashod Nakashian <ashnakash at gmail.com>
diff --git a/loolwsd/ClientSession.cpp b/loolwsd/ClientSession.cpp
index fda1678..f3c2a00 100644
--- a/loolwsd/ClientSession.cpp
+++ b/loolwsd/ClientSession.cpp
@@ -147,7 +147,7 @@ bool ClientSession::_handleInput(const char *buffer, int length)
{
if (!_peer.expired())
{
- return forwardToPeer(_peer, buffer, length);
+ return forwardToPeer(_peer, buffer, length, false);
}
}
else if (tokens[0] == "commandvalues")
@@ -190,11 +190,11 @@ bool ClientSession::_handleInput(const char *buffer, int length)
tokens[0] != "userinactive" && tokens[0] != "useractive")
{
std::string dummyFrame = "dummymsg";
- return forwardToPeer(_peer, dummyFrame.c_str(), dummyFrame.size());
+ return forwardToPeer(_peer, dummyFrame.c_str(), dummyFrame.size(), false);
}
else if (tokens[0] != "requestloksession")
{
- return forwardToPeer(_peer, buffer, length);
+ return forwardToPeer(_peer, buffer, length, false);
}
else
{
@@ -236,7 +236,7 @@ bool ClientSession::loadDocument(const char* /*buffer*/, int /*length*/, StringT
oss << " options=" << _docOptions;
const auto loadRequest = oss.str();
- return forwardToPeer(_peer, loadRequest.c_str(), loadRequest.size());
+ return forwardToPeer(_peer, loadRequest.c_str(), loadRequest.size(), false);
}
catch (const Poco::SyntaxException&)
{
@@ -259,7 +259,7 @@ bool ClientSession::getStatus(const char *buffer, int length)
return sendTextFrame(msg);
}
- return forwardToPeer(_peer, buffer, length);
+ return forwardToPeer(_peer, buffer, length, false);
}
bool ClientSession::setEditLock(const bool value)
@@ -269,7 +269,7 @@ bool ClientSession::setEditLock(const bool value)
const auto msg = "editlock: " + std::to_string(isEditLocked());
const auto mv = std::getenv("LOK_VIEW_CALLBACK") ? "1" : "0";
Log::debug("Forwarding [" + msg + "] to set editlock to " + std::to_string(value) + ". MultiView: " + mv);
- return forwardToPeer(_peer, msg.data(), msg.size());
+ return forwardToPeer(_peer, msg.data(), msg.size(), false);
}
bool ClientSession::getCommandValues(const char *buffer, int length, StringTokenizer& tokens)
@@ -286,7 +286,7 @@ bool ClientSession::getCommandValues(const char *buffer, int length, StringToken
return sendTextFrame(cmdValues);
}
- return forwardToPeer(_peer, buffer, length);
+ return forwardToPeer(_peer, buffer, length, false);
}
bool ClientSession::getPartPageRectangles(const char *buffer, int length)
@@ -297,7 +297,7 @@ bool ClientSession::getPartPageRectangles(const char *buffer, int length)
return sendTextFrame(partPageRectangles);
}
- return forwardToPeer(_peer, buffer, length);
+ return forwardToPeer(_peer, buffer, length, false);
}
bool ClientSession::sendFontRendering(const char *buffer, int length, StringTokenizer& tokens)
@@ -329,7 +329,7 @@ bool ClientSession::sendFontRendering(const char *buffer, int length, StringToke
return sendBinaryFrame(output.data(), output.size());
}
- return forwardToPeer(_peer, buffer, length);
+ return forwardToPeer(_peer, buffer, length, false);
}
bool ClientSession::sendTile(const char * /*buffer*/, int /*length*/, StringTokenizer& tokens)
diff --git a/loolwsd/LOOLSession.cpp b/loolwsd/LOOLSession.cpp
index 22b4df7..d9fcfc1 100644
--- a/loolwsd/LOOLSession.cpp
+++ b/loolwsd/LOOLSession.cpp
@@ -75,27 +75,26 @@ LOOLSession::~LOOLSession()
{
}
-bool LOOLSession::sendTextFrame(const std::string& text)
+bool LOOLSession::sendTextFrame(const char* buffer, const int length)
{
if (!_ws || _ws->poll(Poco::Timespan(0), Socket::SelectMode::SELECT_ERROR))
{
- Log::error(getName() + ": Bad socket while sending [" + getAbbreviatedMessage(text.c_str(), text.size()) + "].");
+ Log::error(getName() + ": Bad socket while sending [" + getAbbreviatedMessage(buffer, length) + "].");
return false;
}
- Log::trace(getName() + " Send: " + getAbbreviatedMessage(text.c_str(), text.size()));
+ Log::trace(getName() + " Send: " + getAbbreviatedMessage(buffer, length));
try
{
std::unique_lock<std::mutex> lock(_mutex);
- const int length = text.size();
if ( length > SMALL_MESSAGE_SIZE )
{
const std::string nextmessage = "nextmessage: size=" + std::to_string(length);
_ws->sendFrame(nextmessage.data(), nextmessage.size());
}
- _ws->sendFrame(text.data(), length);
+ _ws->sendFrame(buffer, length);
return true;
}
catch (const Exception& exc)
diff --git a/loolwsd/LOOLSession.hpp b/loolwsd/LOOLSession.hpp
index 3d6e3f3..976e6dd 100644
--- a/loolwsd/LOOLSession.hpp
+++ b/loolwsd/LOOLSession.hpp
@@ -41,8 +41,12 @@ public:
const std::string& getName() const { return _name; }
bool isDisconnected() const { return _disconnected; }
- bool sendTextFrame(const std::string& text);
bool sendBinaryFrame(const char *buffer, int length);
+ bool sendTextFrame(const char* buffer, const int length);
+ bool sendTextFrame(const std::string& text)
+ {
+ return sendTextFrame(text.data(), text.size());
+ }
bool handleInput(const char *buffer, int length);
@@ -98,7 +102,7 @@ protected:
}
template <typename T>
- bool forwardToPeer(T& p, const char *buffer, int length)
+ bool forwardToPeer(T& p, const char *buffer, int length, const bool binary)
{
const auto message = LOOLProtocol::getAbbreviatedMessage(buffer, length);
@@ -121,7 +125,8 @@ protected:
}
Log::trace(getName() + " -> " + peer->getName() + ": " + message);
- return peer->sendBinaryFrame(buffer, length);
+ return binary ? peer->sendBinaryFrame(buffer, length)
+ : peer->sendTextFrame(buffer, length);
}
private:
diff --git a/loolwsd/PrisonerSession.cpp b/loolwsd/PrisonerSession.cpp
index 0968878..5da9780 100644
--- a/loolwsd/PrisonerSession.cpp
+++ b/loolwsd/PrisonerSession.cpp
@@ -70,6 +70,7 @@ bool PrisonerSession::_handleInput(const char *buffer, int length)
throw Poco::ProtocolException("The session has not been assigned a peer.");
}
+ bool isBinary = true;
if (tokens[0] == "unocommandresult:")
{
const std::string stringMsg(buffer, length);
@@ -112,7 +113,7 @@ bool PrisonerSession::_handleInput(const char *buffer, int length)
errorKind == "passwordrequired:to-modify" ||
errorKind == "wrongpassword")
{
- forwardToPeer(_peer, buffer, length);
+ forwardToPeer(_peer, buffer, length, isBinary);
peer->setLoadFailed(errorKind);
return false;
}
@@ -183,13 +184,13 @@ bool PrisonerSession::_handleInput(const char *buffer, int length)
_docBroker->tileCache().saveTextFile(std::string(buffer, length), "status.txt");
// Forward the status response to the client.
- forwardToPeer(_peer, buffer, length);
+ forwardToPeer(_peer, buffer, length, isBinary);
// And let clients know if they hold the edit lock.
std::string message = "editlock: ";
message += std::to_string(peer->isEditLocked());
Log::debug("Forwarding [" + message + "] in response to status.");
- return forwardToPeer(_peer, message.c_str(), message.size());
+ return forwardToPeer(_peer, message.c_str(), message.size(), isBinary);
}
else if (tokens[0] == "commandvalues:")
{
@@ -254,8 +255,13 @@ bool PrisonerSession::_handleInput(const char *buffer, int length)
}
}
+ // 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.
+ isBinary = buffer[length - 1] != '}' && firstLine.find('{') == std::string::npos;
+
// Forward everything else.
- forwardToPeer(_peer, buffer, length);
+ forwardToPeer(_peer, buffer, length, isBinary);
return true;
}
More information about the Libreoffice-commits
mailing list