1 // The MIT License (MIT) 2 // 3 // Copyright (c) Itay Grudev 2015 - 2016 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 quint16 checksum; 45 }; 46 47 struct ConnectionInfo { ConnectionInfoConnectionInfo48 explicit ConnectionInfo() : 49 msgLen(0), instanceId(0), stage(0) {} 50 qint64 msgLen; 51 quint32 instanceId; 52 quint8 stage; 53 }; 54 55 class SingleApplicationPrivate : public QObject { 56 Q_OBJECT 57 public: 58 enum ConnectionType : quint8 { 59 InvalidConnection = 0, 60 NewInstance = 1, 61 SecondaryInstance = 2, 62 Reconnect = 3 63 }; 64 enum ConnectionStage : quint8 { 65 StageHeader = 0, 66 StageBody = 1, 67 StageConnected = 2, 68 }; 69 Q_DECLARE_PUBLIC(SingleApplication) 70 71 SingleApplicationPrivate( SingleApplication *q_ptr ); 72 ~SingleApplicationPrivate(); 73 74 void genBlockServerName(); 75 void initializeMemoryBlock(); 76 void startPrimary(); 77 void startSecondary(); 78 void connectToPrimary(int msecs, ConnectionType connectionType ); 79 quint16 blockChecksum(); 80 qint64 primaryPid(); 81 void readInitMessageHeader(QLocalSocket *socket); 82 void readInitMessageBody(QLocalSocket *socket); 83 84 SingleApplication *q_ptr; 85 QSharedMemory *memory; 86 QLocalSocket *socket; 87 QLocalServer *server; 88 quint32 instanceNumber; 89 QString blockServerName; 90 SingleApplication::Options options; 91 QMap<QLocalSocket*, ConnectionInfo> connectionMap; 92 93 public Q_SLOTS: 94 void slotConnectionEstablished(); 95 void slotDataAvailable( QLocalSocket*, quint32 ); 96 void slotClientConnectionClosed( QLocalSocket*, quint32 ); 97 }; 98 99 #endif // SINGLEAPPLICATION_P_H 100