2014-01-15 03:19:29 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MediaInfoXP
|
2015-01-25 17:20:37 +01:00
|
|
|
// Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
|
2014-01-15 03:19:29 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "IPC.h"
|
|
|
|
|
2015-05-02 18:54:39 +02:00
|
|
|
//MUtils
|
|
|
|
#include "MUtils/Global.h"
|
2014-01-15 03:19:29 +01:00
|
|
|
|
2015-05-02 18:54:39 +02:00
|
|
|
//Qt
|
2014-01-15 03:19:29 +01:00
|
|
|
#include <QSharedMemory>
|
|
|
|
#include <QSystemSemaphore>
|
|
|
|
|
|
|
|
static const size_t MAX_STR_LEN = 1024;
|
|
|
|
static const size_t MAX_ENTRIES = 16;
|
|
|
|
|
|
|
|
static const char *s_key_smemory = "{35DE4BDD-F88C-41E1-8080-2EDC503757F2}";
|
|
|
|
static const char *s_key_sema_wr = "{C638D990-7553-4D4D-95E8-9B040BA6AE87}";
|
|
|
|
static const char *s_key_sema_rd = "{0C30B0B2-0B03-49D7-8DB3-A7D0DDDEA2B0}";
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
wchar_t data[MAX_ENTRIES][MAX_STR_LEN];
|
|
|
|
size_t posRd;
|
|
|
|
size_t posWr;
|
2014-01-16 02:09:48 +01:00
|
|
|
size_t counter;
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
mixp_ipc_t;
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Send Thread
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
IPCSendThread::IPCSendThread(IPC *ipc, const QString &str)
|
|
|
|
:
|
|
|
|
m_ipc(ipc), m_str(str)
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
m_result = false;
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
void IPCSendThread::run(void)
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_result = m_ipc->pushStr(m_str);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
m_result = false;
|
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Receive Thread
|
2014-01-15 03:19:29 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
IPCReceiveThread::IPCReceiveThread(IPC *ipc)
|
|
|
|
:
|
|
|
|
m_ipc(ipc)
|
|
|
|
{
|
|
|
|
m_stopped = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCReceiveThread::run(void)
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
try
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
receiveLoop();
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
2014-01-16 02:09:48 +01:00
|
|
|
catch(...)
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
qWarning("Exception in IPC receive thread!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCReceiveThread::receiveLoop(void)
|
|
|
|
{
|
|
|
|
while(!m_stopped)
|
|
|
|
{
|
|
|
|
QString temp;
|
|
|
|
if(m_ipc->popStr(temp))
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
if(!temp.isEmpty())
|
|
|
|
{
|
|
|
|
emit receivedStr(temp);
|
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
}
|
2014-01-16 02:09:48 +01:00
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// IPC Class
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-01-15 03:19:29 +01:00
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
IPC::IPC(void)
|
|
|
|
{
|
|
|
|
m_initialized = -1;
|
|
|
|
m_sharedMemory = NULL;
|
|
|
|
m_semaphoreWr = NULL;
|
|
|
|
m_semaphoreRd = NULL;
|
|
|
|
m_recvThread = NULL;
|
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
IPC::~IPC(void)
|
|
|
|
{
|
|
|
|
if(m_recvThread && m_recvThread->isRunning())
|
|
|
|
{
|
|
|
|
qWarning("Receive thread still running -> terminating!");
|
|
|
|
m_recvThread->terminate();
|
|
|
|
m_recvThread->wait();
|
|
|
|
}
|
|
|
|
|
2015-05-02 18:54:39 +02:00
|
|
|
MUTILS_DELETE(m_recvThread);
|
|
|
|
MUTILS_DELETE(m_sharedMemory);
|
|
|
|
MUTILS_DELETE(m_semaphoreWr);
|
|
|
|
MUTILS_DELETE(m_semaphoreRd);
|
2014-01-16 02:09:48 +01:00
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
int IPC::initialize(void)
|
2014-01-15 03:19:29 +01:00
|
|
|
{
|
|
|
|
if(m_initialized >= 0)
|
|
|
|
{
|
|
|
|
return m_initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_semaphoreWr = new QSystemSemaphore(s_key_sema_wr, MAX_ENTRIES);
|
|
|
|
m_semaphoreRd = new QSystemSemaphore(s_key_sema_rd, 0);
|
|
|
|
|
|
|
|
if((m_semaphoreWr->error() != QSystemSemaphore::NoError) || (m_semaphoreRd->error() != QSystemSemaphore::NoError))
|
|
|
|
{
|
|
|
|
qWarning("IPC: Failed to created system semaphores!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sharedMemory = new QSharedMemory(s_key_smemory, this);
|
|
|
|
|
|
|
|
if(m_sharedMemory->create(sizeof(mixp_ipc_t)))
|
|
|
|
{
|
|
|
|
memset(m_sharedMemory->data(), 0, sizeof(mixp_ipc_t));
|
|
|
|
m_initialized = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_sharedMemory->error() == QSharedMemory::AlreadyExists)
|
|
|
|
{
|
|
|
|
qDebug("Not the first instance -> attaching to existing shared memory");
|
|
|
|
if(m_sharedMemory->attach())
|
|
|
|
{
|
|
|
|
m_initialized = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qWarning("IPC: Failed to attach to the shared memory!");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPC::pushStr(const QString &str)
|
|
|
|
{
|
|
|
|
if(m_initialized < 0)
|
|
|
|
{
|
|
|
|
qWarning("Error: IPC not initialized yet!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_semaphoreWr->acquire())
|
|
|
|
{
|
|
|
|
qWarning("IPC: Failed to acquire semaphore!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_sharedMemory->lock())
|
|
|
|
{
|
|
|
|
qWarning("IPC: Failed to lock shared memory!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
bool success = true;
|
|
|
|
|
2014-01-15 03:19:29 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mixp_ipc_t *memory = (mixp_ipc_t*) m_sharedMemory->data();
|
2014-01-16 02:09:48 +01:00
|
|
|
if(memory->counter < MAX_ENTRIES)
|
|
|
|
{
|
|
|
|
wcsncpy_s(memory->data[memory->posWr], MAX_STR_LEN, (wchar_t*)str.utf16(), _TRUNCATE);
|
|
|
|
memory->posWr = (memory->posWr + 1) % MAX_ENTRIES;
|
|
|
|
memory->counter++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("IPC: Shared memory is full -> cannot push string!");
|
|
|
|
success = false;
|
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
/*ignore any exception*/
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sharedMemory->unlock();
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
if(success)
|
|
|
|
{
|
|
|
|
m_semaphoreRd->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPC::popStr(QString &str)
|
|
|
|
{
|
|
|
|
if(m_initialized < 0)
|
|
|
|
{
|
|
|
|
qWarning("Error: IPC not initialized yet!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_semaphoreRd->acquire())
|
|
|
|
{
|
|
|
|
qWarning("IPC: Failed to acquire semaphore!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_sharedMemory->lock())
|
|
|
|
{
|
|
|
|
qWarning("IPC: Failed to lock shared memory!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
bool success = true;
|
|
|
|
|
2014-01-15 03:19:29 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
mixp_ipc_t *memory = (mixp_ipc_t*) m_sharedMemory->data();
|
2014-01-16 02:09:48 +01:00
|
|
|
if(memory->counter > 0)
|
|
|
|
{
|
|
|
|
memory->data[memory->posRd][MAX_STR_LEN-1] = L'\0';
|
|
|
|
str = QString::fromUtf16((const ushort*)memory->data[memory->posRd]);
|
|
|
|
memory->posRd = (memory->posRd + 1) % MAX_ENTRIES;
|
|
|
|
memory->counter--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("IPC: Shared memory is empty -> cannot pop string!");
|
|
|
|
success = false;
|
|
|
|
}
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
/*ignore any exception*/
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sharedMemory->unlock();
|
|
|
|
|
2014-01-16 02:09:48 +01:00
|
|
|
if(success)
|
|
|
|
{
|
|
|
|
m_semaphoreWr->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2014-01-15 03:19:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IPC::sendAsync(const QString &str, const int timeout)
|
|
|
|
{
|
2014-01-16 02:09:48 +01:00
|
|
|
IPCSendThread sendThread(this, str);
|
2014-01-15 03:19:29 +01:00
|
|
|
sendThread.start();
|
|
|
|
|
|
|
|
if(!sendThread.wait(timeout))
|
|
|
|
{
|
|
|
|
qWarning("IPC send operation encountered timeout!");
|
|
|
|
sendThread.terminate();
|
|
|
|
sendThread.wait();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendThread.result();
|
|
|
|
}
|
2014-01-16 02:09:48 +01:00
|
|
|
|
|
|
|
void IPC::startListening(void)
|
|
|
|
{
|
|
|
|
if(!m_recvThread)
|
|
|
|
{
|
|
|
|
m_recvThread = new IPCReceiveThread(this);
|
|
|
|
connect(m_recvThread, SIGNAL(receivedStr(QString)), this, SIGNAL(receivedStr(QString)), Qt::QueuedConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!m_recvThread->isRunning())
|
|
|
|
{
|
|
|
|
m_recvThread->start();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("Receive thread was already running!");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPC::stopListening(void)
|
|
|
|
{
|
|
|
|
if(m_recvThread && m_recvThread->isRunning())
|
|
|
|
{
|
|
|
|
m_recvThread->stop();
|
|
|
|
m_semaphoreRd->release();
|
|
|
|
|
|
|
|
if(!m_recvThread->wait(5000))
|
|
|
|
{
|
|
|
|
qWarning("Receive thread seems deadlocked -> terminating!");
|
|
|
|
m_recvThread->terminate();
|
|
|
|
m_recvThread->wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("Receive thread was not running!");
|
|
|
|
}
|
|
|
|
}
|