Skip to content

Commit

Permalink
Turn advertiser callbacks into lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
ruurdadema committed Mar 12, 2024
1 parent 7c29dd5 commit b9ed8cc
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 58 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,11 @@ This library uses the Bonjour SDK and expects it to be in the default install lo

#include <dnssd/Advertiser.h>

class MyAdvertiser : public dnssd::Advertiser
dnssd::Advertiser advertiser;
advertiser.onAdvertiserErrorAsync ([] (const dnssd::Result& error)
{
public:
void onAdvertiserErrorAsync (const dnssd::Result& error) override
{
std::cout << "Result: " << error.description() << std::endl;
}
};

MyAdvertiser advertiser;
std::cout << "Error: " << error.description() << std::endl;
});

dnssd::TxtRecord txtRecord = {{"key1", "value1"}, {"key2", "value2"}};

Expand Down
19 changes: 8 additions & 11 deletions examples/dnssd-advertiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
#include <string>
#include <vector>

class MyAdvertiser : public dnssd::Advertiser
{
public:
void onAdvertiserErrorAsync (const dnssd::Result& error) override
{
std::cout << "Error: " << error.description() << std::endl;
}
};

bool parseTxtRecord (dnssd::TxtRecord& txtRecord, const std::string& stringValue)
{
if (stringValue.empty())
Expand All @@ -28,7 +19,7 @@ bool parseTxtRecord (dnssd::TxtRecord& txtRecord, const std::string& stringValue
return true;
}

int main (int argc, char* argv[])
int main (int const argc, char* argv[])
{
std::vector<std::string> args;
for (int i = 1; i < argc; i++)
Expand Down Expand Up @@ -64,7 +55,11 @@ int main (int argc, char* argv[])
parseTxtRecord (txtRecord, *it);
}

MyAdvertiser advertiser;
dnssd::Advertiser advertiser;

advertiser.onAdvertiserErrorAsync ([] (const dnssd::Result& error) {
std::cout << "Error: " << error.description() << std::endl;
});

auto result = advertiser.registerService (args[0], "001122334455@SomeName", nullptr, portNumber, txtRecord);
if (result.hasError())
Expand Down Expand Up @@ -97,4 +92,6 @@ int main (int argc, char* argv[])
}

std::cout << "Exit" << std::endl;

return 0;
}
13 changes: 11 additions & 2 deletions include/dnssd/bonjour/BonjourAdvertiser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "../common/IAdvertiser.h"
#include "../common/AdvertiserBase.h"
#include "../common/Result.h"
#include "ScopedDnsServiceRef.h"

Expand All @@ -13,7 +13,7 @@ namespace dnssd
/**
* Apple Bonjour implementation of IAdvertiser. Works on macOS and Windows.
*/
class BonjourAdvertiser : public IAdvertiser
class BonjourAdvertiser : public AdvertiserBase
{
public:
explicit BonjourAdvertiser() = default;
Expand All @@ -31,6 +31,15 @@ class BonjourAdvertiser : public IAdvertiser

private:
ScopedDnsServiceRef mServiceRef;

static void DNSSD_API registerServiceCallBack (
DNSServiceRef serviceRef,
DNSServiceFlags flags,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regType,
const char* replyDomain,
void* context);
};

} // namespace dnssd
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#pragma once

#include <utility>

#include "../common/TxtRecord.h"
#include "Result.h"
#include "Util.h"

#include <functional>

namespace dnssd
{

/**
* Interface class which represents a dnssd advertiser object, which is able to present itself onto the network.
*/
class IAdvertiser
class AdvertiserBase
{
public:
explicit IAdvertiser() = default;
virtual ~IAdvertiser() = default;
using AdvertiserErrorCallback = std::function<void (const Result& error)>;

explicit AdvertiserBase() = default;
virtual ~AdvertiserBase() = default;

/**
* Registers a service with given arguments.
Expand Down Expand Up @@ -53,11 +55,17 @@ class IAdvertiser
virtual void unregisterService() noexcept = 0;

/**
* Called when there was an error.
* Set a callback for when an error happens.
* Note: this call will be made from a background thread and wil not be synchronised.
* @param error The error which occured.
* @param callback The function to be called when an error happens.
*/
virtual void onAdvertiserErrorAsync (const Result& error) { ignore (error); }
virtual void onAdvertiserErrorAsync (AdvertiserErrorCallback callback)
{
onAdvertiserErrorCallback = std::move (callback);
}

protected:
AdvertiserErrorCallback onAdvertiserErrorCallback;
};

} // namespace dnssd
56 changes: 28 additions & 28 deletions src/bonjour/BonjourAdvertiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,6 @@
#include <iostream>
#include <thread>

static void DNSSD_API registerServiceCallBack (
DNSServiceRef serviceRef,
DNSServiceFlags flags,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regType,
const char* replyDomain,
void* context)
{
(void)serviceRef;
(void)flags;
(void)serviceName;
(void)regType;
(void)replyDomain;

dnssd::Result result (errorCode);

if (result.hasError())
{
auto* owner = static_cast<dnssd::BonjourAdvertiser*> (context);
owner->onAdvertiserErrorAsync (result);
owner->unregisterService();
return;
}
}

dnssd::Result dnssd::BonjourAdvertiser::registerService (
const std::string& regType,
const char* name,
Expand Down Expand Up @@ -67,9 +41,35 @@ void dnssd::BonjourAdvertiser::unregisterService() noexcept
mServiceRef = nullptr;
}

dnssd::Result dnssd::BonjourAdvertiser::updateTxtRecord (const dnssd::TxtRecord& txtRecord)
void dnssd::BonjourAdvertiser::registerServiceCallBack (
DNSServiceRef serviceRef,
DNSServiceFlags flags,
DNSServiceErrorType errorCode,
const char* serviceName,
const char* regType,
const char* replyDomain,
void* context)
{
auto record = BonjourTxtRecord (txtRecord);
(void)serviceRef;
(void)flags;
(void)serviceName;
(void)regType;
(void)replyDomain;

Result const result (errorCode);

if (result.hasError())
{
auto* owner = static_cast<BonjourAdvertiser*> (context);
if (owner->onAdvertiserErrorCallback)
owner->onAdvertiserErrorCallback (result);
owner->unregisterService();
}
}

dnssd::Result dnssd::BonjourAdvertiser::updateTxtRecord (const TxtRecord& txtRecord)
{
auto const record = BonjourTxtRecord (txtRecord);

// Second argument's nullptr tells us that we are updating the primary record.
return Result (
Expand Down

0 comments on commit b9ed8cc

Please sign in to comment.