1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef EPOLL_MANAGER_H
17 #define EPOLL_MANAGER_H
18 
19 #include <cinttypes>
20 #include <map>
21 #include <memory>
22 #include <vector>
23 
24 #include "nocopyable.h"
25 
26 #include "i_epoll_event_source.h"
27 
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 class EpollManager final : public IEpollEventSource {
32 public:
33     EpollManager() = default;
34     ~EpollManager();
35     DISALLOW_COPY_AND_MOVE(EpollManager);
36 
37     bool Open();
38     void Close();
39 
40     bool Add(std::shared_ptr<IEpollEventSource> source);
41     void Remove(std::shared_ptr<IEpollEventSource> source);
42     bool Update(std::shared_ptr<IEpollEventSource> source);
43     int32_t Wait(struct epoll_event *events, int32_t maxevents);
44     int32_t WaitTimeout(struct epoll_event *events, int32_t maxevents, int32_t timeout);
45 
46     int32_t GetFd() const override;
47     void Dispatch(const struct epoll_event &ev) override;
48 
49 private:
50     void DispatchOne(const struct epoll_event &ev);
51 
52 private:
53     int32_t epollFd_ { -1 };
54     std::map<int32_t, std::shared_ptr<IEpollEventSource>> sources_;
55 };
56 
GetFd() const57 inline int32_t EpollManager::GetFd() const
58 {
59     return epollFd_;
60 }
61 } // namespace DeviceStatus
62 } // namespace Msdp
63 } // namespace OHOS
64 #endif // EPOLL_MANAGER_H