Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -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 | #pragma once |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | #include <poll.h> |
| 9 | |
| 10 | #include <atomic> |
| 11 | #include <memory> |
| 12 | #include <thread> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "dns_sd.h" |
| 17 | #include "wpi/Synchronization.h" |
| 18 | #include "wpi/mutex.h" |
| 19 | |
| 20 | namespace wpi { |
| 21 | class ResolverThread { |
| 22 | private: |
| 23 | struct private_init {}; |
| 24 | |
| 25 | public: |
| 26 | explicit ResolverThread(const private_init&); |
| 27 | ~ResolverThread() noexcept; |
| 28 | |
| 29 | void AddServiceRef(DNSServiceRef serviceRef, dnssd_sock_t socket); |
| 30 | void RemoveServiceRefInThread(DNSServiceRef serviceRef); |
| 31 | WPI_EventHandle RemoveServiceRefOutsideThread(DNSServiceRef serviceRef); |
| 32 | |
| 33 | static std::shared_ptr<ResolverThread> Get(); |
| 34 | |
| 35 | private: |
| 36 | void ThreadMain(); |
| 37 | bool CleanupRefs(); |
| 38 | |
| 39 | wpi::mutex serviceRefMutex; |
| 40 | std::vector<std::pair<DNSServiceRef, WPI_EventHandle>> serviceRefsToRemove; |
| 41 | std::vector<std::pair<DNSServiceRef, dnssd_sock_t>> serviceRefs; |
| 42 | std::thread thread; |
| 43 | std::atomic_bool running; |
| 44 | }; |
| 45 | } // namespace wpi |