2014-11-08 05:45:21 +08:00
|
|
|
/*
|
2015-06-06 09:40:08 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project
|
|
|
|
|
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
|
|
|
|
|
|
|
|
#include <QSharedMemory>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QVector>
|
2015-02-20 20:26:41 +08:00
|
|
|
#include <QMap>
|
2014-11-06 22:12:10 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <ctime>
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
using IPCEventHandler = std::function<bool (const QByteArray&)>;
|
|
|
|
|
|
|
|
#define IPC_PROTOCOL_VERSION "2"
|
2014-11-06 22:12:10 +08:00
|
|
|
|
|
|
|
class IPC : public QThread
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2015-02-21 20:49:27 +08:00
|
|
|
IPC();
|
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:
|
|
|
|
~IPC();
|
2015-02-20 20:26:41 +08:00
|
|
|
|
2015-02-21 20:49:27 +08:00
|
|
|
static IPC& getInstance();
|
|
|
|
|
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;
|
|
|
|
// When last event was posted
|
|
|
|
time_t lastEvent;
|
|
|
|
// When processEvents() ran last time
|
|
|
|
time_t lastProcessed;
|
|
|
|
IPCEvent events[IPC::EVENT_QUEUE_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
// dest: Settings::getCurrentProfileId() or 0 (main instance).
|
|
|
|
time_t postEvent(const QString& name, const QByteArray &data=QByteArray(), uint32_t dest=0);
|
|
|
|
bool isCurrentOwner();
|
|
|
|
void registerEventHandler(const QString& name, IPCEventHandler handler);
|
|
|
|
bool isEventAccepted(time_t time);
|
2015-03-19 18:06:05 +08:00
|
|
|
bool waitUntilAccepted(time_t time, int32_t timeout=-1);
|
2014-11-06 22:12:10 +08:00
|
|
|
|
|
|
|
protected slots:
|
|
|
|
void processEvents();
|
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
protected:
|
|
|
|
IPCMemory* global();
|
|
|
|
bool runEventHandler(IPCEventHandler handler, const QByteArray& arg);
|
|
|
|
// Only called when global memory IS LOCKED, returns 0 if no evnts present
|
|
|
|
IPCEvent* fetchEvent();
|
2014-11-06 22:12:10 +08:00
|
|
|
|
2015-02-20 20:26:41 +08:00
|
|
|
QTimer timer;
|
|
|
|
uint64_t globalId;
|
|
|
|
QSharedMemory globalMemory;
|
|
|
|
QMap<QString, IPCEventHandler> eventHandlers;
|
2014-11-06 22:12:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // IPC_H
|