2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-26 09:49:07 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_LOOLPROTOCOL_HPP
|
|
|
|
#define INCLUDED_LOOLPROTOCOL_HPP
|
|
|
|
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <cstring>
|
2015-04-27 13:09:27 -05:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2016-04-20 08:44:12 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
|
|
|
|
|
|
|
|
namespace LOOLProtocol
|
|
|
|
{
|
2016-01-06 11:00:44 -06:00
|
|
|
// Protocol Version Number.
|
|
|
|
// See protocol.txt.
|
|
|
|
constexpr unsigned ProtocolMajorVersionNumber = 0;
|
|
|
|
constexpr unsigned ProtocolMinorVersionNumber = 1;
|
|
|
|
|
|
|
|
inline
|
|
|
|
std::string GetProtocolVersion()
|
|
|
|
{
|
|
|
|
return std::to_string(ProtocolMajorVersionNumber) + '.'
|
|
|
|
+ std::to_string(ProtocolMinorVersionNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a string into a version tuple.
|
|
|
|
// Negative numbers for error.
|
2016-02-23 02:22:18 -06:00
|
|
|
std::tuple<int, int, std::string> ParseVersion(const std::string& version);
|
2016-01-06 11:00:44 -06:00
|
|
|
|
2016-01-12 05:07:13 -06:00
|
|
|
bool stringToInteger(const std::string& input, int& value);
|
2016-10-08 11:40:21 -05:00
|
|
|
inline
|
|
|
|
bool parseNameValuePair(const std::string& token, std::string& name, std::string& value, const char delim = '=')
|
|
|
|
{
|
|
|
|
const auto mid = token.find_first_of(delim);
|
|
|
|
if (mid != std::string::npos)
|
|
|
|
{
|
|
|
|
name = token.substr(0, mid);
|
|
|
|
value = token.substr(mid + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
bool parseNameIntegerPair(const std::string& token, std::string& name, int& value, const char delim = '=')
|
|
|
|
{
|
|
|
|
std::string strValue;
|
|
|
|
return parseNameValuePair(token, name, strValue, delim) && stringToInteger(strValue, value);
|
|
|
|
}
|
2016-03-27 13:30:03 -05:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenInteger(const std::string& token, const std::string& name, int& value);
|
|
|
|
bool getTokenString(const std::string& token, const std::string& name, std::string& value);
|
2015-04-27 13:09:27 -05:00
|
|
|
bool getTokenKeyword(const std::string& token, const std::string& name, const std::map<std::string, int>& map, int& value);
|
2015-03-26 09:49:07 -05:00
|
|
|
|
2016-04-20 08:44:12 -05:00
|
|
|
bool getTokenInteger(const Poco::StringTokenizer& tokens, const std::string& name, int& value);
|
|
|
|
bool getTokenString(const Poco::StringTokenizer& tokens, const std::string& name, std::string& value);
|
|
|
|
bool getTokenKeyword(const Poco::StringTokenizer& tokens, const std::string& name, const std::map<std::string, int>& map, int& value);
|
|
|
|
|
2016-09-26 11:31:19 -05:00
|
|
|
bool getTokenIntegerFromMessage(const std::string& message, const std::string& name, int& value);
|
|
|
|
bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value);
|
|
|
|
bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value);
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
// Functions that parse messages. All return false if parsing fails
|
|
|
|
bool parseStatus(const std::string& message, LibreOfficeKitDocumentType& type, int& nParts, int& currentPart, int& width, int& height);
|
2015-04-14 09:50:38 -05:00
|
|
|
|
2016-03-27 13:30:03 -05:00
|
|
|
inline
|
2016-10-08 12:12:16 -05:00
|
|
|
std::string getDelimitedInitialSubstring(const char *message, const int length, const char delim)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
|
|
|
if (message == nullptr || length <= 0)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-10-08 12:12:16 -05:00
|
|
|
const char *founddelim = static_cast<const char *>(std::memchr(message, delim, length));
|
|
|
|
const auto size = (founddelim == nullptr ? length : founddelim - message);
|
2016-03-27 13:30:03 -05:00
|
|
|
return std::string(message, size);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:11:49 -05:00
|
|
|
/// Returns the first token of a message.
|
|
|
|
inline
|
2016-10-08 12:12:16 -05:00
|
|
|
std::string getFirstToken(const char *message, const int length, const char delim)
|
2016-04-11 02:11:49 -05:00
|
|
|
{
|
2016-10-08 12:12:16 -05:00
|
|
|
return getDelimitedInitialSubstring(message, length, delim);
|
2016-04-11 02:11:49 -05:00
|
|
|
}
|
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
2016-10-08 12:12:16 -05:00
|
|
|
std::string getFirstToken(const T& message, const char delim = ' ')
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
2016-10-08 12:12:16 -05:00
|
|
|
return getFirstToken(message.data(), message.size(), delim);
|
2016-03-27 13:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 22:16:09 -05:00
|
|
|
/// Returns true if the token is a user-interaction token.
|
|
|
|
/// Currently this excludes commands sent automatically.
|
|
|
|
/// Notice that this doesn't guarantee editing activity,
|
|
|
|
/// rather just user interaction with the UI.
|
2016-04-06 03:36:33 -05:00
|
|
|
inline
|
|
|
|
bool tokenIndicatesUserInteraction(const std::string& token)
|
|
|
|
{
|
2016-09-26 02:12:48 -05:00
|
|
|
// Exclude tokens that include these keywords, such as canceltiles statusindicator.
|
|
|
|
|
|
|
|
// FIXME: This is wrong. That the token happens to contain (or not) a certain substring is
|
|
|
|
// no guarantee that it "indicates user interaction". It might be like that at the moment,
|
|
|
|
// but that is coincidental. We should check what the actual whole token is, at least, not
|
|
|
|
// look for a substring.
|
|
|
|
|
2016-04-09 22:16:09 -05:00
|
|
|
return (token.find("tile") == std::string::npos &&
|
|
|
|
token.find("status") == std::string::npos &&
|
|
|
|
token.find("state") == std::string::npos);
|
2016-04-06 03:36:33 -05:00
|
|
|
}
|
|
|
|
|
2016-03-27 13:30:03 -05:00
|
|
|
/// Returns the first line of a message.
|
|
|
|
inline
|
|
|
|
std::string getFirstLine(const char *message, const int length)
|
|
|
|
{
|
2016-04-11 02:11:49 -05:00
|
|
|
return getDelimitedInitialSubstring(message, length, '\n');
|
2016-03-27 13:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-10-08 12:12:16 -05:00
|
|
|
/// Returns the first line of any data which payload char*.
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getFirstLine(const T& message)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
|
|
|
return getFirstLine(message.data(), message.size());
|
|
|
|
}
|
|
|
|
|
2016-10-18 07:51:29 -05:00
|
|
|
/// Returns an abbereviation of the message (the first line, indicating truncation). We assume
|
|
|
|
/// that it adhers to the LOOL protocol, i.e. that there is always a first (or only) line that
|
|
|
|
/// is in printable UTF-8. I.e. no encoding of binary bytes is done. The format of the result is
|
|
|
|
/// not guaranteed to be stable. It is to be used for logging purposes only, not for decoding
|
|
|
|
/// protocol frames.
|
2016-10-08 11:40:21 -05:00
|
|
|
inline
|
|
|
|
std::string getAbbreviatedMessage(const char *message, const int length)
|
|
|
|
{
|
|
|
|
if (message == nullptr || length <= 0)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto firstLine = getFirstLine(message, length);
|
|
|
|
|
|
|
|
// If first line is less than the length (minus newline), add ellipsis.
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
|
|
|
return firstLine + "...";
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstLine;
|
|
|
|
}
|
2016-04-10 12:13:05 -05:00
|
|
|
|
|
|
|
inline
|
|
|
|
std::string getAbbreviatedMessage(const std::string& message)
|
|
|
|
{
|
|
|
|
return getAbbreviatedMessage(message.data(), message.size());
|
|
|
|
}
|
2016-05-01 19:31:40 -05:00
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getAbbreviatedMessage(const T& message)
|
2016-05-01 19:31:40 -05:00
|
|
|
{
|
|
|
|
return getAbbreviatedMessage(message.data(), message.size());
|
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|