WhisperCom/include/Whisper/Message.hpp

89 lines
2.3 KiB
C++
Raw Permalink Normal View History

2021-08-21 23:48:06 +02:00
#pragma once
/* 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 https://mozilla.org/MPL/2.0/.
*/
/** @file */
/** @copyright 2021 MPLv2 */
#include <memory>
#include <Whisper/Transmittable.hpp>
/**
* @brief This is the main namespace for all whisper-com related datatypes and classed.
*/
namespace Whisper
{
/**
* @brief All available Message types.
*
* Message Types identify the real type of the payload of the message
*/
enum class MessageType : std::uint8_t
{
/// no message type set or identified
NONE,
/// the message is a Announcement Message Version 1
ANNOUNCEMENT_V1
}; // enum class MessageType
class Message : public Whisper::Transmittable
{
private:
std::uint64_t dstID_;
std::uint64_t srcID_;
std::uint64_t messageID_;
std::uint64_t replyID_;
Whisper::MessageType type_;
std::shared_ptr<Whisper::Transmittable> payload_;
std::shared_ptr<std::vector<std::uint8_t>> rawPayload_;
/**
* @brief Method generates a random uint64 value > 0
*
* Please be aware that this method will never return zero, as
* zero indicates that the id has never been set.
*
* @return std::uint64_t > 0
*/
std::uint64_t randomID_();
void decode(std::shared_ptr<std::vector<std::uint8_t>> data);
public:
Message() :
dstID_(0),
srcID_(0),
messageID_(randomID_()),
replyID_(0),
type_(Whisper::MessageType::NONE),
payload_(nullptr),
rawPayload_(nullptr),
Transmittable() {}
Message(std::shared_ptr<std::vector<std::uint8_t>> data) : Message() {decode(data);}
std::uint64_t size();
Whisper::MessageType type() const { return type_;}
void type(Whisper::MessageType type) {type_=type;}
std::uint64_t id() const {return messageID_;}
std::uint64_t replyId() const { return replyID_;}
std::uint64_t src() const {return srcID_;}
std::uint64_t dst() const {return dstID_;}
std::shared_ptr<std::vector<std::uint8_t>> encode();
std::shared_ptr<Tree::BaseNode> getPayload();
}; // Message
}; // namespace Whisper