WPILibC++ 2024.3.2
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:
23 struct ServiceData {
24 /// IPv4 address.
25 unsigned int ipv4Address;
26 /// Port number.
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 * Starts multicast service resolver.
37 */
38 void Start();
39 /**
40 * Stops multicast service resolver.
41 */
42 void Stop();
43 /**
44 * Returns event handle.
45 *
46 * @return Event handle.
47 */
48 WPI_EventHandle GetEventHandle() const { return event.GetHandle(); }
49 /**
50 * Returns multicast service resolver data.
51 *
52 * @return Multicast service resolver data.
53 */
54 std::vector<ServiceData> GetData() {
55 std::scoped_lock lock{mutex};
56 event.Reset();
57 if (queue.empty()) {
58 return {};
59 }
60 std::vector<ServiceData> ret;
61 queue.swap(ret);
62 return ret;
63 }
64 /**
65 * Returns true if there's a multicast service resolver implementation.
66 *
67 * @return True if there's a multicast service resolver implementation.
68 */
69 bool HasImplementation() const;
70 struct Impl;
71
72 private:
73 void PushData(ServiceData&& data) {
74 std::scoped_lock lock{mutex};
75 queue.emplace_back(std::forward<ServiceData>(data));
76 event.Set();
77 }
78 wpi::Event event{true};
79 std::vector<ServiceData> queue;
80 wpi::mutex mutex;
81 std::unique_ptr<Impl> pImpl;
82};
83} // namespace wpi
84#endif
85
86#ifdef __cplusplus
87extern "C" {
88#endif
89
90typedef unsigned int WPI_MulticastServiceResolverHandle; // NOLINT
91
93 const char* serviceType);
94
97
100
103
106
109
110typedef struct WPI_ServiceData { // NOLINT
111 uint32_t ipv4Address;
112 int32_t port;
113 const char* serviceName;
114 const char* hostName;
115 int32_t txtCount;
116 const char** txtKeys;
117 const char** txtValues;
119
121 WPI_MulticastServiceResolverHandle handle, int32_t* dataCount);
122
123void WPI_FreeServiceData(WPI_ServiceData* serviceData, int32_t length);
124
125#ifdef __cplusplus
126} // extern "C"
127#endif
unsigned int WPI_MulticastServiceResolverHandle
Definition: MulticastServiceResolver.h:90
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
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:48
std::vector< ServiceData > GetData()
Returns multicast service resolver data.
Definition: MulticastServiceResolver.h:54
bool HasImplementation() const
Returns true if there's a multicast service resolver implementation.
basic_string_view< char > string_view
Definition: core.h:501
Definition: ntcore_cpp.h:26
Definition: MulticastServiceResolver.h:110
const char ** txtValues
Definition: MulticastServiceResolver.h:117
uint32_t ipv4Address
Definition: MulticastServiceResolver.h:111
const char * hostName
Definition: MulticastServiceResolver.h:114
int32_t txtCount
Definition: MulticastServiceResolver.h:115
const char ** txtKeys
Definition: MulticastServiceResolver.h:116
const char * serviceName
Definition: MulticastServiceResolver.h:113
int32_t port
Definition: MulticastServiceResolver.h:112
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.
Definition: MulticastServiceResolver.h:25
int port
Port number.
Definition: MulticastServiceResolver.h:27
std::string serviceName
Service name.
Definition: MulticastServiceResolver.h:29
::std::mutex mutex
Definition: mutex.h:17