From 4091eccf9b00bec635fdfcb58368b941c0acbb1f Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 1 Feb 2015 15:07:06 +0100 Subject: [PATCH] Updated IPCChannel class to pass a variable number of parameter strings per message (instead of just one). --- include/MUtils/IPCChannel.h | 8 ++++--- src/Config.h | 2 +- src/Global.cpp | 2 +- src/IPCChannel.cpp | 46 ++++++++++++++++++++++++------------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/include/MUtils/IPCChannel.h b/include/MUtils/IPCChannel.h index 7208829..04c2826 100644 --- a/include/MUtils/IPCChannel.h +++ b/include/MUtils/IPCChannel.h @@ -26,6 +26,7 @@ //Qt #include +#include namespace MUtils { @@ -34,7 +35,8 @@ namespace MUtils class MUTILS_API IPCChannel { public: - static const size_t MAX_MESSAGE_LEN = 4096; + static const quint32 MAX_PARAM_LEN = 4096; + static const quint32 MAX_PARAM_CNT = 4; typedef enum { @@ -50,8 +52,8 @@ namespace MUtils int initialize(void); - bool send(const quint32 &command, const quint32 &flags, const char *const message); - bool read(quint32 &command, quint32 &flags, char *const message, const size_t &buffSize); + bool send(const quint32 &command, const quint32 &flags, const QStringList ¶ms = QStringList()); + bool read(quint32 &command, quint32 &flags, QStringList ¶ms); private: IPCChannel(const IPCChannel&) : p(NULL), m_appVersionNo(-1) { throw "Constructor is disabled!"; } diff --git a/src/Config.h b/src/Config.h index 9d540be..4e95ad1 100644 --- a/src/Config.h +++ b/src/Config.h @@ -31,4 +31,4 @@ #define VER_MUTILS_MAJOR 1 #define VER_MUTILS_MINOR_HI 0 -#define VER_MUTILS_MINOR_LO 0 +#define VER_MUTILS_MINOR_LO 1 diff --git a/src/Global.cpp b/src/Global.cpp index 8af6280..684bcc2 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -523,7 +523,7 @@ int MUtils::Internal::selfTest(const char *const buildKey, const bool debug) static const bool MY_DEBUG_FLAG = MUTILS_DEBUG; static const char *const MY_BUILD_KEY = __DATE__"@"__TIME__; - if(strncmp(buildKey, MY_BUILD_KEY, 14) || (MY_DEBUG_FLAG != debug)) + if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug)) { MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!"); MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!"); diff --git a/src/IPCChannel.cpp b/src/IPCChannel.cpp index 042d59a..eeb8819 100644 --- a/src/IPCChannel.cpp +++ b/src/IPCChannel.cpp @@ -33,7 +33,7 @@ #include #include #include - +#include //CRT #include @@ -89,10 +89,17 @@ namespace MUtils typedef struct { - quint32 command_id; - quint32 flags; - char param[MUtils::IPCChannel::MAX_MESSAGE_LEN]; - quint64 timestamp; + char values[MUtils::IPCChannel::MAX_PARAM_CNT][MUtils::IPCChannel::MAX_PARAM_LEN]; + quint32 count; + } + ipc_msg_data_params_t; + + typedef struct + { + quint32 command_id; + quint32 flags; + ipc_msg_data_params_t params; + quint64 timestamp; } ipc_msg_data_t; @@ -301,7 +308,7 @@ int MUtils::IPCChannel::initialize(void) // SEND MESSAGE /////////////////////////////////////////////////////////////////////////////// -bool MUtils::IPCChannel::send(const quint32 &command, const quint32 &flags, const char *const message) +bool MUtils::IPCChannel::send(const quint32 &command, const quint32 &flags, const QStringList ¶ms) { bool success = false; QReadLocker readLock(&p->lock); @@ -334,9 +341,14 @@ bool MUtils::IPCChannel::send(const quint32 &command, const quint32 &flags, cons ipc_msg.payload.command_id = command; ipc_msg.payload.flags = flags; - if(message) + if(!params.isEmpty()) { - strncpy_s(ipc_msg.payload.param, MAX_MESSAGE_LEN, message, _TRUNCATE); + const quint32 param_count = qMin(MAX_PARAM_CNT, (quint32)params.count()); + for(quint32 i = 0; i < param_count; i++) + { + strncpy_s(ipc_msg.payload.params.values[i], MAX_PARAM_LEN, MUTILS_UTF8(params[i]), _TRUNCATE); + } + ipc_msg.payload.params.count = param_count; } ipc_msg.payload.timestamp = ptr->status.payload.counter++; UPDATE_CHECKSUM(ipc_msg); @@ -376,17 +388,13 @@ bool MUtils::IPCChannel::send(const quint32 &command, const quint32 &flags, cons // READ MESSAGE /////////////////////////////////////////////////////////////////////////////// -bool MUtils::IPCChannel::read(quint32 &command, quint32 &flags, char *const message, const size_t &buffSize) +bool MUtils::IPCChannel::read(quint32 &command, quint32 &flags, QStringList ¶ms) { bool success = false; QReadLocker readLock(&p->lock); - command = 0; - if(message && (buffSize > 0)) - { - message[0] = '\0'; - } - + params.clear(); + if(!p->initialized) { MUTILS_THROW("Shared memory for IPC not initialized yet."); @@ -421,7 +429,13 @@ bool MUtils::IPCChannel::read(quint32 &command, quint32 &flags, char *const mess { command = ipc_msg.payload.command_id; flags = ipc_msg.payload.flags; - strncpy_s(message, buffSize, ipc_msg.payload.param, _TRUNCATE); + const quint32 param_count = qMin(ipc_msg.payload.params.count, MAX_PARAM_CNT); + char temp[MAX_PARAM_LEN]; + for(quint32 i = 0; i < param_count; i++) + { + strncpy_s(temp, MAX_PARAM_LEN, ipc_msg.payload.params.values[i], _TRUNCATE); + params.append(QString::fromUtf8(temp)); + } success = true; } else