-
Notifications
You must be signed in to change notification settings - Fork 4
/
ZmqSocket.h
79 lines (62 loc) · 1.72 KB
/
ZmqSocket.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
#ifndef ZMQSOCKET_H
#define ZMQSOCKET_H
#include <QObject>
#include <QByteArray>
#include <QString>
#include <QSocketNotifier>
#include "ZmqMessage.h"
class ZmqSocket : public QObject
{
Q_OBJECT
public:
explicit ZmqSocket(int type, QObject *parent = 0);
~ZmqSocket();
bool getOpt(int opt_name, void *opt_value, size_t *opt_length);
bool setOpt(int opt_name, const void *opt_value, size_t opt_length);
bool setOpt(int opt_name, const QByteArray& b) {
return setOpt(opt_name, b.constData(), b.size());
}
bool setOpt(int opt_name, int value) {
size_t size = sizeof(value);
return setOpt(opt_name, &value, size);
}
void setIdentity(const QByteArray& name);
QByteArray identity();
void setLinger(int msec);
int linger();
void subscribe(const QByteArray& filter);
void unsubscribe(const QByteArray& filter);
void bind(const char *addr_);
void connectTo(const char *addr_);
bool send(ZmqMessage& msg, int flags=ZMQ_NOBLOCK) {
int rc = zmq_send(socket_, &msg, flags);
if(0 == rc) return true;
if(-1 == rc && zmq_errno() == EAGAIN) return false;
THROW();
}
bool send(const QByteArray& b) {
ZmqMessage msg(b);
return send(msg);
}
bool recv(ZmqMessage *msg_, int flags=ZMQ_NOBLOCK) {
int rc = zmq_recv(socket_, msg_, flags);
if(0 == rc) return true;
if(-1 == rc && zmq_errno() == EAGAIN) return false;
THROW();
}
QList<QByteArray> recv();
int error() const { return error_; }
QString errorString() const { return errorString_; }
signals:
void readyRead();
void readyWrite();
public slots:
protected slots:
void activity();
private:
void *socket_;
QSocketNotifier *notifier_;
int error_;
QString errorString_;
};
#endif // ZMQSOCKET_H