forked from jbagg/QtZeroConf
redesigned QZeroConfService as shared data, wip compiles on mac
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.user
|
45
bonjour.cpp
45
bonjour.cpp
@@ -37,7 +37,7 @@ QZeroConfPrivate::QZeroConfPrivate(QZeroConf *parent)
|
|||||||
browserSocket = NULL;
|
browserSocket = NULL;
|
||||||
resolverSocket = NULL;
|
resolverSocket = NULL;
|
||||||
addressSocket = NULL;
|
addressSocket = NULL;
|
||||||
newService = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QZeroConfPrivate::bsRead()
|
void QZeroConfPrivate::bsRead()
|
||||||
@@ -69,7 +69,7 @@ void QZeroConfPrivate::resolve(void)
|
|||||||
{
|
{
|
||||||
DNSServiceErrorType err;
|
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) {
|
if (err == kDNSServiceErr_NoError) {
|
||||||
int sockfd = DNSServiceRefSockFD(resolver);
|
int sockfd = DNSServiceRefSockFD(resolver);
|
||||||
if (sockfd == -1) {
|
if (sockfd == -1) {
|
||||||
@@ -103,7 +103,7 @@ void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags f
|
|||||||
const char *type, const char *domain, void *userdata)
|
const char *type, const char *domain, void *userdata)
|
||||||
{
|
{
|
||||||
QString key;
|
QString key;
|
||||||
QZeroConfService *zcs;
|
QZeroConfService zcs;
|
||||||
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
|
QZeroConfPrivate *ref = static_cast<QZeroConfPrivate *>(userdata);
|
||||||
|
|
||||||
//qDebug() << name;
|
//qDebug() << name;
|
||||||
@@ -111,12 +111,12 @@ void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags f
|
|||||||
key = name + QString::number(interfaceIndex);
|
key = name + QString::number(interfaceIndex);
|
||||||
if (flags & kDNSServiceFlagsAdd) {
|
if (flags & kDNSServiceFlagsAdd) {
|
||||||
if (!ref->pub->services.contains(key)) {
|
if (!ref->pub->services.contains(key)) {
|
||||||
zcs = new QZeroConfService;
|
//zcs = new QZeroConfService;
|
||||||
zcs->name = name;
|
zcs.setName(name);
|
||||||
zcs->type = type;
|
zcs.setType( type);
|
||||||
zcs->domain = domain;
|
zcs.setDomain(domain);
|
||||||
zcs->interfaceIndex = interfaceIndex;
|
zcs.setInterfaceIndex(interfaceIndex);
|
||||||
if (!ref->newService) {
|
if (!ref->newService.isValid()) {
|
||||||
ref->newService = zcs;
|
ref->newService = zcs;
|
||||||
ref->resolve();
|
ref->resolve();
|
||||||
}
|
}
|
||||||
@@ -128,7 +128,7 @@ void DNSSD_API QZeroConfPrivate::browseCallback(DNSServiceRef, DNSServiceFlags f
|
|||||||
zcs = ref->pub->services[key];
|
zcs = ref->pub->services[key];
|
||||||
ref->pub->services.remove(key);
|
ref->pub->services.remove(key);
|
||||||
emit ref->pub->serviceRemoved(zcs);
|
emit ref->pub->serviceRemoved(zcs);
|
||||||
delete zcs;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -157,15 +157,15 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags
|
|||||||
QByteArray avahiText((const char *)txtRecord, recLen);
|
QByteArray avahiText((const char *)txtRecord, recLen);
|
||||||
QList<QByteArray> pair = avahiText.split('=');
|
QList<QByteArray> pair = avahiText.split('=');
|
||||||
if (pair.size() == 2)
|
if (pair.size() == 2)
|
||||||
ref->newService->txt[pair.at(0)] = pair.at(1);
|
ref->newService.appendTxt(pair.at(0), pair.at(1));
|
||||||
else
|
else
|
||||||
ref->newService->txt[pair.at(0)] = "";
|
ref->newService.appendTxt(pair.at(0));
|
||||||
|
|
||||||
txtLen-= recLen + 1;
|
txtLen-= recLen + 1;
|
||||||
txtRecord+= recLen;
|
txtRecord+= recLen;
|
||||||
}
|
}
|
||||||
ref->newService->host = hostName;
|
ref->newService.setHost(hostName);
|
||||||
ref->newService->port = qFromBigEndian<quint16>(port);
|
ref->newService.setPort(qFromBigEndian<quint16>(port));
|
||||||
err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref);
|
err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref);
|
||||||
if (err == kDNSServiceErr_NoError) {
|
if (err == kDNSServiceErr_NoError) {
|
||||||
int sockfd = DNSServiceRefSockFD(ref->resolver);
|
int sockfd = DNSServiceRefSockFD(ref->resolver);
|
||||||
@@ -198,11 +198,11 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef,
|
|||||||
if ((flags & kDNSServiceFlagsAdd) != 0) {
|
if ((flags & kDNSServiceFlagsAdd) != 0) {
|
||||||
QHostAddress hAddress(address);
|
QHostAddress hAddress(address);
|
||||||
if (hAddress.protocol() == QAbstractSocket::IPv6Protocol)
|
if (hAddress.protocol() == QAbstractSocket::IPv6Protocol)
|
||||||
ref->newService->ipv6 = hAddress;
|
ref->newService.setIpv6(hAddress);
|
||||||
else
|
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)) {
|
if (!ref->pub->services.contains(key)) {
|
||||||
ref->pub->services.insert(key, ref->newService);
|
ref->pub->services.insert(key, ref->newService);
|
||||||
emit ref->pub->serviceAdded(ref->newService);
|
emit ref->pub->serviceAdded(ref->newService);
|
||||||
@@ -212,7 +212,7 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef,
|
|||||||
|
|
||||||
}
|
}
|
||||||
if (!(flags & kDNSServiceFlagsMoreComing)) {
|
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);
|
ref->cleanUp(ref->resolver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -233,10 +233,6 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean)
|
|||||||
delete resolverSocket;
|
delete resolverSocket;
|
||||||
resolverSocket = NULL;
|
resolverSocket = NULL;
|
||||||
}
|
}
|
||||||
if (newService) {
|
|
||||||
delete newService;
|
|
||||||
newService = NULL;
|
|
||||||
}
|
|
||||||
if (work.size()) {
|
if (work.size()) {
|
||||||
newService = work.first();
|
newService = work.first();
|
||||||
work.removeFirst();
|
work.removeFirst();
|
||||||
@@ -249,9 +245,6 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean)
|
|||||||
delete browserSocket;
|
delete browserSocket;
|
||||||
browserSocket = NULL;
|
browserSocket = NULL;
|
||||||
}
|
}
|
||||||
QMap<QString, QZeroConfService *>::iterator i;
|
|
||||||
for (i = pub->services.begin(); i != pub->services.end(); i++)
|
|
||||||
delete *i;
|
|
||||||
pub->services.clear();
|
pub->services.clear();
|
||||||
}
|
}
|
||||||
else if (toClean == dnssRef) {
|
else if (toClean == dnssRef) {
|
||||||
@@ -265,7 +258,7 @@ void QZeroConfPrivate::cleanUp(DNSServiceRef toClean)
|
|||||||
DNSServiceRefDeallocate(toClean);
|
DNSServiceRefDeallocate(toClean);
|
||||||
}
|
}
|
||||||
|
|
||||||
QZeroConf::QZeroConf()
|
QZeroConf::QZeroConf(QObject *parent) : QObject (parent)
|
||||||
{
|
{
|
||||||
pri = new QZeroConfPrivate(this);
|
pri = new QZeroConfPrivate(this);
|
||||||
}
|
}
|
||||||
|
@@ -64,8 +64,8 @@ public:
|
|||||||
DNSServiceRef dnssRef, browser, resolver;
|
DNSServiceRef dnssRef, browser, resolver;
|
||||||
DNSServiceProtocol protocol;
|
DNSServiceProtocol protocol;
|
||||||
QSocketNotifier *bs, *browserSocket, *resolverSocket, *addressSocket;
|
QSocketNotifier *bs, *browserSocket, *resolverSocket, *addressSocket;
|
||||||
QZeroConfService *newService;
|
QZeroConfService newService;
|
||||||
QList<QZeroConfService *> work;
|
QList<QZeroConfService> work;
|
||||||
QByteArray txt;
|
QByteArray txt;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
@@ -115,3 +115,9 @@ android {
|
|||||||
SOURCES+= $$ACR/wide-area.c
|
SOURCES+= $$ACR/wide-area.c
|
||||||
#avahi-core/iface-none.c avahi-core/iface-pfroute.c avahi-core/avahi-reflector.c
|
#avahi-core/iface-none.c avahi-core/iface-pfroute.c avahi-core/avahi-reflector.c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
$$PWD/qzeroconfservice.h
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
$$PWD/qzeroconfservice.cpp
|
||||||
|
24
qzeroconf.h
24
qzeroconf.h
@@ -31,6 +31,7 @@
|
|||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QtCore/QtGlobal>
|
#include <QtCore/QtGlobal>
|
||||||
|
#include "qzeroconfservice.h"
|
||||||
|
|
||||||
#if (!defined(QT_STATIC) && !defined(QZEROCONF_STATIC))
|
#if (!defined(QT_STATIC) && !defined(QZEROCONF_STATIC))
|
||||||
# ifdef QT_BUILD_ZEROCONF_LIB
|
# ifdef QT_BUILD_ZEROCONF_LIB
|
||||||
@@ -42,18 +43,7 @@
|
|||||||
# define Q_ZEROCONF_EXPORT
|
# define Q_ZEROCONF_EXPORT
|
||||||
#endif
|
#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 QZeroConfPrivate;
|
||||||
|
|
||||||
@@ -70,7 +60,7 @@ public:
|
|||||||
serviceNameCollision = -2,
|
serviceNameCollision = -2,
|
||||||
browserFailed = -3,
|
browserFailed = -3,
|
||||||
};
|
};
|
||||||
QZeroConf();
|
QZeroConf(QObject *parent = Q_NULLPTR);
|
||||||
~QZeroConf();
|
~QZeroConf();
|
||||||
void startServicePublish(const char *name, const char *type, const char *domain, quint16 port);
|
void startServicePublish(const char *name, const char *type, const char *domain, quint16 port);
|
||||||
void stopServicePublish(void);
|
void stopServicePublish(void);
|
||||||
@@ -87,13 +77,13 @@ public:
|
|||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void servicePublished(void);
|
void servicePublished(void);
|
||||||
void error(QZeroConf::error_t);
|
void error(QZeroConf::error_t);
|
||||||
void serviceAdded(QZeroConfService *);
|
void serviceAdded(QZeroConfService);
|
||||||
void serviceUpdated(QZeroConfService *);
|
void serviceUpdated(QZeroConfService);
|
||||||
void serviceRemoved(QZeroConfService *);
|
void serviceRemoved(QZeroConfService);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QZeroConfPrivate *pri;
|
QZeroConfPrivate *pri;
|
||||||
QMap<QString, QZeroConfService *> services;
|
QMap<QString, QZeroConfService> services;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
142
qzeroconfservice.cpp
Normal file
142
qzeroconfservice.cpp
Normal file
@@ -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 <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
|
||||||
|
{
|
||||||
|
//TODO is this a proper test
|
||||||
|
return (!data->name.isEmpty()) && (data->port > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
44
qzeroconfservice.h
Normal file
44
qzeroconfservice.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef QZEROCONFSERVICE_H
|
||||||
|
#define QZEROCONFSERVICE_H
|
||||||
|
|
||||||
|
#include <QSharedDataPointer>
|
||||||
|
#include <QHostAddress>
|
||||||
|
|
||||||
|
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 <QByteArray, QByteArray> txt() const;
|
||||||
|
void setTxt(const QMap<QByteArray, QByteArray> txt);
|
||||||
|
void appendTxt(QByteArray idx, QByteArray val = "");
|
||||||
|
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QSharedDataPointer<QZeroConfServiceData> data;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // QZEROCONFSERVICE_H
|
Reference in New Issue
Block a user