Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ if(BUILD_DOC)
add_subdirectory(doc)
endif()

option(BUILD_EXAMPLES "Build example applications" OFF)
option(BUILD_EXAMPLES "Build example applications" ON)
if(BUILD_EXAMPLES)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)
add_subdirectory(examples)
endif()

option(BUILD_TESTS "Build test suite" OFF)
option(BUILD_TESTS "Build test suite" ON)
if(BUILD_TESTS)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Test REQUIRED)
enable_testing()
Expand Down
9 changes: 7 additions & 2 deletions examples/browser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ set(SRC
servicemodel.cpp
)

add_executable(browser WIN32 ${SRC})
if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(browser ${SRC})
else()
add_executable(browser WIN32 ${SRC})
endif()

set_target_properties(browser PROPERTIES
CXX_STANDARD 11
)

target_link_libraries(browser qmdnsengine Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(browser PRIVATE qmdnsengine Qt${QT_VERSION_MAJOR}::Widgets)

install(TARGETS browser
RUNTIME DESTINATION "${EXAMPLE_DIR}"
LIBRARY DESTINATION "${EXAMPLE_DIR}"
COMPONENT examples
)
9 changes: 7 additions & 2 deletions examples/provider/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ set(SRC
provider.cpp
)

add_executable(provider WIN32 ${SRC})
if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(provider ${SRC})
else()
add_executable(provider WIN32 ${SRC})
endif()

target_link_libraries(provider qmdnsengine Qt${QT_VERSION_MAJOR}::Widgets)
target_link_libraries(provider PRIVATE qmdnsengine Qt${QT_VERSION_MAJOR}::Widgets)

install(TARGETS provider
RUNTIME DESTINATION "${EXAMPLE_DIR}"
LIBRARY DESTINATION "${EXAMPLE_DIR}"
COMPONENT examples
)
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ target_include_directories(qmdnsengine PUBLIC
"$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>"
)

target_link_libraries(qmdnsengine Qt${QT_VERSION_MAJOR}::Network)
target_link_libraries(qmdnsengine PUBLIC Qt${QT_VERSION_MAJOR}::Network)

install(TARGETS qmdnsengine
EXPORT qmdnsengine-export
Expand Down
61 changes: 35 additions & 26 deletions src/src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using namespace QMdnsEngine;

ServerPrivate::ServerPrivate(Server *server)
: QObject(server),
q(server)
q(server)
{
connect(&timer, &QTimer::timeout, this, &ServerPrivate::onTimeout);
connect(&ipv4Socket, &QUdpSocket::readyRead, this, &ServerPrivate::onReadyRead);
Expand All @@ -62,26 +62,10 @@ bool ServerPrivate::bindSocket(QUdpSocket &socket, const QHostAddress &address)
return true;
}

// I cannot find the correct combination of flags that allows the socket
// to bind properly on Linux, so on that platform, we must manually create
// the socket and initialize the QUdpSocket with it

#ifdef Q_OS_UNIX
if (!socket.bind(address, MdnsPort, QAbstractSocket::ShareAddress)) {
int arg = 1;
if (setsockopt(socket.socketDescriptor(), SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*>(&arg), sizeof(int))) {
emit q->error(strerror(errno));
return false;
}
#endif
if (!socket.bind(address, MdnsPort, QAbstractSocket::ReuseAddressHint)) {
emit q->error(socket.errorString());
return false;
}
#ifdef Q_OS_UNIX
if (!socket.bind(address, MdnsPort, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint)) {
emit q->error(socket.errorString());
return false;
}
#endif

return true;
}
Expand Down Expand Up @@ -135,25 +119,50 @@ void ServerPrivate::onReadyRead()

Server::Server(QObject *parent)
: AbstractServer(parent),
d(new ServerPrivate(this))
d(new ServerPrivate(this))
{
}

void Server::sendMessage(const Message &message)
{
QByteArray packet;
toPacket(message, packet);
if (message.address().protocol() == QAbstractSocket::IPv4Protocol) {
d->ipv4Socket.writeDatagram(packet, message.address(), message.port());

QUdpSocket *socket = (message.address().protocol() == QAbstractSocket::IPv4Protocol)
? &d->ipv4Socket
: &d->ipv6Socket;

if (message.address().isMulticast()) {
const auto interfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface &iface : interfaces) {
if ((iface.flags() & QNetworkInterface::IsUp) &&
(iface.flags() & QNetworkInterface::CanMulticast)) {

socket->setMulticastInterface(iface);
socket->writeDatagram(packet, message.address(), message.port());
}
}
} else {
d->ipv6Socket.writeDatagram(packet, message.address(), message.port());
socket->writeDatagram(packet, message.address(), message.port());
}
}

void Server::sendMessageToAll(const Message &message)
{
QByteArray packet;
toPacket(message, packet);
d->ipv4Socket.writeDatagram(packet, MdnsIpv4Address, MdnsPort);
d->ipv6Socket.writeDatagram(packet, MdnsIpv6Address, MdnsPort);

const auto interfaces = QNetworkInterface::allInterfaces();
for (const QNetworkInterface &iface : interfaces) {

if ((iface.flags() & QNetworkInterface::IsUp) &&
(iface.flags() & QNetworkInterface::CanMulticast)) {

d->ipv4Socket.setMulticastInterface(iface);
d->ipv4Socket.writeDatagram(packet, MdnsIpv4Address, MdnsPort);

d->ipv6Socket.setMulticastInterface(iface);
d->ipv6Socket.writeDatagram(packet, MdnsIpv6Address, MdnsPort);
}
}
}