1 /*
2  * Copyright (c) 2021 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 #include "event_handler.h"
17 #include "event_reactor.h"
18 #include <sys/epoll.h>
19 
20 namespace OHOS {
21 namespace Utils {
22 
EventHandler(int fd,EventReactor * r)23 EventHandler::EventHandler(int fd, EventReactor* r)
24     :fd_(fd), events_(EventReactor::NONE_EVENT), reactor_(r)
25 {
26 }
27 
EnableRead()28 void EventHandler::EnableRead()
29 {
30     events_ |= EventReactor::READ_EVENT;
31     Update();
32 }
33 
DisableAll()34 void EventHandler::DisableAll()
35 {
36     events_ = EventReactor::NONE_EVENT;
37     Update();
38 }
39 
HandleEvents(uint32_t events)40 void EventHandler::HandleEvents(uint32_t events)
41 {
42     if (events & (EventReactor::READ_EVENT)) {
43         if (readCallback_) {
44             readCallback_();
45         }
46     }
47 }
48 
Update()49 void EventHandler::Update()
50 {
51     if (reactor_ != nullptr) {
52         reactor_->UpdateEventHandler(this);
53     }
54 }
55 
56 } // Utils
57 } // OHOS
58