mirror of
https://github.com/jbagg/QtZeroConf.git
synced 2025-08-01 02:24:25 +02:00
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.user
|
@@ -105,6 +105,8 @@ public:
|
||||
QString key = name + QString::number(interface);
|
||||
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
|
||||
|
||||
QZeroConfService zcs;
|
||||
|
||||
switch (event) {
|
||||
case AVAHI_BROWSER_FAILURE:
|
||||
ref->broswerCleanUp();
|
||||
@@ -122,11 +124,10 @@ public:
|
||||
|
||||
if (!ref->pub->services.contains(key))
|
||||
return;
|
||||
QZeroConfService *zcs;
|
||||
|
||||
zcs = ref->pub->services[key];
|
||||
ref->pub->services.remove(key);
|
||||
emit ref->pub->serviceRemoved(zcs);
|
||||
delete zcs;
|
||||
break;
|
||||
case AVAHI_BROWSER_ALL_FOR_NOW:
|
||||
case AVAHI_BROWSER_CACHE_EXHAUSTED:
|
||||
@@ -150,7 +151,7 @@ public:
|
||||
AVAHI_GCC_UNUSED void* userdata)
|
||||
{
|
||||
bool newRecord = 0;
|
||||
QZeroConfService *zcs;
|
||||
QZeroConfService zcs;
|
||||
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
|
||||
|
||||
QString key = name + QString::number(interface);
|
||||
@@ -159,21 +160,20 @@ public:
|
||||
zcs = ref->pub->services[key];
|
||||
else {
|
||||
newRecord = 1;
|
||||
zcs = new QZeroConfService;
|
||||
zcs->name = name;
|
||||
zcs->type = type;
|
||||
zcs->domain = domain;
|
||||
zcs->host = host_name;
|
||||
zcs->interfaceIndex = interface;
|
||||
zcs->port = port;
|
||||
zcs.setName(name);
|
||||
zcs.setType(type);
|
||||
zcs.setDomain(domain);
|
||||
zcs.setHost(host_name);
|
||||
zcs.setInterfaceIndex(interface);
|
||||
zcs.setPort(port);
|
||||
while (txt) // get txt records
|
||||
{
|
||||
QByteArray avahiText((const char *)txt->text, txt->size);
|
||||
QList<QByteArray> pair = avahiText.split('=');
|
||||
if (pair.size() == 2)
|
||||
zcs->txt[pair.at(0)] = pair.at(1);
|
||||
zcs.appendTxt(pair.at(0), pair.at(1));
|
||||
else
|
||||
zcs->txt[pair.at(0)] = "";
|
||||
zcs.appendTxt(pair.at(0));
|
||||
txt = txt->next;
|
||||
}
|
||||
ref->pub->services.insert(key, zcs);
|
||||
@@ -181,10 +181,11 @@ public:
|
||||
|
||||
char a[AVAHI_ADDRESS_STR_MAX];
|
||||
avahi_address_snprint(a, sizeof(a), address);
|
||||
QHostAddress addr(a);
|
||||
if (protocol == AVAHI_PROTO_INET6)
|
||||
zcs->ipv6 = QHostAddress(a);
|
||||
zcs.setIpv6(addr);
|
||||
else if (protocol == AVAHI_PROTO_INET)
|
||||
zcs->ip = QHostAddress (a);
|
||||
zcs.setIp(addr);
|
||||
|
||||
if (newRecord)
|
||||
emit ref->pub->serviceAdded(zcs);
|
||||
@@ -195,7 +196,6 @@ public:
|
||||
zcs = ref->pub->services[key];
|
||||
ref->pub->services.remove(key);
|
||||
emit ref->pub->serviceRemoved(zcs);
|
||||
delete zcs;
|
||||
// don't delete the resolver here...we need to keep it around so Avahi will keep updating....might be able to resolve the service in the future
|
||||
}
|
||||
}
|
||||
@@ -207,10 +207,9 @@ public:
|
||||
avahi_service_browser_free(browser);
|
||||
browser = NULL;
|
||||
|
||||
QMap<QString, QZeroConfService *>::iterator i;
|
||||
QMap<QString, QZeroConfService>::iterator i;
|
||||
for (i = pub->services.begin(); i != pub->services.end(); i++) {
|
||||
emit pub->serviceRemoved(*i);
|
||||
delete *i;
|
||||
emit pub->serviceRemoved(i.value());
|
||||
}
|
||||
pub->services.clear();
|
||||
|
||||
|
40
bonjour.cpp
40
bonjour.cpp
@@ -37,7 +37,6 @@ QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent)
|
||||
browserSocket = NULL;
|
||||
resolverSocket = NULL;
|
||||
addressSocket = NULL;
|
||||
newService = NULL;
|
||||
}
|
||||
|
||||
void QZeroConfPrivate::bsRead()
|
||||
@@ -69,7 +68,7 @@ void QZeroConfPrivate::resolve(void)
|
||||
{
|
||||
DNSServiceErrorType err;
|
||||
|
||||
err = DNSServiceResolve(&resolver, kDNSServiceFlagsTimeout, newService->interfaceIndex, newService->name.toUtf8(), newService->type.toUtf8(), newService->domain.toUtf8(), (DNSServiceResolveReply) resolverCallback, this);
|
||||
err = DNSServiceResolve(&resolver, kDNSServiceFlagsTimeout, newService.interfaceIndex(), newService.name().toUtf8(), newService.type().toUtf8(), newService.domain().toUtf8(), (DNSServiceResolveReply) resolverCallback, this);
|
||||
if (err == kDNSServiceErr_NoError) {
|
||||
int sockfd = DNSServiceRefSockFD(resolver);
|
||||
if (sockfd == -1) {
|
||||
@@ -103,7 +102,7 @@ void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags f
|
||||
const char *type, const char *domain, void *userdata)
|
||||
{
|
||||
QString key;
|
||||
QZeroConfService *zcs;
|
||||
QZeroConfService zcs;
|
||||
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
|
||||
|
||||
//qDebug() << name;
|
||||
@@ -111,12 +110,11 @@ void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags f
|
||||
key = name + QString::number(interfaceIndex);
|
||||
if (flags & kDNSServiceFlagsAdd) {
|
||||
if (!ref->pub->services.contains(key)) {
|
||||
zcs = new QZeroConfService;
|
||||
zcs->name = name;
|
||||
zcs->type = type;
|
||||
zcs->domain = domain;
|
||||
zcs->interfaceIndex = interfaceIndex;
|
||||
if (!ref->newService) {
|
||||
zcs.setName(name);
|
||||
zcs.setType(type);
|
||||
zcs.setDomain(domain);
|
||||
zcs.setInterfaceIndex(interfaceIndex);
|
||||
if (!ref->newService.isValid()) {
|
||||
ref->newService = zcs;
|
||||
ref->resolve();
|
||||
}
|
||||
@@ -128,7 +126,6 @@ void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags f
|
||||
zcs = ref->pub->services[key];
|
||||
ref->pub->services.remove(key);
|
||||
emit ref->pub->serviceRemoved(zcs);
|
||||
delete zcs;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -157,15 +154,15 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags
|
||||
QByteArray avahiText((const char *)txtRecord, recLen);
|
||||
QList<QByteArray> pair = avahiText.split('=');
|
||||
if (pair.size() == 2)
|
||||
ref->newService->txt[pair.at(0)] = pair.at(1);
|
||||
ref->newService.appendTxt(pair.at(0), pair.at(1));
|
||||
else
|
||||
ref->newService->txt[pair.at(0)] = "";
|
||||
ref->newService.appendTxt(pair.at(0));
|
||||
|
||||
txtLen-= recLen + 1;
|
||||
txtRecord+= recLen;
|
||||
}
|
||||
ref->newService->host = hostName;
|
||||
ref->newService->port = qFromBigEndian<quint16>(port);
|
||||
ref->newService.setHost(hostName);
|
||||
ref->newService.setPort(qFromBigEndian<quint16>(port));
|
||||
err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref);
|
||||
if (err == kDNSServiceErr_NoError) {
|
||||
int sockfd = DNSServiceRefSockFD(ref->resolver);
|
||||
@@ -198,11 +195,11 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef,
|
||||
if ((flags & kDNSServiceFlagsAdd) != 0) {
|
||||
QHostAddress hAddress(address);
|
||||
if (hAddress.protocol() == QAbstractSocket::IPv6Protocol)
|
||||
ref->newService->ipv6 = hAddress;
|
||||
ref->newService.setIpv6(hAddress);
|
||||
else
|
||||
ref->newService->ip = hAddress;
|
||||
ref->newService.setIp(hAddress);
|
||||
|
||||
QString key = ref->newService->name + QString::number(interfaceIndex);
|
||||
QString key = ref->newService.name() + QString::number(interfaceIndex);
|
||||
if (!ref->pub->services.contains(key)) {
|
||||
ref->pub->services.insert(key, ref->newService);
|
||||
emit ref->pub->serviceAdded(ref->newService);
|
||||
@@ -212,7 +209,7 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef,
|
||||
|
||||
}
|
||||
if (!(flags & kDNSServiceFlagsMoreComing)) {
|
||||
ref->newService = NULL; // newService resolve succeeded so don't let cleanUp delete it!
|
||||
ref->newService = QZeroConfService(); // newService resolve succeeded so don't let cleanUp delete it!
|
||||
ref->cleanUp(ref->resolver);
|
||||
}
|
||||
}
|
||||
@@ -233,10 +230,6 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean)
|
||||
delete resolverSocket;
|
||||
resolverSocket = NULL;
|
||||
}
|
||||
if (newService) {
|
||||
delete newService;
|
||||
newService = NULL;
|
||||
}
|
||||
if (work.size()) {
|
||||
newService = work.first();
|
||||
work.removeFirst();
|
||||
@@ -249,10 +242,9 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean)
|
||||
delete browserSocket;
|
||||
browserSocket = NULL;
|
||||
}
|
||||
QMap<QString, QZeroConfService *>::iterator i;
|
||||
QMap<QString, QZeroConfService >::iterator i;
|
||||
for (i = pub->services.begin(); i != pub->services.end(); i++) {
|
||||
emit pub->serviceRemoved(*i);
|
||||
delete *i;
|
||||
}
|
||||
pub->services.clear();
|
||||
}
|
||||
|
@@ -64,8 +64,8 @@ public:
|
||||
DNSServiceRef dnssRef, browser, resolver;
|
||||
DNSServiceProtocol protocol;
|
||||
QSocketNotifier *bs, *browserSocket, *resolverSocket, *addressSocket;
|
||||
QZeroConfService *newService;
|
||||
QList<QZeroConfService *> work;
|
||||
QZeroConfService newService;
|
||||
QList<QZeroConfService> work;
|
||||
QByteArray txt;
|
||||
|
||||
public slots:
|
||||
|
@@ -53,8 +53,8 @@ mainWindow::mainWindow()
|
||||
publishEnabled = 0;
|
||||
buildGUI();
|
||||
|
||||
connect(&zeroConf, SIGNAL(serviceAdded(QZeroConfService *)), this, SLOT(addService(QZeroConfService *)));
|
||||
connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService *)), this, SLOT(removeService(QZeroConfService *)));
|
||||
connect(&zeroConf, &QZeroConf::serviceAdded, this, &mainWindow::addService);
|
||||
connect(&zeroConf, &QZeroConf::serviceRemoved, this, &mainWindow::removeService);
|
||||
connect(qGuiApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(appStateChanged(Qt::ApplicationState)));
|
||||
|
||||
publishEnabled = 1;
|
||||
@@ -145,16 +145,16 @@ void mainWindow::stopPublishClicked()
|
||||
|
||||
// ---------- Discovery Callbacks ----------
|
||||
|
||||
void mainWindow::addService(QZeroConfService *zcs)
|
||||
void mainWindow::addService(QZeroConfService zcs)
|
||||
{
|
||||
qint32 row;
|
||||
QTableWidgetItem *cell;
|
||||
|
||||
row = table.rowCount();
|
||||
table.insertRow(row);
|
||||
cell = new QTableWidgetItem(zcs->name);
|
||||
cell = new QTableWidgetItem(zcs.name());
|
||||
table.setItem(row, 0, cell);
|
||||
cell = new QTableWidgetItem(zcs->ip.toString());
|
||||
cell = new QTableWidgetItem(zcs.ip().toString());
|
||||
table.setItem(row, 1, cell);
|
||||
table.resizeColumnsToContents();
|
||||
#if !(defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
|
||||
@@ -162,17 +162,17 @@ void mainWindow::addService(QZeroConfService *zcs)
|
||||
#endif
|
||||
}
|
||||
|
||||
void mainWindow::removeService(QZeroConfService *zcs)
|
||||
void mainWindow::removeService(QZeroConfService zcs)
|
||||
{
|
||||
qint32 i, row;
|
||||
QTableWidgetItem *nameItem, *ipItem;
|
||||
|
||||
QList <QTableWidgetItem*> search = table.findItems(zcs->name, Qt::MatchExactly);
|
||||
QList <QTableWidgetItem*> search = table.findItems(zcs.name(), Qt::MatchExactly);
|
||||
for (i=0; i<search.size(); i++) {
|
||||
nameItem = search.at(i);
|
||||
row = nameItem->row();
|
||||
ipItem = table.item(row, 1);
|
||||
if (table.item(row, 1)->text() == zcs->ip.toString()) { // match ip address in case of dual homed systems
|
||||
if (table.item(row, 1)->text() == zcs.ip().toString()) { // match ip address in case of dual homed systems
|
||||
delete nameItem;
|
||||
delete ipItem;
|
||||
table.removeRow(row);
|
||||
|
@@ -50,8 +50,8 @@ private slots:
|
||||
void appStateChanged(Qt::ApplicationState state);
|
||||
void startPublishClicked();
|
||||
void stopPublishClicked();
|
||||
void addService(QZeroConfService *item);
|
||||
void removeService(QZeroConfService *item);
|
||||
void addService(QZeroConfService item);
|
||||
void removeService(QZeroConfService item);
|
||||
};
|
||||
|
||||
#endif /* WINDOW_H_ */
|
||||
|
@@ -115,3 +115,9 @@ android {
|
||||
SOURCES+= $$ACR/wide-area.c
|
||||
#avahi-core/iface-none.c avahi-core/iface-pfroute.c avahi-core/avahi-reflector.c
|
||||
}
|
||||
|
||||
HEADERS += \
|
||||
$$PWD/qzeroconfservice.h
|
||||
|
||||
SOURCES += \
|
||||
$$PWD/qzeroconfservice.cpp
|
||||
|
22
qzeroconf.h
22
qzeroconf.h
@@ -31,6 +31,7 @@
|
||||
#include <QHostAddress>
|
||||
#include <QMap>
|
||||
#include <QtCore/QtGlobal>
|
||||
#include "qzeroconfservice.h"
|
||||
|
||||
#if (!defined(QT_STATIC) && !defined(QZEROCONF_STATIC))
|
||||
# ifdef QT_BUILD_ZEROCONF_LIB
|
||||
@@ -42,19 +43,6 @@
|
||||
# define Q_ZEROCONF_EXPORT
|
||||
#endif
|
||||
|
||||
struct QZeroConfService
|
||||
{
|
||||
QString name;
|
||||
QString type;
|
||||
QString domain;
|
||||
QString host;
|
||||
QHostAddress ip;
|
||||
QHostAddress ipv6;
|
||||
quint32 interfaceIndex;
|
||||
quint16 port;
|
||||
QMap <QByteArray, QByteArray> txt;
|
||||
};
|
||||
|
||||
class QZeroConfPrivate;
|
||||
|
||||
class Q_ZEROCONF_EXPORT QZeroConf : public QObject
|
||||
@@ -89,13 +77,13 @@ public:
|
||||
Q_SIGNALS:
|
||||
void servicePublished(void);
|
||||
void error(QZeroConf::error_t);
|
||||
void serviceAdded(QZeroConfService *);
|
||||
void serviceUpdated(QZeroConfService *);
|
||||
void serviceRemoved(QZeroConfService *);
|
||||
void serviceAdded(QZeroConfService);
|
||||
void serviceUpdated(QZeroConfService);
|
||||
void serviceRemoved(QZeroConfService);
|
||||
|
||||
private:
|
||||
QZeroConfPrivate *pri;
|
||||
QMap<QString, QZeroConfService *> services;
|
||||
QMap<QString, QZeroConfService> services;
|
||||
|
||||
|
||||
|
||||
|
145
qzeroconfservice.cpp
Normal file
145
qzeroconfservice.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "qzeroconfservice.h"
|
||||
|
||||
|
||||
class QZeroConfServiceData : public QSharedData
|
||||
{
|
||||
public:
|
||||
QString name;
|
||||
QString type;
|
||||
QString domain;
|
||||
QString host;
|
||||
QHostAddress ip;
|
||||
QHostAddress ipv6;
|
||||
quint32 interfaceIndex;
|
||||
quint16 port = 0;
|
||||
QMap <QByteArray, QByteArray> txt;
|
||||
|
||||
};
|
||||
|
||||
QZeroConfService::QZeroConfService() : data(new QZeroConfServiceData)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QZeroConfService::QZeroConfService(const QZeroConfService &rhs) : data(rhs.data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QZeroConfService &QZeroConfService::operator=(const QZeroConfService &rhs)
|
||||
{
|
||||
if (this != &rhs)
|
||||
data.operator=(rhs.data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
QZeroConfService::~QZeroConfService()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString QZeroConfService::name() const
|
||||
{
|
||||
return data->name;
|
||||
}
|
||||
|
||||
void QZeroConfService::setName(const QString &name)
|
||||
{
|
||||
data->name = name;
|
||||
}
|
||||
|
||||
QString QZeroConfService::type() const
|
||||
{
|
||||
return data->type;
|
||||
}
|
||||
|
||||
void QZeroConfService::setType(const QString &type)
|
||||
{
|
||||
data->type = type;
|
||||
}
|
||||
|
||||
QString QZeroConfService::domain() const
|
||||
{
|
||||
return data->domain;
|
||||
}
|
||||
|
||||
void QZeroConfService::setDomain(const QString &domain)
|
||||
{
|
||||
data->domain = domain;
|
||||
}
|
||||
|
||||
QString QZeroConfService::host() const
|
||||
{
|
||||
return data->host;
|
||||
}
|
||||
|
||||
void QZeroConfService::setHost(const QString &host)
|
||||
{
|
||||
data->host = host;
|
||||
}
|
||||
|
||||
QHostAddress QZeroConfService::ip() const
|
||||
{
|
||||
return data->ip;
|
||||
}
|
||||
|
||||
void QZeroConfService::setIp(QHostAddress &ip)
|
||||
{
|
||||
data->ip = ip;
|
||||
}
|
||||
|
||||
QHostAddress QZeroConfService::ipv6() const
|
||||
{
|
||||
return data->ipv6;
|
||||
}
|
||||
|
||||
void QZeroConfService::setIpv6(const QHostAddress &ipv6)
|
||||
{
|
||||
data->ipv6 = ipv6;
|
||||
}
|
||||
|
||||
quint32 QZeroConfService::interfaceIndex() const
|
||||
{
|
||||
return data->interfaceIndex;
|
||||
}
|
||||
|
||||
void QZeroConfService::setInterfaceIndex(const quint32 &interfaceIndex)
|
||||
{
|
||||
data->interfaceIndex = interfaceIndex;
|
||||
}
|
||||
|
||||
quint16 QZeroConfService::port() const
|
||||
{
|
||||
return data->port;
|
||||
}
|
||||
|
||||
void QZeroConfService::setPort(const quint16 port)
|
||||
{
|
||||
data->port = port;
|
||||
}
|
||||
|
||||
QMap<QByteArray, QByteArray> QZeroConfService::txt() const
|
||||
{
|
||||
return data->txt;
|
||||
}
|
||||
|
||||
void QZeroConfService::setTxt(const QMap<QByteArray, QByteArray> txt)
|
||||
{
|
||||
data->txt = txt;
|
||||
}
|
||||
|
||||
void QZeroConfService::appendTxt(QByteArray idx, QByteArray val)
|
||||
{
|
||||
data->txt[idx] = val;
|
||||
}
|
||||
|
||||
bool QZeroConfService::isValid() const
|
||||
{
|
||||
|
||||
return (!data->name.isEmpty());
|
||||
}
|
||||
|
||||
bool QZeroConfService::operator==(const QZeroConfService &rhs) const
|
||||
{
|
||||
return this->name() == rhs.name() && (this->ip() == rhs.ip() || this->ipv6() == rhs.ipv6());
|
||||
}
|
54
qzeroconfservice.h
Normal file
54
qzeroconfservice.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#ifndef QZEROCONFSERVICE_H
|
||||
#define QZEROCONFSERVICE_H
|
||||
|
||||
#include <QSharedDataPointer>
|
||||
#include <QHostAddress>
|
||||
|
||||
class QZeroConfServiceData;
|
||||
|
||||
class QZeroConfService
|
||||
{
|
||||
Q_GADGET
|
||||
Q_PROPERTY( QString name READ name )
|
||||
Q_PROPERTY( QString type READ type )
|
||||
Q_PROPERTY( QString domain READ domain )
|
||||
Q_PROPERTY( QString host READ host )
|
||||
|
||||
public:
|
||||
|
||||
QZeroConfService();
|
||||
QZeroConfService(const QZeroConfService &);
|
||||
QZeroConfService &operator=(const QZeroConfService &);
|
||||
~QZeroConfService();
|
||||
|
||||
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
QString type() const;
|
||||
void setType(const QString &type);
|
||||
QString domain() const;
|
||||
void setDomain(const QString &domain);
|
||||
QString host() const;
|
||||
void setHost(const QString &host);
|
||||
QHostAddress ip() const;
|
||||
void setIp(QHostAddress &ip);
|
||||
QHostAddress ipv6() const;
|
||||
void setIpv6(const QHostAddress &ipv6);
|
||||
quint32 interfaceIndex() const;
|
||||
void setInterfaceIndex(const quint32 &interfaceIndex);
|
||||
quint16 port() const;
|
||||
void setPort(const quint16 port);
|
||||
QMap <QByteArray, QByteArray> txt() const;
|
||||
void setTxt(const QMap<QByteArray, QByteArray> txt);
|
||||
void appendTxt(QByteArray idx, QByteArray val = "");
|
||||
|
||||
bool isValid() const;
|
||||
bool operator==(const QZeroConfService &rhs) const;
|
||||
|
||||
private:
|
||||
QSharedDataPointer<QZeroConfServiceData> data;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(QZeroConfService)
|
||||
|
||||
#endif // QZEROCONFSERVICE_H
|
Reference in New Issue
Block a user