Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix several issues in CONNECT Control Packet marshaller (changes API) #112

Merged
merged 2 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/mqtt/qmqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ void QMQTT::Client::setVersion(const QMQTT::MQTTVersion version)
d->setVersion(version);
}

QString QMQTT::Client::password() const
QByteArray QMQTT::Client::password() const
{
Q_D(const Client);
return d->password();
}

void QMQTT::Client::setPassword(const QString& password)
void QMQTT::Client::setPassword(const QByteArray &password)
{
Q_D(Client);
d->setPassword(password);
Expand Down Expand Up @@ -251,13 +251,13 @@ void QMQTT::Client::setWillRetain(const bool willRetain)
d->setWillRetain(willRetain);
}

QString QMQTT::Client::willMessage() const
QByteArray QMQTT::Client::willMessage() const
{
Q_D(const Client);
return d->willMessage();
}

void QMQTT::Client::setWillMessage(const QString& willMessage)
void QMQTT::Client::setWillMessage(const QByteArray &willMessage)
{
Q_D(Client);
d->setWillMessage(willMessage);
Expand Down
12 changes: 6 additions & 6 deletions src/mqtt/qmqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Q_MQTT_EXPORT Client : public QObject
Q_PROPERTY(QString _hostName READ hostName WRITE setHostName)
Q_PROPERTY(QString _clientId READ clientId WRITE setClientId)
Q_PROPERTY(QString _username READ username WRITE setUsername)
Q_PROPERTY(QString _password READ password WRITE setPassword)
Q_PROPERTY(QByteArray _password READ password WRITE setPassword)
Q_PROPERTY(quint16 _keepAlive READ keepAlive WRITE setKeepAlive)
Q_PROPERTY(MQTTVersion _version READ version WRITE setVersion)
Q_PROPERTY(bool _autoReconnect READ autoReconnect WRITE setAutoReconnect)
Expand All @@ -114,7 +114,7 @@ class Q_MQTT_EXPORT Client : public QObject
Q_PROPERTY(QString _willTopic READ willTopic WRITE setWillTopic)
Q_PROPERTY(quint8 _willQos READ willQos WRITE setWillQos)
Q_PROPERTY(bool _willRetain READ willRetain WRITE setWillRetain)
Q_PROPERTY(QString _willMessage READ willMessage WRITE setWillMessage)
Q_PROPERTY(QByteArray _willMessage READ willMessage WRITE setWillMessage)
Q_PROPERTY(QString _connectionState READ connectionState)

public:
Expand Down Expand Up @@ -153,7 +153,7 @@ class Q_MQTT_EXPORT Client : public QObject
quint16 port() const;
QString clientId() const;
QString username() const;
QString password() const;
QByteArray password() const;
QMQTT::MQTTVersion version() const;
quint16 keepAlive() const;
bool cleanSession() const;
Expand All @@ -163,7 +163,7 @@ class Q_MQTT_EXPORT Client : public QObject
QString willTopic() const;
quint8 willQos() const;
bool willRetain() const;
QString willMessage() const;
QByteArray willMessage() const;

bool isConnectedToHost() const;

Expand All @@ -173,7 +173,7 @@ public slots:
void setPort(const quint16 port);
void setClientId(const QString& clientId);
void setUsername(const QString& username);
void setPassword(const QString& password);
void setPassword(const QByteArray& password);
void setVersion(const MQTTVersion version);
void setKeepAlive(const quint16 keepAlive);
void setCleanSession(const bool cleanSession);
Expand All @@ -182,7 +182,7 @@ public slots:
void setWillTopic(const QString& willTopic);
void setWillQos(const quint8 willQos);
void setWillRetain(const bool willRetain);
void setWillMessage(const QString& willMessage);
void setWillMessage(const QByteArray& willMessage);

void connectToHost();
void disconnectFromHost();
Expand Down
23 changes: 10 additions & 13 deletions src/mqtt/qmqtt_client_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ void QMQTT::ClientPrivate::sendConnect()
if (!username().isEmpty())
{
flags = FLAG_USERNAME(flags, 1);
}
if (!password().isEmpty())
{
flags = FLAG_PASSWD(flags, 1);
flags = FLAG_PASSWD(flags, !password().isEmpty() ? 1 : 0);
}

//payload
Expand All @@ -225,16 +222,16 @@ void QMQTT::ClientPrivate::sendConnect()
frame.writeString(willTopic());
if(!willMessage().isEmpty())
{
frame.writeString(willMessage());
frame.writeByteArray(_willMessage);
}
}
if (!_username.isEmpty())
{
frame.writeString(_username);
}
if (!_password.isEmpty())
{
frame.writeString(_password);
if (!_password.isEmpty())
{
frame.writeByteArray(_password);
}
}
_network->sendFrame(frame);
}
Expand Down Expand Up @@ -546,12 +543,12 @@ quint16 QMQTT::ClientPrivate::keepAlive() const
return _keepAlive;
}

