-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzmqwrap_base.hpp
82 lines (65 loc) · 2.57 KB
/
zmqwrap_base.hpp
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
/*
* Copyright 2017 <Admobilize>
* MATRIX Labs [http://creator.matrix.one]
*
* ZmqWrap 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/>.
*/
#ifndef SRC_ZMQ_BASE_H_
#define SRC_ZMQ_BASE_H_
#include <memory>
#include <string>
#include "third_party/zmq.hpp"
namespace matrix_labs {
// Read http://api.zeromq.org/2-1:zmq-socket for different ways of using
// sockets. The tables there are really useful.
class ZmqWrapBase {
public:
ZmqWrapBase() {}
// Initialize the socket and the context.
void Init(int n_threads, int type);
// You need to call this *BEFORE* binding the socket
// Set the amount of messages that can remain in the queue. Actions for
// different kinds of sockets.
// ZMQ_PUB: drop.
// ZMQ_SUB: drop.
// ZMQ_PUSH: block.
// ZMQ_PULL: N/A.
void SetHighWaterMark(int high_water_mark) {
socket_->setsockopt(ZMQ_SNDHWM, (void*)&high_water_mark,
sizeof(high_water_mark));
socket_->setsockopt(ZMQ_RCVHWM, (void*)&high_water_mark,
sizeof(high_water_mark));
}
// You can bind a socket or connect a socket. This is confusing when
// using ZMQ because a PUB or SUB can be used as a client or as a server.
// Bind socket.
void Bind(const std::string& bind_string) { socket_->bind(bind_string); }
// Connect socket.
void Connect(const std::string& conn_string) {
socket_->connect(conn_string);
}
// The following methods only make sense when you're using
// the socket in read mode.
// Poll (for socket types where this makes sense. Use -1 for infinity.
bool Poll(int timeout_ms);
// Read a string from the socket.
std::string Read();
// The following methods only make sense when you're using
// the socket in write mode. Some subclasses might provide
// specialized versions (with topics for pub/sub, for instance).
bool Send(const std::string& message_str);
protected:
std::unique_ptr<zmq::context_t> context_;
std::unique_ptr<zmq::socket_t> socket_;
};
} // namespace matrix_labs
#endif // SRC_ZMQ_BASE_H_