-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzmq_subscriber_modfd.cpp
More file actions
52 lines (43 loc) · 1.28 KB
/
Copy pathzmq_subscriber_modfd.cpp
File metadata and controls
52 lines (43 loc) · 1.28 KB
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
#include "zmq_subscriber_modfd.hpp"
#include <iostream>
Subscriber_modfd::Subscriber_modfd() : _context(1), receiver(_context, ZMQ_SUB), _filter("") {}
bool Subscriber_modfd::start(std::string &errStr){
receiver.setsockopt(ZMQ_RCVTIMEO, timRcvMsec);
_stop = false;
try{
if(_in_queue_size) receiver.setsockopt(ZMQ_RCVHWM, _in_queue_size);
receiver.setsockopt(ZMQ_SUBSCRIBE, _filter.c_str(), _filter.length());
for(const auto &it: receiverPoints)
receiver.connect(it);
}
catch(std::exception &e){
errStr = std::string(e.what());
return false;
}
_stopped = false;
return true;
}
bool Subscriber_modfd::stop(){
bool result = true;
_stop = true;
for(const auto &it: receiverPoints){
try{receiver.disconnect(it);}
catch(std::exception &e){result = false;}
}
_stopped = true;
return result;
}
bool Subscriber_modfd::receive(zmq::message_t *message){
if (_block){
if (receiver.recv(message)) return true;
else return false;
}
else {
do{
if (!_stopped && receiver.recv(message, ZMQ_DONTWAIT)) return true;
std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
while(!_stop);
}
return false;
}