Skip to content

Commit

Permalink
Turn Browser callbacks into lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
ruurdadema committed Mar 12, 2024
1 parent b9ed8cc commit d1de967
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 168 deletions.
35 changes: 14 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,22 @@ This library uses the Bonjour SDK and expects it to be in the default install lo

#include <dnssd/Browser.h>

class MyBrowser : public dnssd::Browser
{
public:
void onServiceDiscoveredAsync (const dnssd::ServiceDescription& serviceDescription) override
{
std::cout << "Service discovered: " << serviceDescription.description() << std::endl;
}

void onServiceRemovedAsync (const dnssd::ServiceDescription& serviceDescription) override
{
std::cout << "Service removed: " << serviceDescription.description() << std::endl;
}

void onBrowserErrorAsync (const dnssd::Result& error) override
{
std::cout << "Result: " << error.description() << std::endl;
}
};
dnssd::Browser browser;

browser.onServiceDiscoveredAsync ([] (const dnssd::ServiceDescription& serviceDescription) {
std::cout << "Service discovered: " << serviceDescription.description() << std::endl;
});

browser.onServiceRemovedAsync ([] (const dnssd::ServiceDescription& serviceDescription) {
std::cout << "Service removed: " << serviceDescription.description() << std::endl;
});

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

// Start browsing
if (auto error = browser.browseFor("_http._tcp"))
if (auto error = browser.browseFor ("_http._tcp"))
{
std::cout << "Result: " << error.description() << std::endl;
};
Expand All @@ -62,7 +55,7 @@ This library uses the Bonjour SDK and expects it to be in the default install lo
dnssd::TxtRecord txtRecord = {{"key1", "value1"}, {"key2", "value2"}};

// Register service
if (auto error = advertiser.registerService("_http._tcp", 80, txtRecord))
if (auto error = advertiser.registerService ("_http._tcp", 80, txtRecord))
{
std::cout << "Result: " << error.description() << std::endl;
}
Expand Down
66 changes: 26 additions & 40 deletions examples/dnssd-browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,44 @@
#include <iostream>
#include <string>

class MyBrowser : public dnssd::Browser
int main (int argc, char* argv[])
{
public:
void onServiceDiscoveredAsync (const dnssd::ServiceDescription& serviceDescription) override
if (argc < 2)
{
std::cout << "Service discovered: " << serviceDescription.description() << std::endl;
std::cout << "Expected an argument which specifies the servicetype to browse for (example: _http._tcp)"
<< std::endl;
return -1;
}

void onServiceRemovedAsync (const dnssd::ServiceDescription& serviceDescription) override
{
dnssd::Browser browser;

browser.onServiceDiscovered ([] (const dnssd::ServiceDescription& serviceDescription) {
std::cout << "Service discovered: " << serviceDescription.description() << std::endl;
});

browser.onServiceRemoved ([] (const dnssd::ServiceDescription& serviceDescription) {
std::cout << "Service removed: " << serviceDescription.description() << std::endl;
}
});

void onServiceResolvedAsync (const dnssd::ServiceDescription& serviceDescription, uint32_t interfaceIndex) override
{
browser.onServiceResolved ([] (const dnssd::ServiceDescription& serviceDescription, uint32_t interfaceIndex) {
std::cout << "Service resolved: " << serviceDescription.description() << std::endl;
}
});

void onAddressAddedAsync (
const dnssd::ServiceDescription& serviceDescription,
const std::string& address,
uint32_t interfaceIndex) override
{
std::cout << "Address added (" << address << "): " << serviceDescription.description() << std::endl;
}
browser.onAddressAdded (
[] (const dnssd::ServiceDescription& serviceDescription, const std::string& address, uint32_t interfaceIndex) {
std::cout << "Address added (" << address << "): " << serviceDescription.description() << std::endl;
});

void onAddressRemovedAsync (
const dnssd::ServiceDescription& serviceDescription,
const std::string& address,
uint32_t interfaceIndex) override
{
std::cout << "Address removed (" << address << "): " << serviceDescription.description() << std::endl;
}
browser.onAddressRemoved (
[] (const dnssd::ServiceDescription& serviceDescription, const std::string& address, uint32_t interfaceIndex) {
std::cout << "Address removed (" << address << "): " << serviceDescription.description() << std::endl;
});

void onBrowserErrorAsync (const dnssd::Result& error) override
{
browser.onBrowseError ([] (const dnssd::Result& error) {
std::cout << "Error: " << error.description() << std::endl;
}
};

int main (int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Expected an argument which specifies the servicetype to browse for (example: _http._tcp)"
<< std::endl;
return -1;
}

MyBrowser browser;
});

