35 :
Thread (
"Discovery_broadcast"),
53void NetworkServiceDiscovery::Advertiser::run()
55 if (!
socket.bindToPort (0))
61 while (! threadShouldExit())
64 wait ((
int) minInterval.inMilliseconds());
68void NetworkServiceDiscovery::Advertiser::sendBroadcast()
72 for (
auto& address : IPAddress::getAllAddresses())
77 message.setAttribute (
"address", address.toString());
80 auto data = message.toString (XmlElement::TextFormat().singleLine().withoutHeader());
82 socket.write (broadcastAddress.toString(), broadcastPort,
data.toRawUTF8(), (
int)
data.getNumBytesAsUTF8());
108void NetworkServiceDiscovery::AvailableServiceList::run()
110 while (! threadShouldExit())
112 if (
socket.waitUntilReady (
true, 200) == 1)
115 auto bytesRead =
socket.read (buffer,
sizeof (buffer) - 1,
false);
120 if (xml->hasTagName (serviceTypeUID))
121 handleMessage (*xml);
124 removeTimedOutServices();
135void NetworkServiceDiscovery::AvailableServiceList::handleAsyncUpdate()
137 NullCheckedInvocation::invoke (onChange);
140void NetworkServiceDiscovery::AvailableServiceList::handleMessage (
const XmlElement& xml)
145 if (service.instanceID.trim().isNotEmpty())
152 handleMessage (service);
159 const NetworkServiceDiscovery::Service& s2)
161 return s1.instanceID < s2.instanceID;
167void NetworkServiceDiscovery::AvailableServiceList::handleMessage (
const Service& service)
171 for (
auto& s : services)
173 if (s.instanceID == service.instanceID)
175 if (s.description != service.description
176 || s.address != service.address
177 || s.port != service.port)
180 triggerAsyncUpdate();
183 s.lastSeen = service.lastSeen;
188 services.push_back (service);
189 sortServiceList (services);
190 triggerAsyncUpdate();
193void NetworkServiceDiscovery::AvailableServiceList::removeTimedOutServices()
207 triggerAsyncUpdate();
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
bool bindToPort(int localPortNumber)
Binds the socket to the specified local port.
Automatically locks and unlocks a mutex object.
Represents an IP address.
static IPAddress local(bool IPv6=false) noexcept
Returns an IPv4 or IPv6 address meaning "localhost", equivalent to 127.0.0.1 (IPv4) or ::1 (IPv6)
static IPAddress getInterfaceBroadcastAddress(const IPAddress &interfaceAddress)
If the IPAdress is the address of an interface on the machine, returns the associated broadcast addre...
A relative measure of time.
static RelativeTime seconds(double seconds) noexcept
Creates a new RelativeTime object representing a number of seconds.
bool startThread()
Attempts to start a new thread with default ('Priority::normal') priority.
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
A universally unique 128-bit identifier.
Used to build a tree of elements representing an XML document.
int getIntAttribute(StringRef attributeName, int defaultReturnValue=0) const
Returns the value of a named attribute as an integer.
const String & getStringAttribute(StringRef attributeName) const noexcept
Returns the value of a named attribute.
void setAttribute(const Identifier &attributeName, const String &newValue)
Adds a named attribute to the element.
CriticalSection::ScopedLockType ScopedLock
Automatically locks and unlocks a CriticalSection object.
std::unique_ptr< XmlElement > parseXML(const String &textToParse)
Attempts to parse some XML text, returning a new XmlElement if it was valid.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Advertiser(const String &serviceTypeUID, const String &serviceDescription, int broadcastPort, int connectionPort, RelativeTime minTimeBetweenBroadcasts=RelativeTime::seconds(1.5))
Creates and starts an Advertiser thread, broadcasting with the given properties.
~Advertiser() override
Destructor.
AvailableServiceList(const String &serviceTypeUID, int broadcastPort)
Creates an AvailableServiceList that will bind to the given port number and watch the network for Adv...
~AvailableServiceList() override
Destructor.
std::vector< Service > getServices() const
Returns a list of the currently known services.