qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
ipc.h
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #pragma once
22 
23 #include <QMap>
24 #include <QObject>
25 #include <QSharedMemory>
26 #include <QTimer>
27 #include <ctime>
28 #include <functional>
29 
30 using IPCEventHandler = std::function<bool(const QByteArray&)>;
31 
32 #define IPC_PROTOCOL_VERSION "2"
33 
34 class IPC : public QObject
35 {
36  Q_OBJECT
37 
38 protected:
39  static const int EVENT_TIMER_MS = 1000;
40  static const int EVENT_GC_TIMEOUT = 5;
41  static const int EVENT_QUEUE_SIZE = 32;
42  static const int OWNERSHIP_TIMEOUT_S = 5;
43 
44 public:
45  IPC(uint32_t profileId);
46  ~IPC();
47 
48  struct IPCEvent
49  {
50  uint32_t dest;
51  int32_t sender;
52  char name[16];
53  char data[128];
54  time_t posted;
55  time_t processed;
56  uint32_t flags;
57  bool accepted;
58  bool global;
59  };
60 
61  struct IPCMemory
62  {
63  uint64_t globalId;
64  time_t lastEvent;
65  time_t lastProcessed;
67  };
68 
69  time_t postEvent(const QString& name, const QByteArray& data = QByteArray(), uint32_t dest = 0);
70  bool isCurrentOwner();
71  void registerEventHandler(const QString& name, IPCEventHandler handler);
72  bool isEventAccepted(time_t time);
73  bool waitUntilAccepted(time_t time, int32_t timeout = -1);
74  bool isAttached() const;
75 
76 public slots:
77  void setProfileId(uint32_t profileId);
78 
79 private:
80  IPCMemory* global();
81  bool runEventHandler(IPCEventHandler handler, const QByteArray& arg);
83  void processEvents();
84  bool isCurrentOwnerNoLock();
85 
86 private:
87  QTimer timer;
88  uint64_t globalId;
89  uint32_t profileId;
90  QSharedMemory globalMemory;
91  QMap<QString, IPCEventHandler> eventHandlers;
92 };
IPC::IPCEvent::processed
time_t processed
Definition: ipc.h:55
IPC::isCurrentOwner
bool isCurrentOwner()
Definition: ipc.cpp:176
IPC::IPCMemory::events
IPCEvent events[IPC::EVENT_QUEUE_SIZE]
Definition: ipc.h:66
IPC::IPCEvent::accepted
bool accepted
Definition: ipc.h:57
IPC::isAttached
bool isAttached() const
Definition: ipc.cpp:235
IPC::globalMemory
QSharedMemory globalMemory
Definition: ipc.h:90
IPC::IPCEvent
Definition: ipc.h:48
IPC::IPC
IPC(uint32_t profileId)
Definition: ipc.cpp:72
IPC::setProfileId
void setProfileId(uint32_t profileId)
Definition: ipc.cpp:240
IPC::IPCMemory
Definition: ipc.h:61
IPC::runEventHandler
bool runEventHandler(IPCEventHandler handler, const QByteArray &arg)
Definition: ipc.cpp:272
IPC::OWNERSHIP_TIMEOUT_S
static const int OWNERSHIP_TIMEOUT_S
Definition: ipc.h:42
IPC::IPCEvent::posted
time_t posted
Definition: ipc.h:54
IPC::IPCEvent::global
bool global
Definition: ipc.h:58
IPC::IPCEvent::name
char name[16]
Definition: ipc.h:52
IPC::timer
QTimer timer
Definition: ipc.h:87
IPC::waitUntilAccepted
bool waitUntilAccepted(time_t time, int32_t timeout=-1)
Definition: ipc.cpp:218
IPC::processEvents
void processEvents()
Definition: ipc.cpp:286
IPC::IPCMemory::lastProcessed
time_t lastProcessed
Definition: ipc.h:65
IPC::fetchEvent
IPCEvent * fetchEvent()
Only called when global memory IS LOCKED.
Definition: ipc.cpp:249
IPC::registerEventHandler
void registerEventHandler(const QString &name, IPCEventHandler handler)
Register a handler for an IPC event.
Definition: ipc.cpp:192
IPC::IPCMemory::lastEvent
time_t lastEvent
Definition: ipc.h:64
IPC::EVENT_TIMER_MS
static const int EVENT_TIMER_MS
Definition: ipc.h:39
IPC::global
IPCMemory * global()
Definition: ipc.cpp:353
IPC::profileId
uint32_t profileId
Definition: ipc.h:89
IPC::IPCMemory::globalId
uint64_t globalId
Definition: ipc.h:63
IPC::isEventAccepted
bool isEventAccepted(time_t time)
Definition: ipc.cpp:197
IPC::EVENT_GC_TIMEOUT
static const int EVENT_GC_TIMEOUT
Definition: ipc.h:40
IPC::~IPC
~IPC()
Definition: ipc.cpp:116
IPC::isCurrentOwnerNoLock
bool isCurrentOwnerNoLock()
Only called when global memory IS LOCKED.
Definition: ipc.cpp:343
IPC::eventHandlers
QMap< QString, IPCEventHandler > eventHandlers
Definition: ipc.h:91
IPC
Inter-process communication.
Definition: ipc.h:34
IPC::IPCEvent::dest
uint32_t dest
Definition: ipc.h:50
IPC::IPCEvent::sender
int32_t sender
Definition: ipc.h:51
IPC::postEvent
time_t postEvent(const QString &name, const QByteArray &data=QByteArray(), uint32_t dest=0)
Post IPC event.
Definition: ipc.cpp:136
IPC::IPCEvent::data
char data[128]
Definition: ipc.h:53
IPC::IPCEvent::flags
uint32_t flags
Definition: ipc.h:56
IPC::EVENT_QUEUE_SIZE
static const int EVENT_QUEUE_SIZE
Definition: ipc.h:41
IPC::globalId
uint64_t globalId
Definition: ipc.h:88
IPCEventHandler
std::function< bool(const QByteArray &)> IPCEventHandler
Definition: ipc.h:30