WPILibC++ 2027.0.0-alpha-2
Loading...
Searching...
No Matches
MulticastServiceResolver.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
8
9#ifdef __cplusplus
10#include <functional>
11#include <memory>
12#include <string>
13#include <string_view>
14#include <utility>
15#include <vector>
16
17#include <wpi/mutex.h>
18namespace wpi {
20 public:
21 explicit MulticastServiceResolver(std::string_view serviceType);
23 struct ServiceData {
24 /// IPv4 address in host order.
25 unsigned int ipv4Address;
26 /// Port number in host order.
27 int port;
28 /// Service name.
29 std::string serviceName;
30 /// Host name.
31 std::string hostName;
32 /// Service data payload.
33 std::vector<std::pair<std::string, std::string>> txt;
34 };
35 /**
36 * Set a copy callback to be called when a service is resolved.
37 * Takes presidence over the move callback. Return true to
38 * not send the data to the event queue.
39 */
40 bool SetCopyCallback(std::function<bool(const ServiceData&)> callback);
41 /**
42 * Set a move callback to be called when a service is resolved.
43 * Data is moved into the function and cannot be added to the event queue.
44 */
45 bool SetMoveCallback(std::function<void(ServiceData&&)> callback);
46 /**
47 * Starts multicast service resolver.
48 */
49 void Start();
50 /**
51 * Stops multicast service resolver.
52 */
53 void Stop();
54 /**
55 * Returns event handle.
56 *
57 * @return Event handle.
58 */
59 WPI_EventHandle GetEventHandle() const { return event.GetHandle(); }
60 /**
61 * Returns multicast service resolver data.
62 *
63 * @return Multicast service resolver data.
64 */
65 std::vector<ServiceData> GetData() {
66 std::scoped_lock lock{mutex};
67 event.Reset();
68 if (queue.empty()) {
69 return {};
70 }
71 std::vector<ServiceData> ret;
72 queue.swap(ret);
73 return ret;
74 }
75 /**
76 * Returns true if there's a multicast service resolver implementation.
77 *
78 * @return True if there's a multicast service resolver implementation.
79 */
80 bool HasImplementation() const;
81 struct Impl;
82
83 private:
84 void PushData(ServiceData&& data) {
85 std::scoped_lock lock{mutex};
86 if (copyCallback) {
87 if (!copyCallback(data)) {
88 queue.emplace_back(std::forward<ServiceData>(data));
89 event.Set();
90 }
91 } else if (moveCallback) {
92 moveCallback(std::move(data));
93 } else {
94 queue.emplace_back(std::forward<ServiceData>(data));
95 event.Set();
96 }
97 }
98 wpi::Event event{true};
99 std::vector<ServiceData> queue;
100 wpi::mutex mutex;
101 std::function<bool(const ServiceData&)> copyCallback;
102 std::function<void(ServiceData&&)> moveCallback;
103 std::unique_ptr<Impl> pImpl;
104};
105} // namespace wpi
106#endif
107
108#ifdef __cplusplus
109extern "C" {
110#endif
111
112typedef unsigned int WPI_MulticastServiceResolverHandle; // NOLINT
113
115 const char* serviceType);
116
119
122
125
128
131
132typedef struct WPI_ServiceData { // NOLINT
133 uint32_t ipv4Address;
134 int32_t port;
135 const char* serviceName;
136 const char* hostName;
137 int32_t txtCount;
138 const char** txtKeys;
139 const char** txtValues;
141
143 WPI_MulticastServiceResolverHandle handle, int32_t* dataCount);
144
145void WPI_FreeServiceData(WPI_ServiceData* serviceData, int32_t length);
146
147#ifdef __cplusplus
148} // extern "C"
149#endif
unsigned int WPI_MulticastServiceResolverHandle
Definition MulticastServiceResolver.h:112
void WPI_StopMulticastServiceResolver(WPI_MulticastServiceResolverHandle handle)
void WPI_FreeMulticastServiceResolver(WPI_MulticastServiceResolverHandle handle)
WPI_MulticastServiceResolverHandle WPI_CreateMulticastServiceResolver(const char *serviceType)
void WPI_StartMulticastServiceResolver(WPI_MulticastServiceResolverHandle handle)
WPI_EventHandle WPI_GetMulticastServiceResolverEventHandle(WPI_MulticastServiceResolverHandle handle)
int32_t WPI_GetMulticastServiceResolverHasImplementation(WPI_MulticastServiceResolverHandle handle)
struct WPI_ServiceData WPI_ServiceData
void WPI_FreeServiceData(WPI_ServiceData *serviceData, int32_t length)
WPI_ServiceData * WPI_GetMulticastServiceResolverData(WPI_MulticastServiceResolverHandle handle, int32_t *dataCount)
WPI_Handle WPI_EventHandle
An event handle.
Definition Synchronization.h:25
An atomic signaling event for synchronization.
Definition Synchronization.h:250
Definition MulticastServiceResolver.h:19
bool SetCopyCallback(std::function< bool(const ServiceData &)> callback)
Set a copy callback to be called when a service is resolved.
bool SetMoveCallback(std::function< void(ServiceData &&)> callback)
Set a move callback to be called when a service is resolved.
MulticastServiceResolver(std::string_view serviceType)
void Start()
Starts multicast service resolver.
void Stop()
Stops multicast service resolver.
WPI_EventHandle GetEventHandle() const
Returns event handle.
Definition MulticastServiceResolver.h:59
std::vector< ServiceData > GetData()
Returns multicast service resolver data.
Definition MulticastServiceResolver.h:65
bool HasImplementation() const
Returns true if there's a multicast service resolver implementation.
Definition ntcore_cpp.h:26
::std::mutex mutex
Definition mutex.h:17
Definition MulticastServiceResolver.h:132
const char ** txtValues
Definition MulticastServiceResolver.h:139
uint32_t ipv4Address
Definition MulticastServiceResolver.h:133
const char * hostName
Definition MulticastServiceResolver.h:136
int32_t txtCount
Definition MulticastServiceResolver.h:137
const char ** txtKeys
Definition MulticastServiceResolver.h:138
const char * serviceName
Definition MulticastServiceResolver.h:135
int32_t port
Definition MulticastServiceResolver.h:134
Definition MulticastServiceResolver.h:23
std::vector< std::pair< std::string, std::string > > txt
Service data payload.
Definition MulticastServiceResolver.h:33
std::string hostName
Host name.
Definition MulticastServiceResolver.h:31
unsigned int ipv4Address
IPv4 address in host order.
Definition MulticastServiceResolver.h:25
int port
Port number in host order.
Definition MulticastServiceResolver.h:27
std::string serviceName
Service name.
Definition MulticastServiceResolver.h:29