James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 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 | #if defined(__APPLE__) |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <netinet/in.h> |
| 10 | #include <poll.h> |
| 11 | |
| 12 | #include <atomic> |
| 13 | #include <memory> |
| 14 | #include <thread> |
| 15 | #include <utility> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <wpi/Synchronization.h> |
| 19 | #include <wpi/mutex.h> |
| 20 | |
| 21 | #include "dns_sd.h" |
| 22 | |
| 23 | namespace wpi { |
| 24 | class ResolverThread { |
| 25 | private: |
| 26 | struct private_init {}; |
| 27 | |
| 28 | public: |
| 29 | explicit ResolverThread(const private_init&); |
| 30 | ~ResolverThread() noexcept; |
| 31 | |
| 32 | void AddServiceRef(DNSServiceRef serviceRef, dnssd_sock_t socket); |
| 33 | void RemoveServiceRefInThread(DNSServiceRef serviceRef); |
| 34 | WPI_EventHandle RemoveServiceRefOutsideThread(DNSServiceRef serviceRef); |
| 35 | |
| 36 | static std::shared_ptr<ResolverThread> Get(); |
| 37 | |
| 38 | private: |
| 39 | void ThreadMain(); |
| 40 | bool CleanupRefs(); |
| 41 | |
| 42 | wpi::mutex serviceRefMutex; |
| 43 | std::vector<std::pair<DNSServiceRef, WPI_EventHandle>> serviceRefsToRemove; |
| 44 | std::vector<std::pair<DNSServiceRef, dnssd_sock_t>> serviceRefs; |
| 45 | std::thread thread; |
| 46 | std::atomic_bool running; |
| 47 | }; |
| 48 | } // namespace wpi |
| 49 | |
| 50 | #endif // defined(__APPLE__) |