1
0
mirror of https://github.com/qt/qtmqtt.git synced 2026-10-25 16:16:26 +00:00

Add publish command

Only works for very simple publishes so far, no QoS verification or
such.
This commit is contained in:
Maurice Kalinowski 2017-01-20 09:41:40 +01:00
parent 3b111f8a18
commit 6b771e7dd2
4 changed files with 35 additions and 11 deletions

View File

@ -72,6 +72,13 @@ void QMqttClient::unsubscribe(const QString &topic)
Q_UNUSED(topic);
}
bool QMqttClient::publish(const QString &topic, const QString &message)
{
Q_UNUSED(message);
Q_D(QMqttClient);
return d->m_connection.sendControlPublish(topic, message);
}
QString QMqttClient::hostname() const
{
Q_D(const QMqttClient);

View File

@ -69,7 +69,7 @@ public:
bool subscribe(const QString& topic);
void unsubscribe(const QString& topic);
//bool publish(const QString& message);
bool publish(const QString &topic, const QString& message = QString());
QString hostname() const;
quint16 port() const;

View File

@ -153,9 +153,7 @@ bool QMqttConnection::sendControlConnect()
}
}
const QByteArray send = packet.serialize();
const qint64 res = m_transport->write(send.constData(), send.size());
if (Q_UNLIKELY(res == -1)) {
if (!writePacketToTransport(packet)) {
qWarning("Could not write CONNECT frame to transport");
return false;
}
@ -164,10 +162,18 @@ bool QMqttConnection::sendControlConnect()
return true;
}
bool QMqttConnection::sendControlPublish()
bool QMqttConnection::sendControlPublish(const QString &topic, const QString &message)
{
Q_UNIMPLEMENTED();
return false;
// ### TODO: DUP, QOS, RETAIN
QMqttControlPacket packet(QMqttControlPacket::PUBLISH);
packet.append(topic.toUtf8());
// ### TODO: Add packet identifier (for QOS1/2)
packet.append(message.toUtf8());
return writePacketToTransport(packet);
}
bool QMqttConnection::sendControlSubscribe()
@ -191,9 +197,7 @@ bool QMqttConnection::sendControlPingRequest()
bool QMqttConnection::sendControlDisconnect()
{
const QMqttControlPacket packet(QMqttControlPacket::DISCONNECT);
const QByteArray writeData = packet.serialize();
const qint64 res = m_transport->write(writeData.constData(), writeData.size());
if (Q_UNLIKELY(res == -1)) {
if (!writePacketToTransport(packet)) {
qWarning("Could not write DISCONNECT frame to transport");
return false;
}
@ -269,4 +273,15 @@ void QMqttConnection::transportReadReady()
}
}
bool QMqttConnection::writePacketToTransport(const QMqttControlPacket &p)
{
const QByteArray writeData = p.serialize();
const qint64 res = m_transport->write(writeData.constData(), writeData.size());
if (Q_UNLIKELY(res == -1)) {
qWarning("Could not write frame to transport");
return false;
}
return true;
}
QT_END_NAMESPACE

View File

@ -42,6 +42,7 @@
//
#include "qmqttclient.h"
#include "qmqttcontrolpacket_p.h"
#include <QObject>
QT_BEGIN_NAMESPACE
@ -74,7 +75,7 @@ public:
bool ensureTransportOpen();
bool sendControlConnect();
bool sendControlPublish();
bool sendControlPublish(const QString &topic, const QString &message);
bool sendControlSubscribe();
bool sendControlUnsubscribe();
bool sendControlPingRequest();
@ -95,6 +96,7 @@ public:
bool m_ownTransport{false};
QMqttClient *m_client{nullptr};
private:
bool writePacketToTransport(const QMqttControlPacket &p);
InternalConnectionState m_internalState{BrokerDisconnected};
};