Browse Source

Add interface to request the pid of the running instance.

adaptive-webui-19844
sledgehammer999 11 years ago committed by Vladimir Golovnev (Glassez)
parent
commit
89b82778e6
  1. 54
      src/qtsingleapp/qtlocalpeer.cpp
  2. 3
      src/qtsingleapp/qtlocalpeer.h
  3. 6
      src/qtsingleapp/qtsingleapplication.cpp
  4. 3
      src/qtsingleapp/qtsingleapplication.h

54
src/qtsingleapp/qtlocalpeer.cpp

@ -201,8 +201,62 @@ void QtLocalPeer::receiveConnection() @@ -201,8 +201,62 @@ void QtLocalPeer::receiveConnection()
return;
}
QString message(QString::fromUtf8(uMsg));
#ifdef Q_OS_WIN
if (message == "qbt://pid")
socket->write(QByteArray::number(qApp->applicationPid()));
else
socket->write(ack, qstrlen(ack));
#else
socket->write(ack, qstrlen(ack));
#endif
socket->waitForBytesWritten(1000);
delete socket;
#ifdef Q_OS_WIN
if (message == "qbt://pid")
return;
#endif
emit messageReceived(message); //### (might take a long time to return)
}
#ifdef Q_OS_WIN
ulong QtLocalPeer::getRunningPid() {
if (!isClient())
return false;
QLocalSocket socket;
bool connOk = false;
for (int i = 0; i < 2; i++) {
// Try twice, in case the other instance is just starting up
socket.connectToServer(socketName);
connOk = socket.waitForConnected(5000/2);
if (connOk || i)
break;
Sleep(DWORD(250));
}
if (!connOk)
return false;
QByteArray uMsg("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))
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;
return uMsg.toULong();
}
#endif

3
src/qtsingleapp/qtlocalpeer.h

@ -64,6 +64,9 @@ public: @@ -64,6 +64,9 @@ public:
bool sendMessage(const QString &message, int timeout);
QString applicationId() const
{ return id; }
#ifdef Q_OS_WIN
ulong getRunningPid();
#endif
Q_SIGNALS:
void messageReceived(const QString &message);

6
src/qtsingleapp/qtsingleapplication.cpp

@ -349,3 +349,9 @@ void QtSingleApplication::activateWindow() @@ -349,3 +349,9 @@ void QtSingleApplication::activateWindow()
\obsolete
*/
#ifdef Q_OS_WIN
ulong QtSingleApplication::getRunningPid() {
return peer->getRunningPid();
}
#endif

3
src/qtsingleapp/qtsingleapplication.h

@ -88,6 +88,9 @@ public: @@ -88,6 +88,9 @@ public:
// Obsolete:
void initialize(bool dummy = true)
{ isRunning(); Q_UNUSED(dummy) }
#ifdef Q_OS_WIN
ulong getRunningPid();
#endif
public Q_SLOTS:
bool sendMessage(const QString &message, int timeout = 5000);

Loading…
Cancel
Save