void QMQTT::ClientPrivate::setPassword(const QString& password)
void QMQTT::ClientPrivate::setPassword(const QByteArray& password)
{
_password = password;
}

QString QMQTT::ClientPrivate::password() const
QByteArray QMQTT::ClientPrivate::password() const
{
return _password;
}
Expand Down Expand Up @@ -653,12 +650,12 @@ void QMQTT::ClientPrivate::setWillRetain(const bool willRetain)
_willRetain = willRetain;
}

QString QMQTT::ClientPrivate::willMessage() const
QByteArray QMQTT::ClientPrivate::willMessage() const
{
return _willMessage;
}

void QMQTT::ClientPrivate::setWillMessage(const QString& willMessage)
void QMQTT::ClientPrivate::setWillMessage(const QByteArray& willMessage)
{
_willMessage = willMessage;
}
Expand Down
12 changes: 6 additions & 6 deletions src/mqtt/qmqtt_client_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ClientPrivate
MQTTVersion _version;
QString _clientId;
QString _username;
QString _password;
QByteArray _password;
bool _cleanSession;
quint16 _keepAlive;
ConnectionState _connectionState;
Expand All @@ -73,7 +73,7 @@ class ClientPrivate
QString _willTopic;
quint8 _willQos;
bool _willRetain;
QString _willMessage;
QByteArray _willMessage;
QHash<QAbstractSocket::SocketError, ClientError> _socketErrorHash;
QHash<quint16, QString> _midToTopic;

Expand Down Expand Up @@ -117,8 +117,8 @@ class ClientPrivate
bool cleanSession() const;
void setKeepAlive(const quint16 keepAlive);
quint16 keepAlive() const;
void setPassword(const QString& password);
QString password() const;
void setPassword(const QByteArray& password);
QByteArray password() const;
void setUsername(const QString& username);
QString username() const;
void setVersion(const MQTTVersion);
Expand All @@ -134,11 +134,11 @@ class ClientPrivate
void setWillTopic(const QString& willTopic);
void setWillQos(const quint8 willQos);
void setWillRetain(const bool willRetain);
void setWillMessage(const QString& willMessage);
void setWillMessage(const QByteArray& willMessage);
QString willTopic() const;
quint8 willQos() const;
bool willRetain() const;
QString willMessage() const;
QByteArray willMessage() const;
void initializeErrorHash();
void onNetworkError(QAbstractSocket::SocketError error);

Expand Down
22 changes: 22 additions & 0 deletions src/mqtt/qmqtt_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ quint16 Frame::readInt()
return (msb << 8) | lsb;
}

QByteArray Frame::readByteArray()
{
quint16 len = readInt();
QByteArray data = _data.left(len);
_data.remove(0, len);
return data;
}

QString Frame::readString()
{
quint16 len = readInt();
Expand All @@ -118,6 +126,20 @@ void Frame::writeInt(const quint16 i)
_data.append(LSB(i));
}

void Frame::writeByteArray(const QByteArray &data)
{
if (data.size() > (int)USHRT_MAX)
{
qCritical("qmqtt: Binary data size bigger than %u bytes, truncate it!", USHRT_MAX);
writeInt(USHRT_MAX);
_data.append(data.left(USHRT_MAX));
return;
}

writeInt(data.size());
_data.append(data);
}

void Frame::writeString(const QString &string)
{
QByteArray data = string.toUtf8();
Expand Down
2 changes: 2 additions & 0 deletions src/mqtt/qmqtt_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ class Q_MQTT_EXPORT Frame

quint16 readInt();
quint8 readChar();
QByteArray readByteArray();
QString readString();

void writeInt(const quint16 i);
void writeChar(const quint8 c);
void writeByteArray(const QByteArray &data);
void writeString(const QString &string);
void writeRawData(const QByteArray &data);

Expand Down