yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
bonjour_publisher.cc
Go to the documentation of this file.
2
3#include "util/log.h"
4
5#ifdef __APPLE__
6#include <dns_sd.h>
7#endif
8
9namespace yaze {
10namespace cli {
11namespace api {
12
16
17void BonjourPublisher::Publish(int port, const std::string& rom_title) {
18#ifdef __APPLE__
19 if (published_)
20 return;
21
22 // Build TXT record: version=1, rom=<title>, capabilities=render,commands,annotations
23 TXTRecordRef txt;
24 TXTRecordCreate(&txt, 0, nullptr);
25 TXTRecordSetValue(&txt, "version", 1, "1");
26
27 std::string capabilities = "render,commands,annotations";
28 TXTRecordSetValue(&txt, "capabilities",
29 static_cast<uint8_t>(capabilities.size()),
30 capabilities.c_str());
31
32 if (!rom_title.empty()) {
33 TXTRecordSetValue(&txt, "rom", static_cast<uint8_t>(rom_title.size()),
34 rom_title.c_str());
35 }
36
37 DNSServiceErrorType err =
38 DNSServiceRegister(&service_ref_,
39 0, // no flags
40 0, // all interfaces
41 nullptr, // use default host name
42 "_yaze._tcp.", // service type
43 nullptr, // default domain
44 nullptr, // default host
45 htons(static_cast<uint16_t>(port)),
46 TXTRecordGetLength(&txt), TXTRecordGetBytesPtr(&txt),
47 nullptr, // no callback
48 nullptr); // no context
49
50 TXTRecordDeallocate(&txt);
51
52 if (err == kDNSServiceErr_NoError) {
53 published_ = true;
54 LOG_INFO("BonjourPublisher", "Published _yaze._tcp. on port %d", port);
55 } else {
56 LOG_ERROR("BonjourPublisher", "Failed to publish Bonjour service: %d", err);
57 }
58#else
59 (void)port;
60 (void)rom_title;
61 LOG_INFO("BonjourPublisher",
62 "Bonjour discovery not available on this platform");
63#endif
64}
65
67#ifdef __APPLE__
68 if (!published_)
69 return;
70 if (service_ref_) {
71 DNSServiceRefDeallocate(service_ref_);
72 service_ref_ = nullptr;
73 }
74 published_ = false;
75 LOG_INFO("BonjourPublisher", "Unpublished _yaze._tcp.");
76#endif
77}
78
79} // namespace api
80} // namespace cli
81} // namespace yaze
void Unpublish()
Stop advertising. Safe to call multiple times.
void Publish(int port, const std::string &rom_title="")
Start advertising. Call after the HTTP server socket is bound.
#define LOG_ERROR(category, format,...)
Definition log.h:109
#define LOG_INFO(category, format,...)
Definition log.h:105