zeroconf/Bonjour: move the DNSServiceRegister() call to the constructor
This commit is contained in:
parent
4fbdb3a2d5
commit
3ecd918442
@ -26,6 +26,8 @@
|
|||||||
|
|
||||||
#include <dns_sd.h>
|
#include <dns_sd.h>
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
static constexpr Domain bonjour_domain("bonjour");
|
static constexpr Domain bonjour_domain("bonjour");
|
||||||
@ -36,14 +38,7 @@ class BonjourHelper final {
|
|||||||
SocketEvent socket_event;
|
SocketEvent socket_event;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BonjourHelper(EventLoop &_loop, DNSServiceRef _service_ref)
|
BonjourHelper(EventLoop &_loop, const char *name, unsigned port);
|
||||||
:service_ref(_service_ref),
|
|
||||||
socket_event(_loop,
|
|
||||||
BIND_THIS_METHOD(OnSocketReady),
|
|
||||||
SocketDescriptor(DNSServiceRefSockFD(service_ref)))
|
|
||||||
{
|
|
||||||
socket_event.ScheduleRead();
|
|
||||||
}
|
|
||||||
|
|
||||||
~BonjourHelper() {
|
~BonjourHelper() {
|
||||||
DNSServiceRefDeallocate(service_ref);
|
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;
|
static BonjourHelper *bonjour_monitor;
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -94,22 +121,7 @@ BonjourHelper::Callback([[maybe_unused]] DNSServiceRef sdRef,
|
|||||||
void
|
void
|
||||||
BonjourInit(EventLoop &loop, const char *service_name, unsigned port)
|
BonjourInit(EventLoop &loop, const char *service_name, unsigned port)
|
||||||
{
|
{
|
||||||
DNSServiceRef dnsReference;
|
bonjour_monitor = new BonjourHelper(loop, service_name, port);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -22,6 +22,9 @@
|
|||||||
|
|
||||||
class EventLoop;
|
class EventLoop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws on error.
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
BonjourInit(EventLoop &loop, const char *service_name, unsigned port);
|
BonjourInit(EventLoop &loop, const char *service_name, unsigned port);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user