Files

128 lines
4.4 KiB
C++
Raw Permalink Normal View History

2016-08-16 16:51:22 +02:00
/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
2022-08-13 23:42:40 +02:00
* Copyright (C) 2016-2022 Tom Zander <tom@flowee.org>
2016-08-16 16:51:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-01-22 19:32:15 +01:00
#ifndef FLOWEE_MESSAGEBUILDER_H
#define FLOWEE_MESSAGEBUILDER_H
2016-08-16 16:51:22 +02:00
#include "ConstBuffer.h"
#include "Message.h"
#include <uint256.h>
2023-12-21 15:13:56 +01:00
#include <memory>
2016-08-16 16:51:22 +02:00
namespace Streaming {
class BufferPool;
enum MessageType {
HeaderOnly,
HeaderAndBody,
NoHeader
};
int serialisedUIntSize(std::uint64_t unsignedInteger);
int serialisedIntSize(std::int32_t signedInteger);
2016-08-16 16:51:22 +02:00
class MessageBuilder
{
public:
MessageBuilder(MessageType type = NoHeader, int size = 20000);
2023-12-21 15:13:56 +01:00
MessageBuilder(const std::shared_ptr<BufferPool> &pool, MessageType type = NoHeader);
2020-09-02 13:52:36 +02:00
MessageBuilder(MessageBuilder &o) = delete;
2016-08-16 16:51:22 +02:00
void add(uint32_t tag, uint64_t value);
/// add an utf8 string
void add(uint32_t tag, const std::string &value);
inline void add(uint32_t tag, const char *utf8String) {
return add(tag, std::string(utf8String));
}
void add(uint32_t tag, const std::vector<char> &data);
2018-02-17 12:28:40 +01:00
void add(uint32_t tag, const ConstBuffer &data);
void addByteArray(uint32_t tag, const void *data, int bytes);
2016-08-16 16:51:22 +02:00
void add(uint32_t tag, bool value);
void add(uint32_t tag, int32_t value);
void add(uint32_t tag, double value);
template<unsigned int BITS>
void add(uint32_t tag, const base_blob<BITS> &value) {
2019-06-10 16:44:17 +02:00
addByteArray(tag, static_cast<const void*>(value.begin()), static_cast<int>(value.size()));
}
2021-03-24 18:52:03 +01:00
void add(uint32_t tag, const std::vector<uint8_t> &data) {
addByteArray(tag, static_cast<const void*>(data.data()), static_cast<int>(data.size()));
}
void add(uint32_t tag, const std::vector<int8_t> &data) {
addByteArray(tag, static_cast<const void*>(data.data()), static_cast<int>(data.size()));
}
2016-08-16 16:51:22 +02:00
inline void add(uint32_t tag, float value) {
add(tag, (double) value);
}
/**
* (complete) messages include a message size as the first 4 bytes of the message.
* So when the MessageBuilder is used to build the header, you need to set the
* size when the complete message is finished building to update the size at the
* start of the message.
* If you use the builder for the type HeaderAndBody, this will happen automatically
* on a call to buffer() or message().
* If you use HeaderOnly, you need to call this method.
* This method will just print a warning if you call it for a message Type of BodyOnly.
*/
void setMessageSize(int size);
2020-05-23 14:03:42 +02:00
/**
* @brief buffer returns a buffer object for the message we've built.
* This is an alternative to the message() call, subsequent calls to either
* buffer() or message() will refer to data added after this call.
*/
2016-08-16 16:51:22 +02:00
ConstBuffer buffer();
2019-06-22 23:24:00 +02:00
/**
* Create a message based on the build data and the argument header-data.
2020-05-23 14:03:42 +02:00
* This is an alternative to the buffer() call, subsequent calls to either
* buffer() or message() will refer to data added after this call.
2019-06-22 23:24:00 +02:00
*/
2022-08-13 23:42:40 +02:00
Message message(int serviceId, int messageId, int requestId = -1);
/// convenience overload
inline Message message() {
return message(-1, -1, -1);
}
2016-08-16 16:51:22 +02:00
2019-06-22 23:24:00 +02:00
/**
* Create a message based on the build data using the
* incoming message as a base, assuming that the resurned one is a reply to.
*
* Please notice that it is only legal to call this method for
* no-header type messages.
*/
Message reply(const Message &incoming, int messageId = -1);
2020-09-02 13:52:36 +02:00
MessageBuilder* operator=(const MessageBuilder &o) = delete;
2016-08-16 16:51:22 +02:00
private:
void add(uint32_t tag, const unsigned char *data, unsigned int length);
2023-12-21 15:13:56 +01:00
std::shared_ptr<BufferPool> m_buffer;
2016-08-16 16:51:22 +02:00
bool m_ownsPool;
bool m_inHeader;
bool m_beforeHeader;
int m_headerSize;
MessageType m_messageType;
};
}
#endif