diff options
author | Kai Koehne <kai.koehne@theqtcompany.com> | 2015-02-10 16:21:26 +0100 |
---|---|---|
committer | Kai Koehne <kai.koehne@theqtcompany.com> | 2015-02-20 10:22:28 +0000 |
commit | d6bca86cad962a0078c2a8111133cff123f1ba0a (patch) | |
tree | 0f8cf9863c04675c24efe2bf6b0aa023a56a0f4c /src/libs | |
parent | 0ff36e52afb503a99c8fded093d25321a46b3d19 (diff) |
Use local sockets for client-server communication
Change-Id: I12bfef671ab31ae9fb8c4bb02776517e7f434d27
Task-number: QTIFW-228
Reviewed-by: Karsten Heimrich <karsten.heimrich@theqtcompany.com>
Diffstat (limited to 'src/libs')
-rw-r--r-- | src/libs/installer/keepaliveobject.cpp | 8 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.cpp | 4 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.h | 2 | ||||
-rw-r--r-- | src/libs/installer/protocol.h | 3 | ||||
-rw-r--r-- | src/libs/installer/remoteclient.cpp | 16 | ||||
-rw-r--r-- | src/libs/installer/remoteclient.h | 7 | ||||
-rw-r--r-- | src/libs/installer/remoteclient_p.h | 14 | ||||
-rw-r--r-- | src/libs/installer/remotefileengine.cpp | 2 | ||||
-rw-r--r-- | src/libs/installer/remoteobject.cpp | 14 | ||||
-rw-r--r-- | src/libs/installer/remoteobject.h | 4 | ||||
-rw-r--r-- | src/libs/installer/remoteserver.cpp | 24 | ||||
-rw-r--r-- | src/libs/installer/remoteserver.h | 4 | ||||
-rw-r--r-- | src/libs/installer/remoteserver_p.h | 24 | ||||
-rw-r--r-- | src/libs/installer/remoteserverconnection.cpp | 6 |
14 files changed, 60 insertions, 72 deletions
diff --git a/src/libs/installer/keepaliveobject.cpp b/src/libs/installer/keepaliveobject.cpp index e731a3f48..514ca5a7a 100644 --- a/src/libs/installer/keepaliveobject.cpp +++ b/src/libs/installer/keepaliveobject.cpp @@ -38,7 +38,7 @@ #include <QCoreApplication> #include <QElapsedTimer> #include <QHostAddress> -#include <QTcpSocket> +#include <QLocalSocket> #include <QTimer> namespace QInstaller { @@ -67,12 +67,12 @@ void KeepAliveObject::onTimeout() { // Try to connect to the privileged running server. If we succeed the server side // watchdog gets restarted and the server keeps running for another 30 seconds. - QTcpSocket socket; - socket.connectToHost(RemoteClient::instance().address(), RemoteClient::instance().port()); + QLocalSocket socket; + socket.connectToServer(RemoteClient::instance().socketName()); QElapsedTimer stopWatch; stopWatch.start(); - while ((socket.state() == QAbstractSocket::ConnectingState) + while ((socket.state() == QLocalSocket::ConnectingState) && (stopWatch.elapsed() < 10000) && (!m_quit)) { if ((stopWatch.elapsed() % 2500) == 0) QCoreApplication::processEvents(); diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index 354fba7b5..4af1fa658 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -720,7 +720,7 @@ PackageManagerCore::PackageManagerCore() } PackageManagerCore::PackageManagerCore(qint64 magicmaker, const QList<OperationBlob> &operations, - quint16 port, const QString &key, Protocol::Mode mode) + const QString &socketName, const QString &key, Protocol::Mode mode) : d(new PackageManagerCorePrivate(this, magicmaker, operations)) { Repository::registerMetaType(); // register, cause we stream the type as QVariant @@ -729,7 +729,7 @@ PackageManagerCore::PackageManagerCore(qint64 magicmaker, const QList<OperationB // Creates and initializes a remote client, makes us get admin rights for QFile, QSettings // and QProcess operations. Init needs to called to set the server side authorization key. - RemoteClient::instance().init(port, key, mode, Protocol::StartAs::SuperUser); + RemoteClient::instance().init(socketName, key, mode, Protocol::StartAs::SuperUser); d->initialize(QHash<QString, QString>()); diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index 908278db4..5b2cbdfa7 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -66,7 +66,7 @@ class INSTALLER_EXPORT PackageManagerCore : public QObject public: PackageManagerCore(); PackageManagerCore(qint64 magicmaker, const QList<OperationBlob> &ops, - quint16 port = Protocol::DefaultPort, + const QString &socketName = QString(), const QString &key = QLatin1String(Protocol::DefaultAuthorizationKey), Protocol::Mode mode = Protocol::Mode::Production); ~PackageManagerCore(); diff --git a/src/libs/installer/protocol.h b/src/libs/installer/protocol.h index f737a5fd2..376f0c68d 100644 --- a/src/libs/installer/protocol.h +++ b/src/libs/installer/protocol.h @@ -50,8 +50,7 @@ enum struct StartAs { SuperUser }; -const unsigned short DefaultPort = 39999; -const char DefaultHostAddress[] = "127.0.0.1"; +const char DefaultSocket[] = "ifw_srv"; const char DefaultAuthorizationKey[] = "DefaultAuthorizationKey"; const char Create[] = "Create"; diff --git a/src/libs/installer/remoteclient.cpp b/src/libs/installer/remoteclient.cpp index 22a6946df..12cec0efa 100644 --- a/src/libs/installer/remoteclient.cpp +++ b/src/libs/installer/remoteclient.cpp @@ -52,16 +52,10 @@ RemoteClient &RemoteClient::instance() return instance; } -quint16 RemoteClient::port() const +QString RemoteClient::socketName() const { Q_D(const RemoteClient); - return d->m_port; -} - -QString RemoteClient::address() const -{ - Q_D(const RemoteClient); - return d->m_address; + return d->m_socketName; } QString RemoteClient::authorizationKey() const @@ -71,14 +65,14 @@ QString RemoteClient::authorizationKey() const } /*! - Initializes the client with \a port, the port to write to, with \a key, the key the client + Initializes the client with \a socketName, with the \a key the client sends to authenticate with the server, \a mode and \a startAs. */ -void RemoteClient::init(quint16 port, const QString &key, Protocol::Mode mode, +void RemoteClient::init(const QString &socketName, const QString &key, Protocol::Mode mode, Protocol::StartAs startAs) { Q_D(RemoteClient); - d->init(port, key, mode, startAs); + d->init(socketName, key, mode, startAs); } void RemoteClient::shutdown() diff --git a/src/libs/installer/remoteclient.h b/src/libs/installer/remoteclient.h index 41576877f..c517f3107 100644 --- a/src/libs/installer/remoteclient.h +++ b/src/libs/installer/remoteclient.h @@ -51,13 +51,12 @@ class INSTALLER_EXPORT RemoteClient public: static RemoteClient &instance(); - void init(quint16 port, const QString &key, Protocol::Mode mode, Protocol::StartAs startAs); + void init(const QString &socketName, const QString &key, Protocol::Mode mode, + Protocol::StartAs startAs); void shutdown(); - void init(quint16 port, const QString &address, Protocol::Mode mode); - quint16 port() const; - QString address() const; + QString socketName() const; QString authorizationKey() const; bool isActive() const; diff --git a/src/libs/installer/remoteclient_p.h b/src/libs/installer/remoteclient_p.h index ab016e242..aed18d56e 100644 --- a/src/libs/installer/remoteclient_p.h +++ b/src/libs/installer/remoteclient_p.h @@ -60,8 +60,6 @@ public: : RemoteObject(QLatin1String("RemoteClientPrivate")) , q_ptr(parent) , m_mutex(QMutex::Recursive) - , m_address(QLatin1String(Protocol::DefaultHostAddress)) - , m_port(Protocol::DefaultPort) , m_startServerAs(Protocol::StartAs::User) , m_serverStarted(false) , m_active(false) @@ -88,10 +86,11 @@ public: maybeStopServer(); } - void init(quint16 port, const QString &key, Protocol::Mode mode, Protocol::StartAs startAs) + void init(const QString &socketName, const QString &key, Protocol::Mode mode, + Protocol::StartAs startAs) { + m_socketName = socketName; m_key = key; - m_port = port; m_mode = mode; if (mode == Protocol::Mode::Production) { m_startServerAs = startAs; @@ -99,7 +98,7 @@ public: m_serverArguments = QStringList() << QLatin1String("--startserver") << QString::fromLatin1("%1,%2,%3") .arg(QLatin1String(Protocol::ModeProduction)) - .arg(port) + .arg(socketName) .arg(key); if (!m_object) { @@ -113,7 +112,7 @@ public: } } else if (mode == Protocol::Mode::Debug) { // To be able to debug the client-server connection start and stop the server manually, - // e.g. installer --startserver debug. The server is listening on localhost:39999 then. + // e.g. installer --startserver DEBUG. } } @@ -186,8 +185,7 @@ public: private: RemoteClient *q_ptr; QMutex m_mutex; - QString m_address; - quint16 m_port; + QString m_socketName; Protocol::StartAs m_startServerAs; bool m_serverStarted; bool m_active; diff --git a/src/libs/installer/remotefileengine.cpp b/src/libs/installer/remotefileengine.cpp index 8c82fb4aa..526b5a413 100644 --- a/src/libs/installer/remotefileengine.cpp +++ b/src/libs/installer/remotefileengine.cpp @@ -37,8 +37,6 @@ #include "protocol.h" #include "remoteclient.h" -#include <QTcpSocket> - namespace QInstaller { diff --git a/src/libs/installer/remoteobject.cpp b/src/libs/installer/remoteobject.cpp index 9de13dab9..1c3c31a04 100644 --- a/src/libs/installer/remoteobject.cpp +++ b/src/libs/installer/remoteobject.cpp @@ -68,24 +68,24 @@ RemoteObject::~RemoteObject() bool RemoteObject::authorize() { - if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState)) + if (m_socket && (m_socket->state() == QLocalSocket::ConnectedState)) return true; if (m_socket) delete m_socket; - QScopedPointer<QTcpSocket> socket(new QTcpSocket); - socket->connectToHost(RemoteClient::instance().address(), RemoteClient::instance().port()); + QScopedPointer<QLocalSocket> socket(new QLocalSocket); + socket->connectToServer(RemoteClient::instance().socketName()); QElapsedTimer stopWatch; stopWatch.start(); - while ((socket->state() == QAbstractSocket::ConnectingState) + while ((socket->state() == QLocalSocket::ConnectingState) && (stopWatch.elapsed() < 30000)) { if ((stopWatch.elapsed() % 2500) == 0) QCoreApplication::processEvents(); } - if (socket->state() == QAbstractSocket::ConnectedState) { + if (socket->state() == QLocalSocket::ConnectedState) { QDataStream stream; stream.setDevice(socket.data()); stream << QString::fromLatin1(Protocol::Authorize) << RemoteClient::instance() @@ -112,7 +112,7 @@ bool RemoteObject::connectToServer(const QVariantList &arguments) if (!RemoteClient::instance().isActive()) return false; - if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState)) + if (m_socket && (m_socket->state() == QLocalSocket::ConnectedState)) return true; if (!authorize()) @@ -130,7 +130,7 @@ bool RemoteObject::isConnectedToServer() const { if ((!m_socket) || (!RemoteClient::instance().isActive())) return false; - if (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState)) + if (m_socket && (m_socket->state() == QLocalSocket::ConnectedState)) return true; return false; } diff --git a/src/libs/installer/remoteobject.h b/src/libs/installer/remoteobject.h index 97b7634e7..d756b1f3d 100644 --- a/src/libs/installer/remoteobject.h +++ b/src/libs/installer/remoteobject.h @@ -40,7 +40,7 @@ #include <QDataStream> #include <QObject> -#include <QTcpSocket> +#include <QLocalSocket> namespace QInstaller { @@ -151,7 +151,7 @@ private: private: QString m_type; - QTcpSocket *m_socket; + QLocalSocket *m_socket; mutable QDataStream m_stream; }; diff --git a/src/libs/installer/remoteserver.cpp b/src/libs/installer/remoteserver.cpp index f95ca6553..b5f077b7e 100644 --- a/src/libs/installer/remoteserver.cpp +++ b/src/libs/installer/remoteserver.cpp @@ -68,7 +68,7 @@ RemoteServer::~RemoteServer() void RemoteServer::start() { Q_D(RemoteServer); - if (d->m_tcpServer) + if (d->m_localServer) return; #if defined(Q_OS_UNIX) && !defined(Q_OS_OSX) @@ -79,11 +79,11 @@ void RemoteServer::start() fclose(stderr); #endif - d->m_tcpServer = new TcpServer(d->m_port, d->m_key); - d->m_tcpServer->moveToThread(&d->m_thread); - connect(&d->m_thread, SIGNAL(finished()), d->m_tcpServer, SLOT(deleteLater())); - connect(d->m_tcpServer, SIGNAL(newIncomingConnection()), this, SLOT(restartWatchdog())); - connect(d->m_tcpServer, SIGNAL(shutdownRequested()), this, SLOT(deleteLater())); + d->m_localServer = new LocalServer(d->m_socketName, d->m_key); + d->m_localServer->moveToThread(&d->m_thread); + connect(&d->m_thread, SIGNAL(finished()), d->m_localServer, SLOT(deleteLater())); + connect(d->m_localServer, SIGNAL(newIncomingConnection()), this, SLOT(restartWatchdog())); + connect(d->m_localServer, SIGNAL(shutdownRequested()), this, SLOT(deleteLater())); d->m_thread.start(); if (d->m_mode == Protocol::Mode::Production) { @@ -93,24 +93,24 @@ void RemoteServer::start() } /*! - Initializes the server with \a port, the port to listen on, with \a key, the key the client + Initializes the server with \a socketName, with \a key, the key the client needs to send to authenticate with the server, and \a mode. */ -void RemoteServer::init(quint16 port, const QString &key, Protocol::Mode mode) +void RemoteServer::init(const QString &socketName, const QString &key, Protocol::Mode mode) { Q_D(RemoteServer); - d->m_port = port; + d->m_socketName = socketName; d->m_key = key; d->m_mode = mode; } /*! - Returns the port the server is listening on. + Returns the socket name the server is listening on. */ -quint16 RemoteServer::port() const +QString RemoteServer::socketName() const { Q_D(const RemoteServer); - return d->m_port; + return d->m_socketName; } /*! diff --git a/src/libs/installer/remoteserver.h b/src/libs/installer/remoteserver.h index fcbb6ef7f..77550522d 100644 --- a/src/libs/installer/remoteserver.h +++ b/src/libs/installer/remoteserver.h @@ -55,9 +55,9 @@ public: ~RemoteServer(); void start(); - void init(quint16 port, const QString &authorizationKey, Protocol::Mode mode); + void init(const QString &socketName, const QString &authorizationKey, Protocol::Mode mode); - quint16 port() const; + QString socketName() const; QString authorizationKey() const; private slots: diff --git a/src/libs/installer/remoteserver_p.h b/src/libs/installer/remoteserver_p.h index be65c2c2d..8750e00d9 100644 --- a/src/libs/installer/remoteserver_p.h +++ b/src/libs/installer/remoteserver_p.h @@ -41,26 +41,27 @@ #include <QHostAddress> #include <QPointer> -#include <QTcpServer> +#include <QLocalServer> #include <QTimer> namespace QInstaller { -class TcpServer : public QTcpServer +class LocalServer : public QLocalServer { Q_OBJECT - Q_DISABLE_COPY(TcpServer) + Q_DISABLE_COPY(LocalServer) public: - TcpServer(quint16 port, const QString &key) - : QTcpServer(0) + LocalServer(const QString &socketName, const QString &key) + : QLocalServer(0) , m_key(key) , m_shutdown(false) { - listen(QHostAddress(QLatin1String(Protocol::DefaultHostAddress)), port); + setSocketOptions(QLocalServer::WorldAccessOption); + listen(socketName); } - ~TcpServer() { + ~LocalServer() { shutdown(); } @@ -80,7 +81,7 @@ private slots: } private: - void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE { + void incomingConnection(quintptr socketDescriptor) Q_DECL_OVERRIDE { if (m_shutdown) return; @@ -104,9 +105,8 @@ class RemoteServerPrivate public: explicit RemoteServerPrivate(RemoteServer *server) : q_ptr(server) - , m_tcpServer(0) + , m_localServer(0) , m_key(QLatin1String(Protocol::DefaultAuthorizationKey)) - , m_port(Protocol::DefaultPort) , m_mode(Protocol::Mode::Debug) , m_watchdog(new QTimer) { @@ -116,10 +116,10 @@ public: private: RemoteServer *q_ptr; - TcpServer *m_tcpServer; + LocalServer *m_localServer; QString m_key; - quint16 m_port; + QString m_socketName; QThread m_thread; Protocol::Mode m_mode; QScopedPointer<QTimer> m_watchdog; diff --git a/src/libs/installer/remoteserverconnection.cpp b/src/libs/installer/remoteserverconnection.cpp index 59f892d50..0bd1f54a9 100644 --- a/src/libs/installer/remoteserverconnection.cpp +++ b/src/libs/installer/remoteserverconnection.cpp @@ -40,7 +40,7 @@ #include "utils.h" #include "permissionsettings.h" -#include <QTcpSocket> +#include <QLocalSocket> namespace QInstaller { @@ -58,7 +58,7 @@ RemoteServerConnection::RemoteServerConnection(qintptr socketDescriptor, const Q void RemoteServerConnection::run() { - QTcpSocket socket; + QLocalSocket socket; socket.setSocketDescriptor(m_socketDescriptor); QScopedPointer<PermissionSettings> settings; @@ -66,7 +66,7 @@ void RemoteServerConnection::run() stream.setDevice(&socket); bool authorized = false; - while (socket.state() == QAbstractSocket::ConnectedState) { + while (socket.state() == QLocalSocket::ConnectedState) { // Use a polling approach here to kill the thread as soon as the connections // closes. This seems to be related to the fact that the keep alive thread connects // every second and immediately throws away the socket and therefore the connection. |