mirror of
https://github.com/LibreOffice/online.git
synced 2025-08-10 01:34:37 +00:00
wsd: refactor MessagePayload into own file
Change-Id: Ifc0d2abd2e94d4a1b58915664fb0545dca6e96cc Reviewed-on: https://gerrit.libreoffice.org/33427 Reviewed-by: Ashod Nakashian <ashnakash@gmail.com> Tested-by: Ashod Nakashian <ashnakash@gmail.com>
This commit is contained in:

committed by
Ashod Nakashian

parent
11d7d8c595
commit
6b17f96318
124
common/Message.hpp
Normal file
124
common/Message.hpp
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
||||||
|
/*
|
||||||
|
* 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_MESSAGE_HPP
|
||||||
|
#define INCLUDED_MESSAGE_HPP
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
/// The payload type used to send/receive data.
|
||||||
|
class MessagePayload
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
enum class Type { Text, JSON, Binary };
|
||||||
|
enum class Dir { In, Out };
|
||||||
|
|
||||||
|
/// Construct a text message.
|
||||||
|
/// message must include the full first-line.
|
||||||
|
MessagePayload(const std::string& message,
|
||||||
|
const enum Dir dir,
|
||||||
|
const enum Type type = Type::Text) :
|
||||||
|
_data(message.data(), message.data() + message.size()),
|
||||||
|
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
||||||
|
_id(makeId(dir)),
|
||||||
|
_firstLine(LOOLProtocol::getFirstLine(_data.data(), _data.size())),
|
||||||
|
_abbreviation(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size())),
|
||||||
|
_type(type)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct a message from a string with type and
|
||||||
|
/// reserve extra space (total, including message).
|
||||||
|
/// message must include the full first-line.
|
||||||
|
MessagePayload(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)),
|
||||||
|
_abbreviation(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(message)),
|
||||||
|
_type(type)
|
||||||
|
{
|
||||||
|
_data.resize(message.size());
|
||||||
|
std::memcpy(_data.data(), message.data(), message.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct a message from a character array with type.
|
||||||
|
/// data must be include the full first-line.
|
||||||
|
MessagePayload(const char* p,
|
||||||
|
const size_t len,
|
||||||
|
const enum Dir dir,
|
||||||
|
const enum Type type) :
|
||||||
|
_data(p, p + len),
|
||||||
|
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
||||||
|
_id(makeId(dir)),
|
||||||
|
_firstLine(LOOLProtocol::getFirstLine(_data.data(), _data.size())),
|
||||||
|
_abbreviation(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size())),
|
||||||
|
_type(type)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size() const { return _data.size(); }
|
||||||
|
const std::vector<char>& data() const { return _data; }
|
||||||
|
|
||||||
|
const std::vector<std::string>& tokens() const { return _tokens; }
|
||||||
|
const std::string& firstToken() const { return _tokens[0]; }
|
||||||
|
const std::string& firstLine() const { return _firstLine; }
|
||||||
|
const std::string& abbreviation() const { return _abbreviation; }
|
||||||
|
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] == "{")
|
||||||
|
{
|
||||||
|
const auto 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 auto 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; }
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<char> _data;
|
||||||
|
const std::vector<std::string> _tokens;
|
||||||
|
const std::string _id;
|
||||||
|
const std::string _firstLine;
|
||||||
|
const std::string _abbreviation;
|
||||||
|
const Type _type;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|
@ -26,6 +26,7 @@
|
|||||||
#include <LOOLWebSocket.hpp>
|
#include <LOOLWebSocket.hpp>
|
||||||
#include "Log.hpp"
|
#include "Log.hpp"
|
||||||
#include "MessageQueue.hpp"
|
#include "MessageQueue.hpp"
|
||||||
|
#include "Message.hpp"
|
||||||
#include "TileCache.hpp"
|
#include "TileCache.hpp"
|
||||||
|
|
||||||
/// Base class of a LOOLWebSocket session.
|
/// Base class of a LOOLWebSocket session.
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
#include "Common.hpp"
|
#include "Common.hpp"
|
||||||
#include "Protocol.hpp"
|
#include "Protocol.hpp"
|
||||||
|
#include "Message.hpp"
|
||||||
#include "MessageQueue.hpp"
|
#include "MessageQueue.hpp"
|
||||||
#include "SenderQueue.hpp"
|
#include "SenderQueue.hpp"
|
||||||
#include "Util.hpp"
|
#include "Util.hpp"
|
||||||
|
@ -26,111 +26,6 @@
|
|||||||
#include "Log.hpp"
|
#include "Log.hpp"
|
||||||
#include "TileDesc.hpp"
|
#include "TileDesc.hpp"
|
||||||
|
|
||||||
/// The payload type used to send/receive data.
|
|
||||||
class MessagePayload
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
enum class Type { Text, JSON, Binary };
|
|
||||||
enum class Dir { In, Out };
|
|
||||||
|
|
||||||
/// Construct a text message.
|
|
||||||
/// message must include the full first-line.
|
|
||||||
MessagePayload(const std::string& message,
|
|
||||||
const enum Dir dir,
|
|
||||||
const enum Type type = Type::Text) :
|
|
||||||
_data(message.data(), message.data() + message.size()),
|
|
||||||
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
|
||||||
_id(makeId(dir)),
|
|
||||||
_firstLine(LOOLProtocol::getFirstLine(_data.data(), _data.size())),
|
|
||||||
_abbreviation(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size())),
|
|
||||||
_type(type)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct a message from a string with type and
|
|
||||||
/// reserve extra space (total, including message).
|
|
||||||
/// message must include the full first-line.
|
|
||||||
MessagePayload(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)),
|
|
||||||
_abbreviation(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(message)),
|
|
||||||
_type(type)
|
|
||||||
{
|
|
||||||
_data.resize(message.size());
|
|
||||||
std::memcpy(_data.data(), message.data(), message.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct a message from a character array with type.
|
|
||||||
/// data must be include the full first-line.
|
|
||||||
MessagePayload(const char* p,
|
|
||||||
const size_t len,
|
|
||||||
const enum Dir dir,
|
|
||||||
const enum Type type) :
|
|
||||||
_data(p, p + len),
|
|
||||||
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
|
||||||
_id(makeId(dir)),
|
|
||||||
_firstLine(LOOLProtocol::getFirstLine(_data.data(), _data.size())),
|
|
||||||
_abbreviation(_id + ' ' + LOOLProtocol::getAbbreviatedMessage(_data.data(), _data.size())),
|
|
||||||
_type(type)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const { return _data.size(); }
|
|
||||||
const std::vector<char>& data() const { return _data; }
|
|
||||||
|
|
||||||
const std::vector<std::string>& tokens() const { return _tokens; }
|
|
||||||
const std::string& firstToken() const { return _tokens[0]; }
|
|
||||||
const std::string& firstLine() const { return _firstLine; }
|
|
||||||
const std::string& abbreviation() const { return _abbreviation; }
|
|
||||||
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] == "{")
|
|
||||||
{
|
|
||||||
const auto 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 auto 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; }
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<char> _data;
|
|
||||||
const std::vector<std::string> _tokens;
|
|
||||||
const std::string _id;
|
|
||||||
const std::string _firstLine;
|
|
||||||
const std::string _abbreviation;
|
|
||||||
const Type _type;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct SendItem
|
struct SendItem
|
||||||
{
|
{
|
||||||
std::weak_ptr<LOOLWebSocket> Socket;
|
std::weak_ptr<LOOLWebSocket> Socket;
|
||||||
|
Reference in New Issue
Block a user