Enhanced IPC class to pass an additional "flags" value for each command.

This commit is contained in:
LoRd_MuldeR 2014-02-01 15:30:46 +01:00
parent b7cc4dd122
commit b5ee60f8d9
2 changed files with 36 additions and 19 deletions

View File

@ -49,6 +49,7 @@ typedef struct
{ {
int command; int command;
wchar_t args[MAX_ARG_CNT][MAX_STR_LEN]; wchar_t args[MAX_ARG_CNT][MAX_STR_LEN];
unsigned int flags;
} }
data[MAX_ENTRIES]; data[MAX_ENTRIES];
size_t posRd; size_t posRd;
@ -81,8 +82,8 @@ protected:
IPCCore(void); IPCCore(void);
~IPCCore(void); ~IPCCore(void);
bool popCommand(int &command, QStringList &args); bool popCommand(int &command, QStringList &args, unsigned int &flags);
bool pushCommand(const int &command, const QStringList *args); bool pushCommand(const int &command, const QStringList *args, const unsigned int &flags = 0);
volatile int m_initialized; volatile int m_initialized;
@ -95,23 +96,23 @@ protected:
// Send Thread // Send Thread
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
IPCSendThread::IPCSendThread(IPCCore *ipc, const int &command, const QStringList &args) IPCSendThread::IPCSendThread(IPCCore *ipc, const int &command, const QStringList &args, const unsigned int &flags)
: :
m_ipc(ipc), m_command(command), m_args(new QStringList(args)) m_ipc(ipc), m_command(command), m_args(&args), m_flags(flags)
{ {
m_result = false; m_result = false;
} }
IPCSendThread::~IPCSendThread(void) IPCSendThread::~IPCSendThread(void)
{ {
X264_DELETE(m_args); /*nothing to do here*/
} }
void IPCSendThread::run(void) void IPCSendThread::run(void)
{ {
try try
{ {
m_result = m_ipc->pushCommand(m_command, m_args); m_result = m_ipc->pushCommand(m_command, m_args, m_flags);
} }
catch(...) catch(...)
{ {
@ -131,6 +132,11 @@ IPCReceiveThread::IPCReceiveThread(IPCCore *ipc)
m_stopped = false; m_stopped = false;
} }
IPCReceiveThread::~IPCReceiveThread(void)
{
/*nothing to do here*/
}
void IPCReceiveThread::run(void) void IPCReceiveThread::run(void)
{ {
try try
@ -147,15 +153,17 @@ void IPCReceiveThread::receiveLoop(void)
{ {
while(!m_stopped) while(!m_stopped)
{ {
QStringList args;
int command; int command;
if(m_ipc->popCommand(command, args)) unsigned int flags;
QStringList args;
if(m_ipc->popCommand(command, args, flags))
{ {
if(!m_stopped) if(!m_stopped)
{ {
if((command >= 0) && (command < IPC_OPCODE_MAX)) if((command >= 0) && (command < IPC_OPCODE_MAX))
{ {
emit receivedCommand(command, args); emit receivedCommand(command, args, flags);
} }
else else
{ {
@ -234,7 +242,7 @@ bool IPCCore::initialize(bool &firstInstance)
return false; return false;
} }
bool IPCCore::pushCommand(const int &command, const QStringList *args) bool IPCCore::pushCommand(const int &command, const QStringList *args, const unsigned int &flags)
{ {
if(m_initialized < 0) if(m_initialized < 0)
{ {
@ -266,6 +274,7 @@ bool IPCCore::pushCommand(const int &command, const QStringList *args)
const wchar_t *current = (args && (i < args->count())) ? ((const wchar_t*)((*args)[i].utf16())) : EMPTY_STRING; const wchar_t *current = (args && (i < args->count())) ? ((const wchar_t*)((*args)[i].utf16())) : EMPTY_STRING;
wcsncpy_s(memory->data[memory->posWr].args[i], MAX_STR_LEN, current, _TRUNCATE); wcsncpy_s(memory->data[memory->posWr].args[i], MAX_STR_LEN, current, _TRUNCATE);
} }
memory->data[memory->posWr].flags = flags;
memory->posWr = (memory->posWr + 1) % MAX_ENTRIES; memory->posWr = (memory->posWr + 1) % MAX_ENTRIES;
memory->counter++; memory->counter++;
} }
@ -290,9 +299,10 @@ bool IPCCore::pushCommand(const int &command, const QStringList *args)
return success; return success;
} }
bool IPCCore::popCommand(int &command, QStringList &args) bool IPCCore::popCommand(int &command, QStringList &args, unsigned int &flags)
{ {
command = -1; command = -1;
flags = 0;
args.clear(); args.clear();
if(m_initialized < 0) if(m_initialized < 0)
@ -326,6 +336,7 @@ bool IPCCore::popCommand(int &command, QStringList &args)
const QString str = QString::fromUtf16((const ushort*)memory->data[memory->posRd].args[i]); const QString str = QString::fromUtf16((const ushort*)memory->data[memory->posRd].args[i]);
if(!str.isEmpty()) args << str; else break; if(!str.isEmpty()) args << str; else break;
} }
flags = memory->data[memory->posRd].flags;
memory->posRd = (memory->posRd + 1) % MAX_ENTRIES; memory->posRd = (memory->posRd + 1) % MAX_ENTRIES;
memory->counter--; memory->counter--;
} }
@ -380,7 +391,7 @@ bool IPC::initialize(bool &firstInstance)
return m_ipcCore->initialize(firstInstance); return m_ipcCore->initialize(firstInstance);
} }
bool IPC::sendAsync(const int &command, const QStringList &args) bool IPC::sendAsync(const int &command, const QStringList &args, const unsigned int &flags)
{ {
QMutexLocker lock(&m_mutex); QMutexLocker lock(&m_mutex);
@ -390,7 +401,7 @@ bool IPC::sendAsync(const int &command, const QStringList &args)
return false; return false;
} }
IPCSendThread sendThread(m_ipcCore, command, args); IPCSendThread sendThread(m_ipcCore, command, args, flags);
sendThread.start(); sendThread.start();
if(!sendThread.wait(TIMEOUT_MS)) if(!sendThread.wait(TIMEOUT_MS))
@ -417,7 +428,7 @@ bool IPC::startListening(void)
if(!m_recvThread) if(!m_recvThread)
{ {
m_recvThread = new IPCReceiveThread(m_ipcCore); m_recvThread = new IPCReceiveThread(m_ipcCore);
connect(m_recvThread, SIGNAL(receivedCommand(int,QStringList)), this, SIGNAL(receivedCommand(int,QStringList)), Qt::QueuedConnection); connect(m_recvThread, SIGNAL(receivedCommand(int,QStringList,quint32)), this, SIGNAL(receivedCommand(int,QStringList,quint32)), Qt::QueuedConnection);
} }
if(!m_recvThread->isRunning()) if(!m_recvThread->isRunning())

View File

@ -38,6 +38,10 @@ static const int IPC_OPCODE_ADD_FILE = 1;
static const int IPC_OPCODE_ADD_JOB = 2; static const int IPC_OPCODE_ADD_JOB = 2;
static const int IPC_OPCODE_MAX = 3; static const int IPC_OPCODE_MAX = 3;
//IPC Flags
static const unsigned int IPC_FLAG_FORCE_START = 0x00000001;
static const unsigned int IPC_FLAG_FORCE_ENQUEUE = 0x00000002;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// IPC Handler Class // IPC Handler Class
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -51,7 +55,7 @@ public:
~IPC(void); ~IPC(void);
bool initialize(bool &firstInstance); bool initialize(bool &firstInstance);
bool sendAsync(const int &command, const QStringList &args); bool sendAsync(const int &command, const QStringList &args, const unsigned int &flags = 0);
bool isInitialized(void); bool isInitialized(void);
bool isListening(void); bool isListening(void);
@ -60,7 +64,7 @@ public slots:
bool stopListening(void); bool stopListening(void);
signals: signals:
void receivedCommand(const int &command, const QStringList &args); void receivedCommand(const int &command, const QStringList &args, const quint32 &flags);
protected: protected:
IPCCore *m_ipcCore; IPCCore *m_ipcCore;
@ -78,7 +82,7 @@ class IPCSendThread : public QThread
friend class IPC; friend class IPC;
protected: protected:
IPCSendThread(IPCCore *ipc, const int &command, const QStringList &args); IPCSendThread(IPCCore *ipc, const int &command, const QStringList &args, const unsigned int &flags);
IPCSendThread::~IPCSendThread(void); IPCSendThread::~IPCSendThread(void);
inline bool result(void) { return m_result; } inline bool result(void) { return m_result; }
@ -88,6 +92,7 @@ private:
volatile bool m_result; volatile bool m_result;
IPCCore *const m_ipc; IPCCore *const m_ipc;
const int m_command; const int m_command;
const unsigned int m_flags;
const QStringList *m_args; const QStringList *m_args;
}; };
@ -98,12 +103,13 @@ class IPCReceiveThread : public QThread
protected: protected:
IPCReceiveThread(IPCCore *ipc); IPCReceiveThread(IPCCore *ipc);
~IPCReceiveThread(void);
inline void stop(void) { m_stopped = true; } inline void stop(void) { m_stopped = true; }
virtual void run(void); virtual void run(void);
signals: signals:
void receivedCommand(const int &command, const QStringList &args); void receivedCommand(const int &command, const QStringList &args, const quint32 &flags);
private: private:
void receiveLoop(void); void receiveLoop(void);