Krita Source Code Documentation
Loading...
Searching...
No Matches
QtLocalPeer Class Reference

#include <qtlocalpeer.h>

+ Inheritance diagram for QtLocalPeer:

Signals

void messageReceived (const QString &message)
 

Public Member Functions

QString applicationId () const
 
bool isClient ()
 
 QtLocalPeer (QObject *parent=0, const QString &appId=QString())
 
bool sendMessage (const QString &message, int timeout)
 

Protected Slots

void receiveConnection ()
 

Protected Attributes

QString id
 
QtLP_Private::QtLockedFile lockFile
 
QLocalServer * server
 
QString socketName
 

Static Private Attributes

static const char * ack = "ack"
 

Detailed Description

Definition at line 13 of file qtlocalpeer.h.

Constructor & Destructor Documentation

◆ QtLocalPeer()

QtLocalPeer::QtLocalPeer ( QObject * parent = 0,
const QString & appId = QString() )

Definition at line 34 of file qtlocalpeer.cpp.

35 : QObject(parent), id(appId)
36{
37 QString prefix = id;
38 if (id.isEmpty()) {
39 id = QCoreApplication::applicationFilePath();
40#if defined(Q_OS_WIN)
41 id = id.toLower();
42#endif
43 prefix = id.section(QLatin1Char('/'), -1);
44 }
45 prefix.remove(QRegularExpression("[^a-zA-Z]"));
46 prefix.truncate(6);
47
48 QByteArray idc = id.toUtf8();
49 quint16 idNum = qChecksum(idc.constData(), idc.size());
50 socketName = QLatin1String("qtsingleapp-") + prefix
51 + QLatin1Char('-') + QString::number(idNum, 16);
52
53#if defined(Q_OS_WIN)
54 if (!pProcessIdToSessionId) {
55 QLibrary lib("kernel32");
56 pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId");
57 }
58 if (pProcessIdToSessionId) {
59 DWORD sessionId = 0;
60 pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
61 socketName += QLatin1Char('-') + QString::number(sessionId, 16);
62 }
63#else
64 socketName += QLatin1Char('-') + QString::number(::getuid(), 16);
65#endif
66
67 server = new QLocalServer(this);
68 QString lockName = QDir(QDir::tempPath()).absolutePath()
69 + QLatin1Char('/') + socketName
70 + QLatin1String("-lockfile");
71 lockFile.setFileName(lockName);
72 lockFile.open(QIODevice::ReadWrite);
73}
bool open(OpenMode mode)
QString socketName
Definition qtlocalpeer.h:32
QString id
Definition qtlocalpeer.h:31
QtLP_Private::QtLockedFile lockFile
Definition qtlocalpeer.h:34
QLocalServer * server
Definition qtlocalpeer.h:33

References id, lockFile, QtLP_Private::QtLockedFile::open(), server, and socketName.

Member Function Documentation

◆ applicationId()

QString QtLocalPeer::applicationId ( ) const
inline

Definition at line 21 of file qtlocalpeer.h.

22 { return id; }

References id.

◆ isClient()

bool QtLocalPeer::isClient ( )

Definition at line 77 of file qtlocalpeer.cpp.

78{
79 if (lockFile.isLocked())
80 return false;
81
83 return true;
84
85 bool res = server->listen(socketName);
86#if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0))
87 // ### Workaround
88 if (!res && server->serverError() == QAbstractSocket::AddressInUseError) {
89 QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
90 res = server->listen(socketName);
91 }
92#endif
93 if (!res)
94 qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString()));
95 QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection()));
96 return false;
97}
bool lock(LockMode mode, bool block=true)
void receiveConnection()

References QtLP_Private::QtLockedFile::isLocked(), QtLP_Private::QtLockedFile::lock(), lockFile, receiveConnection(), server, socketName, and QtLP_Private::QtLockedFile::WriteLock.

◆ messageReceived

void QtLocalPeer::messageReceived ( const QString & message)
signal

◆ receiveConnection

void QtLocalPeer::receiveConnection ( )
protectedslot

Definition at line 137 of file qtlocalpeer.cpp.

138{
139 QLocalSocket* socket = server->nextPendingConnection();
140 if (!socket)
141 return;
142
143 while (true) {
144 if (socket->state() == QLocalSocket::UnconnectedState) {
145 qWarning("QtLocalPeer: Peer disconnected");
146 delete socket;
147 return;
148 }
149 if (socket->bytesAvailable() >= qint64(sizeof(quint32)))
150 break;
151 socket->waitForReadyRead();
152 }
153
154 QDataStream ds(socket);
155 QByteArray uMsg;
156 quint32 remaining;
157 ds >> remaining;
158 uMsg.resize(remaining);
159 int got = 0;
160 char* uMsgBuf = uMsg.data();
161 do {
162 got = ds.readRawData(uMsgBuf, remaining);
163 remaining -= got;
164 uMsgBuf += got;
165 } while (remaining && got >= 0 && socket->waitForReadyRead(2000));
166 if (got < 0) {
167 qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
168 delete socket;
169 return;
170 }
171 QString message(QString::fromUtf8(uMsg));
172 socket->write(ack, qstrlen(ack));
173 socket->waitForBytesWritten(1000);
174 socket->waitForDisconnected(1000); // make sure client reads ack
175 delete socket;
176 emit messageReceived(message); //### (might take a long time to return)
177}
void messageReceived(const QString &message)
static const char * ack
Definition qtlocalpeer.h:37

References ack, messageReceived(), and server.

◆ sendMessage()

bool QtLocalPeer::sendMessage ( const QString & message,
int timeout )

Definition at line 100 of file qtlocalpeer.cpp.

101{
102 if (!isClient())
103 return false;
104
105 QLocalSocket socket;
106 bool connOk = false;
107 for(int i = 0; i < 2; i++) {
108 // Try twice, in case the other instance is just starting up
109 socket.connectToServer(socketName);
110 connOk = socket.waitForConnected(timeout/2);
111 if (connOk || i)
112 break;
113 int ms = 250;
114#if defined(Q_OS_WIN)
115 Sleep(DWORD(ms));
116#else
117 struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
118 nanosleep(&ts, NULL);
119#endif
120 }
121 if (!connOk)
122 return false;
123
124 QByteArray uMsg(message.toUtf8());
125 QDataStream ds(&socket);
126 ds.writeBytes(uMsg.constData(), uMsg.size());
127 bool res = socket.waitForBytesWritten(timeout);
128 if (res) {
129 res &= socket.waitForReadyRead(timeout); // wait for ack
130 if (res)
131 res &= (socket.read(qstrlen(ack)) == ack);
132 }
133 return res;
134}
bool isClient()

References ack, isClient(), and socketName.

Member Data Documentation

◆ ack

const char * QtLocalPeer::ack = "ack"
staticprivate

Definition at line 37 of file qtlocalpeer.h.

◆ id

QString QtLocalPeer::id
protected

Definition at line 31 of file qtlocalpeer.h.

◆ lockFile

QtLP_Private::QtLockedFile QtLocalPeer::lockFile
protected

Definition at line 34 of file qtlocalpeer.h.

◆ server

QLocalServer* QtLocalPeer::server
protected

Definition at line 33 of file qtlocalpeer.h.

◆ socketName

QString QtLocalPeer::socketName
protected

Definition at line 32 of file qtlocalpeer.h.


The documentation for this class was generated from the following files: