123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #ifndef NETLINK_LISTENER_H
- #define NETLINK_LISTENER_H
- #include <functional>
- #include <map>
- #include <mutex>
- #include <thread>
- #include <android-base/thread_annotations.h>
- #include <netdutils/Netlink.h>
- #include <netdutils/Slice.h>
- #include <netdutils/Status.h>
- #include <netdutils/UniqueFd.h>
- namespace android {
- namespace net {
- class NetlinkListenerInterface {
- public:
- using DispatchFn = std::function<void(const nlmsghdr& nlmsg, const netdutils::Slice msg)>;
- using SkErrorHandler = std::function<void(const int fd, const int err)>;
- virtual ~NetlinkListenerInterface() = default;
-
- virtual netdutils::Status send(const netdutils::Slice msg) = 0;
-
-
-
-
-
- virtual netdutils::Status subscribe(uint16_t type, const DispatchFn& fn) = 0;
-
-
- virtual netdutils::Status unsubscribe(uint16_t type) = 0;
- virtual void registerSkErrorHandler(const SkErrorHandler& handler) = 0;
- };
- class NetlinkListener : public NetlinkListenerInterface {
- public:
- NetlinkListener(netdutils::UniqueFd event, netdutils::UniqueFd sock, const std::string& name);
- ~NetlinkListener() override;
- netdutils::Status send(const netdutils::Slice msg) override;
- netdutils::Status subscribe(uint16_t type, const DispatchFn& fn) override EXCLUDES(mMutex);
- netdutils::Status unsubscribe(uint16_t type) override EXCLUDES(mMutex);
- void registerSkErrorHandler(const SkErrorHandler& handler) override;
- private:
- netdutils::Status run();
- const netdutils::UniqueFd mEvent;
- const netdutils::UniqueFd mSock;
- const std::string mThreadName;
- std::mutex mMutex;
- std::map<uint16_t, DispatchFn> mDispatchMap GUARDED_BY(mMutex);
- std::thread mWorker;
- SkErrorHandler mErrorHandler;
- };
- }
- }
- #endif
|