OSDN Git Service

Added new IPC handler class. Ported from MediaInfoXP and improved to suite Simple...
[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 public slots:
51         void startListening(void);
52         void stopListening(void);
53
54 signals:
55         void receivedStr(const QString &str);
56
57 protected:
58         bool popCommand(int &command, QStringList &args);
59         bool pushCommand(const int &command, const QStringList *args);
60
61         int m_initialized;
62
63         QSharedMemory *m_sharedMemory;
64         QSystemSemaphore *m_semaphoreRd;
65         QSystemSemaphore *m_semaphoreWr;
66         IPCReceiveThread *m_recvThread;
67 };
68
69 ///////////////////////////////////////////////////////////////////////////////
70 // Utility Classes
71 ///////////////////////////////////////////////////////////////////////////////
72
73 class IPCSendThread : public QThread
74 {
75         Q_OBJECT
76         friend class IPC;
77
78 protected:
79         IPCSendThread(IPC *ipc, const int &command, const QStringList &args);
80         IPCSendThread::~IPCSendThread(void);
81
82         inline bool result(void) { return m_result; }
83
84         virtual void run(void);
85
86 private:
87         volatile bool m_result;
88         IPC *const m_ipc;
89         const int m_command;
90         const QStringList *m_args;
91 };
92
93 class IPCReceiveThread : public QThread
94 {
95         Q_OBJECT
96         friend class IPC;
97
98 protected:
99         IPCReceiveThread(IPC *ipc);
100         inline void stop(void) { m_stopped = true; }
101
102         virtual void run(void);
103
104 signals:
105         void receivedCommand(const int &command, const QStringList &args);
106
107 private:
108         void receiveLoop(void);
109         volatile bool m_stopped;
110         IPC *const m_ipc;
111 };