2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Envelope.h
|
|
|
|
|
*
|
|
|
|
|
* When you send or receive a message to the rabbitMQ server, it is encapsulated
|
|
|
|
|
* in an envelope that contains additional meta information as well.
|
|
|
|
|
*
|
2023-03-27 00:31:55 +08:00
|
|
|
* @copyright 2014 - 2023 Copernica BV
|
2014-01-04 19:45:04 +08:00
|
|
|
*/
|
|
|
|
|
|
2015-11-01 17:48:13 +08:00
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2016-06-23 20:42:50 +08:00
|
|
|
/**
|
|
|
|
|
* Dependencies
|
|
|
|
|
*/
|
|
|
|
|
#include "metadata.h"
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class definition
|
2020-08-17 21:16:24 +08:00
|
|
|
* The envelope extends from MetaData, although this is conceptually not entirely
|
|
|
|
|
* correct: and envelope _holds_ meta data and a body, so it would have been more
|
|
|
|
|
* correct to make the MetaData instance a member. But by extending we automatically
|
|
|
|
|
* make all meta-data properties accesible.
|
2014-01-04 19:45:04 +08:00
|
|
|
*/
|
2014-01-05 20:08:35 +08:00
|
|
|
class Envelope : public MetaData
|
2014-01-04 19:45:04 +08:00
|
|
|
{
|
2014-01-06 01:50:41 +08:00
|
|
|
protected:
|
2014-01-28 22:06:20 +08:00
|
|
|
/**
|
2017-03-08 20:32:51 +08:00
|
|
|
* Pointer to the body data (the memory is not managed by the AMQP library!)
|
2014-01-04 19:45:04 +08:00
|
|
|
* @var const char *
|
|
|
|
|
*/
|
2018-04-21 07:20:17 +08:00
|
|
|
const char *_body;
|
2015-04-22 20:24:00 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Size of the data
|
2014-01-05 20:08:35 +08:00
|
|
|
* @var uint64_t
|
2014-01-04 19:45:04 +08:00
|
|
|
*/
|
2014-01-06 01:50:41 +08:00
|
|
|
uint64_t _bodySize;
|
2017-03-08 20:32:51 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
* The data buffer that you pass to this constructor must be valid during
|
|
|
|
|
* the lifetime of the Envelope object.
|
2014-01-05 20:08:35 +08:00
|
|
|
* @param body
|
2014-01-04 19:45:04 +08:00
|
|
|
* @param size
|
|
|
|
|
*/
|
2018-04-21 07:20:17 +08:00
|
|
|
Envelope(const char *body, uint64_t size) : MetaData(), _body(body), _bodySize(size) {}
|
2023-03-27 20:12:37 +08:00
|
|
|
Envelope(const std::string_view &body) : Envelope(body.data(), body.size()) {}
|
2023-03-27 00:31:55 +08:00
|
|
|
Envelope(const char *body) : Envelope(body, strlen(body)) {}
|
2023-03-27 20:12:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor that preserves meta-data, but installs different body
|
|
|
|
|
* @param metadata
|
|
|
|
|
* @param body
|
|
|
|
|
* @param size
|
|
|
|
|
*/
|
|
|
|
|
Envelope(const MetaData &metadata, const char *body, uint64_t size) : MetaData(metadata), _body(body), _bodySize(size) {}
|
|
|
|
|
Envelope(const MetaData &metadata, const std::string_view &body) : Envelope(metadata, body.data(), body.size()) {}
|
|
|
|
|
Envelope(const MetaData &metadata, const char *body) : Envelope(metadata, body, strlen(body)) {}
|
2014-01-04 19:45:04 +08:00
|
|
|
|
2020-08-17 21:16:24 +08:00
|
|
|
/**
|
|
|
|
|
* Read envelope frmo an input-buffer
|
|
|
|
|
* This method is the counterpart of the Envelope::fill() method, and is not used
|
|
|
|
|
* by the library itself, but might be useful for applications that want to store
|
|
|
|
|
* envelopes.
|
|
|
|
|
* @param frame
|
|
|
|
|
*/
|
|
|
|
|
Envelope(InBuffer &buffer) : MetaData(buffer)
|
|
|
|
|
{
|
|
|
|
|
// extract the properties
|
|
|
|
|
_bodySize = buffer.nextUint64();
|
|
|
|
|
_body = buffer.nextData(_bodySize);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-22 20:24:00 +08:00
|
|
|
/**
|
2017-03-08 20:32:51 +08:00
|
|
|
* Disabled copy constructor
|
2015-04-28 16:58:49 +08:00
|
|
|
* @param envelope the envelope to copy
|
|
|
|
|
*/
|
2017-03-08 20:32:51 +08:00
|
|
|
Envelope(const Envelope &envelope) = delete;
|
2015-04-28 16:58:49 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
2018-04-21 07:20:17 +08:00
|
|
|
virtual ~Envelope() {}
|
2015-04-28 16:58:49 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Access to the full message data
|
|
|
|
|
* @return buffer
|
|
|
|
|
*/
|
2014-01-05 20:08:35 +08:00
|
|
|
const char *body() const
|
2014-01-04 19:45:04 +08:00
|
|
|
{
|
2014-01-05 20:08:35 +08:00
|
|
|
return _body;
|
2014-01-04 19:45:04 +08:00
|
|
|
}
|
2015-04-22 20:24:00 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Size of the body
|
2014-01-05 20:08:35 +08:00
|
|
|
* @return uint64_t
|
2014-01-04 19:45:04 +08:00
|
|
|
*/
|
2014-01-05 20:08:35 +08:00
|
|
|
uint64_t bodySize() const
|
2014-01-04 19:45:04 +08:00
|
|
|
{
|
2014-01-06 01:50:41 +08:00
|
|
|
return _bodySize;
|
|
|
|
|
}
|
2020-08-17 21:16:24 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Size of the envelope, this is the size of the meta+data plus the number of bytes
|
|
|
|
|
* required to store the size of the body + the actual body. This method is not used
|
|
|
|
|
* by the AMQP-CPP library, but could be useful if you feel the need to store
|
|
|
|
|
* @return size_t
|
|
|
|
|
*/
|
|
|
|
|
size_t size() const
|
|
|
|
|
{
|
|
|
|
|
// this is the size of the meta-data + the size of the body
|
|
|
|
|
return MetaData::size() + _bodySize + sizeof(uint64_t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fill an output buffer
|
|
|
|
|
* This method is not used by this library, but could be useful if you want to store
|
|
|
|
|
* the meta-data + message contents (
|
|
|
|
|
* @param buffer
|
|
|
|
|
*/
|
|
|
|
|
void fill(OutBuffer &buffer) const
|
|
|
|
|
{
|
|
|
|
|
// first we store the meta-data
|
|
|
|
|
MetaData::fill(buffer);
|
|
|
|
|
|
|
|
|
|
// now the size of the message body + the actual body
|
|
|
|
|
buffer.add(_bodySize);
|
|
|
|
|
buffer.add(_body, _bodySize);
|
|
|
|
|
}
|
2014-01-04 19:45:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End of namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|