63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
#include "Ipc/ZmqMoudle.h"
|
|
|
|
#if 0
|
|
#include <memory>
|
|
#include <assert.h>
|
|
|
|
#include <zmq.hpp>
|
|
|
|
ZmqMoudle::ZmqMoudle(ZmqMoudleCallback callback)
|
|
: callback_(callback){
|
|
|
|
}
|
|
|
|
void ZmqMoudle::Start() {
|
|
if (work_) {
|
|
return;
|
|
}
|
|
|
|
shouldExit_.store(false);
|
|
auto run = [this]() {
|
|
zmq::context_t context(1);
|
|
zmq::socket_t subscriber(context, ZMQ_SUB);
|
|
subscriber.connect("tcp://127.0.0.1:55661");
|
|
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
|
|
|
|
bool isFirst = true;
|
|
while (!shouldExit_.load()) {
|
|
zmq::message_t message;
|
|
if (subscriber.recv(message, zmq::recv_flags::none)) {
|
|
if (isFirst) {
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
// 打印自 Epoch 以来的时间(以秒为单位)
|
|
auto duration = std::chrono::duration<double>(start.time_since_epoch());
|
|
int64_t time = duration.count();
|
|
time = 0;
|
|
isFirst = true;
|
|
}
|
|
const char* data = static_cast<const char*>(message.data());
|
|
if (nullptr != callback_) {
|
|
callback_(data, message.size());
|
|
}
|
|
} else {
|
|
::_sleep(1);
|
|
}
|
|
}
|
|
};
|
|
|
|
work_.reset(new std::thread(run));
|
|
}
|
|
|
|
void ZmqMoudle::Stop() {
|
|
if (!work_) {
|
|
return;
|
|
}
|
|
|
|
shouldExit_.store(true);
|
|
if (work_->joinable()) {
|
|
work_->join();
|
|
}
|
|
}
|
|
#endif
|