示例#1
0
quint16 ClientPrivate::sendUnsubscribe(const QString &topic)
{
    quint16 mid = gmid++;
    Frame frame(SETQOS(UNSUBSCRIBE, MQTT_QOS1));
    frame.writeInt(mid);
    frame.writeString(topic);
    network->sendFrame(frame);
    return mid;
}
quint16 QMQTT::ClientPrivate::sendUnsubscribe(const QString &topic)
{
    quint16 mid = nextmid();
    Frame frame(SETQOS(UNSUBSCRIBE, QOS1));
    frame.writeInt(mid);
    frame.writeString(topic);
    _network->sendFrame(frame);
    return mid;
}
示例#3
0
quint16 ClientPrivate::sendSubscribe(const QString & topic, quint8 qos)
{
    quint16 mid = nextmid();
    Frame frame(SETQOS(SUBSCRIBE, MQTT_QOS1));
    frame.writeInt(mid);
    frame.writeString(topic);
    frame.writeChar(qos);
    network->sendFrame(frame);
    return mid;
}
示例#4
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);
}
示例#5
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);
}
示例#6
0
quint16 ClientPrivate::sendPublish(Message &msg)
{
    quint8 header = PUBLISH;
    header = SETRETAIN(header, msg.retain() ? 1 : 0);
    header = SETQOS(header, msg.qos());
    header = SETDUP(header, msg.dup() ? 1 : 0);
    Frame frame(header);
    frame.writeString(msg.topic());
    if(msg.qos() > MQTT_QOS0) {
        if(msg.id() == 0) {
            msg.setId(gmid++);
        }
        frame.writeInt(msg.id());
    }
    if(!msg.payload().isEmpty()) {
        frame.writeRawData(msg.payload());
    }
    network->sendFrame(frame);
    return msg.id();
}
quint16 QMQTT::ClientPrivate::sendPublish(const Message& msg)
{
    Message message(msg);

    quint8 header = PUBLISH;
    header = SETRETAIN(header, message.retain() ? 1 : 0);
    header = SETQOS(header, message.qos());
    header = SETDUP(header, message.dup() ? 1 : 0);
    Frame frame(header);
    frame.writeString(message.topic());
    if(message.qos() > QOS0) {
        if(message.id() == 0) {
            message.setId(nextmid());
        }
        frame.writeInt(message.id());
    }
    if(!message.payload().isEmpty()) {
        frame.writeRawData(message.payload());
    }
    _network->sendFrame(frame);
    return message.id();
}