forked from jbagg/QtZeroConf
switch from QExplicitlySharedDataPointer to QSharedPointer
This commit is contained in:
@ -142,21 +142,22 @@ public:
|
|||||||
if (ref->pub->services.contains(key))
|
if (ref->pub->services.contains(key))
|
||||||
zcs = ref->pub->services[key];
|
zcs = ref->pub->services[key];
|
||||||
else {
|
else {
|
||||||
|
zcs = QZeroConfService(new QZeroConfServiceData);
|
||||||
newRecord = 1;
|
newRecord = 1;
|
||||||
zcs.setName(name);
|
zcs->m_name = name;
|
||||||
zcs.setType(type);
|
zcs->m_type = type;
|
||||||
zcs.setDomain(domain);
|
zcs->m_domain = domain;
|
||||||
zcs.setHost(host_name);
|
zcs->m_host = host_name;
|
||||||
zcs.setInterfaceIndex(interface);
|
zcs->m_interfaceIndex = interface;
|
||||||
zcs.setPort(port);
|
zcs->m_port = port;
|
||||||
while (txt) // get txt records
|
while (txt) // get txt records
|
||||||
{
|
{
|
||||||
QByteArray avahiText((const char *)txt->text, txt->size);
|
QByteArray avahiText((const char *)txt->text, txt->size);
|
||||||
QList<QByteArray> pair = avahiText.split('=');
|
QList<QByteArray> pair = avahiText.split('=');
|
||||||
if (pair.size() == 2)
|
if (pair.size() == 2)
|
||||||
zcs.appendTxt(pair.at(0), pair.at(1));
|
zcs->m_txt[pair.at(0)] = pair.at(1);
|
||||||
else
|
else
|
||||||
zcs.appendTxt(pair.at(0));
|
zcs->m_txt[pair.at(0)] = "";
|
||||||
txt = txt->next;
|
txt = txt->next;
|
||||||
}
|
}
|
||||||
ref->pub->services.insert(key, zcs);
|
ref->pub->services.insert(key, zcs);
|
||||||
@ -166,9 +167,9 @@ public:
|
|||||||
avahi_address_snprint(a, sizeof(a), address);
|
avahi_address_snprint(a, sizeof(a), address);
|
||||||
QHostAddress addr(a);
|
QHostAddress addr(a);
|
||||||
if (protocol == AVAHI_PROTO_INET6)
|
if (protocol == AVAHI_PROTO_INET6)
|
||||||
zcs.setIpv6(addr);
|
zcs->setIpv6(addr);
|
||||||
else if (protocol == AVAHI_PROTO_INET)
|
else if (protocol == AVAHI_PROTO_INET)
|
||||||
zcs.setIp(addr);
|
zcs->setIp(addr);
|
||||||
|
|
||||||
if (newRecord)
|
if (newRecord)
|
||||||
emit ref->pub->serviceAdded(zcs);
|
emit ref->pub->serviceAdded(zcs);
|
||||||
|
@ -172,21 +172,22 @@ public:
|
|||||||
if (ref->pub->services.contains(key))
|
if (ref->pub->services.contains(key))
|
||||||
zcs = ref->pub->services[key];
|
zcs = ref->pub->services[key];
|
||||||
else {
|
else {
|
||||||
|
zcs = QZeroConfService(new QZeroConfServiceData);
|
||||||
newRecord = 1;
|
newRecord = 1;
|
||||||
zcs.setName(name);
|
zcs->m_name = name;
|
||||||
zcs.setType(type);
|
zcs->m_type = type;
|
||||||
zcs.setDomain(domain);
|
zcs->m_domain = domain;
|
||||||
zcs.setHost(host_name);
|
zcs->m_host = host_name;
|
||||||
zcs.setInterfaceIndex(interface);
|
zcs->m_interfaceIndex = interface;
|
||||||
zcs.setPort(port);
|
zcs->m_port = port;
|
||||||
while (txt) // get txt records
|
while (txt) // get txt records
|
||||||
{
|
{
|
||||||
QByteArray avahiText((const char *)txt->text, txt->size);
|
QByteArray avahiText((const char *)txt->text, txt->size);
|
||||||
QList<QByteArray> pair = avahiText.split('=');
|
QList<QByteArray> pair = avahiText.split('=');
|
||||||
if (pair.size() == 2)
|
if (pair.size() == 2)
|
||||||
zcs.appendTxt(pair.at(0), pair.at(1));
|
zcs->m_txt[pair.at(0)] = pair.at(1);
|
||||||
else
|
else
|
||||||
zcs.appendTxt(pair.at(0));
|
zcs->m_txt[pair.at(0)] = "";
|
||||||
txt = txt->next;
|
txt = txt->next;
|
||||||
}
|
}
|
||||||
ref->pub->services.insert(key, zcs);
|
ref->pub->services.insert(key, zcs);
|
||||||
@ -196,9 +197,9 @@ public:
|
|||||||
avahi_address_snprint(a, sizeof(a), address);
|
avahi_address_snprint(a, sizeof(a), address);
|
||||||
QHostAddress addr(a);
|
QHostAddress addr(a);
|
||||||
if (protocol == AVAHI_PROTO_INET6)
|
if (protocol == AVAHI_PROTO_INET6)
|
||||||
zcs.setIpv6(addr);
|
zcs->setIpv6(addr);
|
||||||
else if (protocol == AVAHI_PROTO_INET)
|
else if (protocol == AVAHI_PROTO_INET)
|
||||||
zcs.setIp(addr);
|
zcs->setIp(addr);
|
||||||
|
|
||||||
if (newRecord)
|
if (newRecord)
|
||||||
emit ref->pub->serviceAdded(zcs);
|
emit ref->pub->serviceAdded(zcs);
|
||||||
|
25
bonjour.cpp
25
bonjour.cpp
@ -68,7 +68,7 @@ void QZeroConfPrivate::resolve(void)
|
|||||||
{
|
{
|
||||||
DNSServiceErrorType err;
|
DNSServiceErrorType err;
|
||||||
|
|
||||||
err = DNSServiceResolve(&resolver, kDNSServiceFlagsTimeout, work.head().interfaceIndex(), work.head().name().toUtf8(), work.head().type().toUtf8(), work.head().domain().toUtf8(), (DNSServiceResolveReply) resolverCallback, this);
|
err = DNSServiceResolve(&resolver, kDNSServiceFlagsTimeout, work.head()->interfaceIndex(), work.head()->name().toUtf8(), work.head()->type().toUtf8(), work.head()->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) {
|
||||||
@ -110,10 +110,11 @@ 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.setName(name);
|
zcs = QZeroConfService(new QZeroConfServiceData);
|
||||||
zcs.setType(type);
|
zcs->m_name = name;
|
||||||
zcs.setDomain(domain);
|
zcs->m_type = type;
|
||||||
zcs.setInterfaceIndex(interfaceIndex);
|
zcs->m_domain = domain;
|
||||||
|
zcs->m_interfaceIndex = interfaceIndex;
|
||||||
if (!ref->work.size()) {
|
if (!ref->work.size()) {
|
||||||
ref->work.enqueue(zcs);
|
ref->work.enqueue(zcs);
|
||||||
ref->resolve();
|
ref->resolve();
|
||||||
@ -154,15 +155,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->work.head().appendTxt(pair.at(0), pair.at(1));
|
ref->work.head()->m_txt[pair.at(0)] = pair.at(1);
|
||||||
else
|
else
|
||||||
ref->work.head().appendTxt(pair.at(0));
|
ref->work.head()->m_txt[pair.at(0)] = "";
|
||||||
|
|
||||||
txtLen-= recLen + 1;
|
txtLen-= recLen + 1;
|
||||||
txtRecord+= recLen;
|
txtRecord+= recLen;
|
||||||
}
|
}
|
||||||
ref->work.head().setHost(hostName);
|
ref->work.head()->m_host = hostName;
|
||||||
ref->work.head().setPort(qFromBigEndian<quint16>(port));
|
ref->work.head()->m_port = 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);
|
||||||
@ -195,11 +196,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->work.head().setIpv6(hAddress);
|
ref->work.head()->setIpv6(hAddress);
|
||||||
else
|
else
|
||||||
ref->work.head().setIp(hAddress);
|
ref->work.head()->setIp(hAddress);
|
||||||
|
|
||||||
QString key = ref->work.head().name() + QString::number(interfaceIndex);
|
QString key = ref->work.head()->name() + QString::number(interfaceIndex);
|
||||||
if (!ref->pub->services.contains(key)) {
|
if (!ref->pub->services.contains(key)) {
|
||||||
ref->pub->services.insert(key, ref->work.head());
|
ref->pub->services.insert(key, ref->work.head());
|
||||||
emit ref->pub->serviceAdded(ref->work.head());
|
emit ref->pub->serviceAdded(ref->work.head());
|
||||||
|
@ -155,9 +155,9 @@ void mainWindow::addService(QZeroConfService zcs)
|
|||||||
|
|
||||||
row = table.rowCount();
|
row = table.rowCount();
|
||||||
table.insertRow(row);
|
table.insertRow(row);
|
||||||
cell = new QTableWidgetItem(zcs.name());
|
cell = new QTableWidgetItem(zcs->name());
|
||||||
table.setItem(row, 0, cell);
|
table.setItem(row, 0, cell);
|
||||||
cell = new QTableWidgetItem(zcs.ip().toString());
|
cell = new QTableWidgetItem(zcs->ip().toString());
|
||||||
table.setItem(row, 1, cell);
|
table.setItem(row, 1, cell);
|
||||||
table.resizeColumnsToContents();
|
table.resizeColumnsToContents();
|
||||||
#if !(defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
|
#if !(defined(Q_OS_IOS) || defined(Q_OS_ANDROID))
|
||||||
@ -171,12 +171,12 @@ void mainWindow::removeService(QZeroConfService zcs)
|
|||||||
QTableWidgetItem *nameItem, *ipItem;
|
QTableWidgetItem *nameItem, *ipItem;
|
||||||
qDebug() << "Removed service: " << zcs;
|
qDebug() << "Removed service: " << zcs;
|
||||||
|
|
||||||
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++) {
|
for (i=0; i<search.size(); i++) {
|
||||||
nameItem = search.at(i);
|
nameItem = search.at(i);
|
||||||
row = nameItem->row();
|
row = nameItem->row();
|
||||||
ipItem = table.item(row, 1);
|
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 nameItem;
|
||||||
delete ipItem;
|
delete ipItem;
|
||||||
table.removeRow(row);
|
table.removeRow(row);
|
||||||
|
@ -1,152 +1,10 @@
|
|||||||
#include <QMutexLocker>
|
#include <QDebug>
|
||||||
#include "qzeroconfservice.h"
|
#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;
|
|
||||||
QMutex lock;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
{
|
|
||||||
QMutexLocker locker(&data->lock);
|
|
||||||
return data->ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
void QZeroConfService::setIp(QHostAddress &ip)
|
|
||||||
{
|
|
||||||
QMutexLocker locker(&data->lock);
|
|
||||||
data->ip = ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
QHostAddress QZeroConfService::ipv6() const
|
|
||||||
{
|
|
||||||
QMutexLocker locker(&data->lock);
|
|
||||||
return data->ipv6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void QZeroConfService::setIpv6(const QHostAddress &ipv6)
|
|
||||||
{
|
|
||||||
QMutexLocker locker(&data->lock);
|
|
||||||
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::operator==(const QZeroConfService &rhs) const
|
|
||||||
{
|
|
||||||
return this->name() == rhs.name() && (this->ip() == rhs.ip() || this->ipv6() == rhs.ipv6());
|
|
||||||
}
|
|
||||||
|
|
||||||
QDebug operator<<(QDebug debug, const QZeroConfService &service)
|
QDebug operator<<(QDebug debug, const QZeroConfService &service)
|
||||||
{
|
{
|
||||||
QDebugStateSaver saver(debug);
|
QDebugStateSaver saver(debug);
|
||||||
debug.nospace() << "Zeroconf Service: " + service.name() + " @ " + service.host() + " ("+ service.ip().toString() + ":" + QString::number( service.port()) + ")";
|
debug.nospace() << "Zeroconf Service: " + service->name() + " @ " + service->host() + " ("+ service->ip().toString() + ":" + QString::number( service->port()) + ")";
|
||||||
return debug.maybeSpace();
|
return debug.maybeSpace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
#ifndef QZEROCONFSERVICE_H
|
#ifndef QZEROCONFSERVICE_H
|
||||||
#define QZEROCONFSERVICE_H
|
#define QZEROCONFSERVICE_H
|
||||||
|
|
||||||
#include <QExplicitlySharedDataPointer>
|
|
||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
|
#include <QMutexLocker>
|
||||||
|
#include <QSharedPointer>
|
||||||
#include "qzeroconfglobal.h"
|
#include "qzeroconfglobal.h"
|
||||||
|
|
||||||
class QZeroConfServiceData;
|
|
||||||
class QZeroConfPrivate;
|
class QZeroConfPrivate;
|
||||||
|
|
||||||
class Q_ZEROCONF_EXPORT QZeroConfService
|
class Q_ZEROCONF_EXPORT QZeroConfServiceData
|
||||||
{
|
{
|
||||||
Q_GADGET
|
Q_GADGET
|
||||||
Q_PROPERTY( QString name READ name )
|
Q_PROPERTY( QString name READ name )
|
||||||
@ -19,40 +19,51 @@ class Q_ZEROCONF_EXPORT QZeroConfService
|
|||||||
friend class QZeroConfPrivate;
|
friend class QZeroConfPrivate;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
inline QString name() const {return m_name;}
|
||||||
QZeroConfService();
|
inline QString type() const {return m_type;}
|
||||||
QZeroConfService(const QZeroConfService &);
|
inline QString domain() const {return m_domain;}
|
||||||
QZeroConfService &operator=(const QZeroConfService &);
|
inline QString host() const {return m_host;}
|
||||||
~QZeroConfService();
|
QHostAddress ip()
|
||||||
|
{
|
||||||
QString name() const;
|
QMutexLocker locker(&m_lock);
|
||||||
QString type() const;
|
return m_ip;
|
||||||
QString domain() const;
|
}
|
||||||
QString host() const;
|
QHostAddress ipv6()
|
||||||
QHostAddress ip() const;
|
{
|
||||||
QHostAddress ipv6() const;
|
QMutexLocker locker(&m_lock);
|
||||||
quint32 interfaceIndex() const;
|
return m_ipv6;
|
||||||
quint16 port() const;
|
}
|
||||||
QMap <QByteArray, QByteArray> txt() const;
|
inline quint32 interfaceIndex() const {return m_interfaceIndex;}
|
||||||
|
quint16 port() const {return m_port;}
|
||||||
bool operator==(const QZeroConfService &rhs) const;
|
QMap <QByteArray, QByteArray> txt() const {return m_txt;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setName(const QString &name);
|
void setIp(QHostAddress &ip)
|
||||||
void setType(const QString &type);
|
{
|
||||||
void setDomain(const QString &domain);
|
QMutexLocker locker(&m_lock);
|
||||||
void setHost(const QString &host);
|
m_ip = ip;
|
||||||
void setIp(QHostAddress &ip);
|
}
|
||||||
void setIpv6(const QHostAddress &ipv6);
|
void setIpv6(const QHostAddress &ipv6)
|
||||||
void setInterfaceIndex(const quint32 &interfaceIndex);
|
{
|
||||||
void setPort(const quint16 port);
|
QMutexLocker locker(&m_lock);
|
||||||
void setTxt(const QMap<QByteArray, QByteArray> txt);
|
m_ipv6 = ipv6;
|
||||||
void appendTxt(QByteArray idx, QByteArray val = "");
|
}
|
||||||
QExplicitlySharedDataPointer<QZeroConfServiceData> data;
|
QString m_name;
|
||||||
|
QString m_type;
|
||||||
|
QString m_domain;
|
||||||
|
QString m_host;
|
||||||
|
QHostAddress m_ip;
|
||||||
|
QHostAddress m_ipv6;
|
||||||
|
quint32 m_interfaceIndex;
|
||||||
|
quint16 m_port;
|
||||||
|
QMap <QByteArray, QByteArray> m_txt;
|
||||||
|
QMutex m_lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
QDebug operator<<(QDebug debug, const QZeroConfService &service);
|
typedef QSharedPointer<QZeroConfServiceData> QZeroConfService;
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QZeroConfService)
|
Q_DECLARE_METATYPE(QZeroConfService)
|
||||||
|
|
||||||
|
QDebug operator<<(QDebug debug, const QZeroConfService &service);
|
||||||
|
|
||||||
#endif // QZEROCONFSERVICE_H
|
#endif // QZEROCONFSERVICE_H
|
||||||
|
Reference in New Issue
Block a user