auto result = browser.browseFor (argv[1]);
auto const result = browser.browseFor (argv[1]);
if (result.hasError())
{
std::cout << "Error: " << result.description() << std::endl;
Expand Down
8 changes: 4 additions & 4 deletions include/dnssd/bonjour/BonjourBrowser.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "../common/Result.h"
#include "../common/IBrowser.h"
#include "../common/BrowserBase.h"
#include "../common/Log.h"
#include "../common/Result.h"

#include "Service.h"
#include "SharedConnection.h"
Expand All @@ -17,15 +17,15 @@ namespace dnssd
/**
* Apple Bonjour implementation of IBrowser. Works on macOS and Windows.
*/
class BonjourBrowser : public IBrowser
class BonjourBrowser : public BrowserBase
{
public:
explicit BonjourBrowser();
~BonjourBrowser() override;

// MARK: IBrowser implementations -
Result browseFor (const std::string& service) override;
bool reportIfError (const Result& result) noexcept;
bool reportIfError (const Result& result) const noexcept;

/**
* Called by dns_sd logic in response to a browse reply.
Expand Down
111 changes: 111 additions & 0 deletions include/dnssd/common/BrowserBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once

#include "../ServiceDescription.h"
#include "Result.h"
#include "dnssd/bonjour/Service.h"

#include <functional>

namespace dnssd
{

/**
* Interface class which represents a Bonjour browser.
*/
class BrowserBase
{
public:
using ServiceDiscoveredAsyncCallback = std::function<void (const ServiceDescription& serviceDescription)>;
using ServiceRemovedAsyncCallback = std::function<void (const ServiceDescription& serviceDescription)>;
using ServiceResolvedAsyncCallback =
std::function<void (const ServiceDescription& serviceDescription, uint32_t interfaceIndex)>;
using AddressAddedAsyncCallback = std::function<
void (const ServiceDescription& serviceDescription, const std::string& address, uint32_t interfaceIndex)>;
using AddressRemovedAsyncCallback = std::function<
void (const ServiceDescription& serviceDescription, const std::string& address, uint32_t interfaceIndex)>;
using BrowseErrorAsyncCallback = std::function<void (const Result& error)>;

BrowserBase() = default;
virtual ~BrowserBase() = default;

/**
* Starts browsing for a service
* @param serviceType The service type (ie. _http._tcp.).
* @return Returns an Result indicating success or failure.
*/
virtual Result browseFor (const std::string& serviceType) = 0;

/**
* Sets a callback which gets called when a service was discovered.
* Note: this call will be made from a background thread and wil not be synchronised.
* @param callback Callback with a reference to the ServiceDescription of this service.
*/
virtual void onServiceDiscovered (ServiceDiscoveredAsyncCallback callback)
{
onServiceDiscoveredCallback = std::move (callback);
}

/**
* Sets a callback which gets called when a service was removed.
* Note: this call will be made from a background thread and wil not be synchronised.
* @param callback Callback with the ServiceDescription of the removed service, which will also get removed after
* this call.
*/
virtual void onServiceRemoved (ServiceRemovedAsyncCallback callback)
{
onServiceRemovedCallback = std::move (callback);
}

/**
* Sets a callback which gets called when a service was resolved (ie. address information was resolved).
* Note: this call will be made from a background thread and wil not be synchronised.
* @param callback Callback with the service description and the index of the interface on which the service was
* resolved on.
*/
virtual void onServiceResolved (ServiceResolvedAsyncCallback callback)
{
onServiceResolvedCallback = std::move (callback);
}

/**
* Sets a callback which gets called when an address became available (ie. service became reachable on this
* address). Note: this call will be made from a background thread and wil not be synchronised.
* @param callback Callback with the service description, the added address and the interface index.
*/
virtual void onAddressAdded (AddressAddedAsyncCallback callback)
{
onAddressAddedCallback = std::move (callback);
}

/**
* Sets a callback which gets called when an address became unavailable (ie. service no longer reachable on this
* address). Note: this call will be made from a background thread and wil not be synchronised.
* @param callback Callback with the service description, the added address and the interface index.
*/
virtual void onAddressRemoved (AddressRemovedAsyncCallback callback)
{
onAddressRemovedCallback = std::move (callback);
}

/**
* Sets a callback which gets called when there was an error during browsing for a service.
* Note: this call will be made from a background thread and wil not be synchronised.
* @param callback A callback with a Result indicating what problem occurred.
*/
virtual void onBrowseError (BrowseErrorAsyncCallback callback)
{
onBrowseErrorCallback = std::move (callback);
}

protected:
friend Service;

ServiceDiscoveredAsyncCallback onServiceDiscoveredCallback;
ServiceResolvedAsyncCallback onServiceResolvedCallback;
ServiceRemovedAsyncCallback onServiceRemovedCallback;
AddressAddedAsyncCallback onAddressAddedCallback;
AddressRemovedAsyncCallback onAddressRemovedCallback;
BrowseErrorAsyncCallback onBrowseErrorCallback;
};

} // namespace dnssd
95 changes: 0 additions & 95 deletions include/dnssd/common/IBrowser.h

This file was deleted.

Loading

0 comments on commit d1de967

Please sign in to comment.