mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-04-02 18:31:50 -04:00
* migrate to cmake * move the 3rd party libs * organize 3rdparty * update the 3rd party version * refine the CMakeLists.txt * update install command * update install command * update install command * update install command * update dependency * update the dependency * update the dependency * remove CPM.cmake * remove QtCreator project file * update cmake file * improve cmake file * add cmakew * use wget * remove echo * update the doc * remove the screenshot * update the doc * update the install steps * check the openconnect version * update the doc * update install scripts * fix install scripts * improve message * improve message * improve install scripts * improve the version check * improve the version check * improve install script * add version * organize includes * add version bump * update CI * update CI * add the release flag * update message
88 lines
2.6 KiB
C++
88 lines
2.6 KiB
C++
#include <QtCore/QVariantMap>
|
|
#include <plog/Log.h>
|
|
|
|
#include "cdpcommandmanager.h"
|
|
|
|
CDPCommandManager::CDPCommandManager(QObject *parent)
|
|
: QObject(parent)
|
|
, networkManager(new QNetworkAccessManager)
|
|
, socket(new QWebSocket)
|
|
{
|
|
// WebSocket setup
|
|
QObject::connect(socket, &QWebSocket::connected, this, &CDPCommandManager::ready);
|
|
QObject::connect(socket, &QWebSocket::textMessageReceived, this, &CDPCommandManager::onTextMessageReceived);
|
|
QObject::connect(socket, &QWebSocket::disconnected, this, &CDPCommandManager::onSocketDisconnected);
|
|
QObject::connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &CDPCommandManager::onSocketError);
|
|
}
|
|
|
|
CDPCommandManager::~CDPCommandManager()
|
|
{
|
|
delete networkManager;
|
|
delete socket;
|
|
}
|
|
|
|
void CDPCommandManager::initialize(QString endpoint)
|
|
{
|
|
QNetworkReply *reply = networkManager->get(QNetworkRequest(endpoint));
|
|
|
|
QObject::connect(
|
|
reply, &QNetworkReply::finished,
|
|
[reply, this]() {
|
|
if (reply->error()) {
|
|
PLOGE << "CDP request error";
|
|
return;
|
|
}
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
|
QJsonArray pages = doc.array();
|
|
QJsonObject page = pages.first().toObject();
|
|
QString wsUrl = page.value("webSocketDebuggerUrl").toString();
|
|
|
|
socket->open(wsUrl);
|
|
}
|
|
);
|
|
}
|
|
|
|
CDPCommand *CDPCommandManager::sendCommand(QString cmd)
|
|
{
|
|
QVariantMap emptyParams;
|
|
return sendCommend(cmd, emptyParams);
|
|
}
|
|
|
|
CDPCommand *CDPCommandManager::sendCommend(QString cmd, QVariantMap ¶ms)
|
|
{
|
|
int id = ++commandId;
|
|
CDPCommand *command = new CDPCommand(id, cmd, params);
|
|
socket->sendTextMessage(command->toJson());
|
|
commandPool.insert(id, command);
|
|
|
|
return command;
|
|
}
|
|
|
|
void CDPCommandManager::onTextMessageReceived(QString message)
|
|
{
|
|
QJsonDocument responseDoc = QJsonDocument::fromJson(message.toUtf8());
|
|
QJsonObject response = responseDoc.object();
|
|
|
|
// Response for method
|
|
if (response.contains("id")) {
|
|
int id = response.value("id").toInt();
|
|
if (commandPool.contains(id)) {
|
|
CDPCommand *command = commandPool.take(id);
|
|
command->finished();
|
|
}
|
|
} else { // Response for event
|
|
emit eventReceived(response.value("method").toString(), response.value("params").toObject());
|
|
}
|
|
}
|
|
|
|
void CDPCommandManager::onSocketDisconnected()
|
|
{
|
|
PLOGI << "WebSocket disconnected";
|
|
}
|
|
|
|
void CDPCommandManager::onSocketError(QAbstractSocket::SocketError error)
|
|
{
|
|
PLOGE << "WebSocket error" << error;
|
|
}
|