2017-01-21 18:47:49 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2017-01-21 18:47:49 -06:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-05-29 10:26:16 -05:00
|
|
|
#include <functional>
|
2017-01-21 18:47:49 -06:00
|
|
|
|
2017-01-21 18:51:02 -06:00
|
|
|
#include "Protocol.hpp"
|
2017-03-26 22:03:15 -05:00
|
|
|
#include "Log.hpp"
|
2017-01-21 18:51:02 -06:00
|
|
|
|
2017-01-21 18:47:49 -06:00
|
|
|
/// The payload type used to send/receive data.
|
2017-01-21 18:51:02 -06:00
|
|
|
class Message
|
2017-01-21 18:47:49 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum class Type { Text, JSON, Binary };
|
|
|
|
enum class Dir { In, Out };
|
|
|
|
|
|
|
|
/// Construct a text message.
|
|
|
|
/// message must include the full first-line.
|
2017-01-21 18:51:02 -06:00
|
|
|
Message(const std::string& message,
|
2017-01-21 22:50:08 -06:00
|
|
|
const enum Dir dir) :
|
2017-01-22 15:09:08 -06:00
|
|
|
_forwardToken(getForwardToken(message.data(), message.size())),
|
2021-05-04 20:05:51 -05:00
|
|
|
_data(copyDataAfterOffset(message.data(), message.size(), _forwardToken.size())),
|
2020-05-21 09:22:49 -05:00
|
|
|
_tokens(Util::tokenize(_data.data(), _data.size())),
|
2017-01-21 18:47:49 -06:00
|
|
|
_id(makeId(dir)),
|
2017-01-21 22:50:08 -06:00
|
|
|
_type(detectType())
|
2017-01-21 18:47:49 -06:00
|
|
|
{
|
2021-05-04 21:09:26 -05:00
|
|
|
LOG_TRC("Message " << abbr());
|
2017-01-21 18:47:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a message from a string with type and
|
|
|
|
/// reserve extra space (total, including message).
|
|
|
|
/// message must include the full first-line.
|
2017-01-21 18:51:02 -06:00
|
|
|
Message(const std::string& message,
|
|
|
|
const enum Dir dir,
|
|
|
|
const size_t reserve) :
|
2017-01-22 15:09:08 -06:00
|
|
|
_forwardToken(getForwardToken(message.data(), message.size())),
|
2021-05-04 20:05:51 -05:00
|
|
|
_data(copyDataAfterOffset(message.data(), message.size(), _forwardToken.size())),
|
2020-05-21 09:22:49 -05:00
|
|
|
_tokens(Util::tokenize(message.data() + _forwardToken.size(), message.size() - _forwardToken.size())),
|
2017-01-21 18:47:49 -06:00
|
|
|
_id(makeId(dir)),
|
2017-01-21 22:50:08 -06:00
|
|
|
_type(detectType())
|
2017-01-21 18:47:49 -06:00
|
|
|
{
|
2021-05-04 20:05:51 -05:00
|
|
|
_data.reserve(std::max(reserve, message.size()));
|
2021-05-04 21:09:26 -05:00
|
|
|
LOG_TRC("Message " << abbr());
|
2017-01-21 18:47:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a message from a character array with type.
|
2017-01-21 18:51:02 -06:00
|
|
|
/// Note: p must include the full first-line.
|
|
|
|
Message(const char* p,
|
|
|
|
const size_t len,
|
2017-01-21 22:50:08 -06:00
|
|
|
const enum Dir dir) :
|
2017-01-22 15:09:08 -06:00
|
|
|
_forwardToken(getForwardToken(p, len)),
|
2021-05-04 20:05:51 -05:00
|
|
|
_data(copyDataAfterOffset(p, len, _forwardToken.size())),
|
2020-05-21 09:22:49 -05:00
|
|
|
_tokens(Util::tokenize(_data.data(), _data.size())),
|
2017-01-21 18:47:49 -06:00
|
|
|
_id(makeId(dir)),
|
2017-01-21 22:50:08 -06:00
|
|
|
_type(detectType())
|
2017-01-21 18:47:49 -06:00
|
|
|
{
|
2021-05-04 21:09:26 -05:00
|
|
|
LOG_TRC("Message " << abbr());
|
2017-01-21 18:47:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return _data.size(); }
|
|
|
|
const std::vector<char>& data() const { return _data; }
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
const StringVector& tokens() const { return _tokens; }
|
2017-01-22 15:09:08 -06:00
|
|
|
const std::string& forwardToken() const { return _forwardToken; }
|
2020-02-28 03:50:58 -06:00
|
|
|
std::string firstToken() const { return _tokens[0]; }
|
2021-03-07 16:31:01 -06:00
|
|
|
bool firstTokenMatches(const std::string& target) const { return _tokens[0] == target; }
|
2020-02-28 03:50:58 -06:00
|
|
|
std::string operator[](size_t index) const { return _tokens[index]; }
|
2017-01-21 19:12:17 -06:00
|
|
|
|
2021-08-27 10:02:24 -05:00
|
|
|
const std::string& firstLine()
|
2021-08-27 06:56:46 -05:00
|
|
|
{
|
2021-08-27 11:34:31 -05:00
|
|
|
assignFirstLineIfEmpty();
|
2021-08-27 06:56:46 -05:00
|
|
|
return _firstLine;
|
|
|
|
}
|
|
|
|
|
2021-08-27 11:34:31 -05:00
|
|
|
|
2017-01-31 21:46:46 -06:00
|
|
|
bool getTokenInteger(const std::string& name, int& value)
|
|
|
|
{
|
|
|
|
return LOOLProtocol::getTokenInteger(_tokens, name, value);
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:12:17 -06:00
|
|
|
/// Return the abbreviated message for logging purposes.
|
2021-05-04 21:09:26 -05:00
|
|
|
std::string abbr() const {
|
|
|
|
return _id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size());
|
|
|
|
}
|
2017-01-21 18:47:49 -06:00
|
|
|
const std::string& id() const { return _id; }
|
|
|
|
|
|
|
|
/// Returns the json part of the message, if any.
|
|
|
|
std::string jsonString() const
|
|
|
|
{
|
2020-06-15 09:26:59 -05:00
|
|
|
if (_tokens.size() > 1 && _tokens[1].size() && _tokens[1][0] == '{')
|
2017-01-21 18:47:49 -06:00
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
const size_t firstTokenSize = _tokens[0].size();
|
2017-01-21 18:47:49 -06:00
|
|
|
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)
|
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
const size_t curSize = _data.size();
|
2017-01-21 18:47:49 -06:00
|
|
|
_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; }
|
|
|
|
|
2019-05-29 10:26:16 -05:00
|
|
|
/// Allows some in-line re-writing of the message
|
2020-06-01 22:21:22 -05:00
|
|
|
void rewriteDataBody(const std::function<bool (std::vector<char> &)>& func)
|
2019-05-29 10:26:16 -05:00
|
|
|
{
|
2021-08-27 11:34:31 -05:00
|
|
|
// Make sure _firstLine is assigned before we change _data
|
|
|
|
assignFirstLineIfEmpty();
|
2019-05-29 10:26:16 -05:00
|
|
|
if (func(_data))
|
|
|
|
{
|
|
|
|
// Check - just the body.
|
2021-08-27 11:34:31 -05:00
|
|
|
assert(_firstLine == LOOLProtocol::getFirstLine(_data.data(), _data.size()));
|
2019-05-29 10:26:16 -05:00
|
|
|
assert(_type == detectType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:47:49 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-27 11:34:31 -05:00
|
|
|
void assignFirstLineIfEmpty()
|
|
|
|
{
|
|
|
|
if(_firstLine.empty())
|
|
|
|
{
|
|
|
|
_firstLine = LOOLProtocol::getFirstLine(_data.data(), _data.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 22:50:08 -06:00
|
|
|
Type detectType() const
|
|
|
|
{
|
2020-06-03 16:54:57 -05:00
|
|
|
if (_tokens.equals(0, "tile:") ||
|
|
|
|
_tokens.equals(0, "tilecombine:") ||
|
|
|
|
_tokens.equals(0, "renderfont:") ||
|
|
|
|
_tokens.equals(0, "windowpaint:"))
|
2017-01-21 22:50:08 -06:00
|
|
|
{
|
|
|
|
return Type::Binary;
|
|
|
|
}
|
|
|
|
|
2021-05-04 20:05:51 -05:00
|
|
|
if (_data.size() > 0 && _data[_data.size() - 1] == '}')
|
2017-01-21 22:50:08 -06:00
|
|
|
{
|
|
|
|
return Type::JSON;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All others are plain text.
|
|
|
|
return Type::Text;
|
|
|
|
}
|
|
|
|
|
2017-01-22 15:09:08 -06:00
|
|
|
std::string getForwardToken(const char* buffer, int length)
|
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
std::string forward = LOOLProtocol::getFirstToken(buffer, length);
|
2017-01-22 15:09:08 -06:00
|
|
|
return (forward.find('-') != std::string::npos ? forward : std::string());
|
|
|
|
}
|
|
|
|
|
2021-05-04 20:05:51 -05:00
|
|
|
std::vector<char> copyDataAfterOffset(const char *p, size_t len, size_t fromOffset)
|
2017-01-22 15:09:08 -06:00
|
|
|
{
|
2021-05-04 20:05:51 -05:00
|
|
|
if (!p || fromOffset >= len)
|
|
|
|
return std::vector<char>();
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
for (i = fromOffset; i < len; ++i)
|
2017-01-22 15:09:08 -06:00
|
|
|
{
|
2021-05-04 20:05:51 -05:00
|
|
|
if (p[i] != ' ')
|
|
|
|
break;
|
2017-01-22 15:09:08 -06:00
|
|
|
}
|
2021-05-04 20:05:51 -05:00
|
|
|
if (i < len)
|
|
|
|
return std::vector<char>(p + i, p + len);
|
|
|
|
else
|
|
|
|
return std::vector<char>();
|
2017-01-22 15:09:08 -06:00
|
|
|
}
|
|
|
|
|
2017-01-21 18:47:49 -06:00
|
|
|
private:
|
2017-01-22 15:09:08 -06:00
|
|
|
const std::string _forwardToken;
|
2017-01-21 18:47:49 -06:00
|
|
|
std::vector<char> _data;
|
2020-02-28 03:50:58 -06:00
|
|
|
const StringVector _tokens;
|
2017-01-21 18:47:49 -06:00
|
|
|
const std::string _id;
|
2021-08-27 06:56:46 -05:00
|
|
|
std::string _firstLine;
|
2017-01-21 18:47:49 -06:00
|
|
|
const Type _type;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|