45 lines
1.3 KiB
C++
45 lines
1.3 KiB
C++
#include <QThread>
|
|
#include <QDebug>
|
|
#include <signal.h>
|
|
|
|
#include "mdns.h"
|
|
|
|
// 添加一个类来运行 mDNS 服务
|
|
class MdnsServiceThread : public QThread {
|
|
Q_OBJECT
|
|
public:
|
|
MdnsServiceThread(const QString& hostname, const QString& serviceName, int servicePort, QObject* parent = nullptr)
|
|
: QThread(parent),
|
|
m_hostname(hostname),
|
|
m_serviceName(serviceName),
|
|
m_servicePort(servicePort) {}
|
|
|
|
void stop() {
|
|
extern volatile sig_atomic_t running;
|
|
running = 0; // 停止 mDNS 主循环
|
|
m_running = false; // 设置运行标志为 false
|
|
}
|
|
|
|
protected:
|
|
void run() override {
|
|
qDebug() << "Starting mDNS service...";
|
|
extern volatile sig_atomic_t running; // 使用全局变量控制运行状态
|
|
running = 1;
|
|
|
|
// 调用阻塞的 service_mdns 方法
|
|
int result = service_mdns(m_hostname.toStdString().c_str(), m_serviceName.toStdString().c_str(), m_servicePort);
|
|
if (result < 0) {
|
|
qDebug() << "Failed to start mDNS service";
|
|
}
|
|
else {
|
|
qDebug() << "mDNS service stopped";
|
|
}
|
|
}
|
|
|
|
private:
|
|
QString m_hostname;
|
|
QString m_serviceName;
|
|
int m_servicePort;
|
|
bool m_running = true; // 默认运行状态
|
|
};
|