From dc9e4ca9234cddf7d7c0171d2c5a137551878c37 Mon Sep 17 00:00:00 2001 From: Jacques GUILLOU Date: Mon, 17 Sep 2018 11:27:46 +0200 Subject: [PATCH] IPC: Serialize payload as byte array --- CMakeLists.txt | 2 +- src/ipc/dbus/ipc-dbus.cpp | 21 +++++ src/ipc/dbus/ipc-dbus.h | 161 +++++++++++++++++++++++--------------- 3 files changed, 122 insertions(+), 62 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a8a2a9b1..2bfe7ee8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ find_package(Qt5 REQUIRED Core Gui Qml Quick) set(CMAKE_CONFIG_INSTALLATION_PATH ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 14) if(NOT MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Wextra") endif() diff --git a/src/ipc/dbus/ipc-dbus.cpp b/src/ipc/dbus/ipc-dbus.cpp index 57ceb38a..24385e81 100644 --- a/src/ipc/dbus/ipc-dbus.cpp +++ b/src/ipc/dbus/ipc-dbus.cpp @@ -54,6 +54,27 @@ namespace facelift { namespace dbus { +DBusIPCMessage DBusIPCMessage::call(const QDBusConnection &connection) +{ + if (m_outputPayload) { + m_message << m_outputPayload->getContent(); + } + qDebug() << "Sending IPC message : " << toString(); + auto replyDbusMessage = connection.call(m_message); + DBusIPCMessage reply(replyDbusMessage); + return reply; +} + + +void DBusIPCMessage::send(const QDBusConnection &connection) +{ + if (m_outputPayload) { + m_message << m_outputPayload->getContent(); + } + qDebug() << "Sending IPC message : " << toString(); + bool successful = connection.send(m_message); + Q_ASSERT(successful); +} DBusManager::DBusManager() : m_busConnection(QDBusConnection::sessionBus()) { diff --git a/src/ipc/dbus/ipc-dbus.h b/src/ipc/dbus/ipc-dbus.h index 7347430a..4d1d77ed 100644 --- a/src/ipc/dbus/ipc-dbus.h +++ b/src/ipc/dbus/ipc-dbus.h @@ -47,6 +47,8 @@ #include #include #include +#include +#include #include "FaceliftModel.h" #include "FaceliftUtils.h" @@ -64,6 +66,9 @@ namespace dbus { using namespace facelift; +class OutputPayLoad; +class InputPayLoad; + class FaceliftIPCLibDBus_EXPORT DBusIPCMessage { @@ -89,20 +94,9 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCMessage m_message = QDBusMessage::createSignal(path, interface, signal); } - DBusIPCMessage call(const QDBusConnection &connection) - { - qDebug() << "Sending IPC message : " << toString(); - auto replyDbusMessage = connection.call(m_message); - DBusIPCMessage reply(replyDbusMessage); - return reply; - } + DBusIPCMessage call(const QDBusConnection &connection); - void send(const QDBusConnection &connection) - { - qDebug() << "Sending IPC message : " << toString(); - bool successful = connection.send(m_message); - Q_ASSERT(successful); - } + void send(const QDBusConnection &connection); QString member() const { @@ -121,21 +115,6 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCMessage return DBusIPCMessage(m_message.createErrorReply(msg, member)); } - template - void readNextParameter(Type &v) - { - auto asVariant = m_message.arguments()[m_readPos++]; - // qDebug() << asVariant; - v = asVariant.value(); - } - - template - void writeSimple(const Type &v) - { - // qDebug() << "Writing to message : " << v; - msg() << v; - } - QString signature() const { return m_message.signature(); @@ -151,17 +130,77 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCMessage return (m_message.type() == QDBusMessage::ErrorMessage); } + OutputPayLoad& outputPayLoad(); + + InputPayLoad& inputPayLoad(); + private: - QDBusMessage &msg() + + QDBusMessage m_message; + std::unique_ptr m_outputPayload; + std::unique_ptr m_inputPayload; +}; + + +class OutputPayLoad { + +public: + OutputPayLoad() : m_dataStream(&m_payloadArray, QIODevice::WriteOnly) { - return m_message; } - QDBusMessage m_message; + template + void writeSimple(const Type &v) + { +// qDebug() << "Writing to message : " << v; + m_dataStream << v; + } - size_t m_readPos = 0; + const QByteArray& getContent() const { + return m_payloadArray; + } + +private: + QByteArray m_payloadArray; + QDataStream m_dataStream; +}; + +class InputPayLoad { + +public: + InputPayLoad(const QByteArray& payloadArray) : m_payloadArray(payloadArray), m_dataStream(m_payloadArray) + { + } + + template + void readNextParameter(Type &v) + { + m_dataStream >> v; +// qDebug() << "Read from message : " << v; + } + +private: + QByteArray m_payloadArray; + QDataStream m_dataStream; }; + +inline OutputPayLoad& DBusIPCMessage::outputPayLoad() { + if (m_outputPayload == nullptr) { + m_outputPayload = std::make_unique(); + } + return *m_outputPayload; +} + +inline InputPayLoad& DBusIPCMessage::inputPayLoad() { + if (m_inputPayload == nullptr) { + auto byteArray = m_message.arguments()[0].value(); + m_inputPayload = std::make_unique(byteArray); + } + return *m_inputPayload; +} + + class DBusIPCServiceAdapterBase; @@ -174,12 +213,12 @@ struct IPCTypeHandler s << "i"; } - static void write(DBusIPCMessage &msg, const Type &v) + static void write(OutputPayLoad &msg, const Type &v) { msg.writeSimple(v); } - static void read(DBusIPCMessage &msg, Type &v) + static void read(InputPayLoad &msg, Type &v) { msg.readNextParameter(v); } @@ -195,12 +234,12 @@ struct IPCTypeHandler s << "d"; } - static void write(DBusIPCMessage &msg, const float &v) + static void write(OutputPayLoad &msg, const float &v) { msg.writeSimple((double)v); } - static void read(DBusIPCMessage &msg, float &v) + static void read(InputPayLoad &msg, float &v) { double d; msg.readNextParameter(d); @@ -218,12 +257,12 @@ struct IPCTypeHandler s << "b"; } - static void write(DBusIPCMessage &msg, const bool &v) + static void write(OutputPayLoad &msg, const bool &v) { msg.writeSimple(v); } - static void read(DBusIPCMessage &msg, bool &v) + static void read(InputPayLoad &msg, bool &v) { msg.readNextParameter(v); } @@ -239,12 +278,12 @@ struct IPCTypeHandler s << "s"; } - static void write(DBusIPCMessage &msg, const QString &v) + static void write(OutputPayLoad &msg, const QString &v) { msg.writeSimple(v); } - static void read(DBusIPCMessage &msg, QString &v) + static void read(InputPayLoad &msg, QString &v) { msg.readNextParameter(v); } @@ -300,17 +339,17 @@ struct IPCTypeHandler(msg)); + for_each_in_tuple_const(param.asTuple(), StreamWriteFunction(msg)); param.id(); msg << param.id(); } - static void read(DBusIPCMessage &msg, Type ¶m) + static void read(InputPayLoad &msg, Type ¶m) { typename Type::FieldTupleTypes tuple; - for_each_in_tuple(tuple, StreamReadFunction(msg)); + for_each_in_tuple(tuple, StreamReadFunction(msg)); param.setValue(tuple); ModelElementID id; msg.readNextParameter(id); @@ -328,12 +367,12 @@ struct IPCTypeHandler::value>:: s << "i"; } - static void write(DBusIPCMessage &msg, const Type ¶m) + static void write(OutputPayLoad &msg, const Type ¶m) { msg.writeSimple(static_cast(param)); } - static void read(DBusIPCMessage &msg, Type ¶m) + static void read(InputPayLoad &msg, Type ¶m) { int i; msg.readNextParameter(i); @@ -351,7 +390,7 @@ struct IPCTypeHandler > IPCTypeHandler::writeDBUSSignature(s); } - static void write(DBusIPCMessage &msg, const QList &list) + static void write(OutputPayLoad &msg, const QList &list) { int count = list.size(); msg.writeSimple(count); @@ -360,7 +399,7 @@ struct IPCTypeHandler > } } - static void read(DBusIPCMessage &msg, QList &list) + static void read(InputPayLoad &msg, QList &list) { list.clear(); int count; @@ -384,7 +423,7 @@ struct IPCTypeHandler > IPCTypeHandler::writeDBUSSignature(s); } - static void write(DBusIPCMessage &msg, const QMap &map) + static void write(OutputPayLoad &msg, const QMap &map) { int count = map.size(); msg.writeSimple(count); @@ -394,7 +433,7 @@ struct IPCTypeHandler > } } - static void read(DBusIPCMessage &msg, QMap &map) + static void read(InputPayLoad &msg, QMap &map) { map.clear(); int count; @@ -458,7 +497,7 @@ appendDBUSSignalArgumentsSignature(QTextStream &s, std::tuple &t, const template -DBusIPCMessage &operator<<(DBusIPCMessage &msg, const Type &v) +OutputPayLoad &operator<<(OutputPayLoad &msg, const Type &v) { IPCTypeHandler::write(msg, v); return msg; @@ -466,7 +505,7 @@ DBusIPCMessage &operator<<(DBusIPCMessage &msg, const Type &v) template -DBusIPCMessage &operator>>(DBusIPCMessage &msg, Type &v) +InputPayLoad &operator>>(InputPayLoad &msg, Type &v) { IPCTypeHandler::read(msg, v); return msg; @@ -474,7 +513,7 @@ DBusIPCMessage &operator>>(DBusIPCMessage &msg, Type &v) template -DBusIPCMessage &operator>>(DBusIPCMessage &msg, Property &property) +InputPayLoad &operator>>(InputPayLoad &msg, Property &property) { Type v; IPCTypeHandler::read(msg, v); @@ -578,13 +617,13 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCServiceAdapterBase : public IPCServiceAda struct SerializeParameterFunction { - SerializeParameterFunction(DBusIPCMessage &msg, DBusIPCServiceAdapterBase &parent) : + SerializeParameterFunction(OutputPayLoad &msg, DBusIPCServiceAdapterBase &parent) : m_msg(msg), m_parent(parent) { } - DBusIPCMessage &m_msg; + OutputPayLoad &m_msg; DBusIPCServiceAdapterBase &m_parent; template @@ -600,7 +639,7 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCServiceAdapterBase : public IPCServiceAda void serializeValue(DBusIPCMessage &msg, const Type &v) { typedef typename IPCTypeRegisterHandler::SerializedType SerializedType; - IPCTypeHandler::write(msg, IPCTypeRegisterHandler::convertToSerializedType(v, *this)); + IPCTypeHandler::write(msg.outputPayLoad(), IPCTypeRegisterHandler::convertToSerializedType(v, *this)); } template @@ -608,7 +647,7 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCServiceAdapterBase : public IPCServiceAda { typedef typename IPCTypeRegisterHandler::SerializedType SerializedType; SerializedType serializedValue; - IPCTypeHandler::read(msg, serializedValue); + IPCTypeHandler::read(msg.inputPayLoad(), serializedValue); IPCTypeRegisterHandler::convertToDeserializedType(v, serializedValue, *this); } @@ -618,7 +657,7 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCServiceAdapterBase : public IPCServiceAda DBusIPCMessage msg(objectPath(), interfaceName(), SIGNAL_TRIGGERED_SIGNAL_NAME); serializeValue(msg, signalName); auto argTuple = std::make_tuple(args ...); - for_each_in_tuple(argTuple, SerializeParameterFunction(msg, *this)); + for_each_in_tuple(argTuple, SerializeParameterFunction(msg.outputPayLoad(), *this)); msg.send(dbusManager().connection()); } @@ -830,7 +869,7 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCProxyBinder : public IPCProxyBinderBase void serializeValue(DBusIPCMessage &msg, const Type &v) { typedef typename IPCTypeRegisterHandler::SerializedType SerializedType; - IPCTypeHandler::write(msg, IPCTypeRegisterHandler::convertToSerializedType(v, *this)); + IPCTypeHandler::write(msg.outputPayLoad(), IPCTypeRegisterHandler::convertToSerializedType(v, *this)); } template @@ -871,7 +910,7 @@ class FaceliftIPCLibDBus_EXPORT DBusIPCProxyBinder : public IPCProxyBinderBase template void operator()(const Type &v) { - IPCTypeHandler::SerializedType>::write(m_msg, + IPCTypeHandler::SerializedType>::write(m_msg.outputPayLoad(), IPCTypeRegisterHandler::convertToSerializedType(v, m_parent)); } }; @@ -960,7 +999,7 @@ class DBusIPCProxy : public IPCProxyBase, protected { typedef typename IPCTypeRegisterHandler::SerializedType SerializedType; SerializedType serializedValue; - IPCTypeHandler::read(msg, serializedValue); + IPCTypeHandler::read(msg.inputPayLoad(), serializedValue); IPCTypeRegisterHandler::convertToDeserializedType(v, serializedValue, *this); }