qmqtt_network.cpp
8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
#include "qmqtt_network_p.h"
#include "qmqtt_socket_p.h"
#include "qmqtt_ssl_socket_p.h"
#include "qmqtt_timer_p.h"
#include "qmqtt_websocket_p.h"
#include "qmqtt_frame.h"
#include <QDataStream>
const quint16 DEFAULT_PORT = 1883;
const quint16 DEFAULT_SSL_PORT = 8883;
const bool DEFAULT_AUTORECONNECT = false;
const int DEFAULT_AUTORECONNECT_INTERVAL_MS = 5000;
QMQTT::Network::Network(QObject* parent)
: NetworkInterface(parent)
, _port(DEFAULT_PORT)
, _autoReconnect(DEFAULT_AUTORECONNECT)
, _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS)
, _socket(new QMQTT::Socket)
, _autoReconnectTimer(new QMQTT::Timer)
, _readState(Header)
{
initialize();
}
#ifndef QT_NO_SSL
QMQTT::Network::Network(const QSslConfiguration& config, QObject *parent)
: NetworkInterface(parent)
, _port(DEFAULT_SSL_PORT)
, _autoReconnect(DEFAULT_AUTORECONNECT)
, _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS)
, _socket(new QMQTT::SslSocket(config))
, _autoReconnectTimer(new QMQTT::Timer)
, _readState(Header)
{
initialize();
connect(_socket, &QMQTT::SslSocket::sslErrors, this, &QMQTT::Network::sslErrors);
}
#endif // QT_NO_SSL
#ifdef QT_WEBSOCKETS_LIB
#ifndef QT_NO_SSL
QMQTT::Network::Network(const QString& origin,
QWebSocketProtocol::Version version,
const QSslConfiguration* sslConfig,
QObject* parent)
: NetworkInterface(parent)
, _port(DEFAULT_SSL_PORT)
, _autoReconnect(DEFAULT_AUTORECONNECT)
, _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS)
, _socket(new QMQTT::WebSocket(origin, version, sslConfig))
, _autoReconnectTimer(new QMQTT::Timer)
, _readState(Header)
{
initialize();
}
#endif // QT_NO_SSL
QMQTT::Network::Network(const QString& origin,
QWebSocketProtocol::Version version,
QObject* parent)
: NetworkInterface(parent)
, _port(DEFAULT_PORT)
, _autoReconnect(DEFAULT_AUTORECONNECT)
, _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS)
, _socket(new QMQTT::WebSocket(origin, version))
, _autoReconnectTimer(new QMQTT::Timer)
, _readState(Header)
{
initialize();
}
#endif // QT_WEBSOCKETS_LIB
QMQTT::Network::Network(SocketInterface* socketInterface, TimerInterface* timerInterface,
QObject* parent)
: NetworkInterface(parent)
, _port(DEFAULT_PORT)
, _autoReconnect(DEFAULT_AUTORECONNECT)
, _autoReconnectInterval(DEFAULT_AUTORECONNECT_INTERVAL_MS)
, _socket(socketInterface)
, _autoReconnectTimer(timerInterface)
, _readState(Header)
{
initialize();
}
void QMQTT::Network::initialize()
{
_socket->setParent(this);
_autoReconnectTimer->setParent(this);
_autoReconnectTimer->setSingleShot(true);
_autoReconnectTimer->setInterval(_autoReconnectInterval);
QObject::connect(_socket, &SocketInterface::connected, this, &Network::connected);
QObject::connect(_socket, &SocketInterface::disconnected, this, &Network::onDisconnected);
QObject::connect(_socket->ioDevice(), &QIODevice::readyRead, this, &Network::onSocketReadReady);
QObject::connect(
_autoReconnectTimer, &TimerInterface::timeout,
this, static_cast<void (Network::*)()>(&Network::connectToHost));
QObject::connect(_socket,
static_cast<void (SocketInterface::*)(QAbstractSocket::SocketError)>(&SocketInterface::error),
this, &Network::onSocketError);
}
QMQTT::Network::~Network()
{
}
bool QMQTT::Network::isConnectedToHost() const
{
return _socket->state() == QAbstractSocket::ConnectedState;
}
void QMQTT::Network::connectToHost(const QHostAddress& host, const quint16 port)
{
// Reset the hostname, because if it is not empty connectToHost() will use it instead of _host.
_hostName.clear();
_host = host;
_port = port;
connectToHost();
}
void QMQTT::Network::connectToHost(const QString& hostName, const quint16 port)
{
_hostName = hostName;
_port = port;
connectToHost();
}
void QMQTT::Network::connectToHost()
{
_readState = Header;
if (_hostName.isEmpty())
{
_socket->connectToHost(_host, _port);
}
else
{
_socket->connectToHost(_hostName, _port);
}
}
void QMQTT::Network::onSocketError(QAbstractSocket::SocketError socketError)
{
emit error(socketError);
if(_autoReconnect)
{
_autoReconnectTimer->start();
}
}
void QMQTT::Network::sendFrame(const Frame& frame)
{
if(_socket->state() == QAbstractSocket::ConnectedState)
{
QDataStream out(_socket->ioDevice());
frame.write(out);
}
}
void QMQTT::Network::disconnectFromHost()
{
_socket->disconnectFromHost();
}
QAbstractSocket::SocketState QMQTT::Network::state() const
{
return _socket->state();
}
bool QMQTT::Network::autoReconnect() const
{
return _autoReconnect;
}
void QMQTT::Network::setAutoReconnect(const bool autoReconnect)
{
_autoReconnect = autoReconnect;
}
int QMQTT::Network::autoReconnectInterval() const
{
return _autoReconnectInterval;
}
void QMQTT::Network::setAutoReconnectInterval(const int autoReconnectInterval)
{
_autoReconnectInterval = autoReconnectInterval;
_autoReconnectTimer->setInterval(_autoReconnectInterval);
}
void QMQTT::Network::onSocketReadReady()
{
QIODevice *ioDevice = _socket->ioDevice();
// Only read the available (cached) bytes, so the read will never block.
QByteArray data = ioDevice->read(ioDevice->bytesAvailable());
foreach(char byte, data) {
switch (_readState) {
case Header:
_header = static_cast<quint8>(byte);
_readState = Length;
_length = 0;
_shift = 0;
_data.resize(0); // keep allocated buffer
break;
case Length:
_length |= (byte & 0x7F) << _shift;
_shift += 7;
if ((byte & 0x80) != 0)
break;
if (_length == 0) {
_readState = Header;
Frame frame(_header, _data);
emit received(frame);
break;
}
_readState = PayLoad;
_data.reserve(_length);
break;
case PayLoad:
_data.append(byte);
--_length;
if (_length > 0)
break;
_readState = Header;
Frame frame(_header, _data);
emit received(frame);
break;
}
}
}
void QMQTT::Network::onDisconnected()
{
emit disconnected();
if(_autoReconnect)
{
_autoReconnectTimer->start();
}
}
#ifndef QT_NO_SSL
void QMQTT::Network::ignoreSslErrors(const QList<QSslError>& errors)
{
_socket->ignoreSslErrors(errors);
}
void QMQTT::Network::ignoreSslErrors()
{
_socket->ignoreSslErrors();
}
QSslConfiguration QMQTT::Network::sslConfiguration() const
{
return _socket->sslConfiguration();
}
void QMQTT::Network::setSslConfiguration(const QSslConfiguration& config)
{
_socket->setSslConfiguration(config);
}
#endif // QT_NO_SSL