From 5895c408bed1f5fc1a71fd37c460c83938d4a11b Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Fri, 6 Oct 2017 15:55:46 +0200 Subject: [PATCH 1/9] redesigned QZeroConfService as shared data, wip compiles on mac --- .gitignore | 1 + bonjour.cpp | 45 ++++++-------- bonjour_p.h | 4 +- qtzeroconf.pri | 6 ++ qzeroconf.h | 24 +++----- qzeroconfservice.cpp | 142 +++++++++++++++++++++++++++++++++++++++++++ qzeroconfservice.h | 44 ++++++++++++++ 7 files changed, 221 insertions(+), 45 deletions(-) create mode 100644 .gitignore create mode 100644 qzeroconfservice.cpp create mode 100644 qzeroconfservice.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a9d35c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.user diff --git a/bonjour.cpp b/bonjour.cpp index 8ded869..9076eb3 100644 --- a/bonjour.cpp +++ b/bonjour.cpp @@ -37,7 +37,7 @@ QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent) browserSocket = NULL; resolverSocket = NULL; addressSocket = NULL; - newService = NULL; + } void QZeroConfPrivate::bsRead() @@ -69,7 +69,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 +103,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(userdata); //qDebug() << name; @@ -111,12 +111,12 @@ 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 = new QZeroConfService; + zcs.setName(name); + zcs.setType( type); + zcs.setDomain(domain); + zcs.setInterfaceIndex(interfaceIndex); + if (!ref->newService.isValid()) { ref->newService = zcs; ref->resolve(); } @@ -128,7 +128,7 @@ 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 +157,15 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags QByteArray avahiText((const char *)txtRecord, recLen); QList 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(port); + ref->newService.setHost(hostName); + ref->newService.setPort(qFromBigEndian(port)); err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref); if (err == kDNSServiceErr_NoError) { int sockfd = DNSServiceRefSockFD(ref->resolver); @@ -198,11 +198,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 +212,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 +233,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,9 +245,6 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean) delete browserSocket; browserSocket = NULL; } - QMap::iterator i; - for (i = pub->services.begin(); i != pub->services.end(); i++) - delete *i; pub->services.clear(); } else if (toClean == dnssRef) { @@ -265,7 +258,7 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean) DNSServiceRefDeallocate(toClean); } -QZeroConf::QZeroConf() +QZeroConf::QZeroConf(QObject *parent) : QObject (parent) { pri = new QZeroConfPrivate(this); } diff --git a/bonjour_p.h b/bonjour_p.h index 907634c..792411b 100644 --- a/bonjour_p.h +++ b/bonjour_p.h @@ -64,8 +64,8 @@ public: DNSServiceRef dnssRef, browser, resolver; DNSServiceProtocol protocol; QSocketNotifier *bs, *browserSocket, *resolverSocket, *addressSocket; - QZeroConfService *newService; - QList work; + QZeroConfService newService; + QList work; QByteArray txt; public slots: diff --git a/qtzeroconf.pri b/qtzeroconf.pri index 5143b60..1b29cfa 100644 --- a/qtzeroconf.pri +++ b/qtzeroconf.pri @@ -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 diff --git a/qzeroconf.h b/qzeroconf.h index ff28105..3ccb4c8 100644 --- a/qzeroconf.h +++ b/qzeroconf.h @@ -31,6 +31,7 @@ #include #include #include +#include "qzeroconfservice.h" #if (!defined(QT_STATIC) && !defined(QZEROCONF_STATIC)) # ifdef QT_BUILD_ZEROCONF_LIB @@ -42,18 +43,7 @@ # define Q_ZEROCONF_EXPORT #endif -struct QZeroConfService -{ - QString name; - QString type; - QString domain; - QString host; - QHostAddress ip; - QHostAddress ipv6; - quint32 interfaceIndex; - quint16 port; - QMap txt; -}; + class QZeroConfPrivate; @@ -70,7 +60,7 @@ public: serviceNameCollision = -2, browserFailed = -3, }; - QZeroConf(); + QZeroConf(QObject *parent = Q_NULLPTR); ~QZeroConf(); void startServicePublish(const char *name, const char *type, const char *domain, quint16 port); void stopServicePublish(void); @@ -87,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 services; + QMap services; diff --git a/qzeroconfservice.cpp b/qzeroconfservice.cpp new file mode 100644 index 0000000..48e8e29 --- /dev/null +++ b/qzeroconfservice.cpp @@ -0,0 +1,142 @@ +#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 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 QZeroConfService::txt() const +{ + return data->txt; +} + +void QZeroConfService::setTxt(const QMap txt) +{ + data->txt = txt; +} + +void QZeroConfService::appendTxt(QByteArray idx, QByteArray val) +{ + data->txt[idx] = val; +} + +bool QZeroConfService::isValid() const +{ + //TODO is this a proper test + return (!data->name.isEmpty()) && (data->port > 0); +} + + diff --git a/qzeroconfservice.h b/qzeroconfservice.h new file mode 100644 index 0000000..35241cc --- /dev/null +++ b/qzeroconfservice.h @@ -0,0 +1,44 @@ +#ifndef QZEROCONFSERVICE_H +#define QZEROCONFSERVICE_H + +#include +#include + +class QZeroConfServiceData; + +class QZeroConfService +{ +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 txt() const; + void setTxt(const QMap txt); + void appendTxt(QByteArray idx, QByteArray val = ""); + + bool isValid() const; + +private: + QSharedDataPointer data; +}; + +#endif // QZEROCONFSERVICE_H From 3be8677b02a961e8b76410b4c349f045bd22fa2a Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Fri, 6 Oct 2017 17:17:19 +0200 Subject: [PATCH 2/9] ajusted avahi implementation for shared data updated the example for shared data --- avahiclient.cpp | 41 +++++++++++++++++++---------------------- example/window.cpp | 16 ++++++++-------- example/window.h | 4 ++-- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/avahiclient.cpp b/avahiclient.cpp index 8f3036c..589be89 100644 --- a/avahiclient.cpp +++ b/avahiclient.cpp @@ -105,6 +105,8 @@ public: QString key = name + QString::number(interface); QZeroConfPrivate *ref = static_cast(userdata); + QZeroConfService zcs; + switch (event) { case AVAHI_BROWSER_FAILURE: ref->broswerCleanUp(); @@ -122,13 +124,12 @@ 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_ALL_FOR_NOW: case AVAHI_BROWSER_CACHE_EXHAUSTED: break; } @@ -150,7 +151,7 @@ public: AVAHI_GCC_UNUSED void* userdata) { bool newRecord = 0; - QZeroConfService *zcs; + QZeroConfService zcs; QZeroConfPrivate *ref = static_cast(userdata); QString key = name + QString::number(interface); @@ -158,22 +159,21 @@ public: if (ref->pub->services.contains(key)) 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; + newRecord = 1; + 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 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,8 +196,7 @@ 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 + // 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,9 +207,6 @@ public: avahi_service_browser_free(browser); browser = NULL; - QMap::iterator i; - for (i = pub->services.begin(); i != pub->services.end(); i++) - delete *i; pub->services.clear(); QMap::iterator r; @@ -230,7 +227,7 @@ public: }; -QZeroConf::QZeroConf() +QZeroConf::QZeroConf(QObject *parent) : QObject(parent) { pri = new QZeroConfPrivate(this); } diff --git a/example/window.cpp b/example/window.cpp index f58e85f..c5c35e8 100644 --- a/example/window.cpp +++ b/example/window.cpp @@ -52,8 +52,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, SIGNAL(serviceAdded(QZeroConfService )), this, SLOT(addService(QZeroConfService ))); + connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService )), this, SLOT(removeService(QZeroConfService ))); zeroConf.startBrowser("_qtzeroconf_test._tcp"); startPublish(); @@ -126,16 +126,16 @@ void mainWindow::stopPublish() // ---------- 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)) @@ -143,17 +143,17 @@ void mainWindow::addService(QZeroConfService *zcs) #endif } -void mainWindow::removeService(QZeroConfService *zcs) +void mainWindow::removeService(QZeroConfService zcs) { qint32 i, row; QTableWidgetItem *nameItem, *ipItem; - QList search = table.findItems(zcs->name, Qt::MatchExactly); + QList search = table.findItems(zcs.name(), Qt::MatchExactly); for (i=0; irow(); 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); diff --git a/example/window.h b/example/window.h index b259b50..f99bfdf 100644 --- a/example/window.h +++ b/example/window.h @@ -48,8 +48,8 @@ private: private slots: void startPublish(); void stopPublish(); - void addService(QZeroConfService *item); - void removeService(QZeroConfService *item); + void addService(QZeroConfService item); + void removeService(QZeroConfService item); }; #endif /* WINDOW_H_ */ From a77c8ccea53004fe64c5e4b32cff31ca84db4baa Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Mon, 16 Oct 2017 14:55:39 +0200 Subject: [PATCH 3/9] modified sample for shareddata --- example/window.cpp | 18 +++++++++--------- example/window.h | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/example/window.cpp b/example/window.cpp index f58e85f..ea332ef 100644 --- a/example/window.cpp +++ b/example/window.cpp @@ -52,10 +52,10 @@ 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, SIGNAL(serviceAdded(QZeroConfService )), this, SLOT(addService(QZeroConfService ))); + connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService )), this, SLOT(removeService(QZeroConfService ))); - zeroConf.startBrowser("_qtzeroconf_test._tcp"); + zeroConf.startBrowser("_workstation._tcp"); startPublish(); } @@ -126,16 +126,16 @@ void mainWindow::stopPublish() // ---------- 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)) @@ -143,17 +143,17 @@ void mainWindow::addService(QZeroConfService *zcs) #endif } -void mainWindow::removeService(QZeroConfService *zcs) +void mainWindow::removeService(QZeroConfService zcs) { qint32 i, row; QTableWidgetItem *nameItem, *ipItem; - QList search = table.findItems(zcs->name, Qt::MatchExactly); + QList search = table.findItems(zcs.name(), Qt::MatchExactly); for (i=0; irow(); 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); diff --git a/example/window.h b/example/window.h index b259b50..f99bfdf 100644 --- a/example/window.h +++ b/example/window.h @@ -48,8 +48,8 @@ private: private slots: void startPublish(); void stopPublish(); - void addService(QZeroConfService *item); - void removeService(QZeroConfService *item); + void addService(QZeroConfService item); + void removeService(QZeroConfService item); }; #endif /* WINDOW_H_ */ From 3ec363004893faf725d5f1d002ee0297dd3ae689 Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Mon, 16 Oct 2017 17:06:05 +0200 Subject: [PATCH 4/9] made QZeroConfService compareable --- qzeroconfservice.cpp | 5 +++++ qzeroconfservice.h | 1 + 2 files changed, 6 insertions(+) diff --git a/qzeroconfservice.cpp b/qzeroconfservice.cpp index 48e8e29..4afdb6a 100644 --- a/qzeroconfservice.cpp +++ b/qzeroconfservice.cpp @@ -139,4 +139,9 @@ bool QZeroConfService::isValid() const return (!data->name.isEmpty()) && (data->port > 0); } +bool QZeroConfService::operator==(const QZeroConfService &rhs) const +{ + return this->name() == rhs.name() && this->ip() == rhs.ip() && this->ipv6() == rhs.ipv6(); +} + diff --git a/qzeroconfservice.h b/qzeroconfservice.h index 35241cc..a50ee2e 100644 --- a/qzeroconfservice.h +++ b/qzeroconfservice.h @@ -36,6 +36,7 @@ public: void appendTxt(QByteArray idx, QByteArray val = ""); bool isValid() const; + bool operator==(const QZeroConfService &rhs) const; private: QSharedDataPointer data; From 2ab99b3772547a208a4c4006266652dc6c770150 Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Tue, 17 Oct 2017 08:56:41 +0200 Subject: [PATCH 5/9] fixed the example after testing --- example/window.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/window.cpp b/example/window.cpp index ea332ef..df73dde 100644 --- a/example/window.cpp +++ b/example/window.cpp @@ -55,7 +55,7 @@ mainWindow::mainWindow() connect(&zeroConf, SIGNAL(serviceAdded(QZeroConfService )), this, SLOT(addService(QZeroConfService ))); connect(&zeroConf, SIGNAL(serviceRemoved(QZeroConfService )), this, SLOT(removeService(QZeroConfService ))); - zeroConf.startBrowser("_workstation._tcp"); + zeroConf.startBrowser("_qtzeroconf_test._tcp"); startPublish(); } From de81832a862d2371cb0a2310a7db59aef6c4880f Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Tue, 17 Oct 2017 13:52:15 +0200 Subject: [PATCH 6/9] Fixed isValid() checks --- qzeroconfservice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qzeroconfservice.cpp b/qzeroconfservice.cpp index 4afdb6a..3a8b39e 100644 --- a/qzeroconfservice.cpp +++ b/qzeroconfservice.cpp @@ -135,8 +135,8 @@ void QZeroConfService::appendTxt(QByteArray idx, QByteArray val) bool QZeroConfService::isValid() const { - //TODO is this a proper test - return (!data->name.isEmpty()) && (data->port > 0); + + return (!data->name.isEmpty()); } bool QZeroConfService::operator==(const QZeroConfService &rhs) const From 846069fb199c37c6d547412173d0d3f9a3546318 Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Tue, 17 Oct 2017 18:46:23 +0200 Subject: [PATCH 7/9] QZeroConfService became a Q_GADGET for easy usage in QML --- qzeroconfservice.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/qzeroconfservice.h b/qzeroconfservice.h index a50ee2e..9b1b392 100644 --- a/qzeroconfservice.h +++ b/qzeroconfservice.h @@ -8,7 +8,14 @@ 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 &); @@ -42,4 +49,6 @@ private: QSharedDataPointer data; }; +Q_DECLARE_METATYPE(QZeroConfService) + #endif // QZEROCONFSERVICE_H From 2d21502972697684aa95228aaba0f4c231a66c4c Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Thu, 19 Oct 2017 20:51:49 +0200 Subject: [PATCH 8/9] spaces to tabs --- avahiclient.cpp | 76 ++++++++++++++++++++++---------------------- avahicore.cpp | 42 ++++++++++++------------ bonjour.cpp | 44 ++++++++++++------------- bonjour_p.h | 14 ++++---- example/main.cpp | 10 +++--- example/window.cpp | 26 +++++++-------- example/window.h | 14 ++++---- qzeroconf.h | 20 ++++++------ qzeroconfservice.cpp | 68 +++++++++++++++++++-------------------- qzeroconfservice.h | 62 ++++++++++++++++++------------------ 10 files changed, 187 insertions(+), 189 deletions(-) diff --git a/avahiclient.cpp b/avahiclient.cpp index c755aaf..2925083 100644 --- a/avahiclient.cpp +++ b/avahiclient.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : avahiclient.cpp - Created : 20 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : avahiclient.cpp + Created : 20 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Avahi-client wrapper for use in Desktop Linux systems + Avahi-client wrapper for use in Desktop Linux systems --------------------------------------------------------------------------------------------------- **************************************************************************************************/ //#include // @@ -105,7 +105,7 @@ public: QString key = name + QString::number(interface); QZeroConfPrivate *ref = static_cast(userdata); - QZeroConfService zcs; + QZeroConfService zcs; switch (event) { case AVAHI_BROWSER_FAILURE: @@ -129,29 +129,29 @@ public: ref->pub->services.remove(key); emit ref->pub->serviceRemoved(zcs); break; - case AVAHI_BROWSER_ALL_FOR_NOW: + case AVAHI_BROWSER_ALL_FOR_NOW: case AVAHI_BROWSER_CACHE_EXHAUSTED: break; } } static void resolveCallback( - AVAHI_GCC_UNUSED AvahiServiceResolver *r, - AVAHI_GCC_UNUSED AvahiIfIndex interface, - AVAHI_GCC_UNUSED AvahiProtocol protocol, - AvahiResolverEvent event, - const char *name, - const char *type, - const char *domain, - const char *host_name, - const AvahiAddress *address, - uint16_t port, - AvahiStringList *txt, - AvahiLookupResultFlags, - AVAHI_GCC_UNUSED void* userdata) + AVAHI_GCC_UNUSED AvahiServiceResolver *r, + AVAHI_GCC_UNUSED AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, + const char *type, + const char *domain, + const char *host_name, + const AvahiAddress *address, + uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags, + AVAHI_GCC_UNUSED void* userdata) { bool newRecord = 0; - QZeroConfService zcs; + QZeroConfService zcs; QZeroConfPrivate *ref = static_cast(userdata); QString key = name + QString::number(interface); @@ -159,21 +159,21 @@ public: if (ref->pub->services.contains(key)) zcs = ref->pub->services[key]; else { - newRecord = 1; - zcs.setName(name); - zcs.setType(type); - zcs.setDomain( domain); - zcs.setHost( host_name); - zcs.setInterfaceIndex(interface); - zcs.setPort(port); + newRecord = 1; + 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 pair = avahiText.split('='); if (pair.size() == 2) - zcs.appendTxt(pair.at(0), pair.at(1)); + zcs.appendTxt(pair.at(0), pair.at(1)); else - zcs.appendTxt(pair.at(0)); + zcs.appendTxt(pair.at(0)); txt = txt->next; } ref->pub->services.insert(key, zcs); @@ -181,11 +181,11 @@ public: char a[AVAHI_ADDRESS_STR_MAX]; avahi_address_snprint(a, sizeof(a), address); - QHostAddress addr(a); + QHostAddress addr(a); if (protocol == AVAHI_PROTO_INET6) - zcs.setIpv6(addr); + zcs.setIpv6(addr); else if (protocol == AVAHI_PROTO_INET) - zcs.setIp(addr); + zcs.setIp(addr); if (newRecord) emit ref->pub->serviceAdded(zcs); @@ -196,7 +196,7 @@ public: zcs = ref->pub->services[key]; ref->pub->services.remove(key); emit ref->pub->serviceRemoved(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 + // 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,9 +207,9 @@ public: avahi_service_browser_free(browser); browser = NULL; - QMap::iterator i; + QMap::iterator i; for (i = pub->services.begin(); i != pub->services.end(); i++) { - emit pub->serviceRemoved(i.value()); + emit pub->serviceRemoved(i.value()); } @@ -217,7 +217,7 @@ public: QMap::iterator r; for (r = resolvers.begin(); r != resolvers.end(); r++) - avahi_service_resolver_free(*r); + avahi_service_resolver_free(*r); resolvers.clear(); } @@ -303,7 +303,7 @@ void QZeroConf::clearServiceTxtRecords() void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol) { - AvahiProtocol avahiProtocol; + AvahiProtocol avahiProtocol; if (pri->browser) emit error(QZeroConf::browserFailed); diff --git a/avahicore.cpp b/avahicore.cpp index 091514b..dc83818 100644 --- a/avahicore.cpp +++ b/avahicore.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : avahicore.cpp - Created : 9 September 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : avahicore.cpp + Created : 9 September 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Avahi-core wrapper for use in embedded Linux systems (Android) + Avahi-core wrapper for use in embedded Linux systems (Android) --------------------------------------------------------------------------------------------------- **************************************************************************************************/ //#include // @@ -67,9 +67,9 @@ public: switch (state) { case AVAHI_SERVER_RUNNING: ref->ready = 1; - if (ref->registerWaiting) { - ref->registerWaiting = 0; - ref->registerService(ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), ref->port); + if (ref->registerWaiting) { + ref->registerWaiting = 0; + ref->registerService(ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), ref->port); } break; case AVAHI_SERVER_COLLISION: @@ -147,19 +147,19 @@ public: } static void resolveCallback( - AVAHI_GCC_UNUSED AvahiSServiceResolver *r, - AVAHI_GCC_UNUSED AvahiIfIndex interface, - AVAHI_GCC_UNUSED AvahiProtocol protocol, - AvahiResolverEvent event, - const char *name, - const char *type, - const char *domain, - const char *host_name, - const AvahiAddress *address, - uint16_t port, - AvahiStringList *txt, - AvahiLookupResultFlags, - AVAHI_GCC_UNUSED void* userdata) + AVAHI_GCC_UNUSED AvahiSServiceResolver *r, + AVAHI_GCC_UNUSED AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, + const char *type, + const char *domain, + const char *host_name, + const AvahiAddress *address, + uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags, + AVAHI_GCC_UNUSED void* userdata) { bool newRecord = 0; QZeroConfService *zcs; diff --git a/bonjour.cpp b/bonjour.cpp index 041b974..c44ce94 100644 --- a/bonjour.cpp +++ b/bonjour.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : bonjour.cpp - Created : 20 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : bonjour.cpp + Created : 20 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Wrapper for Apple's Bonjour library for use on Windows, MACs and iOS + Wrapper for Apple's Bonjour library for use on Windows, MACs and iOS --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include "qzeroconf.h" @@ -69,7 +69,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 +103,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(userdata); //qDebug() << name; @@ -111,12 +111,12 @@ 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.setName(name); - zcs.setType( type); - zcs.setDomain(domain); - zcs.setInterfaceIndex(interfaceIndex); - if (!ref->newService.isValid()) { + //zcs = new QZeroConfService; + zcs.setName(name); + zcs.setType( type); + zcs.setDomain(domain); + zcs.setInterfaceIndex(interfaceIndex); + if (!ref->newService.isValid()) { ref->newService = zcs; ref->resolve(); } @@ -157,15 +157,15 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags QByteArray avahiText((const char *)txtRecord, recLen); QList pair = avahiText.split('='); if (pair.size() == 2) - ref->newService.appendTxt(pair.at(0), pair.at(1)); + ref->newService.appendTxt(pair.at(0), pair.at(1)); else - ref->newService.appendTxt(pair.at(0)); + ref->newService.appendTxt(pair.at(0)); txtLen-= recLen + 1; txtRecord+= recLen; } - ref->newService.setHost(hostName); - ref->newService.setPort(qFromBigEndian(port)); + ref->newService.setHost(hostName); + ref->newService.setPort(qFromBigEndian(port)); err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref); if (err == kDNSServiceErr_NoError) { int sockfd = DNSServiceRefSockFD(ref->resolver); @@ -198,11 +198,11 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef, if ((flags & kDNSServiceFlagsAdd) != 0) { QHostAddress hAddress(address); if (hAddress.protocol() == QAbstractSocket::IPv6Protocol) - ref->newService.setIpv6(hAddress); + ref->newService.setIpv6(hAddress); else - ref->newService.setIp(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 +212,7 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef, } if (!(flags & kDNSServiceFlagsMoreComing)) { - ref->newService = QZeroConfService(); // 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); } } @@ -246,7 +246,7 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean) browserSocket = NULL; } - QMap::iterator i; + QMap::iterator i; for (i = pub->services.begin(); i != pub->services.end(); i++) { emit pub->serviceRemoved(*i); } diff --git a/bonjour_p.h b/bonjour_p.h index 792411b..d339a27 100644 --- a/bonjour_p.h +++ b/bonjour_p.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : bonjour_p.h - Created : 22 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : bonjour_p.h + Created : 22 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Part of wrapper for Apple's Bonjour library for use on Windows, MACs and iOS. Needed for slots. + Part of wrapper for Apple's Bonjour library for use on Windows, MACs and iOS. Needed for slots. --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #ifndef QZEROCONFPRIVATE_H_ @@ -64,8 +64,8 @@ public: DNSServiceRef dnssRef, browser, resolver; DNSServiceProtocol protocol; QSocketNotifier *bs, *browserSocket, *resolverSocket, *addressSocket; - QZeroConfService newService; - QList work; + QZeroConfService newService; + QList work; QByteArray txt; public slots: diff --git a/example/main.cpp b/example/main.cpp index 1f0e45c..9780e71 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf Example - File name : main.cpp - Created : 3 November 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf Example + File name : main.cpp + Created : 3 November 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Example app to demonstrate service publishing and service discovery + Example app to demonstrate service publishing and service discovery --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include diff --git a/example/window.cpp b/example/window.cpp index cc10272..e04c1da 100644 --- a/example/window.cpp +++ b/example/window.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf Example - File name : window.cpp - Created : 3 November 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf Example + File name : window.cpp + Created : 3 November 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Example app to demonstrate service publishing and service discovery + Example app to demonstrate service publishing and service discovery --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include @@ -54,8 +54,8 @@ mainWindow::mainWindow() buildGUI(); - connect(&zeroConf, &QZeroConf::serviceAdded, this, &mainWindow::addService); - connect(&zeroConf, &QZeroConf::serviceRemoved, this, &mainWindow::removeService); + 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; @@ -93,7 +93,7 @@ void mainWindow::buildGUI() QString mainWindow::buildName(void) { - QString name; + QString name; QList list = QNetworkInterface::allInterfaces(); // now you have interfaces list @@ -102,7 +102,7 @@ QString mainWindow::buildName(void) name.remove(0, 6); name+= ')'; name.prepend("Qt ZeroConf Test - " OS_NAME " ("); - return name; + return name; } void mainWindow::appStateChanged(Qt::ApplicationState state) @@ -154,9 +154,9 @@ void mainWindow::addService(QZeroConfService zcs) 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)) @@ -169,12 +169,12 @@ void mainWindow::removeService(QZeroConfService zcs) qint32 i, row; QTableWidgetItem *nameItem, *ipItem; - QList search = table.findItems(zcs.name(), Qt::MatchExactly); + QList search = table.findItems(zcs.name(), Qt::MatchExactly); for (i=0; irow(); 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); diff --git a/example/window.h b/example/window.h index d7f61d6..97a984a 100644 --- a/example/window.h +++ b/example/window.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf Example - File name : window.h - Created : 3 November 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf Example + File name : window.h + Created : 3 November 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Example app to demonstrate service publishing and service discovery + Example app to demonstrate service publishing and service discovery --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #ifndef WINDOW_H_ @@ -51,8 +51,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); }; diff --git a/qzeroconf.h b/qzeroconf.h index d64fa40..4c9d08f 100644 --- a/qzeroconf.h +++ b/qzeroconf.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : qzeroconf.h - Created : 20 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : qzeroconf.h + Created : 20 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - QtZeroConf class definition + QtZeroConf class definition --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #ifndef QZEROCONF_H_ @@ -60,7 +60,7 @@ public: serviceNameCollision = -2, browserFailed = -3, }; - QZeroConf(QObject *parent = Q_NULLPTR); + QZeroConf(QObject *parent = Q_NULLPTR); ~QZeroConf(); void startServicePublish(const char *name, const char *type, const char *domain, quint16 port); void stopServicePublish(void); @@ -79,13 +79,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 services; + QMap services; diff --git a/qzeroconfservice.cpp b/qzeroconfservice.cpp index c17dae7..f02d691 100644 --- a/qzeroconfservice.cpp +++ b/qzeroconfservice.cpp @@ -4,15 +4,15 @@ class QZeroConfServiceData : public QSharedData { public: - QString name; - QString type; - QString domain; - QString host; - QHostAddress ip; - QHostAddress ipv6; - quint32 interfaceIndex; - quint16 port = 0; - QMap txt; + QString name; + QString type; + QString domain; + QString host; + QHostAddress ip; + QHostAddress ipv6; + quint32 interfaceIndex; + quint16 port = 0; + QMap txt; }; @@ -28,9 +28,9 @@ QZeroConfService::QZeroConfService(const QZeroConfService &rhs) : data(rhs.data) QZeroConfService &QZeroConfService::operator=(const QZeroConfService &rhs) { - if (this != &rhs) - data.operator=(rhs.data); - return *this; + if (this != &rhs) + data.operator=(rhs.data); + return *this; } QZeroConfService::~QZeroConfService() @@ -40,108 +40,106 @@ QZeroConfService::~QZeroConfService() QString QZeroConfService::name() const { - return data->name; + return data->name; } void QZeroConfService::setName(const QString &name) { - data->name = name; + data->name = name; } QString QZeroConfService::type() const { - return data->type; + return data->type; } void QZeroConfService::setType(const QString &type) { - data->type = type; + data->type = type; } QString QZeroConfService::domain() const { - return data->domain; + return data->domain; } void QZeroConfService::setDomain(const QString &domain) { - data->domain = domain; + data->domain = domain; } QString QZeroConfService::host() const { - return data->host; + return data->host; } void QZeroConfService::setHost(const QString &host) { - data->host = host; + data->host = host; } QHostAddress QZeroConfService::ip() const { - return data->ip; + return data->ip; } void QZeroConfService::setIp(QHostAddress &ip) { - data->ip = ip; + data->ip = ip; } QHostAddress QZeroConfService::ipv6() const { - return data->ipv6; + return data->ipv6; } void QZeroConfService::setIpv6(const QHostAddress &ipv6) { - data->ipv6 = ipv6; + data->ipv6 = ipv6; } quint32 QZeroConfService::interfaceIndex() const { - return data->interfaceIndex; + return data->interfaceIndex; } void QZeroConfService::setInterfaceIndex(const quint32 &interfaceIndex) { - data->interfaceIndex = interfaceIndex; + data->interfaceIndex = interfaceIndex; } quint16 QZeroConfService::port() const { - return data->port; + return data->port; } void QZeroConfService::setPort(const quint16 port) { - data->port = port; + data->port = port; } QMap QZeroConfService::txt() const { - return data->txt; + return data->txt; } void QZeroConfService::setTxt(const QMap txt) { - data->txt = txt; + data->txt = txt; } void QZeroConfService::appendTxt(QByteArray idx, QByteArray val) { - data->txt[idx] = val; + data->txt[idx] = val; } bool QZeroConfService::isValid() const { - return (!data->name.isEmpty()); + return (!data->name.isEmpty()); } bool QZeroConfService::operator==(const QZeroConfService &rhs) const { - return this->name() == rhs.name() && (this->ip() == rhs.ip() || this->ipv6() == rhs.ipv6()); + return this->name() == rhs.name() && (this->ip() == rhs.ip() || this->ipv6() == rhs.ipv6()); } - - diff --git a/qzeroconfservice.h b/qzeroconfservice.h index 9b1b392..853ba31 100644 --- a/qzeroconfservice.h +++ b/qzeroconfservice.h @@ -8,45 +8,45 @@ 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 ) + 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(); + 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 txt() const; - void setTxt(const QMap txt); - void appendTxt(QByteArray idx, QByteArray val = ""); + 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 txt() const; + void setTxt(const QMap txt); + void appendTxt(QByteArray idx, QByteArray val = ""); - bool isValid() const; - bool operator==(const QZeroConfService &rhs) const; + bool isValid() const; + bool operator==(const QZeroConfService &rhs) const; private: - QSharedDataPointer data; + QSharedDataPointer data; }; Q_DECLARE_METATYPE(QZeroConfService) From 323d9494f55c479e832a3c0e8290f56ee094aef6 Mon Sep 17 00:00:00 2001 From: Matthias Kollmann Date: Mon, 23 Oct 2017 20:12:00 +0200 Subject: [PATCH 9/9] Applied undo_some_whitespace_changes.patch --- avahiclient.cpp | 51 ++++++++++++++++++++++------------------------ avahicore.cpp | 42 +++++++++++++++++++------------------- bonjour.cpp | 43 +++++++++++++++++--------------------- bonjour_p.h | 10 ++++----- example/main.cpp | 10 ++++----- example/window.cpp | 24 ++++++++++------------ example/window.h | 12 +++++------ qzeroconf.h | 14 ++++++------- 8 files changed, 96 insertions(+), 110 deletions(-) diff --git a/avahiclient.cpp b/avahiclient.cpp index 2925083..dc9f097 100644 --- a/avahiclient.cpp +++ b/avahiclient.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : avahiclient.cpp - Created : 20 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : avahiclient.cpp + Created : 20 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Avahi-client wrapper for use in Desktop Linux systems + Avahi-client wrapper for use in Desktop Linux systems --------------------------------------------------------------------------------------------------- **************************************************************************************************/ //#include // @@ -136,19 +136,19 @@ public: } static void resolveCallback( - AVAHI_GCC_UNUSED AvahiServiceResolver *r, - AVAHI_GCC_UNUSED AvahiIfIndex interface, - AVAHI_GCC_UNUSED AvahiProtocol protocol, - AvahiResolverEvent event, - const char *name, - const char *type, - const char *domain, - const char *host_name, - const AvahiAddress *address, - uint16_t port, - AvahiStringList *txt, - AvahiLookupResultFlags, - AVAHI_GCC_UNUSED void* userdata) + AVAHI_GCC_UNUSED AvahiServiceResolver *r, + AVAHI_GCC_UNUSED AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, + const char *type, + const char *domain, + const char *host_name, + const AvahiAddress *address, + uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags, + AVAHI_GCC_UNUSED void* userdata) { bool newRecord = 0; QZeroConfService zcs; @@ -162,8 +162,8 @@ public: newRecord = 1; zcs.setName(name); zcs.setType(type); - zcs.setDomain( domain); - zcs.setHost( host_name); + zcs.setDomain(domain); + zcs.setHost(host_name); zcs.setInterfaceIndex(interface); zcs.setPort(port); while (txt) // get txt records @@ -209,15 +209,13 @@ public: QMap::iterator i; for (i = pub->services.begin(); i != pub->services.end(); i++) { - emit pub->serviceRemoved(i.value()); - + emit pub->serviceRemoved(i.value()); } - pub->services.clear(); QMap::iterator r; for (r = resolvers.begin(); r != resolvers.end(); r++) - avahi_service_resolver_free(*r); + avahi_service_resolver_free(*r); resolvers.clear(); } @@ -233,8 +231,7 @@ public: }; - -QZeroConf::QZeroConf(QObject *parent) : QObject(parent) +QZeroConf::QZeroConf(QObject *parent) : QObject (parent) { pri = new QZeroConfPrivate(this); } @@ -303,7 +300,7 @@ void QZeroConf::clearServiceTxtRecords() void QZeroConf::startBrowser(QString type, QAbstractSocket::NetworkLayerProtocol protocol) { - AvahiProtocol avahiProtocol; + AvahiProtocol avahiProtocol; if (pri->browser) emit error(QZeroConf::browserFailed); diff --git a/avahicore.cpp b/avahicore.cpp index dc83818..091514b 100644 --- a/avahicore.cpp +++ b/avahicore.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : avahicore.cpp - Created : 9 September 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : avahicore.cpp + Created : 9 September 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Avahi-core wrapper for use in embedded Linux systems (Android) + Avahi-core wrapper for use in embedded Linux systems (Android) --------------------------------------------------------------------------------------------------- **************************************************************************************************/ //#include // @@ -67,9 +67,9 @@ public: switch (state) { case AVAHI_SERVER_RUNNING: ref->ready = 1; - if (ref->registerWaiting) { - ref->registerWaiting = 0; - ref->registerService(ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), ref->port); + if (ref->registerWaiting) { + ref->registerWaiting = 0; + ref->registerService(ref->name.toUtf8(), ref->type.toUtf8(), ref->domain.toUtf8(), ref->port); } break; case AVAHI_SERVER_COLLISION: @@ -147,19 +147,19 @@ public: } static void resolveCallback( - AVAHI_GCC_UNUSED AvahiSServiceResolver *r, - AVAHI_GCC_UNUSED AvahiIfIndex interface, - AVAHI_GCC_UNUSED AvahiProtocol protocol, - AvahiResolverEvent event, - const char *name, - const char *type, - const char *domain, - const char *host_name, - const AvahiAddress *address, - uint16_t port, - AvahiStringList *txt, - AvahiLookupResultFlags, - AVAHI_GCC_UNUSED void* userdata) + AVAHI_GCC_UNUSED AvahiSServiceResolver *r, + AVAHI_GCC_UNUSED AvahiIfIndex interface, + AVAHI_GCC_UNUSED AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, + const char *type, + const char *domain, + const char *host_name, + const AvahiAddress *address, + uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags, + AVAHI_GCC_UNUSED void* userdata) { bool newRecord = 0; QZeroConfService *zcs; diff --git a/bonjour.cpp b/bonjour.cpp index c44ce94..6efcbcb 100644 --- a/bonjour.cpp +++ b/bonjour.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : bonjour.cpp - Created : 20 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : bonjour.cpp + Created : 20 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Wrapper for Apple's Bonjour library for use on Windows, MACs and iOS + Wrapper for Apple's Bonjour library for use on Windows, MACs and iOS --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include "qzeroconf.h" @@ -37,7 +37,6 @@ QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent) browserSocket = NULL; resolverSocket = NULL; addressSocket = 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(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.setName(name); - zcs.setType( type); - zcs.setDomain(domain); - zcs.setInterfaceIndex(interfaceIndex); - if (!ref->newService.isValid()) { + 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); - } } else { @@ -157,15 +154,15 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags QByteArray avahiText((const char *)txtRecord, recLen); QList pair = avahiText.split('='); if (pair.size() == 2) - ref->newService.appendTxt(pair.at(0), pair.at(1)); + ref->newService.appendTxt(pair.at(0), pair.at(1)); else - ref->newService.appendTxt(pair.at(0)); + ref->newService.appendTxt(pair.at(0)); txtLen-= recLen + 1; txtRecord+= recLen; } - ref->newService.setHost(hostName); - ref->newService.setPort(qFromBigEndian(port)); + ref->newService.setHost(hostName); + ref->newService.setPort(qFromBigEndian(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.setIpv6(hAddress); + ref->newService.setIpv6(hAddress); else - ref->newService.setIp(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); @@ -245,12 +242,10 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean) delete browserSocket; browserSocket = NULL; } - QMap::iterator i; for (i = pub->services.begin(); i != pub->services.end(); i++) { emit pub->serviceRemoved(*i); } - pub->services.clear(); } else if (toClean == dnssRef) { diff --git a/bonjour_p.h b/bonjour_p.h index d339a27..675bcf3 100644 --- a/bonjour_p.h +++ b/bonjour_p.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : bonjour_p.h - Created : 22 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : bonjour_p.h + Created : 22 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Part of wrapper for Apple's Bonjour library for use on Windows, MACs and iOS. Needed for slots. + Part of wrapper for Apple's Bonjour library for use on Windows, MACs and iOS. Needed for slots. --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #ifndef QZEROCONFPRIVATE_H_ diff --git a/example/main.cpp b/example/main.cpp index 9780e71..1f0e45c 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf Example - File name : main.cpp - Created : 3 November 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf Example + File name : main.cpp + Created : 3 November 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Example app to demonstrate service publishing and service discovery + Example app to demonstrate service publishing and service discovery --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include diff --git a/example/window.cpp b/example/window.cpp index e04c1da..61abf41 100644 --- a/example/window.cpp +++ b/example/window.cpp @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf Example - File name : window.cpp - Created : 3 November 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf Example + File name : window.cpp + Created : 3 November 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Example app to demonstrate service publishing and service discovery + Example app to demonstrate service publishing and service discovery --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #include @@ -53,13 +53,11 @@ mainWindow::mainWindow() publishEnabled = 0; buildGUI(); - 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; - } void mainWindow::buildGUI() @@ -93,7 +91,7 @@ void mainWindow::buildGUI() QString mainWindow::buildName(void) { - QString name; + QString name; QList list = QNetworkInterface::allInterfaces(); // now you have interfaces list @@ -102,7 +100,7 @@ QString mainWindow::buildName(void) name.remove(0, 6); name+= ')'; name.prepend("Qt ZeroConf Test - " OS_NAME " ("); - return name; + return name; } void mainWindow::appStateChanged(Qt::ApplicationState state) @@ -154,9 +152,9 @@ void mainWindow::addService(QZeroConfService zcs) 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)) @@ -169,12 +167,12 @@ void mainWindow::removeService(QZeroConfService zcs) qint32 i, row; QTableWidgetItem *nameItem, *ipItem; - QList search = table.findItems(zcs.name(), Qt::MatchExactly); + QList search = table.findItems(zcs.name(), Qt::MatchExactly); for (i=0; irow(); 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); diff --git a/example/window.h b/example/window.h index 97a984a..ca702df 100644 --- a/example/window.h +++ b/example/window.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf Example - File name : window.h - Created : 3 November 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf Example + File name : window.h + Created : 3 November 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - Example app to demonstrate service publishing and service discovery + Example app to demonstrate service publishing and service discovery --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #ifndef WINDOW_H_ @@ -47,13 +47,11 @@ private: bool publishEnabled; private slots: - void appStateChanged(Qt::ApplicationState state); void startPublishClicked(); void stopPublishClicked(); void addService(QZeroConfService item); void removeService(QZeroConfService item); - }; #endif /* WINDOW_H_ */ diff --git a/qzeroconf.h b/qzeroconf.h index 4c9d08f..89b1120 100644 --- a/qzeroconf.h +++ b/qzeroconf.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU Lesser General Public License along with QtZeroConf. If not, see . --------------------------------------------------------------------------------------------------- - Project name : QtZeroConf - File name : qzeroconf.h - Created : 20 July 2015 - Author(s) : Jonathan Bagg + Project name : QtZeroConf + File name : qzeroconf.h + Created : 20 July 2015 + Author(s) : Jonathan Bagg --------------------------------------------------------------------------------------------------- - QtZeroConf class definition + QtZeroConf class definition --------------------------------------------------------------------------------------------------- **************************************************************************************************/ #ifndef QZEROCONF_H_ @@ -43,8 +43,6 @@ # define Q_ZEROCONF_EXPORT #endif - - class QZeroConfPrivate; class Q_ZEROCONF_EXPORT QZeroConf : public QObject @@ -60,7 +58,7 @@ public: serviceNameCollision = -2, browserFailed = -3, }; - QZeroConf(QObject *parent = Q_NULLPTR); + QZeroConf(QObject *parent = Q_NULLPTR); ~QZeroConf(); void startServicePublish(const char *name, const char *type, const char *domain, quint16 port); void stopServicePublish(void);