libmosquittopp-示例客户端挂在loop_stop()方法上

libmosquittopp - sample client hangs on loop_stop() method

本文关键字:stop 方法 loop 客户端 libmosquittopp-      更新时间:2023-10-16

我正在尝试为我的家庭应用程序创建一个简单的mqtt客户端,并且我正在使用libmosquittopp(这是libmosquitto的C 版本)。
该库的文档没有太多的文档,但是我找到了两个示例(此处和此处),这些示例帮助我为" MQTTWRAPPER"类创建了代码。

这是我的代码:

mqttwrapper.h:

#pragma once
#include <mosquittopp.h>
#include <string>
class MQTTWrapper : public mosqpp::mosquittopp
{
public:
    MQTTWrapper(const char* id, const char* host_, int port_);
    virtual ~MQTTWrapper();
    void myPublish(std::string topic, std::string value);
private:
    void on_connect(int rc);
    void on_publish(int mid);
    std::string host;
    int port;
};

mqttwrapper.cpp

#include "MQTTWrapper.h"
#include <iostream>
MQTTWrapper::MQTTWrapper(const char* id, const char* host_, int port_) :
    mosquittopp(id), host(host_), port(port_)
{
    mosqpp::lib_init();
    int keepalive = 10;
    if (username_pw_set("sampleuser", "samplepass") != MOSQ_ERR_SUCCESS) {
        std::cout << "setting passwd failed" << std::endl;
    }
    connect_async(host.c_str(), port, keepalive);
    if (loop_start() != MOSQ_ERR_SUCCESS) {
        std::cout << "loop_start failed" << std::endl;
    }
}
MQTTWrapper::~MQTTWrapper()
{
    std::cout << "1" << std::endl;
    if (loop_stop() != MOSQ_ERR_SUCCESS) {
        std::cout << "loop_stop failed" << std::endl;
    }
    std::cout << "2" << std::endl;
    mosqpp::lib_cleanup();
    std::cout << "3" << std::endl;
}
void MQTTWrapper::on_connect(int rc)
{
    std::cout << "Connected with code " << rc << "." << std::endl;
}
void MQTTWrapper::myPublish(std::string topic, std::string value) {
    int ret = publish(NULL, topic.c_str(), value.size(), value.c_str(), 1, false);
    if (ret != MOSQ_ERR_SUCCESS) {
        std::cout << "Sending failed." << std::endl;
    }
}
void MQTTWrapper::on_publish(int mid) {
    std::cout << "Published message with id: " << mid << std::endl;
}

和我的main():

#include <iostream>
#include <string>
#include "MQTTWrapper.h"
int main(int argc, char *argv[])
{
    MQTTWrapper* mqtt;
    mqtt = new MQTTWrapper("Lewiatan IoT", "my.cloudmqtt.host", 12345);
    std::string value("Test123");
    mqtt->myPublish("sensors/temp", value);
    std::cout << "about to delete mqtt" << std::endl;
    delete mqtt;
    std::cout << "mqtt deleted" << std::endl;
    return 0;
}

对不起,这么多代码。

我的问题是,当我进行编译并执行时 - 我的应用程序在loop_stop()方法上无限悬挂(我只等待9分钟)在mqttwrapper destructor中。
用libmosquittopp 1.4.8(Debian软件包)进行测试,然后在删除它之后 - 从Github中使用1.4.9版。

loop_start()loop_stop(bool force=false)应启动/停止处理消息传递的单独线程。

我已经用强制停止(loop_stop(true))对其进行了测试,但是这样我的应用程序停止了,并且不发布任何数据。loop_stop()另一方面发布数据,但随后停止。

控制台输出(make && ./executable):

g++ -c MQTTWrapper.cpp
g++ -c main.cpp
g++ -o executable main.o MQTTWrapper.o -lmosquittopp
about to delete mqtt
1
Connected with code 0.
Published message with id: 1
(here it hangs infinitely...)

我的问题:
为什么此loop_stop()悬挂以及如何修复它?

(任何文档/教程/示例赞赏)

loop_stop()之前尝试调用disconnect()。您还应该记住,您正在有效地这样做:

connect_async();
loop_start();
loop_stop();

客户端甚至可能没有机会连接,也可能没有在告诉它停止之前启动线程。

值得考虑在回调中运行操作:

on_connect -> call publish
on_publish -> call disconnect