bigsnitch/networkthread.cpp

42 lines
1,002 B
C++
Raw Normal View History

2020-08-08 11:44:16 +00:00
#include "networkthread.h"
NetworkThread::NetworkThread(QObject *parent) : QObject(parent)
{
}
void NetworkThread::process() {
2020-08-09 12:07:14 +00:00
zmq::context_t ctx(1);
zmq::socket_t sock(ctx, zmq::socket_type::pair);
2020-08-08 11:44:16 +00:00
2020-08-09 12:07:14 +00:00
try {
sock.bind("tcp://127.0.0.1:12345");
} catch (zmq::error_t err) {
qDebug() << "failed binding socket" << err.what();
emit error(err.what());
return;
};
2020-08-08 11:44:16 +00:00
2020-08-09 12:07:14 +00:00
while(true){
bool connected = false;
2020-08-09 12:38:41 +00:00
while(!connected) {
sock.send(zmq::buffer(INIT_MSG_STR.c_str(), INIT_MSG_STR.length()), zmq::send_flags::dontwait);
2020-08-09 12:07:14 +00:00
zmq::message_t msg;
const auto ret = sock.recv(msg, zmq::recv_flags::dontwait);
if(ret) {
2020-08-09 12:38:41 +00:00
if(msg.to_string() == ACK_MSG_STR) {
2020-08-09 12:07:14 +00:00
connected = true;
}
}
}
while(connected) {
2020-08-08 11:44:16 +00:00
zmq::message_t msg;
const auto ret = sock.recv(msg, zmq::recv_flags::dontwait);
2020-08-09 12:07:14 +00:00
if(ret) {
2020-08-08 11:44:16 +00:00
qDebug() << msg.to_string().c_str();
2020-08-09 12:07:14 +00:00
emit httpMessage(json(msg.to_string()));
}
}
}
2020-08-08 11:44:16 +00:00
}