Update qtlocalpeer.cpp

This commit is contained in:
Saksham Goyal 2024-10-09 02:16:42 -04:00 committed by GitHub
parent 07f20b275e
commit 681374d665
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -78,29 +78,16 @@
#include <QFileInfo> #include <QFileInfo>
#include <QLocalServer> #include <QLocalServer>
#include <QLocalSocket> #include <QLocalSocket>
#include <QLockFile>
namespace QtLP_Private
{
#include "qtlockedfile.cpp"
#if defined(Q_OS_WIN)
#include "qtlockedfile_win.cpp"
#else
#include "qtlockedfile_unix.cpp"
#endif
}
const char ACK[] = "ack"; const char ACK[] = "ack";
QtLocalPeer::QtLocalPeer(const QString &path, QObject *parent) QtLocalPeer::QtLocalPeer(const QString &path, QObject *parent)
: QObject(parent) : QObject(parent)
, m_socketName(path + u"/ipc-socket") , m_socketName(path + u"/ipc-socket")
, m_server(new QLocalServer(this)) , m_lockFile(path + u"/lockfile")
{ {
m_server->setSocketOptions(QLocalServer::UserAccessOption); m_server->setSocketOptions(QLocalServer::UserAccessOption);
m_lockFile.setFileName(path + u"/lockfile");
m_lockFile.open(QIODevice::ReadWrite);
} }
QtLocalPeer::~QtLocalPeer() QtLocalPeer::~QtLocalPeer()
@ -108,7 +95,6 @@ QtLocalPeer::~QtLocalPeer()
if (!isClient()) if (!isClient())
{ {
m_lockFile.unlock(); m_lockFile.unlock();
m_lockFile.remove();
} }
} }
@ -117,7 +103,7 @@ bool QtLocalPeer::isClient()
if (m_lockFile.isLocked()) if (m_lockFile.isLocked())
return false; return false;
if (!m_lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false)) if (!m_lockFile.lock())
return true; return true;
bool res = m_server->listen(m_socketName); bool res = m_server->listen(m_socketName);
@ -150,18 +136,18 @@ bool QtLocalPeer::sendMessage(const QString &message, const int timeout)
connOk = socket.waitForConnected(timeout/2); connOk = socket.waitForConnected(timeout/2);
if (connOk || i) if (connOk || i)
break; break;
int ms = 250; const int ms = 250;
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
::Sleep(DWORD(ms)); ::Sleep(DWORD(ms));
#else #else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 }; const struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
::nanosleep(&ts, nullptr); ::nanosleep(&ts, nullptr);
#endif #endif
} }
if (!connOk) if (!connOk)
return false; return false;
QByteArray uMsg(message.toUtf8()); const QByteArray uMsg(message.toUtf8());
QDataStream ds(&socket); QDataStream ds(&socket);
ds.writeBytes(uMsg.constData(), uMsg.size()); ds.writeBytes(uMsg.constData(), uMsg.size());
bool res = socket.waitForBytesWritten(timeout); bool res = socket.waitForBytesWritten(timeout);
@ -188,14 +174,14 @@ void QtLocalPeer::receiveConnection()
delete socket; delete socket;
return; return;
} }
if (socket->bytesAvailable() >= qint64(sizeof(quint32))) if (socket->bytesAvailable() >= int64_t(sizeof(int32_t)))
break; break;
socket->waitForReadyRead(); socket->waitForReadyRead();
} }
QDataStream ds(socket); QDataStream ds(socket);
QByteArray uMsg; QByteArray uMsg;
quint32 remaining; uint32_t remaining;
ds >> remaining; ds >> remaining;
if (remaining > 65535) if (remaining > 65535)
{ {