VPN connection moved to separate thread

This commit is contained in:
pokamest 2021-10-26 12:59:20 +03:00
parent 1b1a5be607
commit 44f4d083bf
10 changed files with 166 additions and 104 deletions

View file

@ -24,6 +24,9 @@ VpnLogic::VpnLogic(UiLogic *logic, QObject *parent):
connect(uiLogic()->m_vpnConnection, &VpnConnection::connectionStateChanged, this, &VpnLogic::onConnectionStateChanged);
connect(uiLogic()->m_vpnConnection, &VpnConnection::vpnProtocolError, this, &VpnLogic::onVpnProtocolError);
connect(this, &VpnLogic::connectToVpn, uiLogic()->m_vpnConnection, &VpnConnection::connectToVpn, Qt::QueuedConnection);
connect(this, &VpnLogic::disconnectFromVpn, uiLogic()->m_vpnConnection, &VpnConnection::disconnectFromVpn, Qt::QueuedConnection);
if (m_settings.isAutoConnect() && m_settings.defaultServerIndex() >= 0) {
QTimer::singleShot(1000, this, [this](){
set_pushButtonConnectEnabled(false);
@ -166,20 +169,19 @@ void VpnLogic::onConnectWorker(int serverIndex, const ServerCredentials &credent
qApp->processEvents();
ErrorCode errorCode = uiLogic()->m_vpnConnection->connectToVpn(
serverIndex, credentials, container, containerConfig);
emit connectToVpn(serverIndex, credentials, container, containerConfig);
if (errorCode) {
//ui->pushButton_connect->setChecked(false);
uiLogic()->setDialogConnectErrorText(errorString(errorCode));
emit uiLogic()->showConnectErrorDialog();
return;
}
// if (errorCode) {
// //ui->pushButton_connect->setChecked(false);
// uiLogic()->setDialogConnectErrorText(errorString(errorCode));
// emit uiLogic()->showConnectErrorDialog();
// return;
// }
}
void VpnLogic::onDisconnect()
{
set_pushButtonConnectChecked(false);
uiLogic()->m_vpnConnection->disconnectFromVpn();
emit disconnectFromVpn();
}

View file

@ -48,5 +48,10 @@ public slots:
void onConnectionStateChanged(VpnProtocol::ConnectionState state);
void onVpnProtocolError(amnezia::ErrorCode errorCode);
signals:
void connectToVpn(int serverIndex,
const ServerCredentials &credentials, DockerContainer container, const QJsonObject &containerConfig);
void disconnectFromVpn();
};
#endif // VPN_LOGIC_H

View file

@ -77,7 +77,9 @@ UiLogic::UiLogic(QObject *parent) :
{
m_containersModel = new ContainersModel(this);
m_protocolsModel = new ProtocolsModel(this);
m_vpnConnection = new VpnConnection(this);
m_vpnConnection = new VpnConnection();
m_vpnConnection->moveToThread(&m_vpnConnectionThread);
m_vpnConnectionThread.start();
m_appSettingsLogic = new AppSettingsLogic(this);
m_generalSettingsLogic = new GeneralSettingsLogic(this);
@ -264,16 +266,21 @@ void UiLogic::setDialogConnectErrorText(const QString &dialogConnectErrorText)
UiLogic::~UiLogic()
{
hide();
m_vpnConnection->disconnectFromVpn();
for (int i = 0; i < 50; i++) {
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
QThread::msleep(100);
if (m_vpnConnection->isDisconnected()) {
break;
emit hide();
if (m_vpnConnection->connectionState() != VpnProtocol::ConnectionState::Disconnected) {
m_vpnConnection->disconnectFromVpn();
for (int i = 0; i < 50; i++) {
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
QThread::msleep(100);
if (m_vpnConnection->isDisconnected()) {
break;
}
}
}
m_vpnConnectionThread.quit();
m_vpnConnectionThread.wait(3000);
delete m_vpnConnection;
qDebug() << "Application closed";

View file

@ -5,6 +5,7 @@
#include <QQmlEngine>
#include <functional>
#include <QKeyEvent>
#include <QThread>
#include "property_helper.h"
#include "pages.h"
@ -210,6 +211,7 @@ private:
QMap<Protocol, PageProtocolLogicBase *> m_protocolLogicMap;
VpnConnection* m_vpnConnection;
QThread m_vpnConnectionThread;
Settings m_settings;