singleapplication_p.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // W A R N I N G !!!
  24. // -----------------
  25. //
  26. // This file is not part of the SingleApplication API. It is used purely as an
  27. // implementation detail. This header file may change from version to
  28. // version without notice, or may even be removed.
  29. //
  30. #ifndef SINGLEAPPLICATION_P_H
  31. #define SINGLEAPPLICATION_P_H
  32. #include <QtCore/QSharedMemory>
  33. #include <QtNetwork/QLocalServer>
  34. #include <QtNetwork/QLocalSocket>
  35. #include "singleapplication.h"
  36. struct InstancesInfo {
  37. bool primary;
  38. quint32 secondary;
  39. qint64 primaryPid;
  40. char primaryUser[128];
  41. quint16 checksum; // Must be the last field
  42. };
  43. struct ConnectionInfo {
  44. qint64 msgLen = 0;
  45. quint32 instanceId = 0;
  46. quint8 stage = 0;
  47. };
  48. class SingleApplicationPrivate : public QObject {
  49. Q_OBJECT
  50. public:
  51. enum ConnectionType : quint8 {
  52. InvalidConnection = 0,
  53. NewInstance = 1,
  54. SecondaryInstance = 2,
  55. Reconnect = 3
  56. };
  57. enum ConnectionStage : quint8 {
  58. StageInitHeader = 0,
  59. StageInitBody = 1,
  60. StageConnectedHeader = 2,
  61. StageConnectedBody = 3,
  62. };
  63. Q_DECLARE_PUBLIC(SingleApplication)
  64. SingleApplicationPrivate( SingleApplication *q_ptr );
  65. ~SingleApplicationPrivate() override;
  66. static QString getUsername();
  67. void genBlockServerName();
  68. void initializeMemoryBlock() const;
  69. void startPrimary();
  70. void startSecondary();
  71. bool connectToPrimary( int msecs, ConnectionType connectionType );
  72. quint16 blockChecksum() const;
  73. qint64 primaryPid() const;
  74. QString primaryUser() const;
  75. bool isFrameComplete(QLocalSocket *sock);
  76. void readMessageHeader(QLocalSocket *socket, ConnectionStage nextStage);
  77. void readInitMessageBody(QLocalSocket *socket);
  78. void writeAck(QLocalSocket *sock);
  79. bool writeConfirmedFrame(int msecs, const QByteArray &msg);
  80. bool writeConfirmedMessage(int msecs, const QByteArray &msg);
  81. static void randomSleep();
  82. void addAppData(const QString &data);
  83. QStringList appData() const;
  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. QStringList appDataList;
  93. public Q_SLOTS:
  94. void slotConnectionEstablished();
  95. void slotDataAvailable( QLocalSocket*, quint32 );
  96. void slotClientConnectionClosed( QLocalSocket*, quint32 );
  97. };
  98. #endif // SINGLEAPPLICATION_P_H