@ -43,56 +43,46 @@
|
||||
#include <QString>
|
||||
#include <memory>
|
||||
|
||||
|
||||
class QLocalServer;
|
||||
class LockedFile;
|
||||
|
||||
class ApplicationId
|
||||
{
|
||||
public: /* methods */
|
||||
class ApplicationId {
|
||||
public: /* methods */
|
||||
// traditional app = installed system wide and used in a multi-user environment
|
||||
static ApplicationId fromTraditionalApp();
|
||||
// ID based on a path with all the application data (no two instances with the same data path should run)
|
||||
static ApplicationId fromPathAndVersion(const QString & dataPath, const QString & version);
|
||||
static ApplicationId fromPathAndVersion(const QString& dataPath, const QString& version);
|
||||
// custom ID
|
||||
static ApplicationId fromCustomId(const QString & id);
|
||||
static ApplicationId fromCustomId(const QString& id);
|
||||
// custom ID, based on a raw string previously acquired from 'toString'
|
||||
static ApplicationId fromRawString(const QString & id);
|
||||
static ApplicationId fromRawString(const QString& id);
|
||||
|
||||
QString toString() { return m_id; }
|
||||
|
||||
QString toString()
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
private: /* methods */
|
||||
ApplicationId(const QString& value) { m_id = value; }
|
||||
|
||||
private: /* methods */
|
||||
ApplicationId(const QString & value)
|
||||
{
|
||||
m_id = value;
|
||||
}
|
||||
|
||||
private: /* data */
|
||||
private: /* data */
|
||||
QString m_id;
|
||||
};
|
||||
|
||||
class LocalPeer : public QObject
|
||||
{
|
||||
class LocalPeer : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
LocalPeer(QObject *parent, const ApplicationId &appId);
|
||||
public:
|
||||
LocalPeer(QObject* parent, const ApplicationId& appId);
|
||||
~LocalPeer();
|
||||
bool isClient();
|
||||
bool sendMessage(const QByteArray &message, int timeout);
|
||||
bool sendMessage(const QByteArray& message, int timeout);
|
||||
ApplicationId applicationId() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void messageReceived(const QByteArray &message);
|
||||
Q_SIGNALS:
|
||||
void messageReceived(const QByteArray& message);
|
||||
|
||||
protected Q_SLOTS:
|
||||
protected Q_SLOTS:
|
||||
void receiveConnection();
|
||||
|
||||
protected:
|
||||
protected:
|
||||
ApplicationId id;
|
||||
QString socketName;
|
||||
std::unique_ptr<QLocalServer> server;
|
||||
|
@ -38,21 +38,20 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
#include "LocalPeer.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QDataStream>
|
||||
#include <QTime>
|
||||
#include <QDir>
|
||||
#include <QLocalServer>
|
||||
#include <QLocalSocket>
|
||||
#include <QDir>
|
||||
#include <QRegularExpression>
|
||||
#include <QTime>
|
||||
#include "LockedFile.h"
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
#include <QLibrary>
|
||||
#include <qt_windows.h>
|
||||
typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*);
|
||||
#include <QLibrary>
|
||||
typedef BOOL(WINAPI* PProcessIdToSessionId)(DWORD, DWORD*);
|
||||
static PProcessIdToSessionId pProcessIdToSessionId = 0;
|
||||
#endif
|
||||
#if defined(Q_OS_UNIX)
|
||||
@ -60,9 +59,9 @@ static PProcessIdToSessionId pProcessIdToSessionId = 0;
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <QCryptographicHash>
|
||||
|
||||
static const char* ack = "ack";
|
||||
|
||||
@ -79,13 +78,11 @@ ApplicationId ApplicationId::fromTraditionalApp()
|
||||
quint16 idNum = qChecksum(idc.constData(), idc.size());
|
||||
auto socketName = QLatin1String("qtsingleapp-") + prefix + QLatin1Char('-') + QString::number(idNum, 16);
|
||||
#if defined(Q_OS_WIN)
|
||||
if (!pProcessIdToSessionId)
|
||||
{
|
||||
if (!pProcessIdToSessionId) {
|
||||
QLibrary lib("kernel32");
|
||||
pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId");
|
||||
}
|
||||
if (pProcessIdToSessionId)
|
||||
{
|
||||
if (pProcessIdToSessionId) {
|
||||
DWORD sessionId = 0;
|
||||
pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
|
||||
socketName += QLatin1Char('-') + QString::number(sessionId, 16);
|
||||
@ -114,8 +111,7 @@ ApplicationId ApplicationId::fromRawString(const QString& id)
|
||||
return ApplicationId(id);
|
||||
}
|
||||
|
||||
LocalPeer::LocalPeer(QObject * parent, const ApplicationId &appId)
|
||||
: QObject(parent), id(appId)
|
||||
LocalPeer::LocalPeer(QObject* parent, const ApplicationId& appId) : QObject(parent), id(appId)
|
||||
{
|
||||
socketName = id.toString();
|
||||
server.reset(new QLocalServer());
|
||||
@ -124,9 +120,7 @@ LocalPeer::LocalPeer(QObject * parent, const ApplicationId &appId)
|
||||
lockFile->open(QIODevice::ReadWrite);
|
||||
}
|
||||
|
||||
LocalPeer::~LocalPeer()
|
||||
{
|
||||
}
|
||||
LocalPeer::~LocalPeer() {}
|
||||
|
||||
ApplicationId LocalPeer::applicationId() const
|
||||
{
|
||||
@ -145,7 +139,7 @@ bool LocalPeer::isClient()
|
||||
#if defined(Q_OS_UNIX)
|
||||
// ### Workaround
|
||||
if (!res && server->serverError() == QAbstractSocket::AddressInUseError) {
|
||||
QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
|
||||
QFile::remove(QDir::cleanPath(QDir::tempPath()) + QLatin1Char('/') + socketName);
|
||||
res = server->listen(socketName);
|
||||
}
|
||||
#endif
|
||||
@ -155,8 +149,7 @@ bool LocalPeer::isClient()
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool LocalPeer::sendMessage(const QByteArray &message, int timeout)
|
||||
bool LocalPeer::sendMessage(const QByteArray& message, int timeout)
|
||||
{
|
||||
if (!isClient())
|
||||
return false;
|
||||
@ -164,17 +157,15 @@ bool LocalPeer::sendMessage(const QByteArray &message, int timeout)
|
||||
QLocalSocket socket;
|
||||
bool connOk = false;
|
||||
int tries = 2;
|
||||
for(int i = 0; i < tries; i++) {
|
||||
for (int i = 0; i < tries; i++) {
|
||||
// Try twice, in case the other instance is just starting up
|
||||
socket.connectToServer(socketName);
|
||||
connOk = socket.waitForConnected(timeout/2);
|
||||
if (!connOk && i < (tries - 1))
|
||||
{
|
||||
connOk = socket.waitForConnected(timeout / 2);
|
||||
if (!connOk && i < (tries - 1)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||
}
|
||||
}
|
||||
if (!connOk)
|
||||
{
|
||||
if (!connOk) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -182,36 +173,30 @@ bool LocalPeer::sendMessage(const QByteArray &message, int timeout)
|
||||
QDataStream ds(&socket);
|
||||
|
||||
ds.writeBytes(uMsg.constData(), uMsg.size());
|
||||
if(!socket.waitForBytesWritten(timeout))
|
||||
{
|
||||
if (!socket.waitForBytesWritten(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// wait for 'ack'
|
||||
if(!socket.waitForReadyRead(timeout))
|
||||
{
|
||||
if (!socket.waitForReadyRead(timeout)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// make sure we got 'ack'
|
||||
if(!(socket.read(qstrlen(ack)) == ack))
|
||||
{
|
||||
if (!(socket.read(qstrlen(ack)) == ack)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void LocalPeer::receiveConnection()
|
||||
{
|
||||
QLocalSocket* socket = server->nextPendingConnection();
|
||||
if (!socket)
|
||||
{
|
||||
if (!socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (socket->bytesAvailable() < static_cast<int>(sizeof(quint32)))
|
||||
{
|
||||
while (socket->bytesAvailable() < static_cast<int>(sizeof(quint32))) {
|
||||
socket->waitForReadyRead();
|
||||
}
|
||||
QDataStream ds(socket);
|
||||
@ -221,21 +206,19 @@ void LocalPeer::receiveConnection()
|
||||
uMsg.resize(remaining);
|
||||
int got = 0;
|
||||
char* uMsgBuf = uMsg.data();
|
||||
do
|
||||
{
|
||||
do {
|
||||
got = ds.readRawData(uMsgBuf, remaining);
|
||||
remaining -= got;
|
||||
uMsgBuf += got;
|
||||
} while (remaining && got >= 0 && socket->waitForReadyRead(2000));
|
||||
if (got < 0)
|
||||
{
|
||||
if (got < 0) {
|
||||
qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
|
||||
delete socket;
|
||||
return;
|
||||
}
|
||||
socket->write(ack, qstrlen(ack));
|
||||
socket->waitForBytesWritten(1000);
|
||||
socket->waitForDisconnected(1000); // make sure client reads ack
|
||||
socket->waitForDisconnected(1000); // make sure client reads ack
|
||||
delete socket;
|
||||
emit messageReceived(uMsg); //### (might take a long time to return)
|
||||
emit messageReceived(uMsg); //### (might take a long time to return)
|
||||
}
|
||||
|
@ -80,8 +80,7 @@
|
||||
|
||||
\sa QFile::QFile()
|
||||
*/
|
||||
LockedFile::LockedFile()
|
||||
: QFile()
|
||||
LockedFile::LockedFile() : QFile()
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
wmutex = 0;
|
||||
@ -97,8 +96,7 @@ LockedFile::LockedFile()
|
||||
|
||||
\sa QFile::QFile()
|
||||
*/
|
||||
LockedFile::LockedFile(const QString &name)
|
||||
: QFile(name)
|
||||
LockedFile::LockedFile(const QString& name) : QFile(name)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
wmutex = 0;
|
||||
|
@ -45,13 +45,12 @@
|
||||
#include <QVector>
|
||||
#endif
|
||||
|
||||
class LockedFile : public QFile
|
||||
{
|
||||
public:
|
||||
class LockedFile : public QFile {
|
||||
public:
|
||||
enum LockMode { NoLock = 0, ReadLock, WriteLock };
|
||||
|
||||
LockedFile();
|
||||
LockedFile(const QString &name);
|
||||
LockedFile(const QString& name);
|
||||
~LockedFile();
|
||||
|
||||
bool open(OpenMode mode);
|
||||
@ -61,8 +60,7 @@ public:
|
||||
bool isLocked() const;
|
||||
LockMode lockMode() const;
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
#ifdef Q_OS_WIN
|
||||
Qt::HANDLE wmutex;
|
||||
Qt::HANDLE rmutex;
|
||||
|
@ -38,10 +38,10 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "LockedFile.h"
|
||||
|
||||
@ -75,12 +75,10 @@ bool LockedFile::lock(LockMode mode, bool block)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
m_lock_mode = mode;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool LockedFile::unlock()
|
||||
{
|
||||
if (!isOpen()) {
|
||||
|
@ -38,9 +38,9 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "LockedFile.h"
|
||||
#include <qt_windows.h>
|
||||
#include <QFileInfo>
|
||||
#include "LockedFile.h"
|
||||
|
||||
#define MUTEX_PREFIX "QtLockedFile mutex "
|
||||
// Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
|
||||
@ -50,8 +50,7 @@ Qt::HANDLE LockedFile::getMutexHandle(int idx, bool doCreate)
|
||||
{
|
||||
if (mutexname.isEmpty()) {
|
||||
QFileInfo fi(*this);
|
||||
mutexname = QString::fromLatin1(MUTEX_PREFIX)
|
||||
+ fi.absoluteFilePath().toLower();
|
||||
mutexname = QString::fromLatin1(MUTEX_PREFIX) + fi.absoluteFilePath().toLower();
|
||||
}
|
||||
QString mname(mutexname);
|
||||
if (idx >= 0)
|
||||
@ -64,8 +63,7 @@ Qt::HANDLE LockedFile::getMutexHandle(int idx, bool doCreate)
|
||||
qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (LPCWSTR)mname.utf16());
|
||||
if (!mutex) {
|
||||
if (GetLastError() != ERROR_FILE_NOT_FOUND)
|
||||
@ -81,20 +79,18 @@ bool LockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
|
||||
Q_ASSERT(mutex);
|
||||
DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0);
|
||||
switch (res) {
|
||||
case WAIT_OBJECT_0:
|
||||
case WAIT_ABANDONED:
|
||||
return true;
|
||||
break;
|
||||
case WAIT_TIMEOUT:
|
||||
break;
|
||||
default:
|
||||
qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
|
||||
case WAIT_OBJECT_0:
|
||||
case WAIT_ABANDONED:
|
||||
return true;
|
||||
break;
|
||||
case WAIT_TIMEOUT:
|
||||
break;
|
||||
default:
|
||||
qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool LockedFile::lock(LockMode mode, bool block)
|
||||
{
|
||||
if (!isOpen()) {
|
||||
@ -130,8 +126,7 @@ bool LockedFile::lock(LockMode mode, bool block)
|
||||
qWarning("QtLockedFile::lock(): too many readers");
|
||||
rmutex = 0;
|
||||
ok = false;
|
||||
}
|
||||
else if (!rmutex) {
|
||||
} else if (!rmutex) {
|
||||
rmutex = getMutexHandle(idx, true);
|
||||
if (!rmutex || !waitMutex(rmutex, false))
|
||||
ok = false;
|
||||
@ -143,8 +138,7 @@ bool LockedFile::lock(LockMode mode, bool block)
|
||||
ReleaseMutex(wmutex);
|
||||
if (!ok)
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
Q_ASSERT(rmutexes.isEmpty());
|
||||
for (int i = 0; i < MAX_READERS; i++) {
|
||||
Qt::HANDLE mutex = getMutexHandle(i, false);
|
||||
@ -152,8 +146,7 @@ bool LockedFile::lock(LockMode mode, bool block)
|
||||
rmutexes.append(mutex);
|
||||
}
|
||||
if (rmutexes.size()) {
|
||||
DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),
|
||||
TRUE, block ? INFINITE : 0);
|
||||
DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(), TRUE, block ? INFINITE : 0);
|
||||
if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) {
|
||||
if (res != WAIT_TIMEOUT)
|
||||
qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed");
|
||||
@ -182,9 +175,8 @@ bool LockedFile::unlock()
|
||||
ReleaseMutex(rmutex);
|
||||
CloseHandle(rmutex);
|
||||
rmutex = 0;
|
||||
}
|
||||
else {
|
||||
foreach(Qt::HANDLE mutex, rmutexes) {
|
||||
} else {
|
||||
foreach (Qt::HANDLE mutex, rmutexes) {
|
||||
ReleaseMutex(mutex);
|
||||
CloseHandle(mutex);
|
||||
}
|
||||
|
Reference in New Issue
Block a user