From dbf8675de3a29643676f779340c79d4fc98b81b7 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Tue, 17 Sep 2013 16:21:23 +0400 Subject: [PATCH] Some improvements of requesting pid of running instance code. --- src/main.cpp | 6 ++- src/qtsingleapp/qtlocalpeer.cpp | 49 +++++++++++-------------- src/qtsingleapp/qtlocalpeer.h | 2 +- src/qtsingleapp/qtsingleapplication.cpp | 2 +- src/qtsingleapp/qtsingleapplication.h | 3 +- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index a1912fb48..809e3a084 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,6 +75,10 @@ Q_IMPORT_PLUGIN(qico) #include "misc.h" #include "preferences.h" +#if defined(Q_OS_WIN) && !defined(QBT_HAS_GETCURRENTPID) +#error You seem to have updated QtSingleApplication without porting our custom QtSingleApplication::getRunningPid() function. Please see previous version to understate how it works. +#endif + class UsageDisplay: public QObject { Q_OBJECT @@ -207,7 +211,7 @@ int main(int argc, char *argv[]) { qDebug("qBittorrent is already running for this user."); // Read torrents given on command line #ifdef Q_OS_WIN - DWORD pid = app.getRunningPid(); + DWORD pid = (DWORD)app.getRunningPid(); if (pid > 0) { BOOL b = AllowSetForegroundWindow(pid); qDebug("AllowSetForegroundWindow() returns %s", b ? "TRUE" : "FALSE"); diff --git a/src/qtsingleapp/qtlocalpeer.cpp b/src/qtsingleapp/qtlocalpeer.cpp index fcf2d4a83..fe770c6e1 100644 --- a/src/qtsingleapp/qtlocalpeer.cpp +++ b/src/qtsingleapp/qtlocalpeer.cpp @@ -202,10 +202,12 @@ void QtLocalPeer::receiveConnection() } QString message(QString::fromUtf8(uMsg)); #ifdef Q_OS_WIN - if (message == "qbt://pid") - socket->write(QByteArray::number(qApp->applicationPid())); - else + if (message == "qbt://pid") { + qint64 pid = GetCurrentProcessId(); + socket->write((const char *)&pid, sizeof pid); + } else { socket->write(ack, qstrlen(ack)); + } #else socket->write(ack, qstrlen(ack)); #endif @@ -219,9 +221,9 @@ void QtLocalPeer::receiveConnection() } #ifdef Q_OS_WIN -ulong QtLocalPeer::getRunningPid() { +qint64 QtLocalPeer::getRunningPid() { if (!isClient()) - return false; + return 0; QLocalSocket socket; bool connOk = false; @@ -231,32 +233,23 @@ ulong QtLocalPeer::getRunningPid() { connOk = socket.waitForConnected(5000/2); if (connOk || i) break; - Sleep(DWORD(250)); + Sleep(250); } - if (!connOk) - return false; - QByteArray uMsg("qbt://pid"); + if (!connOk) return -1; + + const char* msg = "qbt://pid"; QDataStream ds(&socket); - ds.writeBytes(uMsg.constData(), uMsg.size()); - bool res = socket.waitForBytesWritten(5000); - res &= socket.waitForReadyRead(5000); - if (!res) - return -1; - while (socket.bytesAvailable() < (int)sizeof(quint32)) + ds.writeBytes(msg, qstrlen(msg)); + bool res = socket.waitForBytesWritten(5000) && socket.waitForReadyRead(5000); + if (!res) return -1; + + DWORD pid; + qint64 pid_size = sizeof pid; + while (socket.bytesAvailable() < pid_size) socket.waitForReadyRead(); - uMsg.clear(); - quint32 remaining = socket.bytesAvailable(); - uMsg.resize(remaining); - int got = 0; - char* uMsgBuf = uMsg.data(); - do { - got = ds.readRawData(uMsgBuf, remaining); - remaining -= got; - uMsgBuf += got; - } while (remaining && got >= 0 && socket.waitForReadyRead(2000)); - if (got < 0) - return -1; + if (socket.read((char *)&pid, pid_size) < pid_size) + return -1; - return uMsg.toULong(); + return pid; } #endif diff --git a/src/qtsingleapp/qtlocalpeer.h b/src/qtsingleapp/qtlocalpeer.h index 3a6d46959..2fa091f3f 100644 --- a/src/qtsingleapp/qtlocalpeer.h +++ b/src/qtsingleapp/qtlocalpeer.h @@ -65,7 +65,7 @@ public: QString applicationId() const { return id; } #ifdef Q_OS_WIN - ulong getRunningPid(); + qint64 getRunningPid(); #endif Q_SIGNALS: diff --git a/src/qtsingleapp/qtsingleapplication.cpp b/src/qtsingleapp/qtsingleapplication.cpp index fa0b0b6a6..c3b894348 100644 --- a/src/qtsingleapp/qtsingleapplication.cpp +++ b/src/qtsingleapp/qtsingleapplication.cpp @@ -351,7 +351,7 @@ void QtSingleApplication::activateWindow() */ #ifdef Q_OS_WIN -ulong QtSingleApplication::getRunningPid() { +qint64 QtSingleApplication::getRunningPid() { return peer->getRunningPid(); } #endif diff --git a/src/qtsingleapp/qtsingleapplication.h b/src/qtsingleapp/qtsingleapplication.h index e45e373c9..0ffd8f6ee 100644 --- a/src/qtsingleapp/qtsingleapplication.h +++ b/src/qtsingleapp/qtsingleapplication.h @@ -89,7 +89,8 @@ public: void initialize(bool dummy = true) { isRunning(); Q_UNUSED(dummy) } #ifdef Q_OS_WIN - ulong getRunningPid(); +#define QBT_HAS_GETCURRENTPID + qint64 getRunningPid(); #endif public Q_SLOTS: