| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | /* | ||
| 3 | * This file is part of the Falcon Player (FPP) and is Copyright (C) | ||
| 4 | * 2013-2022 by the Falcon Player Developers. | ||
| 5 | * | ||
| 6 | * The Falcon Player (FPP) is free software, and is covered under | ||
| 7 | * multiple Open Source licenses. Please see the included 'LICENSES' | ||
| 8 | * file for descriptions of what files are covered by each license. | ||
| 9 | * | ||
| 10 | * This source file is covered under the LGPL v2.1 as described in the | ||
| 11 | * included LICENSE.LGPL file. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <functional> | ||
| 15 | #include <list> | ||
| 16 | #include <map> | ||
| 17 | #include <mutex> | ||
| 18 | #include <string> | ||
| 19 | |||
| 20 | class NetworkMonitor { | ||
| 21 | public: | ||
| 22 | enum class NetEventType { | ||
| 23 | NEW_LINK, | ||
| 24 | DEL_LINK, | ||
| 25 | NEW_ADDR, | ||
| 26 | DEL_ADDR | ||
| 27 | }; | ||
| 28 | |||
| 29 | 2346 | NetworkMonitor() : | |
| 30 | 2346 | curId(0) {} | |
| 31 | 458 | ~NetworkMonitor() {} | |
| 32 | |||
| 33 | void Init(std::map<int, std::function<bool(int)>>& callbacks); | ||
| 34 | |||
| 35 | int registerCallback(std::function<void(NetEventType, int, const std::string&)>& callback); | ||
| 36 | int addCallback(std::function<void(NetEventType, int, const std::string&)>& callback) { return registerCallback(callback); } | ||
| 37 | void removeCallback(int id); | ||
| 38 | |||
| 39 | static NetworkMonitor INSTANCE; | ||
| 40 | |||
| 41 | private: | ||
| 42 | void callCallbacks(NetEventType, int, const std::string& n); | ||
| 43 | // Guards `callbacks` against concurrent access: register/remove run on the | ||
| 44 | // main thread (channel-output Init/Close, MultiSync setup) while dispatch | ||
| 45 | // runs on the netlink/epoll thread. Held across dispatch so removeCallback | ||
| 46 | // cannot return (letting the caller destroy the captured object) while a | ||
| 47 | // callback is still executing - an unsynchronized std::map here let a | ||
| 48 | // netlink event during teardown corrupt the tree and crash at a later free. | ||
| 49 | std::mutex callbackLock; | ||
| 50 | std::map<int, std::function<void(NetEventType, int, const std::string&)>> callbacks; | ||
| 51 | int curId; | ||
| 52 | }; | ||
| 53 |