-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathZmqMessage.h
99 lines (80 loc) · 2.3 KB
/
ZmqMessage.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef ZMQMESSAGE_H
#define ZMQMESSAGE_H
#include <zmq.h>
#ifndef ZMQ_VERSION
#error ZMQ_VERSION not defined - zmq.h is too old - version 2.1.0 is required
#endif
#if ZMQ_VERSION < 20100
#error zmq.h is too old - version 2.1.0 is required
#endif
#include <QString>
#include <QByteArray>
#include <string.h>
#include <assert.h>
#define USE_EXCEPTIONS 1
#ifdef USE_EXCEPTIONS
#include "ZmqException.h"
#define THROW() throw ZmqException()
#else
#define THROW() do { error_ = ZMQERRNO; \
errorString_ = QString::fromAscii(zmq_strerror(error_)); return; }
#endif
class ZmqMessage : private zmq_msg_t
{
friend class ZmqSocket;
public:
ZmqMessage() {
if(0 != zmq_msg_init(this)) THROW();
}
ZmqMessage(size_t size_){
if(0 != zmq_msg_init_size(this, size_)) THROW();
}
ZmqMessage(void *data_, size_t size_, zmq_free_fn *ffn=0, void *hint=0) {
if(0 != zmq_msg_init_data(this, data_, size_, ffn, hint)) THROW();
}
ZmqMessage(const QByteArray& b) {
if(0 != zmq_msg_init_size(this, b.size())) THROW();
memcpy(data(), b.constData(), b.size());
}
~ZmqMessage() {
int rc = zmq_msg_close(this);
assert(rc == 0);
}
void copy(ZmqMessage *msg) {
if(0 != zmq_msg_copy(this, (zmq_msg_t*)msg)) THROW();
}
void move(ZmqMessage *msg) {
if(0 != zmq_msg_move(this, (zmq_msg_t*)msg)) THROW();
}
void clone(ZmqMessage *msg) {
rebuild(msg->size());
memcpy(data(), msg->data(), size());
}
void clear() { rebuild(); }
void rebuild() {
if(0 != zmq_msg_close(this)) THROW();
if(0 != zmq_msg_init(this)) THROW();
}
void rebuild(size_t size_) {
if(0 != zmq_msg_close(this)) THROW();
if(0 != zmq_msg_init_size(this, size_)) THROW();
}
void rebuild(void *data_, size_t size_, zmq_free_fn *ffn=0, void *hint=0) {
if(0 != zmq_msg_close(this)) THROW();
if(0 != zmq_msg_init_data(this, data_, size_, ffn, hint)) THROW();
}
void rebuild(const QByteArray& b) {
if(0 != zmq_msg_close(this)) THROW();
if(0 != zmq_msg_init_size(this, b.size())) THROW();
memcpy(data(), b.constData(), b.size());
}
size_t size() { return zmq_msg_size(this); }
void *data() { return zmq_msg_data(this); }
QByteArray toByteArray() {
return QByteArray((const char *)data(), size());
}
private:
int error_;
QString errorString_;
};
#endif // ZMQMESSAGE_H