• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "utils_log.h"
17 #include "common_event_sys_errors.h"
18 #include "io_event_reactor.h"
19 #include "io_event_handler.h"
20 
21 namespace OHOS {
22 namespace Utils {
IOEventHandler()23 IOEventHandler::IOEventHandler()
24     :prev_(nullptr), next_(nullptr), fd_(IO_EVENT_INVALID_FD), events_(Events::EVENT_NONE),
25     cb_(nullptr), enabled_(false) {}
26 
IOEventHandler(int fd,EventId events,const EventCallback & cb)27 IOEventHandler::IOEventHandler(int fd, EventId events, const EventCallback& cb)
28     :prev_(nullptr), next_(nullptr), fd_(fd), events_(events), cb_(cb), enabled_(false) {}
29 
~IOEventHandler()30 IOEventHandler::~IOEventHandler()
31 {
32     if (prev_ != nullptr) {
33         prev_->next_ = next_;
34     }
35 
36     if (next_ != nullptr) {
37         next_->prev_ = prev_;
38     }
39 
40     prev_ = nullptr;
41     next_ = nullptr;
42 }
43 
Start(IOEventReactor * reactor)44 bool IOEventHandler::Start(IOEventReactor* reactor)
45 {
46     ErrCode res = reactor->AddHandler(this);
47     if (res != EVENT_SYS_ERR_OK) {
48         UTILS_LOGE("%{public}s: Try add handler failed.", __FUNCTION__);
49         return false;
50     }
51 
52     return true;
53 }
54 
Stop(IOEventReactor * reactor)55 bool IOEventHandler::Stop(IOEventReactor* reactor)
56 {
57     ErrCode res = reactor->RemoveHandler(this);
58     if (res != EVENT_SYS_ERR_OK) {
59         UTILS_LOGE("%{public}s: Try remove handler failed.", __FUNCTION__);
60         return false;
61     }
62 
63     return true;
64 }
65 
Update(IOEventReactor * reactor)66 bool IOEventHandler::Update(IOEventReactor* reactor)
67 {
68     ErrCode res = reactor->UpdateHandler(this);
69     if (res != EVENT_SYS_ERR_OK) {
70         UTILS_LOGE("%{public}s: Try update handler failed.", __FUNCTION__);
71         return false;
72     }
73 
74     return true;
75 }
76 
EnableRead()77 void IOEventHandler::EnableRead()
78 {
79     events_ |= Events::EVENT_READ;
80 }
81 
EnableWrite()82 void IOEventHandler::EnableWrite()
83 {
84     events_ |= Events::EVENT_WRITE;
85 }
86 
DisableWrite()87 void IOEventHandler::DisableWrite()
88 {
89     events_ &= ~Events::EVENT_WRITE;
90 }
DisableAll()91 void IOEventHandler::DisableAll()
92 {
93     events_ = Events::EVENT_NONE;
94 }
95 
96 } // Utils
97 } // OHOS