| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Ford Motor Company |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtRemoteObjects module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QTREMOTEOBJECTPACKET_P_H |
| 41 | #define QTREMOTEOBJECTPACKET_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include "qtremoteobjectglobal.h" |
| 55 | #include "qremoteobjectsource.h" |
| 56 | #include "qconnectionfactories_p.h" |
| 57 | |
| 58 | #include <QtCore/qhash.h> |
| 59 | #include <QtCore/qmap.h> |
| 60 | #include <QtCore/qpair.h> |
| 61 | #include <QtCore/qurl.h> |
| 62 | #include <QtCore/qvariant.h> |
| 63 | #include <QtCore/qloggingcategory.h> |
| 64 | |
| 65 | #include <cstdlib> |
| 66 | |
| 67 | QT_BEGIN_NAMESPACE |
| 68 | |
| 69 | class QMetaObjectBuilder; |
| 70 | class QRemoteObjectSourceBase; |
| 71 | class QRemoteObjectRootSource; |
| 72 | |
| 73 | namespace QRemoteObjectPackets { |
| 74 | |
| 75 | Q_NAMESPACE |
| 76 | |
| 77 | class DataStreamPacket; |
| 78 | |
| 79 | struct ObjectInfo |
| 80 | { |
| 81 | QString name; |
| 82 | QString typeName; |
| 83 | QByteArray signature; |
| 84 | }; |
| 85 | |
| 86 | inline QDebug operator<<(QDebug dbg, const ObjectInfo &info) |
| 87 | { |
| 88 | dbg.nospace() << "ObjectInfo(" << info.name << ", " << info.typeName << ", " << info.signature <<")" ; |
| 89 | return dbg.space(); |
| 90 | } |
| 91 | |
| 92 | inline QDataStream& operator<<(QDataStream &stream, const ObjectInfo &info) |
| 93 | { |
| 94 | return stream << info.name << info.typeName << info.signature; |
| 95 | } |
| 96 | |
| 97 | inline QDataStream& operator>>(QDataStream &stream, ObjectInfo &info) |
| 98 | { |
| 99 | return stream >> info.name >> info.typeName >> info.signature; |
| 100 | } |
| 101 | |
| 102 | typedef QVector<ObjectInfo> ObjectInfoList; |
| 103 | |
| 104 | enum class ObjectType : quint8 { CLASS, MODEL, GADGET }; |
| 105 | Q_ENUM_NS(ObjectType) |
| 106 | |
| 107 | // Use a short name, as QVariant::save writes the name every time a qvariant of |
| 108 | // this type is serialized |
| 109 | class QRO_ |
| 110 | { |
| 111 | public: |
| 112 | QRO_() : type(ObjectType::CLASS), isNull(true) {} |
| 113 | explicit QRO_(QRemoteObjectSourceBase *source); |
| 114 | explicit QRO_(const QVariant &value); |
| 115 | QString name, typeName; |
| 116 | ObjectType type; |
| 117 | bool isNull; |
| 118 | QByteArray classDefinition; |
| 119 | QByteArray parameters; |
| 120 | }; |
| 121 | |
| 122 | inline QDebug operator<<(QDebug dbg, const QRO_ &info) |
| 123 | { |
| 124 | dbg.nospace() << "QRO_(name: " << info.name << ", typeName: " << info.typeName << ", type: " << info.type |
| 125 | << ", valid: " << (info.isNull ? "true" : "false" ) << ", paremeters: {" << info.parameters <<")" |
| 126 | << (info.classDefinition.isEmpty() ? " no definitions)" : " with definitions)" ); |
| 127 | return dbg.space(); |
| 128 | } |
| 129 | |
| 130 | QDataStream& operator<<(QDataStream &stream, const QRO_ &info); |
| 131 | |
| 132 | QDataStream& operator>>(QDataStream &stream, QRO_ &info); |
| 133 | |
| 134 | void serializeObjectListPacket(DataStreamPacket&, const ObjectInfoList&); |
| 135 | void deserializeObjectListPacket(QDataStream&, ObjectInfoList&); |
| 136 | |
| 137 | //Helper class for creating a QByteArray from a QRemoteObjectPacket |
| 138 | class DataStreamPacket : public QDataStream |
| 139 | { |
| 140 | public: |
| 141 | DataStreamPacket(quint16 id = QtRemoteObjects::InvokePacket) |
| 142 | : QDataStream(&array, QIODevice::WriteOnly) |
| 143 | , baseAddress(0) |
| 144 | , size(0) |
| 145 | { |
| 146 | this->setVersion(QtRemoteObjects::dataStreamVersion); |
| 147 | *this << quint32(0); |
| 148 | *this << id; |
| 149 | } |
| 150 | void setId(quint16 id) |
| 151 | { |
| 152 | device()->seek(pos: baseAddress); |
| 153 | *this << quint32(0); |
| 154 | *this << id; |
| 155 | } |
| 156 | |
| 157 | void finishPacket() |
| 158 | { |
| 159 | size = device()->pos(); |
| 160 | device()->seek(pos: baseAddress); |
| 161 | *this << quint32(size - baseAddress - sizeof(quint32)); |
| 162 | } |
| 163 | QByteArray array; |
| 164 | int baseAddress; |
| 165 | int size; |
| 166 | |
| 167 | private: |
| 168 | Q_DISABLE_COPY(DataStreamPacket) |
| 169 | }; |
| 170 | |
| 171 | const QVariant encodeVariant(const QVariant &value); |
| 172 | QVariant &decodeVariant(QVariant &value, int type); |
| 173 | |
| 174 | void serializeProperty(QDataStream &, const QRemoteObjectSourceBase *source, int internalIndex); |
| 175 | |
| 176 | void serializeHandshakePacket(DataStreamPacket &); |
| 177 | void serializeInitPacket(DataStreamPacket &, const QRemoteObjectRootSource*); |
| 178 | void serializeProperties(DataStreamPacket &, const QRemoteObjectSourceBase*); |
| 179 | void deserializeInitPacket(QDataStream &, QVariantList&); |
| 180 | |
| 181 | void serializeInitDynamicPacket(DataStreamPacket &, const QRemoteObjectRootSource*); |
| 182 | void serializeDefinition(QDataStream &, const QRemoteObjectSourceBase*); |
| 183 | |
| 184 | void serializeAddObjectPacket(DataStreamPacket &, const QString &name, bool isDynamic); |
| 185 | void deserializeAddObjectPacket(QDataStream &, bool &isDynamic); |
| 186 | |
| 187 | void serializeRemoveObjectPacket(DataStreamPacket&, const QString &name); |
| 188 | //There is no deserializeRemoveObjectPacket - no parameters other than id and name |
| 189 | |
| 190 | void serializeInvokePacket(DataStreamPacket&, const QString &name, int call, int index, const QVariantList &args, int serialId = -1, int propertyIndex = -1); |
| 191 | void deserializeInvokePacket(QDataStream& in, int &call, int &index, QVariantList &args, int &serialId, int &propertyIndex); |
| 192 | |
| 193 | void serializeInvokeReplyPacket(DataStreamPacket&, const QString &name, int ackedSerialId, const QVariant &value); |
| 194 | void deserializeInvokeReplyPacket(QDataStream& in, int &ackedSerialId, QVariant &value); |
| 195 | |
| 196 | //TODO do we need the object name or could we go with an id in backend code, this could be a costly allocation |
| 197 | void serializePropertyChangePacket(QRemoteObjectSourceBase *source, int signalIndex); |
| 198 | void deserializePropertyChangePacket(QDataStream& in, int &index, QVariant &value); |
| 199 | |
| 200 | // Heartbeat packets |
| 201 | void serializePingPacket(DataStreamPacket &ds, const QString &name); |
| 202 | void serializePongPacket(DataStreamPacket &ds, const QString &name); |
| 203 | |
| 204 | |
| 205 | } // namespace QRemoteObjectPackets |
| 206 | |
| 207 | QT_END_NAMESPACE |
| 208 | |
| 209 | Q_DECLARE_METATYPE(QRemoteObjectPackets::QRO_) |
| 210 | |
| 211 | #endif |
| 212 | |