OSDN Git Service

Some small improvements to IPC handling.
[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
26 class QSharedMemory;
27 class QStringList;
28 class QSystemSemaphore;
29 class IPCSendThread;
30 class IPCReceiveThread;
31
32 class IPC : public QObject
33 {
34         Q_OBJECT
35         friend class IPCReceiveThread;
36         friend class IPCSendThread;
37
38 public:
39         IPC(void);
40         ~IPC(void);
41
42         static const int IPC_OPCODE_PING     = 0;
43         static const int IPC_OPCODE_ADD_FILE = 1;
44         static const int IPC_OPCODE_ADD_JOB  = 2;
45         static const int IPC_OPCODE_MAX      = 3;
46
47         bool initialize(bool &firstInstance);
48         bool sendAsync(const int &command, const QStringList &args, const int timeout = 5000);
49         
50         inline bool isInitialized(void) { return (m_initialized >= 0); }
51         inline bool isListening(void);
52
53 public slots:
54         bool startListening(void);
55         bool stopListening(void);
56
57 signals:
58         void receivedCommand(const int &command, const QStringList &args);
59
60 protected:
61         bool popCommand(int &command, QStringList &args, volatile bool *abortFlag);
62         bool pushCommand(const int &command, const QStringList *args);
63
64         int m_initialized;
65
66         QSharedMemory *m_sharedMemory;
67         QSystemSemaphore *m_semaphoreRd;
68         QSystemSemaphore *m_semaphoreWr;
69         IPCReceiveThread *m_recvThread;
70 };
71
72 ///////////////////////////////////////////////////////////////////////////////
73 // Utility Classes
74 ///////////////////////////////////////////////////////////////////////////////
75
76 class IPCSendThread : public QThread
77 {
78         Q_OBJECT
79         friend class IPC;
80
81 protected:
82         IPCSendThread(IPC *ipc, const int &command, const QStringList &args);
83         IPCSendThread::~IPCSendThread(void);
84
85         inline bool result(void) { return m_result; }
86
87         virtual void run(void);
88
89 private:
90         volatile bool m_result;
91         IPC *const m_ipc;
92         const int m_command;
93         const QStringList *m_args;
94 };
95
96 class IPCReceiveThread : public QThread
97 {
98         Q_OBJECT
99         friend class IPC;
100
101 protected:
102         IPCReceiveThread(IPC *ipc);
103         inline void stop(void) { m_stopped = true; }
104
105         virtual void run(void);
106
107 signals:
108         void receivedCommand(const int &command, const QStringList &args);
109
110 private:
111         void receiveLoop(void);
112         volatile bool m_stopped;
113         IPC *const m_ipc;
114 };
115
116 ///////////////////////////////////////////////////////////////////////////////
117 // Inline Functions
118 ///////////////////////////////////////////////////////////////////////////////
119
120 inline bool IPC::isListening(void)
121 {
122         return (m_recvThread && m_recvThread->isRunning());
123 }