WPILibC++ 2027.0.0-alpha-4
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
11#include <functional>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <utility>
16#include <vector>
17
18#include "wpi/util/mutex.hpp"
19
20namespace wpi::net {
21
23 public:
24 explicit MulticastServiceResolver(std::string_view serviceType);
25
27
28 struct ServiceData {
29 /// IPv4 address in host order.
30 unsigned int ipv4Address;
31 /// Port number in host order.
32 int port;
33 /// Service name.
34 std::string serviceName;
35 /// Host name.
36 std::string hostName;
37 /// Service data payload.
38 std::vector<std::pair<std::string, std::string>> txt;
39 };
40
41 /**
42 * Set a copy callback to be called when a service is resolved.
43 * Takes presidence over the move callback. Return true to
44 * not send the data to the event queue.
45 */
46 bool SetCopyCallback(std::function<bool(const ServiceData&)> callback);
47
48 /**
49 * Set a move callback to be called when a service is resolved.
50 * Data is moved into the function and cannot be added to the event queue.
51 */
52 bool SetMoveCallback(std::function<void(ServiceData&&)> callback);
53
54 /**
55 * Starts multicast service resolver.
56 */
57 void Start();
58
59 /**
60 * Stops multicast service resolver.
61 */
62 void Stop();
63
64 /**
65 * Returns event handle.
66 *
67 * @return Event handle.
68 */
69 WPI_EventHandle GetEventHandle() const { return event.GetHandle(); }
70
71 /**
72 * Returns multicast service resolver data.
73 *
74 * @return Multicast service resolver data.
75 */
76 std::vector<ServiceData> GetData() {
77 std::scoped_lock lock{mutex};
78
79 event.Reset();
80 if (queue.empty()) {
81 return {};
82 }
83
84 std::vector<ServiceData> ret;
85 queue.swap(ret);
86 return ret;
87 }
88
89 /**
90 * Returns true if there's a multicast service resolver implementation.
91 *
92 * @return True if there's a multicast service resolver implementation.
93 */
94 bool HasImplementation() const;
95
96 struct Impl;
97
98 private:
99 void PushData(ServiceData&& data) {
100 std::scoped_lock lock{mutex};
101
102 if (copyCallback) {
103 if (!copyCallback(data)) {
104 queue.emplace_back(std::forward<ServiceData>(data));
105 event.Set();
106 }
107 } else if (moveCallback) {
108 moveCallback(std::move(data));
109 } else {
110 queue.emplace_back(std::forward<ServiceData>(data));
111 event.Set();
112 }
113 }
114
115 wpi::util::Event event{true};
116 std::vector<ServiceData> queue;
117 wpi::util::mutex mutex;
118 std::function<bool(const ServiceData&)> copyCallback;
119 std::function<void(ServiceData&&)> moveCallback;
120 std::unique_ptr<Impl> pImpl;
121};
122
123} // namespace wpi::net
124
125#endif
126
127#ifdef __cplusplus
128extern "C" {
129#endif
130
131typedef unsigned int WPI_MulticastServiceResolverHandle; // NOLINT
132
134 const char* serviceType);
135
138
141
144
147
150
151typedef struct WPI_ServiceData { // NOLINT
152 uint32_t ipv4Address;
153 int32_t port;
154 const char* serviceName;
155 const char* hostName;
156 int32_t txtCount;
157 const char** txtKeys;
158 const char** txtValues;
160
162 WPI_MulticastServiceResolverHandle handle, int32_t* dataCount);
163
164void WPI_FreeServiceData(WPI_ServiceData* serviceData, int32_t length);
165
166#ifdef __cplusplus
167} // extern "C"
168#endif
unsigned int WPI_MulticastServiceResolverHandle
Definition MulticastServiceResolver.h:131
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)
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:18
void Stop()
Stops multicast service resolver.
bool HasImplementation() const
Returns true if there's a multicast service resolver implementation.
void Start()
Starts multicast service resolver.
MulticastServiceResolver(std::string_view serviceType)
bool SetCopyCallback(std::function< bool(const ServiceData &)> callback)
Set a copy callback to be called when a service is resolved.
WPI_EventHandle GetEventHandle() const
Returns event handle.
Definition MulticastServiceResolver.h:69
bool SetMoveCallback(std::function< void(ServiceData &&)> callback)
Set a move callback to be called when a service is resolved.
std::vector< ServiceData > GetData()
Returns multicast service resolver data.
Definition MulticastServiceResolver.h:76
Definition raw_socket_ostream.hpp:9
::std::mutex mutex
Definition mutex.hpp:17
Definition MulticastServiceResolver.h:151
const char ** txtValues
Definition MulticastServiceResolver.h:158
uint32_t ipv4Address
Definition MulticastServiceResolver.h:152
const char * hostName
Definition MulticastServiceResolver.h:155
int32_t txtCount
Definition MulticastServiceResolver.h:156
const char ** txtKeys
Definition MulticastServiceResolver.h:157
const char * serviceName
Definition MulticastServiceResolver.h:154
int32_t port
Definition MulticastServiceResolver.h:153
Definition MulticastServiceResolver.h:28
std::string serviceName
Service name.
Definition MulticastServiceResolver.h:34
int port
Port number in host order.
Definition MulticastServiceResolver.h:32
unsigned int ipv4Address
IPv4 address in host order.
Definition MulticastServiceResolver.h:30
std::vector< std::pair< std::string, std::string > > txt
Service data payload.
Definition MulticastServiceResolver.h:38
std::string hostName
Host name.
Definition MulticastServiceResolver.h:36