2014-11-08 05:45:21 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-11-08 05:45:21 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-08 05:45:21 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-08 05:45:21 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-08 05:45:21 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-08 05:45:21 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-11-06 22:12:10 +08:00
|
|
|
#ifndef IPC_H
|
|
|
|
#define IPC_H
|
|
|
|
|
2015-10-11 19:49:22 +08:00
|
|
|
#include <QMap>
|
|
|
|
#include <QObject>
|
2014-11-06 22:12:10 +08:00
|
|
|
#include <QSharedMemory>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QVector>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <ctime>
|
|
|
|
#include <functional>
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
using IPCEventHandler = std::function<bool(const QByteArray&)>;
|
2015-02-20 20:26:41 +08:00
|
|
|
|
|
|
|
#define IPC_PROTOCOL_VERSION "2"
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-10-11 19:49:22 +08:00
|
|
|
class IPC : public QObject
|
2014-11-06 22:12:10 +08:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2017-02-26 19:52:45 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
protected:
|
|
|
|
static const int EVENT_TIMER_MS = 1000;
|
|
|
|
static const int EVENT_GC_TIMEOUT = 5;
|
|
|
|
static const int EVENT_QUEUE_SIZE = 32;
|
|
|
|
static const int OWNERSHIP_TIMEOUT_S = 5;
|
2014-11-06 22:12:10 +08:00
|
|
|
|
|
|
|
public:
|
2017-04-18 04:44:58 +08:00
|
|
|
IPC(uint32_t profileId);
|
2014-11-06 22:12:10 +08:00
|
|
|
~IPC();
|
2015-02-20 20:26:41 +08:00
|
|
|
|
|
|
|
struct IPCEvent
|
|
|
|
{
|
|
|
|
uint32_t dest;
|
|
|
|
int32_t sender;
|
|
|
|
char name[16];
|
|
|
|
char data[128];
|
|
|
|
time_t posted;
|
|
|
|
time_t processed;
|
|
|
|
uint32_t flags;
|
|
|
|
bool accepted;
|
|
|
|
bool global;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IPCMemory
|
|
|
|
{
|
|
|
|
uint64_t globalId;
|
|
|
|
time_t lastEvent;
|
|
|
|
time_t lastProcessed;
|
|
|
|
IPCEvent events[IPC::EVENT_QUEUE_SIZE];
|
|
|
|
};
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
time_t postEvent(const QString& name, const QByteArray& data = QByteArray(), uint32_t dest = 0);
|
2015-02-20 20:26:41 +08:00
|
|
|
bool isCurrentOwner();
|
|
|
|
void registerEventHandler(const QString& name, IPCEventHandler handler);
|
|
|
|
bool isEventAccepted(time_t time);
|
2017-02-26 19:52:45 +08:00
|
|
|
bool waitUntilAccepted(time_t time, int32_t timeout = -1);
|
2017-11-22 01:25:39 +08:00
|
|
|
bool isAttached() const;
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2017-05-21 01:20:28 +08:00
|
|
|
public slots:
|
|
|
|
void setProfileId(uint32_t profileId);
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2017-05-21 01:20:28 +08:00
|
|
|
private:
|
2015-02-20 20:26:41 +08:00
|
|
|
IPCMemory* global();
|
|
|
|
bool runEventHandler(IPCEventHandler handler, const QByteArray& arg);
|
2017-05-21 01:20:28 +08:00
|
|
|
IPCEvent* fetchEvent();
|
|
|
|
void processEvents();
|
2017-09-23 06:52:21 +08:00
|
|
|
bool isCurrentOwnerNoLock();
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2017-05-21 01:20:28 +08:00
|
|
|
private:
|
2015-02-20 20:26:41 +08:00
|
|
|
QTimer timer;
|
|
|
|
uint64_t globalId;
|
2017-05-21 01:20:28 +08:00
|
|
|
uint32_t profileId;
|
2015-02-20 20:26:41 +08:00
|
|
|
QSharedMemory globalMemory;
|
|
|
|
QMap<QString, IPCEventHandler> eventHandlers;
|
2014-11-06 22:12:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // IPC_H
|