1 // The MIT License (MIT) 2 // 3 // Copyright (c) Itay Grudev 2015 - 2020 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 // 24 // W A R N I N G !!! 25 // ----------------- 26 // 27 // This file is not part of the SingleApplication API. It is used purely as an 28 // implementation detail. This header file may change from version to 29 // version without notice, or may even be removed. 30 // 31 32 #ifndef SINGLEAPPLICATION_P_H 33 #define SINGLEAPPLICATION_P_H 34 35 #include <QtCore/QSharedMemory> 36 #include <QtNetwork/QLocalServer> 37 #include <QtNetwork/QLocalSocket> 38 #include "singleapplication.h" 39 40 struct InstancesInfo { 41 bool primary; 42 quint32 secondary; 43 qint64 primaryPid; 44 char primaryUser[128]; 45 quint16 checksum; // Must be the last field 46 }; 47 48 struct ConnectionInfo { 49 qint64 msgLen = 0; 50 quint32 instanceId = 0; 51 quint8 stage = 0; 52 }; 53 54 class SingleApplicationPrivate : public QObject { 55 Q_OBJECT 56 public: 57 enum ConnectionType : quint8 { 58 InvalidConnection = 0, 59 NewInstance = 1, 60 SecondaryInstance = 2, 61 Reconnect = 3 62 }; 63 enum ConnectionStage : quint8 { 64 StageInitHeader = 0, 65 StageInitBody = 1, 66 StageConnectedHeader = 2, 67 StageConnectedBody = 3, 68 }; 69 Q_DECLARE_PUBLIC(SingleApplication) 70 71 SingleApplicationPrivate( SingleApplication *q_ptr ); 72 ~SingleApplicationPrivate() override; 73 74 static QString getUsername(); 75 void genBlockServerName(); 76 void initializeMemoryBlock() const; 77 void startPrimary(); 78 void startSecondary(); 79 bool connectToPrimary( int msecs, ConnectionType connectionType ); 80 quint16 blockChecksum() const; 81 qint64 primaryPid() const; 82 QString primaryUser() const; 83 bool isFrameComplete(QLocalSocket *sock); 84 void readMessageHeader(QLocalSocket *socket, ConnectionStage nextStage); 85 void readInitMessageBody(QLocalSocket *socket); 86 void writeAck(QLocalSocket *sock); 87 bool writeConfirmedFrame(int msecs, const QByteArray &msg); 88 bool writeConfirmedMessage(int msecs, const QByteArray &msg); 89 static void randomSleep(); 90 void addAppData(const QString &data); 91 QStringList appData() const; 92 93 SingleApplication *q_ptr; 94 QSharedMemory *memory; 95 QLocalSocket *socket; 96 QLocalServer *server; 97 quint32 instanceNumber; 98 QString blockServerName; 99 SingleApplication::Options options; 100 QMap<QLocalSocket*, ConnectionInfo> connectionMap; 101 QStringList appDataList; 102 103 public Q_SLOTS: 104 void slotConnectionEstablished(); 105 void slotDataAvailable( QLocalSocket*, quint32 ); 106 void slotClientConnectionClosed( QLocalSocket*, quint32 ); 107 }; 108 109 #endif // SINGLEAPPLICATION_P_H 110