Ejemplo n.º 1
0
void QMQTT::ClientPrivate::sendConnect()
{
    QString magic(PROTOCOL_MAGIC);
    quint8 header = CONNECT;
    quint8 flags = 0;

    //header
    Frame frame(SETQOS(header, QOS1));

    //flags
    flags = FLAG_CLEANSESS(flags, _cleanSession ? 1 : 0 );
    flags = FLAG_WILL(flags, willTopic().isEmpty() ? 0 : 1);
    if (!willTopic().isEmpty())
    {
        flags = FLAG_WILLQOS(flags, willQos());
        flags = FLAG_WILLRETAIN(flags, willRetain() ? 1 : 0);
    }
    if (!username().isEmpty())
    {
        flags = FLAG_USERNAME(flags, 1);
    }
    if (!password().isEmpty())
    {
        flags = FLAG_PASSWD(flags, 1);
    }

    //payload
    frame.writeString(magic);
    frame.writeChar(PROTOCOL_VERSION_MAJOR);
    frame.writeChar(flags);
    frame.writeInt(_keepAlive);
    if(_clientId.isEmpty()) {
        _clientId = randomClientId();
    }
    frame.writeString(_clientId);
    if(!willTopic().isEmpty())
    {
        frame.writeString(willTopic());
        if(!willMessage().isEmpty())
        {
            frame.writeString(willMessage());
        }
    }
    if (!_username.isEmpty())
    {
        frame.writeString(_username);
    }
    if (!_password.isEmpty()){
        frame.writeString(_password);
    }
    _network->sendFrame(frame);
}
Ejemplo n.º 2
0
void ClientPrivate::sendConnect()
{
    QString magic(PROTOCOL_MAGIC);
    quint8 header = CONNECT;
    quint8 flags = 0;

    qCDebug(client) << "sendConnect....";

    //header
    Frame frame(SETQOS(header, MQTT_QOS1));

    qCDebug(client) << "CONNECT header: " << frame.header();

    //flags
    flags = FLAG_CLEANSESS(flags, cleansess ? 1 : 0 );
    flags = FLAG_WILL(flags, will ? 1 : 0);
    if (will) {
        flags = FLAG_WILLQOS(flags, will->qos());
        flags = FLAG_WILLRETAIN(flags, will->retain() ? 1 : 0);
    }
    if (!username.isEmpty()) {
        flags = FLAG_USERNAME(flags, 1);
    }
    if (!password.isEmpty()) {
        flags = FLAG_PASSWD(flags, 1);
    }

    //payload
    frame.writeString(magic);
    frame.writeChar(MQTT_PROTO_MAJOR);
    frame.writeChar(flags);
    frame.writeInt(keepalive);
    if(clientId.isEmpty()) {
        clientId = randomClientId();
    }
    frame.writeString(clientId);
    if(will != NULL) {
        frame.writeString(will->topic());
        frame.writeString(will->message());
    }
    if (!username.isEmpty()) {
        frame.writeString(username);
    }
    if (!password.isEmpty()) {
        frame.writeString(password);
    }
    network->sendFrame(frame);
}