85 lines
2.0 KiB
C++
85 lines
2.0 KiB
C++
#ifndef MESSAGE_H
|
|
#define MESSAGE_H
|
|
|
|
#include "ErrorCode.h"
|
|
#include <string_view>
|
|
#include <zmq.h>
|
|
|
|
namespace ZeroMQ {
|
|
|
|
class Message {
|
|
public:
|
|
Message() noexcept;
|
|
~Message() noexcept;
|
|
Message(Message &&rhs) noexcept;
|
|
Message &operator=(Message &&rhs) noexcept;
|
|
bool operator==(const Message &other) const noexcept;
|
|
|
|
bool operator!=(const Message &other) const noexcept;
|
|
|
|
Message(size_t size) {
|
|
int rc = zmq_msg_init_size(&m_message, size);
|
|
if (rc != 0) throw makeErrorCode();
|
|
}
|
|
Message(const void *dataSrc, size_t size);
|
|
Message(std::string_view string) : Message(string.data(), string.size()) {
|
|
}
|
|
|
|
void *data() noexcept {
|
|
return zmq_msg_data(&m_message);
|
|
}
|
|
const void *data() const noexcept {
|
|
return zmq_msg_data(const_cast<zmq_msg_t *>(&m_message));
|
|
}
|
|
|
|
template <typename T = void *>
|
|
T const *data() const noexcept {
|
|
return static_cast<T const *>(data());
|
|
}
|
|
|
|
size_t size() const noexcept {
|
|
return zmq_msg_size(const_cast<zmq_msg_t *>(&m_message));
|
|
}
|
|
zmq_msg_t *handle() noexcept {
|
|
return &m_message;
|
|
}
|
|
const zmq_msg_t *handle() const noexcept {
|
|
return &m_message;
|
|
}
|
|
bool more() const noexcept {
|
|
int rc = zmq_msg_more(const_cast<zmq_msg_t *>(&m_message));
|
|
return rc != 0;
|
|
}
|
|
|
|
/**
|
|
* @brief copy from message
|
|
*/
|
|
void copy(Message &message);
|
|
|
|
/**
|
|
* @brief zeromq的字符串不是以\0结束的,所以调用std::string_view::data()可能会导致错误
|
|
*
|
|
* @return std::string_view
|
|
*/
|
|
std::string_view toStringView() const noexcept {
|
|
return std::string_view(static_cast<const char *>(data()), size());
|
|
}
|
|
|
|
std::string dump() const;
|
|
|
|
protected:
|
|
Message(const Message &) = delete;
|
|
void operator=(const Message &) = delete;
|
|
|
|
private:
|
|
// The underlying message
|
|
zmq_msg_t m_message;
|
|
};
|
|
} // namespace ZeroMQ
|
|
|
|
namespace std {
|
|
ostream &operator<<(ostream &stream, const ZeroMQ::Message &message);
|
|
}
|
|
|
|
#endif // MESSAGE_H
|