zeroconf/Bonjour: move the DNSServiceRegister() call to the constructor

This commit is contained in:
Max Kellermann 2021-02-24 13:56:51 +01:00
parent 4fbdb3a2d5
commit 3ecd918442
2 changed files with 39 additions and 24 deletions

View File

@ -26,6 +26,8 @@
#include <dns_sd.h>
#include <stdexcept>
#include <arpa/inet.h>
static constexpr Domain bonjour_domain("bonjour");
@ -36,14 +38,7 @@ class BonjourHelper final {
SocketEvent socket_event;
public:
BonjourHelper(EventLoop &_loop, DNSServiceRef _service_ref)
:service_ref(_service_ref),
socket_event(_loop,
BIND_THIS_METHOD(OnSocketReady),
SocketDescriptor(DNSServiceRefSockFD(service_ref)))
{
socket_event.ScheduleRead();
}
BonjourHelper(EventLoop &_loop, const char *name, unsigned port);
~BonjourHelper() {
DNSServiceRefDeallocate(service_ref);
@ -69,6 +64,38 @@ protected:
}
};
/**
* A wrapper for DNSServiceRegister() which returns the DNSServiceRef
* and throws on error.
*/
static DNSServiceRef
RegisterBonjour(const char *name, const char *type, unsigned port,
DNSServiceRegisterReply callback, void *ctx)
{
DNSServiceRef ref;
DNSServiceErrorType error = DNSServiceRegister(&ref,
0, 0, name, type,
nullptr, nullptr,
htons(port), 0,
nullptr,
callback, ctx);
if (error != kDNSServiceErr_NoError)
throw std::runtime_error("DNSServiceRegister() failed");
return ref;
}
BonjourHelper::BonjourHelper(EventLoop &_loop, const char *name, unsigned port)
:service_ref(RegisterBonjour(name, SERVICE_TYPE, port,
Callback, nullptr)),
socket_event(_loop,
BIND_THIS_METHOD(OnSocketReady),
SocketDescriptor(DNSServiceRefSockFD(service_ref)))
{
socket_event.ScheduleRead();
}
static BonjourHelper *bonjour_monitor;
void
@ -94,22 +121,7 @@ BonjourHelper::Callback([[maybe_unused]] DNSServiceRef sdRef,
void
BonjourInit(EventLoop &loop, const char *service_name, unsigned port)
{
DNSServiceRef dnsReference;
DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
0, 0, service_name,
SERVICE_TYPE, nullptr, nullptr,
htons(port), 0,
nullptr,
BonjourHelper::Callback,
nullptr);
if (error != kDNSServiceErr_NoError) {
LogError(bonjour_domain,
"Failed to register zeroconf service");
return;
}
bonjour_monitor = new BonjourHelper(loop, dnsReference);
bonjour_monitor = new BonjourHelper(loop, service_name, port);
}
void

View File

@ -22,6 +22,9 @@
class EventLoop;
/**
* Throws on error.
*/
void
BonjourInit(EventLoop &loop, const char *service_name, unsigned port);