48 lines
1.6 KiB
C++
48 lines
1.6 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, char* select_ip, QObject* parent = nullptr)
|
|
: QThread(parent),
|
|
m_select_ip(select_ip),
|
|
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;
|
|
|
|
//int result = service_mdns(m_hostname.toStdString().c_str(), m_serviceName.toStdString().c_str(), m_servicePort, "192.168.1.170");
|
|
//int result = service_mdns(m_hostname.toStdString().c_str(), m_serviceName.toStdString().c_str(), m_servicePort, "172.17.128.1");
|
|
int result = service_mdns(m_hostname.toStdString().c_str(), m_serviceName.toStdString().c_str(), m_servicePort, m_select_ip);
|
|
if (result < 0) {
|
|
qDebug() << "Failed to start mDNS service";
|
|
}
|
|
else {
|
|
qDebug() << "mDNS service stopped";
|
|
}
|
|
}
|
|
|
|
private:
|
|
char* m_select_ip;
|
|
QString m_hostname;
|
|
QString m_serviceName;
|
|
int m_servicePort;
|
|
bool m_running = true; // 默认运行状态
|
|
};
|