OSDN Git Service

Enhanced IPC class to pass an additional "flags" value for each command.
[x264-launcher/x264-launcher.git] / src / ipc.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #pragma once
23
24 #include <QThread>
25 #include <QMutex>
26
27 class QSharedMemory;
28 class QStringList;
29 class QSystemSemaphore;
30
31 class IPCCore;
32 class IPCReceiveThread;
33 class IPCSendThread;
34
35 //IPC Commands
36 static const int IPC_OPCODE_PING     = 0;
37 static const int IPC_OPCODE_ADD_FILE = 1;
38 static const int IPC_OPCODE_ADD_JOB  = 2;
39 static const int IPC_OPCODE_MAX      = 3;
40
41 //IPC Flags
42 static const unsigned int IPC_FLAG_FORCE_START   = 0x00000001;
43 static const unsigned int IPC_FLAG_FORCE_ENQUEUE = 0x00000002;
44
45 ///////////////////////////////////////////////////////////////////////////////
46 // IPC Handler Class
47 ///////////////////////////////////////////////////////////////////////////////
48
49 class IPC : public QObject
50 {
51         Q_OBJECT
52
53 public:
54         IPC(void);
55         ~IPC(void);
56
57         bool initialize(bool &firstInstance);
58         bool sendAsync(const int &command, const QStringList &args, const unsigned int &flags = 0);
59         bool isInitialized(void);
60         bool isListening(void);
61
62 public slots:
63         bool startListening(void);
64         bool stopListening(void);
65
66 signals:
67         void receivedCommand(const int &command, const QStringList &args, const quint32 &flags);
68
69 protected:
70         IPCCore *m_ipcCore;
71         IPCReceiveThread *m_recvThread;
72         QMutex m_mutex;
73 };
74
75 ///////////////////////////////////////////////////////////////////////////////
76 // Utility Classes
77 ///////////////////////////////////////////////////////////////////////////////
78
79 class IPCSendThread : public QThread
80 {
81         Q_OBJECT
82         friend class IPC;
83
84 protected:
85         IPCSendThread(IPCCore *ipc, const int &command, const QStringList &args, const unsigned int &flags);
86         IPCSendThread::~IPCSendThread(void);
87
88         inline bool result(void) { return m_result; }
89         virtual void run(void);
90
91 private:
92         volatile bool m_result;
93         IPCCore *const m_ipc;
94         const int m_command;
95         const unsigned int m_flags;
96         const QStringList *m_args;
97 };
98
99 class IPCReceiveThread : public QThread
100 {
101         Q_OBJECT
102         friend class IPC;
103
104 protected:
105         IPCReceiveThread(IPCCore *ipc);
106         ~IPCReceiveThread(void);
107
108         inline void stop(void) { m_stopped = true; }
109         virtual void run(void);
110
111 signals:
112         void receivedCommand(const int &command, const QStringList &args, const quint32 &flags);
113
114 private:
115         void receiveLoop(void);
116         volatile bool m_stopped;
117         IPCCore *const m_ipc;
118 };