From 8b7b40d3ab99cf069c570423c64389d71a84fec4 Mon Sep 17 00:00:00 2001 From: Tim Blume Date: Sat, 20 Mar 2021 17:04:13 +0100 Subject: [PATCH] foo --- include/api.h | 8 ++++ include/proxyinterface.h | 15 +++---- networkthread.cpp | 87 ---------------------------------------- networkthread.h | 29 -------------- proxyhandler.cpp | 10 +++-- 5 files changed, 22 insertions(+), 127 deletions(-) delete mode 100644 networkthread.cpp delete mode 100644 networkthread.h diff --git a/include/api.h b/include/api.h index ed7b25c..c7797d3 100644 --- a/include/api.h +++ b/include/api.h @@ -3,6 +3,14 @@ //major minor patch #define LITTLESNITCH_VERSION 100 +#include + +#if defined(IS_PROXY_LIBRARY) +# define MYSHAREDLIB_EXPORT Q_DECL_EXPORT +#else +# define MYSHAREDLIB_EXPORT Q_DECL_IMPORT +#endif + #include #include #include diff --git a/include/proxyinterface.h b/include/proxyinterface.h index 3620a91..3f144c2 100644 --- a/include/proxyinterface.h +++ b/include/proxyinterface.h @@ -11,22 +11,23 @@ namespace http { /*! Interface for implementing proxies - There are no signals, because apparently Qt Plugins do not support interfaces, which inherit from QObject. - (it will fail upon loading the plugin) + You need to implement the following signals: + + void process(std::string path); // called in a separate thread, has to block, will not be called again + void finished(); // thread finished and can be deleted + void error(QString err); // an error occured + void message(http::Flow flow); // a http::Flow was received + */ class ProxyInterface { public: + std::string path; virtual ~ProxyInterface() = default; - //! called in a custom thread, may block. - virtual void process(std::string path) = 0; //! returns, whether the proxy is currently connected virtual bool connected() = 0; //! disconnects the handle virtual void disconnect() = 0; // options/settings - std::function finished; - std::function error; - std::function message; }; } diff --git a/networkthread.cpp b/networkthread.cpp deleted file mode 100644 index 0703f51..0000000 --- a/networkthread.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "networkthread.h" -#include - -NetworkThread::NetworkThread(QObject *parent) : - QObject(parent), - context(1) -{ - -} - -void NetworkThread::connect() { - socket = new zmq::socket_t(context, zmq::socket_type::pair); - int linger_time = 0; - socket->set(zmq::sockopt::linger, linger_time); - 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"; -} - -void NetworkThread::reconnect() { - this->disconnect(); - this->connect(); -} - -void NetworkThread::process() { - connect(); - while(true) { - zmq::message_t response; - const auto ret = socket->recv(response, zmq::recv_flags::dontwait); - if(!ret) { - continue; - } - 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(); - } else if(msg_type == "responseheaders") { - qDebug() << "received " << msg_type.c_str(); - } else if(msg_type == "response") { - qDebug() << "received " << msg_type.c_str(); - emit httpMessage(j); - } else if(msg_type == "requestheaders") { - qDebug() << "received " << msg_type.c_str(); - } else if(msg_type == "request") { - qDebug() << "received " << msg_type.c_str(); - } else if(msg_type == "ping") { - qDebug() << "received " << msg_type.c_str(); - } 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") { - } 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); - } -} diff --git a/networkthread.h b/networkthread.h deleted file mode 100644 index e566155..0000000 --- a/networkthread.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include - -class NetworkThread : public QObject -{ - Q_OBJECT -private: - std::set received; - zmq::context_t context; - zmq::socket_t *socket; - std::set accepted_flows; - void connect(); - void disconnect(); - void reconnect(); -public: - explicit NetworkThread(QObject *parent = nullptr); -public slots: - void process(); -signals: - void finished(); - void error(QString err); - void httpMessage(http::Flow flow); -}; diff --git a/proxyhandler.cpp b/proxyhandler.cpp index ce98db6..1d67792 100644 --- a/proxyhandler.cpp +++ b/proxyhandler.cpp @@ -12,14 +12,16 @@ bool http::ProxyHandler::loadPlugin(QObject *proxy) auto thread = new QThread; proxy->moveToThread(thread); + // TODO: get path from config or something + inst->path = "tcp://127.0.0.1:12345"; QObject::connect(thread, SIGNAL (started()), proxy, SLOT (process())); - //connect(worker, SIGNAL (error(QString)), this, SLOT (errorString(QString))); - QObject::connect(worker, SIGNAL (httpMessage(http::Flow)), current_session, SLOT (saveFlow(http::Flow)), Qt::QueuedConnection); - QObject::connect(worker, SIGNAL (httpMessage(http::Flow)), this, SLOT (updateHistory()), Qt::QueuedConnection); + //QObject::connect(proxy, SIGNAL (error(QString)), this, SLOT (errorString(QString))); + //QObject::connect(proxy, SIGNAL (message(http::Flow)), current_session, SLOT (saveFlow(http::Flow)), Qt::QueuedConnection); + //QObject::connect(proxy, SIGNAL (message(http::Flow)), this, SLOT (updateHistory()), Qt::QueuedConnection); thread->start(); - proxies.insert({"foo", inst, thread}); + //proxies.insert({"foo", inst, thread}); return true; }