singleapplication_p.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "singleapplication.h"
  33. #include <QtCore/QSharedMemory>
  34. #include <QtNetwork/QLocalServer>
  35. #include <QtNetwork/QLocalSocket>
  36. struct InstancesInfo
  37. {
  38. bool primary;
  39. quint32 secondary;
  40. qint64 primaryPid;
  41. char primaryUser[128];
  42. quint16 checksum; // Must be the last field
  43. };
  44. struct ConnectionInfo
  45. {
  46. qint64 msgLen = 0;
  47. quint32 instanceId = 0;
  48. quint8 stage = 0;
  49. };
  50. class SingleApplicationPrivate : public QObject
  51. {
  52. Q_OBJECT
  53. public:
  54. enum ConnectionType : quint8
  55. {
  56. InvalidConnection = 0,
  57. NewInstance = 1,
  58. SecondaryInstance = 2,
  59. Reconnect = 3
  60. };
  61. enum ConnectionStage : quint8
  62. {
  63. StageHeader = 0,
  64. StageBody = 1,
  65. StageConnected = 2,
  66. };
  67. Q_DECLARE_PUBLIC(SingleApplication)
  68. SingleApplicationPrivate(SingleApplication* q_ptr);
  69. ~SingleApplicationPrivate() override;
  70. QString getUsername();
  71. void genBlockServerName();
  72. void initializeMemoryBlock();
  73. void startPrimary();
  74. void startSecondary();
  75. bool connectToPrimary(int msecs, ConnectionType connectionType);
  76. quint16 blockChecksum();
  77. qint64 primaryPid();
  78. QString primaryUser();
  79. void readInitMessageHeader(QLocalSocket* socket);
  80. void readInitMessageBody(QLocalSocket* socket);
  81. void randomSleep();
  82. SingleApplication* q_ptr;
  83. QSharedMemory* memory;
  84. QLocalSocket* socket;
  85. QLocalServer* server;
  86. quint32 instanceNumber;
  87. QString blockServerName;
  88. SingleApplication::Options options;
  89. QMap<QLocalSocket*, ConnectionInfo> connectionMap;
  90. public Q_SLOTS:
  91. void slotConnectionEstablished();
  92. void slotDataAvailable(QLocalSocket*, quint32);
  93. void slotClientConnectionClosed(QLocalSocket*, quint32);
  94. };
  95. #endif // SINGLEAPPLICATION_P_H