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))
|
||||
zcs = ref->pub->services[key];
|
||||
else {
|
||||
zcs = QZeroConfService(new QZeroConfServiceData);
|
||||
newRecord = 1;
|
||||
zcs.setName(name);
|
||||
zcs.setType(type);
|
||||
zcs.setDomain(domain);
|
||||
zcs.setHost(host_name);
|
||||
zcs.setInterfaceIndex(interface);
|
||||
zcs.setPort(port);
|
||||
zcs->m_name = name;
|
||||
zcs->m_type = type;
|
||||
zcs->m_domain = domain;
|
||||
zcs->m_host = host_name;
|
||||
zcs->m_interfaceIndex = interface;
|
||||
zcs->m_port = port;
|
||||
while (txt) // get txt records
|
||||
{
|
||||
QByteArray avahiText((const char *)txt->text, txt->size);
|
||||
QList<QByteArray> pair = avahiText.split('=');
|
||||
if (pair.size() == 2)
|
||||
zcs.appendTxt(pair.at(0), pair.at(1));
|
||||
zcs->m_txt[pair.at(0)] = pair.at(1);
|
||||
else
|
||||
zcs.appendTxt(pair.at(0));
|
||||
zcs->m_txt[pair.at(0)] = "";
|
||||
txt = txt->next;
|
||||
}
|
||||
ref->pub->services.insert(key, zcs);
|
||||
@ -166,9 +167,9 @@ public:
|
||||
avahi_address_snprint(a, sizeof(a), address);
|
||||
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);
|
||||
|
@ -172,21 +172,22 @@ public:
|
||||
if (ref->pub->services.contains(key))
|
||||
zcs = ref->pub->services[key];
|
||||
else {
|
||||
zcs = QZeroConfService(new QZeroConfServiceData);
|
||||
newRecord = 1;
|
||||
zcs.setName(name);
|
||||
zcs.setType(type);
|
||||
zcs.setDomain(domain);
|
||||
zcs.setHost(host_name);
|
||||
zcs.setInterfaceIndex(interface);
|
||||
zcs.setPort(port);
|
||||
zcs->m_name = name;
|
||||
zcs->m_type = type;
|
||||
zcs->m_domain = domain;
|
||||
zcs->m_host = host_name;
|
||||
zcs->m_interfaceIndex = interface;
|
||||
zcs->m_port = port;
|
||||
while (txt) // get txt records
|
||||
{
|
||||
QByteArray avahiText((const char *)txt->text, txt->size);
|
||||
QList<QByteArray> pair = avahiText.split('=');
|
||||
if (pair.size() == 2)
|
||||
zcs.appendTxt(pair.at(0), pair.at(1));
|
||||
zcs->m_txt[pair.at(0)] = pair.at(1);
|
||||
else
|
||||
zcs.appendTxt(pair.at(0));
|
||||
zcs->m_txt[pair.at(0)] = "";
|
||||
txt = txt->next;
|
||||
}
|
||||
ref->pub->services.insert(key, zcs);
|
||||
@ -196,9 +197,9 @@ public:
|
||||
avahi_address_snprint(a, sizeof(a), address);
|
||||
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);
|
||||
|
25
bonjour.cpp
25
bonjour.cpp
@ -68,7 +68,7 @@ void QZeroConfPrivate::resolve(void)
|
||||
{
|
||||
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) {
|
||||
int sockfd = DNSServiceRefSockFD(resolver);
|
||||
if (sockfd == -1) {
|
||||
@ -110,10 +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.setName(name);
|
||||
zcs.setType(type);
|
||||
zcs.setDomain(domain);
|
||||
zcs.setInterfaceIndex(interfaceIndex);
|
||||
zcs = QZeroConfService(new QZeroConfServiceData);
|
||||
zcs->m_name = name;
|
||||
zcs->m_type = type;
|
||||
zcs->m_domain = domain;
|
||||
zcs->m_interfaceIndex = interfaceIndex;
|
||||
if (!ref->work.size()) {
|
||||
ref->work.enqueue(zcs);
|
||||
ref->resolve();
|
||||
@ -154,15 +155,15 @@ void DNSSD_API QZeroConfPrivate::resolverCallback(DNSServiceRef, DNSServiceFlags
|
||||
QByteArray avahiText((const char *)txtRecord, recLen);
|
||||
QList<QByteArray> pair = avahiText.split('=');
|
||||
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
|
||||
ref->work.head().appendTxt(pair.at(0));
|
||||
ref->work.head()->m_txt[pair.at(0)] = "";
|
||||
|
||||
txtLen-= recLen + 1;
|
||||
txtRecord+= recLen;
|
||||
}
|
||||
ref->work.head().setHost(hostName);
|
||||
ref->work.head().setPort(qFromBigEndian<quint16>(port));
|
||||
ref->work.head()->m_host = hostName;
|
||||
ref->work.head()->m_port = qFromBigEndian<quint16>(port);
|
||||
err = DNSServiceGetAddrInfo(&ref->resolver, kDNSServiceFlagsForceMulticast, interfaceIndex, ref->protocol, hostName, (DNSServiceGetAddrInfoReply) addressReply, ref);
|
||||
if (err == kDNSServiceErr_NoError) {
|
||||
int sockfd = DNSServiceRefSockFD(ref->resolver);
|
||||
@ -195,11 +196,11 @@ void DNSSD_API QZeroConfPrivate::addressReply(DNSServiceRef sdRef,
|
||||
if ((flags & kDNSServiceFlagsAdd) != 0) {
|
||||
QHostAddress hAddress(address);
|
||||
if (hAddress.protocol() == QAbstractSocket::IPv6Protocol)
|
||||
ref->work.head().setIpv6(hAddress);
|
||||
ref->work.head()->setIpv6(hAddress);
|
||||
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)) {
|
||||
ref->pub->services.insert(key, ref->work.head());
|
||||
emit ref->pub->serviceAdded(ref->work.head());
|
||||
|
@ -155,9 +155,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))
|
||||
@ -171,12 +171,12 @@ void mainWindow::removeService(QZeroConfService zcs)
|
||||
QTableWidgetItem *nameItem, *ipItem;
|
||||
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++) {
|
||||
nameItem = search.at(i);
|
||||
row = nameItem->row();
|
||||
ipItem = table.item(row, 1);
|
||||
if (table.item(row, 1)->text() == zcs.ip().toString()) { // match ip address in case of dual homed systems
|
||||
if (table.item(row, 1)->text() == zcs->ip().toString()) { // match ip address in case of dual homed systems
|
||||
delete nameItem;
|
||||
delete ipItem;
|
||||
table.removeRow(row);
|
||||
|
@ -1,152 +1,10 @@
|
||||
#include <QMutexLocker>
|
||||
#include <QDebug>
|
||||
#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)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
#ifndef QZEROCONFSERVICE_H
|
||||
#define QZEROCONFSERVICE_H
|
||||
|
||||
#include <QExplicitlySharedDataPointer>
|
||||
#include <QHostAddress>
|
||||
#include <QMutexLocker>
|
||||
#include <QSharedPointer>
|
||||
#include "qzeroconfglobal.h"
|
||||
|
||||
class QZeroConfServiceData;
|
||||
class QZeroConfPrivate;
|
||||
|
||||
class Q_ZEROCONF_EXPORT QZeroConfService
|
||||
class Q_ZEROCONF_EXPORT QZeroConfServiceData
|
||||
{
|
||||
Q_GADGET
|
||||
Q_PROPERTY( QString name READ name )
|
||||
@ -19,40 +19,51 @@ class Q_ZEROCONF_EXPORT QZeroConfService
|
||||
friend class QZeroConfPrivate;
|
||||
|
||||
public:
|
||||
|
||||
QZeroConfService();
|
||||
QZeroConfService(const QZeroConfService &);
|
||||
QZeroConfService &operator=(const QZeroConfService &);
|
||||
~QZeroConfService();
|
||||
|
||||
QString name() const;
|
||||
QString type() const;
|
||||
QString domain() const;
|
||||
QString host() const;
|
||||
QHostAddress ip() const;
|
||||
QHostAddress ipv6() const;
|
||||
quint32 interfaceIndex() const;
|
||||
quint16 port() const;
|
||||
QMap <QByteArray, QByteArray> txt() const;
|
||||
|
||||
bool operator==(const QZeroConfService &rhs) const;
|
||||
inline QString name() const {return m_name;}
|
||||
inline QString type() const {return m_type;}
|
||||
inline QString domain() const {return m_domain;}
|
||||
inline QString host() const {return m_host;}
|
||||
QHostAddress ip()
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
return m_ip;
|
||||
}
|
||||
QHostAddress ipv6()
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
return m_ipv6;
|
||||
}
|
||||
inline quint32 interfaceIndex() const {return m_interfaceIndex;}
|
||||
quint16 port() const {return m_port;}
|
||||
QMap <QByteArray, QByteArray> txt() const {return m_txt;}
|
||||
|
||||
private:
|
||||
void setName(const QString &name);
|
||||
void setType(const QString &type);
|
||||
void setDomain(const QString &domain);
|
||||
void setHost(const QString &host);
|
||||
void setIp(QHostAddress &ip);
|
||||
void setIpv6(const QHostAddress &ipv6);
|
||||
void setInterfaceIndex(const quint32 &interfaceIndex);
|
||||
void setPort(const quint16 port);
|
||||
void setTxt(const QMap<QByteArray, QByteArray> txt);
|
||||
void appendTxt(QByteArray idx, QByteArray val = "");
|
||||
QExplicitlySharedDataPointer<QZeroConfServiceData> data;
|
||||
void setIp(QHostAddress &ip)
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
m_ip = ip;
|
||||
}
|
||||
void setIpv6(const QHostAddress &ipv6)
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
m_ipv6 = ipv6;
|
||||
}
|
||||
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)
|
||||
|
||||
QDebug operator<<(QDebug debug, const QZeroConfService &service);
|
||||
|
||||
#endif // QZEROCONFSERVICE_H
|
||||
|
Reference in New Issue
Block a user