singleapplication_p.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. StageHeader = 0,
  59. StageBody = 1,
  60. StageConnected = 2,
  61. };
  62. Q_DECLARE_PUBLIC(SingleApplication)
  63. SingleApplicationPrivate( SingleApplication *q_ptr );
  64. ~SingleApplicationPrivate() override;
  65. static QString getUsername();
  66. void genBlockServerName();
  67. void initializeMemoryBlock() const;
  68. void startPrimary();
  69. void startSecondary();
  70. bool connectToPrimary( int msecs, ConnectionType connectionType );
  71. quint16 blockChecksum() const;
  72. qint64 primaryPid() const;
  73. QString primaryUser() const;
  74. void readInitMessageHeader(QLocalSocket *socket);
  75. void readInitMessageBody(QLocalSocket *socket);
  76. static void randomSleep();
  77. SingleApplication *q_ptr;
  78. QSharedMemory *memory;
  79. QLocalSocket *socket;
  80. QLocalServer *server;
  81. quint32 instanceNumber;
  82. QString blockServerName;
  83. SingleApplication::Options options;
  84. QMap<QLocalSocket*, ConnectionInfo> connectionMap;
  85. public Q_SLOTS:
  86. void slotConnectionEstablished();
  87. void slotDataAvailable( QLocalSocket*, quint32 );
  88. void slotClientConnectionClosed( QLocalSocket*, quint32 );
  89. };
  90. #endif // SINGLEAPPLICATION_P_H