adding in sample code

This commit is contained in:
talksik
2025-03-10 11:13:32 -07:00
commit 990ffd1ec1
6 changed files with 137 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
+22
View File
@@ -0,0 +1,22 @@
# Overview
`cpp-bonjour` is meant to be a pure C++ library that one can use on MacOS (iOS not tested). It uses the stdlib `<dns_sd.h>`.
# How to test
1. Run the broadcaster.
```sh
cd broadcaster
clang++ -o broadcaster main.cpp
./broadcaster
```
You should see output of the service details of the service we are advertising.
2. Scan for services
```sh
cd scanner
clang++ -o scanner main.cpp
./scanner
```
Having done this, the output should include the resolved service.
BIN
View File
Binary file not shown.
+51
View File
@@ -0,0 +1,51 @@
#include <cstring>
#include <dns_sd.h>
#include <iostream>
void register_service_callback(DNSServiceRef service, DNSServiceFlags flags,
DNSServiceErrorType errorCode, const char *name,
const char *regtype, const char *domain,
void *context) {
if (errorCode == kDNSServiceErr_NoError) {
std::cout << "Service registered successfully!" << std::endl;
std::cout << "Service Name: " << name << std::endl;
std::cout << "Service Type: " << regtype << std::endl;
std::cout << "Domain: " << domain << std::endl;
} else {
std::cerr << "Error registering service: " << errorCode << std::endl;
}
}
int main() {
DNSServiceRef serviceRef;
// Register a service with Bonjour
const char *serviceName = "My HTTP Service";
const char *serviceType = "_flowy._tcp";
uint16_t servicePort = 8080;
// Register the service
DNSServiceErrorType err = DNSServiceRegister(
&serviceRef, 0, 0, serviceName, serviceType, nullptr, nullptr,
htons(servicePort), 0, nullptr, register_service_callback, nullptr);
if (err != kDNSServiceErr_NoError) {
std::cerr << "Error registering service: " << err << std::endl;
return 1;
}
// Run the Bonjour service (advertise the service)
std::cout << "Advertising service: " << serviceName << " on port "
<< servicePort << std::endl;
// Now keep the event loop running properly to process mDNS queries
// This will block here while the service is being advertised
while (true) {
// Processing the events - this is the blocking call
DNSServiceProcessResult(serviceRef);
}
// Cleanup when done (this line will never be reached in this version)
DNSServiceRefDeallocate(serviceRef);
return 0;
}
+62
View File
@@ -0,0 +1,62 @@
#include <cstdio>
#include <dns_sd.h>
void DNSSD_API serviceResolveCallback(
DNSServiceRef service, DNSServiceFlags flags, uint32_t interfaceIndex,
DNSServiceErrorType errorCode, const char *fullName, const char *hostTarget,
uint16_t port, uint16_t txtLen, const unsigned char *txtRecord,
void *context) {
if (errorCode == kDNSServiceErr_NoError) {
printf("Resolved Service: %s\n", fullName);
printf("Host: %s\n", hostTarget);
printf("Port: %u\n", ntohs(port)); // Convert the port to host byte order
} else {
printf("Failed to resolve service: %d\n", errorCode);
}
}
void DNSSD_API serviceBrowseCallback(
DNSServiceRef service, DNSServiceFlags flags, uint32_t interfaceIndex,
DNSServiceErrorType errorCode, const char *serviceName, const char *regtype,
const char *replyDomain, void *context) {
if (errorCode == kDNSServiceErr_NoError) {
printf("Service found: %s.%s%s\n", serviceName, regtype,
replyDomain ? replyDomain : "");
// Now resolve the service to get port and other details
DNSServiceRef resolveRef;
DNSServiceErrorType resolveError =
DNSServiceResolve(&resolveRef, 0, interfaceIndex, serviceName, regtype,
replyDomain, serviceResolveCallback, nullptr);
if (resolveError != kDNSServiceErr_NoError) {
printf("Error resolving service: %d\n", resolveError);
} else {
// Process the resolved service
DNSServiceProcessResult(resolveRef);
DNSServiceRefDeallocate(resolveRef);
}
}
}
int browseServices() {
DNSServiceRef serviceRef;
DNSServiceErrorType error = DNSServiceBrowse(
&serviceRef, 0, 0, "_flowy._tcp", NULL, serviceBrowseCallback, NULL);
if (error != kDNSServiceErr_NoError) {
fprintf(stderr, "Error browsing services: %d\n", error);
return -1;
}
// Process the mDNS service (this will block until services are discovered)
DNSServiceProcessResult(serviceRef);
DNSServiceRefDeallocate(serviceRef);
return 0;
}
int main() {
browseServices();
return 0;
}
BIN
View File
Binary file not shown.