2020-08-08 11:44:16 +00:00
|
|
|
#include "networkthread.h"
|
2020-08-13 03:35:22 +00:00
|
|
|
#include <iostream>
|
2020-08-08 11:44:16 +00:00
|
|
|
|
2020-08-13 03:35:22 +00:00
|
|
|
NetworkThread::NetworkThread(QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
context(1)
|
2020-08-08 11:44:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-13 03:35:22 +00:00
|
|
|
void NetworkThread::connect() {
|
|
|
|
socket = new zmq::socket_t(context, zmq::socket_type::pair);
|
2020-09-03 17:54:16 +00:00
|
|
|
int linger_time = 0;
|
|
|
|
socket->set(zmq::sockopt::linger, linger_time);
|
2020-08-13 03:35:22 +00:00
|
|
|
try {
|
|
|
|
socket->bind("tcp://127.0.0.1:12345");
|
|
|
|
} catch (zmq::error_t err) {
|
|
|
|
qDebug() << "failed binding socket" << err.what();
|
|
|
|
emit error(err.what());
|
|
|
|
throw;
|
|
|
|
};
|
|
|
|
qDebug() << "connected";
|
|
|
|
}
|
|
|
|
|
|
|
|
void NetworkThread::disconnect() {
|
|
|
|
delete socket;
|
|
|
|
qDebug() << "disconnected";
|
|
|
|
}
|
2020-08-08 11:44:16 +00:00
|
|
|
|
2020-08-13 03:35:22 +00:00
|
|
|
void NetworkThread::reconnect() {
|
|
|
|
this->disconnect();
|
|
|
|
this->connect();
|
|
|
|
}
|
2020-08-08 11:44:16 +00:00
|
|
|
|
2020-08-13 03:35:22 +00:00
|
|
|
void NetworkThread::process() {
|
|
|
|
connect();
|
|
|
|
while(true) {
|
|
|
|
zmq::message_t response;
|
|
|
|
const auto ret = socket->recv(response, zmq::recv_flags::dontwait);
|
2020-08-21 18:40:37 +00:00
|
|
|
if(!ret) {
|
|
|
|
continue;
|
2020-08-13 03:35:22 +00:00
|
|
|
}
|
2020-08-21 18:40:37 +00:00
|
|
|
json j;
|
|
|
|
try {
|
|
|
|
j = json::parse(response.to_string());
|
|
|
|
} catch (nlohmann::detail::parse_error& err) {
|
|
|
|
qDebug() << err.what();
|
|
|
|
qDebug() << "malformed json received " << response.to_string().c_str();
|
|
|
|
}
|
|
|
|
//std::cout << std::setw(4) << j << "\n\n";
|
|
|
|
std::string msg_type;
|
|
|
|
if(!json_get(j, msg_type, "msg")) {
|
|
|
|
qDebug() << "broken message received " << response.to_string().c_str();
|
2020-09-02 22:23:37 +00:00
|
|
|
} else if(msg_type == "responseheaders") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
2020-08-21 18:40:37 +00:00
|
|
|
} else if(msg_type == "response") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
emit httpMessage(j);
|
2020-09-02 22:23:37 +00:00
|
|
|
} else if(msg_type == "requestheaders") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
2020-08-21 18:40:37 +00:00
|
|
|
} else if(msg_type == "request") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
} else if(msg_type == "ping") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
2020-09-02 22:23:37 +00:00
|
|
|
} else if(msg_type == "error") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
|
|
|
|
// websocket events
|
|
|
|
} else if(msg_type == "websocket_handshake") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
} else if(msg_type == "websocket_start") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
std::cout << std::setw(4) << j << "\n\n";
|
|
|
|
} else if(msg_type == "websocket_message") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
} else if(msg_type == "websocket_end") {
|
|
|
|
qDebug() << "received " << msg_type.c_str();
|
|
|
|
} else if(msg_type == "websocket_error") {
|
2020-08-21 18:40:37 +00:00
|
|
|
} else {
|
|
|
|
qDebug() << "unknown or broken message type received: " << msg_type.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "sending ack";
|
|
|
|
std::string m = "{\"msg\": \"ack\"}";
|
|
|
|
socket->send(zmq::buffer(m.c_str(), m.length()), zmq::send_flags::dontwait);
|
2020-08-13 03:35:22 +00:00
|
|
|
}
|
2020-08-08 11:44:16 +00:00
|
|
|
}
|