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)23EventHandler::EventHandler(int fd, EventReactor* r) 24 :fd_(fd), events_(EventReactor::NONE_EVENT), reactor_(r) 25 { 26 } 27 EnableRead()28void EventHandler::EnableRead() 29 { 30 events_ |= EventReactor::READ_EVENT; 31 Update(); 32 } 33 EnableWrite()34void EventHandler::EnableWrite() 35 { 36 events_ |= EventReactor::WRITE_EVENT; 37 Update(); 38 } 39 DisableWrite()40void EventHandler::DisableWrite() 41 { 42 events_ &= ~EventReactor::WRITE_EVENT; 43 Update(); 44 } DisableAll()45void EventHandler::DisableAll() 46 { 47 events_ = EventReactor::NONE_EVENT; 48 Update(); 49 } 50 HandleEvents(uint32_t events)51void EventHandler::HandleEvents(uint32_t events) 52 { 53 if (events & (EventReactor::CLOSE_EVENT)) { 54 if (closeCallback_) { 55 closeCallback_(); 56 } 57 } 58 59 if (events & (EventReactor::ERROR_EVENT)) { 60 if (errorCallback_) { 61 errorCallback_(); 62 } 63 } 64 65 if (events & (EventReactor::READ_EVENT)) { 66 if (readCallback_) { 67 readCallback_(); 68 } 69 } 70 71 if (events & (EventReactor::WRITE_EVENT)) { 72 if (writeCallback_) { 73 writeCallback_(); 74 } 75 } 76 } 77 Update()78void EventHandler::Update() 79 { 80 if (reactor_ != nullptr) { 81 reactor_->UpdateEventHandler(this); 82 } 83 } 84 85 } // Utils 86 } // OHOS