1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017-2015 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 QCONNECTIONFACTORIES_P_H |
41 | #define QCONNECTIONFACTORIES_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 <QtNetwork/qabstractsocket.h> |
55 | #include <QtCore/qdatastream.h> |
56 | #include <QtCore/qiodevice.h> |
57 | #include <QtCore/qpointer.h> |
58 | |
59 | #include <QtRemoteObjects/qtremoteobjectglobal.h> |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | namespace QtRemoteObjects { |
64 | |
65 | static const int dataStreamVersion = QDataStream::Qt_5_12; |
66 | static const QLatin1String protocolVersion("QtRO 1.3" ); |
67 | |
68 | } |
69 | |
70 | class Q_REMOTEOBJECTS_EXPORT IoDeviceBase : public QObject |
71 | { |
72 | Q_OBJECT |
73 | Q_DISABLE_COPY(IoDeviceBase) |
74 | |
75 | public: |
76 | explicit IoDeviceBase(QObject *parent = nullptr); |
77 | ~IoDeviceBase() override; |
78 | |
79 | bool read(QtRemoteObjects::QRemoteObjectPacketTypeEnum &, QString &); |
80 | |
81 | virtual void write(const QByteArray &data); |
82 | virtual void write(const QByteArray &data, qint64); |
83 | virtual bool isOpen() const { return !isClosing(); } |
84 | virtual void close(); |
85 | virtual qint64 bytesAvailable() const; |
86 | virtual QIODevice *connection() const = 0; |
87 | void initializeDataStream(); |
88 | QDataStream& stream() { return m_dataStream; } |
89 | inline bool isClosing() const { return m_isClosing; } |
90 | void addSource(const QString &); |
91 | void removeSource(const QString &); |
92 | QSet<QString> remoteObjects() const; |
93 | |
94 | Q_SIGNALS: |
95 | void readyRead(); |
96 | void disconnected(); |
97 | |
98 | protected: |
99 | virtual QString deviceType() const = 0; |
100 | virtual void doClose() = 0; |
101 | bool m_isClosing; |
102 | |
103 | private: |
104 | quint32 m_curReadSize; |
105 | QDataStream m_dataStream; |
106 | QSet<QString> m_remoteObjects; |
107 | }; |
108 | |
109 | class Q_REMOTEOBJECTS_EXPORT ServerIoDevice : public IoDeviceBase |
110 | { |
111 | Q_OBJECT |
112 | Q_DISABLE_COPY(ServerIoDevice) |
113 | |
114 | public: |
115 | explicit ServerIoDevice(QObject *parent = nullptr); |
116 | |
117 | protected: |
118 | QString deviceType() const override; |
119 | }; |
120 | |
121 | class Q_REMOTEOBJECTS_EXPORT QConnectionAbstractServer : public QObject |
122 | { |
123 | Q_OBJECT |
124 | Q_DISABLE_COPY(QConnectionAbstractServer) |
125 | |
126 | public: |
127 | explicit QConnectionAbstractServer(QObject *parent = nullptr); |
128 | ~QConnectionAbstractServer() override; |
129 | |
130 | virtual bool hasPendingConnections() const = 0; |
131 | ServerIoDevice* nextPendingConnection(); |
132 | virtual QUrl address() const = 0; |
133 | virtual bool listen(const QUrl &address) = 0; |
134 | virtual QAbstractSocket::SocketError serverError() const = 0; |
135 | virtual void close() = 0; |
136 | |
137 | protected: |
138 | virtual ServerIoDevice* configureNewConnection() = 0; |
139 | |
140 | Q_SIGNALS: |
141 | void newConnection(); |
142 | }; |
143 | |
144 | class Q_REMOTEOBJECTS_EXPORT ClientIoDevice : public IoDeviceBase |
145 | { |
146 | Q_OBJECT |
147 | Q_DISABLE_COPY(ClientIoDevice) |
148 | |
149 | public: |
150 | explicit ClientIoDevice(QObject *parent = nullptr); |
151 | ~ClientIoDevice() override; |
152 | |
153 | void disconnectFromServer(); |
154 | virtual void connectToServer() = 0; |
155 | |
156 | QUrl url() const; |
157 | |
158 | Q_SIGNALS: |
159 | void shouldReconnect(ClientIoDevice*); |
160 | |
161 | protected: |
162 | virtual void doDisconnectFromServer() = 0; |
163 | QString deviceType() const override; |
164 | |
165 | private: |
166 | friend class QtROClientFactory; |
167 | |
168 | QUrl m_url; |
169 | }; |
170 | |
171 | class ExternalIoDevice : public IoDeviceBase |
172 | { |
173 | Q_OBJECT |
174 | |
175 | public: |
176 | explicit ExternalIoDevice(QIODevice *device, QObject *parent=nullptr); |
177 | QIODevice *connection() const override; |
178 | bool isOpen() const override; |
179 | |
180 | protected: |
181 | void doClose() override; |
182 | QString deviceType() const override; |
183 | QPointer<QIODevice> m_device; |
184 | }; |
185 | |
186 | class QtROServerFactory |
187 | { |
188 | public: |
189 | Q_REMOTEOBJECTS_EXPORT static QtROServerFactory *instance(); |
190 | |
191 | QConnectionAbstractServer *create(const QUrl &url, QObject *parent = nullptr) |
192 | { |
193 | auto creatorFunc = m_creatorFuncs.value(akey: url.scheme()); |
194 | return creatorFunc ? (*creatorFunc)(parent) : nullptr; |
195 | } |
196 | |
197 | template<typename T> |
198 | void registerType(const QString &id) |
199 | { |
200 | m_creatorFuncs[id] = [](QObject *parent) -> QConnectionAbstractServer * { |
201 | return new T(parent); |
202 | }; |
203 | } |
204 | |
205 | bool isValid(const QUrl &url) |
206 | { |
207 | return m_creatorFuncs.contains(akey: url.scheme()); |
208 | } |
209 | |
210 | private: |
211 | friend class QtROFactoryLoader; |
212 | QtROServerFactory(); |
213 | |
214 | using CreatorFunc = QConnectionAbstractServer * (*)(QObject *); |
215 | QHash<QString, CreatorFunc> m_creatorFuncs; |
216 | }; |
217 | |
218 | class QtROClientFactory |
219 | { |
220 | public: |
221 | Q_REMOTEOBJECTS_EXPORT static QtROClientFactory *instance(); |
222 | |
223 | /// creates an object from a string |
224 | ClientIoDevice *create(const QUrl &url, QObject *parent = nullptr) |
225 | { |
226 | auto creatorFunc = m_creatorFuncs.value(akey: url.scheme()); |
227 | if (!creatorFunc) |
228 | return nullptr; |
229 | |
230 | ClientIoDevice *res = (*creatorFunc)(parent); |
231 | if (res) |
232 | res->m_url = url; |
233 | return res; |
234 | } |
235 | |
236 | template<typename T> |
237 | void registerType(const QString &id) |
238 | { |
239 | m_creatorFuncs[id] = [](QObject *parent) -> ClientIoDevice * { |
240 | return new T(parent); |
241 | }; |
242 | } |
243 | |
244 | bool isValid(const QUrl &url) |
245 | { |
246 | return m_creatorFuncs.contains(akey: url.scheme()); |
247 | } |
248 | |
249 | private: |
250 | friend class QtROFactoryLoader; |
251 | QtROClientFactory(); |
252 | |
253 | using CreatorFunc = ClientIoDevice * (*)(QObject *); |
254 | QHash<QString, CreatorFunc> m_creatorFuncs; |
255 | }; |
256 | |
257 | template <typename T> |
258 | inline void qRegisterRemoteObjectsClient(const QString &id) |
259 | { |
260 | QtROClientFactory::instance()->registerType<T>(id); |
261 | } |
262 | |
263 | template <typename T> |
264 | inline void qRegisterRemoteObjectsServer(const QString &id) |
265 | { |
266 | QtROServerFactory::instance()->registerType<T>(id); |
267 | } |
268 | |
269 | QT_END_NAMESPACE |
270 | |
271 | #endif |
272 | |