/******************************************************************************
**
** 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 <typename T>
T QMqttConnection::readBufferTyped(qint64 *dataSize)
{
Q_STATIC_ASSERT(std::is_integral<T>::value);
T result;
readBuffer(reinterpret_cast<char *>(&result), sizeof(result));
if (dataSize != nullptr)
*dataSize -= sizeof(result);
return qFromBigEndian(result);
}
template<>
QByteArray QMqttConnection::readBufferTyped(qint64 *dataSize)
{
const quint16 size = readBufferTyped<quint16>(dataSize);
QByteArray ba(int(size), Qt::Uninitialized);
readBuffer(ba.data(), size);
if (dataSize)
*dataSize -= size;
return ba;
}
template<>
QString QMqttConnection::readBufferTyped(qint64 *dataSize)
{
return QString::fromUtf8(readBufferTyped<QByteArray>(dataSize));
}
QMqttConnection::QMqttConnection(QObject *parent) : QObject(parent)
{
}
QMqttConnection::~QMqttConnection()
{
if (m_internalState == BrokerConnected)
sendControlDisconnect();
if (m_ownTransport && m_transport)
delete m_transport;
}
void QMqttConnection::timerEvent(QTimerEvent *event)
{
if (Q_LIKELY(event->timerId() == m_pingTimer.timerId())) {
sendControlPingRequest();
return;
}
QObject::timerEvent(event);
}
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::transportReadyRead);
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::transportReadyRead);
}
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;
#ifndef QT_NO_SSL
if (QSslSocket *sslSocket = qobject_cast<QSslSocket *>(socket))
QObject::connect(sslSocket, &QSslSocket::encrypted, this, &QMqttConnection::transportConnectionEstablished);
else
#endif
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::transportReadyRead);
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 = qobject_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 = qobject_cast<QSslSocket *>(m_transport);
Q_ASSERT(socket);
if (socket->state() == QAbstractSocket::ConnectedState)
return sendControlConnect();
m_internalState = BrokerConnecting;
if (!m_sslConfiguration.isNull())
socket->setSslConfiguration(m_sslConfiguration);
socket->connectToHostEncrypted(m_clientPrivate->m_hostname, m_clientPrivate->m_port, sslPeerName);
}
#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 QMqttClient::MQTT_3_1_1:
packet.append("MQTT");
packet.append(char(4)); // Version 3.1.1
break;
case QMqttClient::MQTT_5_0:
packet.append("MQTT");
packet.append(char(5)); // Version 5.0
break;
}
// 3.1.2.3 Connect Flags
quint8 flags = 0;
// Clean session
if (m_clientPrivate->m_cleanSession)
flags |= 1 << 1;
if (!m_clientPrivate-
没有合适的资源?快使用搜索试试~ 我知道了~
mqtt_client.zip
共97个文件
h:37个
cpp:26个
qdoc:8个
需积分: 49 96 下载量 173 浏览量
2020-04-20
11:51:35
上传
评论 1
收藏 1.83MB ZIP 举报
温馨提示
基于Qt实现MQTT客户端通信示例,实现了客户端之间的订阅、发布消息、接收消息的功能。mqtt目前主要有两个版本,一个是第三方库,一个是qt官方库,两者都需要自己下载源码生成库,另外在方法接口、函数的使用上还是有些区别,注意千万不要弄混了,我当时就是把方法弄混了折腾了好久。。。。
资源推荐
资源详情
资源评论
收起资源包目录
mqtt_client.zip (97个子文件)
mqtt_client
mainwindow.ui 10KB
mqtt_client.pro 2KB
mqtt_client.pro.user 22KB
src
src.pro 35B
mqtt
qmqttpublishproperties.h 4KB
qmqttconnection_p.h 6KB
qmqttpublishproperties.cpp 9KB
qmqttmessage_p.h 2KB
qmqttpublishproperties_p.h 2KB
qmqttauthenticationproperties.h 2KB
qmqtttopicfilter.h 4KB
qmqtttopicname.cpp 6KB
qmqttconnection.cpp 73KB
mqtt.pro 1KB
qmqttclient_p.h 3KB
qmqttconnectionproperties_p.h 3KB
qmqtttopicname.h 3KB
qmqttglobal.h 3KB
qmqttclient.h 9KB
qmqtttopicfilter.cpp 10KB
qmqttsubscriptionproperties.cpp 5KB
doc
style
style.css 2KB
src
module.qdoc 2KB
index.qdoc 3KB
overview.qdoc 9KB
external-resources.qdoc 1KB
images
mqtt.png 9KB
qtmqtt.qdocconf 2KB
qmqttsubscription.h 3KB
qmqtttype.h 2KB
qmqttconnectionproperties.cpp 18KB
qmqttsubscription.cpp 6KB
qmqttmessage.cpp 5KB
qmqttconnectionproperties.h 6KB
qmqttmessage.h 3KB
qmqttauthenticationproperties.cpp 4KB
qmqttsubscriptionproperties.h 3KB
qmqttcontrolpacket_p.h 3KB
qmqttcontrolpacket.cpp 4KB
qmqttsubscription_p.h 2KB
qmqtttype.cpp 13KB
qmqttclient.cpp 30KB
include
QtMqtt
QtMqttDepends 204B
src.pro.user 22KB
mainwindow.cpp 5KB
lib
Qt5Mqtt.dll 185KB
Qt5Mqttd.dll 5.88MB
Qt5Mqttd.prl 1KB
Qt5Mqtt.prl 1KB
cmake
Qt5Mqtt
ExtraSourceIncludes.cmake 281B
Qt5MqttConfig.cmake 7KB
Qt5MqttConfigVersion.cmake 299B
pkgconfig
Qt5Mqtt.pc 277B
libQt5Mqtt.a 243KB
libQt5Mqttd.a 243KB
main.cpp 183B
mqtt
qmqttpublishproperties.h 4KB
qmqttconnection_p.h 6KB
qmqttpublishproperties.cpp 9KB
qmqttmessage_p.h 2KB
qmqttpublishproperties_p.h 2KB
qmqttauthenticationproperties.h 2KB
qmqtttopicfilter.h 4KB
qmqtttopicname.cpp 6KB
qmqttconnection.cpp 73KB
mqtt.pro 1KB
qmqttclient_p.h 3KB
qmqttconnectionproperties_p.h 3KB
qmqtttopicname.h 3KB
qmqttglobal.h 3KB
qmqttclient.h 9KB
qmqtttopicfilter.cpp 10KB
qmqttsubscriptionproperties.cpp 5KB
doc
style
style.css 2KB
src
module.qdoc 2KB
index.qdoc 3KB
overview.qdoc 9KB
external-resources.qdoc 1KB
images
mqtt.png 9KB
qtmqtt.qdocconf 2KB
qmqttsubscription.h 3KB
qmqtttype.h 2KB
qmqttconnectionproperties.cpp 18KB
qmqttsubscription.cpp 6KB
qmqttmessage.cpp 5KB
qmqttconnectionproperties.h 6KB
qmqttmessage.h 3KB
qmqttauthenticationproperties.cpp 4KB
qmqttsubscriptionproperties.h 3KB
qmqttcontrolpacket_p.h 3KB
qmqttcontrolpacket.cpp 4KB
qmqttsubscription_p.h 2KB
qmqtttype.cpp 13KB
qmqttclient.cpp 30KB
include
QtMqtt
QtMqttDepends 204B
mainwindow.h 827B
include
QtMqtt
QtMqttDepends 204B
共 97 条
- 1
资源评论
每木昔月
- 粉丝: 329
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
- 音乐展示网页、基于Stenography的图像数字水印添加与提取,以及基于颜色矩和Tamura算法的图像相似度评估算法py源码
- 基于EmguCV(OpenCV .net封装),图像数字水印加解密算法的实现,其中包含最低有效位算法,离散傅里叶变换算法+文档书
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功