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