/******************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtMqtt module.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
******************************************************************************/
#include "qmqttconnection_p.h"
#include "qmqttconnectionproperties_p.h"
#include "qmqttcontrolpacket_p.h"
#include "qmqttmessage_p.h"
#include "qmqttpublishproperties_p.h"
#include "qmqttsubscription_p.h"
#include "qmqttclient_p.h"
#include <QtCore/QLoggingCategory>
#include <QtNetwork/QSslSocket>
#include <QtNetwork/QTcpSocket>
#include <limits>
#include <cstdint>
QT_BEGIN_NAMESPACE
Q_LOGGING_CATEGORY(lcMqttConnection, "qt.mqtt.connection")
Q_LOGGING_CATEGORY(lcMqttConnectionVerbose, "qt.mqtt.connection.verbose");
template<>
quint32 QMqttConnection::readBufferTyped(qint64 *dataSize)
{
if (dataSize)
*dataSize -= sizeof(quint32);
return qFromBigEndian<quint32>(reinterpret_cast<const quint32 *>(readBuffer(4).constData()));
}
template<>
quint16 QMqttConnection::readBufferTyped(qint64 *dataSize)
{
if (dataSize)
*dataSize -= sizeof(quint16);
return qFromBigEndian<quint16>(reinterpret_cast<const quint16 *>(readBuffer(2).constData()));
}
template<>
quint8 QMqttConnection::readBufferTyped(qint64 *dataSize)
{
quint8 result;
readBuffer(reinterpret_cast<char *>(&result), 1);
if (dataSize)
*dataSize -= sizeof(quint8);
return result;
}
template<>
QString QMqttConnection::readBufferTyped(qint64 *dataSize)
{
const quint16 size = readBufferTyped<quint16>(dataSize);
if (dataSize)
*dataSize -= size;
const QByteArray ba = readBuffer(size);
return QString::fromUtf8(reinterpret_cast<const char *>(ba.constData()), ba.size());
}
template<>
QByteArray QMqttConnection::readBufferTyped(qint64 *dataSize)
{
const quint16 size = readBufferTyped<quint16>(dataSize);
if (dataSize)
*dataSize -= size;
return readBuffer(size);
}
QMqttConnection::QMqttConnection(QObject *parent) : QObject(parent)
{
m_pingTimer.setSingleShot(false);
m_pingTimer.connect(&m_pingTimer, &QTimer::timeout, this, &QMqttConnection::sendControlPingRequest);
}
QMqttConnection::~QMqttConnection()
{
if (m_internalState == BrokerConnected)
sendControlDisconnect();
if (m_ownTransport && m_transport)
delete m_transport;
}
void QMqttConnection::setTransport(QIODevice *device, QMqttClient::TransportType transport)
{
qCDebug(lcMqttConnection) << Q_FUNC_INFO << device << " Type:" << transport;
if (m_transport) {
disconnect(m_transport, &QIODevice::aboutToClose, this, &QMqttConnection::transportConnectionClosed);
disconnect(m_transport, &QIODevice::readyRead, this, &QMqttConnection::transportReadReady);
if (m_ownTransport)
delete m_transport;
}
m_transport = device;
m_transportType = transport;
m_ownTransport = false;
connect(m_transport, &QIODevice::aboutToClose, this, &QMqttConnection::transportConnectionClosed);
connect(m_transport, &QIODevice::readyRead, this, &QMqttConnection::transportReadReady);
}
QIODevice *QMqttConnection::transport() const
{
return m_transport;
}
bool QMqttConnection::ensureTransport(bool createSecureIfNeeded)
{
Q_UNUSED(createSecureIfNeeded); // QT_NO_SSL
qCDebug(lcMqttConnection) << Q_FUNC_INFO << m_transport;
if (m_transport) {
if (m_ownTransport)
delete m_transport;
else
return true;
}
// We are asked to create a transport layer
if (m_clientPrivate->m_hostname.isEmpty() || m_clientPrivate->m_port == 0) {
qCDebug(lcMqttConnection) << "No hostname specified, not able to create a transport layer.";
return false;
}
auto socket =
#ifndef QT_NO_SSL
createSecureIfNeeded ? new QSslSocket() :
#endif
new QTcpSocket();
m_transport = socket;
m_ownTransport = true;
m_transportType =
#ifndef QT_NO_SSL
createSecureIfNeeded ? QMqttClient::SecureSocket :
#endif
QMqttClient::AbstractSocket;
connect(socket, &QAbstractSocket::connected, this, &QMqttConnection::transportConnectionEstablished);
connect(socket, &QAbstractSocket::disconnected, this, &QMqttConnection::transportConnectionClosed);
connect(socket, static_cast<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error),
this, &QMqttConnection::transportError);
connect(m_transport, &QIODevice::aboutToClose, this, &QMqttConnection::transportConnectionClosed);
connect(m_transport, &QIODevice::readyRead, this, &QMqttConnection::transportReadReady);
return true;
}
bool QMqttConnection::ensureTransportOpen(const QString &sslPeerName)
{
qCDebug(lcMqttConnection) << Q_FUNC_INFO << m_transportType;
if (m_transportType == QMqttClient::IODevice) {
if (m_transport->isOpen())
return sendControlConnect();
if (!m_transport->open(QIODevice::ReadWrite)) {
qCDebug(lcMqttConnection) << "Could not open Transport IO device.";
m_internalState = BrokerDisconnected;
return false;
}
return sendControlConnect();
}
if (m_transportType == QMqttClient::AbstractSocket) {
auto socket = dynamic_cast<QTcpSocket*>(m_transport);
Q_ASSERT(socket);
if (socket->state() == QAbstractSocket::ConnectedState)
return sendControlConnect();
m_internalState = BrokerConnecting;
socket->connectToHost(m_clientPrivate->m_hostname, m_clientPrivate->m_port);
}
#ifndef QT_NO_SSL
else if (m_transportType == QMqttClient::SecureSocket) {
auto socket = dynamic_cast<QSslSocket*>(m_transport);
Q_ASSERT(socket);
if (socket->state() == QAbstractSocket::ConnectedState)
return sendControlConnect();
m_internalState = BrokerConnecting;
socket->connectToHostEncrypted(m_clientPrivate->m_hostname, m_clientPrivate->m_port, sslPeerName);
if (!socket->waitForConnected()) {
qCDebug(lcMqttConnection) << "Could not establish socket connection for transport.";
return false;
}
if (!socket->waitForEncrypted()) {
qCDebug(lcMqttConnection) << "Could not initiate encryption.";
return false;
}
}
#else
Q_UNUSED(sslPeerName);
#endif
return true;
}
bool QMqttConnection::sendControlConnect()
{
qCDebug(lcMqttConnection) << Q_FUNC_INFO;
QMqttControlPacket packet(QMqttControlPacket::CONNECT);
// Variable header
// 3.1.2.1 Protocol Name
// 3.1.2.2 Protocol Level
switch (m_clientPrivate->m_protocolVersion) {
case QMqttClient::MQTT_3_1:
packet.append("MQIsdp");
packet.append(char(3)); // Version 3.1
break;
case QMqttC
没有合适的资源?快使用搜索试试~ 我知道了~
qtmqtt客户端(支持ssl)亲测可用
共41个文件
h:20个
cpp:15个
a:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 2 下载量 50 浏览量
2023-03-05
09:00:54
上传
评论
收藏 1.67MB RAR 举报
温馨提示
qtmqtt客户端(支持ssl)亲测可用
资源推荐
资源详情
资源评论
收起资源包目录
qtmqtt_demo2.rar (41个子文件)
qtmqtt_demo2
mqtt
include
qmqttauthenticationproperties.h 2KB
qmqttpublishproperties.h 4KB
qmqttglobal.h 3KB
qmqtttopicfilter.h 4KB
qmqtttype.h 2KB
qmqttsubscription.h 3KB
qmqtttopicname.h 3KB
qmqttconnection_p.h 6KB
qmqttcontrolpacket_p.h 3KB
qmqttsubscription_p.h 2KB
qmqttmessage.h 3KB
qmqttsubscriptionproperties.h 3KB
qmqttclient_p.h 3KB
qmqttmessage_p.h 2KB
qmqttclient.h 8KB
qmqttconnectionproperties_p.h 3KB
qmqttpublishproperties_p.h 2KB
qmqttconnectionproperties.h 6KB
lib
libQt5Mqtt.a 241KB
libQt5Mqttd.a 241KB
src
qmqtttopicname.cpp 6KB
qmqttmessage.cpp 5KB
qmqttcontrolpacket.cpp 4KB
qmqttauthenticationproperties.cpp 4KB
qmqttpublishproperties.cpp 8KB
qmqtttopicfilter.cpp 10KB
qmqttsubscription.cpp 6KB
qmqttclient.cpp 28KB
qmqttsubscriptionproperties.cpp 5KB
qmqttconnection.cpp 68KB
qmqtttype.cpp 12KB
qmqttconnectionproperties.cpp 18KB
bin
Qt5Mqttd.dll 8.24MB
Qt5Mqtt.dll 180KB
mainwindow.h 3KB
mainwindow.cpp 7KB
aiot_mqtt_sign.h 259B
aiot_mqtt_sign.cpp 12KB
main.cpp 745B
mainwindow.ui 5KB
qtmqtt_demo.pro 792B
共 41 条
- 1
资源评论
- qq_381343652023-08-12发现一个宝藏资源,赶紧冲冲冲!支持大佬~
- 福福福福福2023-11-12资源很不错,内容和描述一致,值得借鉴,赶紧学起来!
不脱发的码农~~~~
- 粉丝: 9w+
- 资源: 186
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功