libreoffice-online/common/Message.hpp

224 lines
7 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* 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/.
*/
#pragma once
#include <atomic>
#include <cstring>
#include <string>
#include <vector>
#include <functional>
#include "Protocol.hpp"
#include "StringVector.hpp"
#include "Log.hpp"
#include "Util.hpp"
/// The payload type used to send/receive data.
class Message
{
public:
enum class Type { Text, JSON, Binary };
enum class Dir { In, Out };
/// Construct a text message.
/// message must include the full first-line.
Message(const std::string& message,
const enum Dir dir) :
_forwardToken(getForwardToken(message.data(), message.size())),
_data(copyDataAfterOffset(message.data(), message.size(), _forwardToken.size())),
_tokens(StringVector::tokenize(_data.data(), _data.size())),
_id(makeId(dir)),
_type(detectType())
{
LOG_TRC("Message " << abbr());
}
/// Construct a message from a string with type and
/// reserve extra space (total, including message).
/// message must include the full first-line.
Message(const std::string& message,
const enum Dir dir,
const size_t reserve) :
_forwardToken(getForwardToken(message.data(), message.size())),
_data(copyDataAfterOffset(message.data(), message.size(), _forwardToken.size())),
_tokens(StringVector::tokenize(message.data() + _forwardToken.size(), message.size() - _forwardToken.size())),
_id(makeId(dir)),
_type(detectType())
{
_data.reserve(std::max(reserve, message.size()));
LOG_TRC("Message " << abbr());
}
/// Construct a message from a character array with type.
/// Note: p must include the full first-line.
Message(const char* p,
const size_t len,
const enum Dir dir) :
_forwardToken(getForwardToken(p, len)),
_data(copyDataAfterOffset(p, len, _forwardToken.size())),
_tokens(StringVector::tokenize(_data.data(), _data.size())),
_id(makeId(dir)),
_type(detectType())
{
LOG_TRC("Message " << abbr());
}
size_t size() const { return _data.size(); }
const std::vector<char>& data() const { return _data; }
const StringVector& tokens() const { return _tokens; }
const std::string& forwardToken() const { return _forwardToken; }
std::string firstToken() const { return _tokens[0]; }
bool firstTokenMatches(const std::string& target) const { return _tokens[0] == target; }
std::string operator[](size_t index) const { return _tokens[index]; }
/// Find a subarray in the raw message.
int find(const char* sub, const std::size_t subLen) const
{
return Util::findSubArray(&_data[0], _data.size(), sub, subLen);
}
/// Returns true iff the subarray exists in the raw message.
bool contains(const char* p, const std::size_t len) const { return find(p, len) >= 0; }
/// Returns true iff the subarray exists in the raw message.
bool contains(const std::string &msg) const { return contains(msg.c_str(), msg.size()); }
const std::string& firstLine()
{
assignFirstLineIfEmpty();
return _firstLine;
}
bool getTokenInteger(const std::string& name, int& value)
{
return COOLProtocol::getTokenInteger(_tokens, name, value);
}
/// Return the abbreviated message for logging purposes.
std::string abbr() const {
return _id + ' ' + COOLProtocol::getAbbreviatedMessage(_data.data(), _data.size());
}
const std::string& id() const { return _id; }
/// Returns the json part of the message, if any.
std::string jsonString() const
{
if (_tokens.size() > 1 && _tokens[1].size() >= 1 &&
(_tokens[1][0] == '{' || _tokens[1][0] == '['))
{
const size_t firstTokenSize = _tokens[0].size();
return std::string(_data.data() + firstTokenSize, _data.size() - firstTokenSize);
}
return std::string();
}
/// Append more data to the message.
void append(const char* p, const size_t len)
{
const size_t curSize = _data.size();
_data.resize(curSize + len);
std::memcpy(_data.data() + curSize, p, len);
}
/// Returns true if and only if the payload is considered Binary.
bool isBinary() const { return _type == Type::Binary; }
/// Allows some in-line re-writing of the message
void rewriteDataBody(const std::function<bool (std::vector<char> &)>& func)
{
// Make sure _firstLine is assigned before we change _data
assignFirstLineIfEmpty();
if (func(_data))
{
// Check - just the body.
assert(_firstLine == COOLProtocol::getFirstLine(_data.data(), _data.size()));
assert(_type == detectType());
}
}
private:
/// Constructs a unique ID.
static std::string makeId(const enum Dir dir)
{
static std::atomic<unsigned> Counter;
return (dir == Dir::In ? 'i' : 'o') + std::to_string(++Counter);
}
void assignFirstLineIfEmpty()
{
if(_firstLine.empty())
{
_firstLine = COOLProtocol::getFirstLine(_data.data(), _data.size());
}
}
Type detectType() const
{
if (_tokens.equals(0, "tile:") ||
_tokens.equals(0, "tilecombine:") ||
deltas: track, transmit and cache deltas (disabled for now) Squashed from feature/deltas-expanded. TileCache changes: + add montonic sequence (wid) numbers to TileData + account for sizes of TileData with multiple blobs + simplify saving and notifying of tiles Sends updates (via appendChanges) based on the sequence the right mix of keyframes and/or deltas required as a single message, and parse and apply those on the JS side. We continue to use PNG for slide previews and dialogs, but remove PngCache - used by document tiles only. Annotates delta: properly as a binary package for the websocket. Distinguishes between deltas and keyframes we get from the Kit based on an initial un-compressed prefix character which we then discard. kit can be forced to render a keyframe by oldWid=0 Track invalidity on tiles themselves - to keep the keyframe around. We need to be able to track that a tile is invalid, and so subscribe to the updated version as/when it is ready - but we also want to store the keyframe underneath any deltas. force rendering of a keyframe for an empty slot in the TileCache. force tile sequence to be zero for combinedtiles - so the client can always request standalone tiles with explicit combinedtiles, or tile requests. move Blob to Common.hpp use zero size for un-changed tiles. remove obsolete render-id, and color deltas in debug mode. cleanup unit tests for non-png tile results. Change-Id: I987f84ac4e58004758a243c233b19a6f0d60f8c2 Signed-off-by: Michael Meeks <michael.meeks@collabora.com>
2022-03-25 13:32:01 -05:00
_tokens.equals(0, "delta:") ||
_tokens.equals(0, "renderfont:") ||
_tokens.equals(0, "rendersearchresult:") ||
_tokens.equals(0, "windowpaint:") ||
_tokens.equals(0, "urp:") )
{
return Type::Binary;
}
if (_data.size() > 0 && (_data[_data.size() - 1] == '}' || _data[_data.size() - 1] == ']'))
{
return Type::JSON;
}
// All others are plain text.
return Type::Text;
}
std::string getForwardToken(const char* buffer, int length)
{
std::string forward = COOLProtocol::getFirstToken(buffer, length);
return (forward.find('-') != std::string::npos ? forward : std::string());
}
std::vector<char> copyDataAfterOffset(const char *p, size_t len, size_t fromOffset)
{
if (!p || fromOffset >= len)
return std::vector<char>();
size_t i;
for (i = fromOffset; i < len; ++i)
{
if (p[i] != ' ')
break;
}
if (i < len)
return std::vector<char>(p + i, p + len);
else
return std::vector<char>();
}
private:
const std::string _forwardToken;
std::vector<char> _data;
const StringVector _tokens;
const std::string _id;
std::string _firstLine;
const Type _type;
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */