13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci#include <mutex> 173f4cbf05Sopenharmony_ci#include "utils_log.h" 183f4cbf05Sopenharmony_ci#include "common_event_sys_errors.h" 193f4cbf05Sopenharmony_ci#include "io_event_reactor.h" 203f4cbf05Sopenharmony_ci#include "io_event_handler.h" 213f4cbf05Sopenharmony_ci 223f4cbf05Sopenharmony_cinamespace OHOS { 233f4cbf05Sopenharmony_cinamespace Utils { 243f4cbf05Sopenharmony_ciIOEventHandler::IOEventHandler() 253f4cbf05Sopenharmony_ci :prev_(nullptr), next_(nullptr), fd_(IO_EVENT_INVALID_FD), events_(Events::EVENT_NONE), 263f4cbf05Sopenharmony_ci cb_(nullptr), enabled_(false) {} 273f4cbf05Sopenharmony_ci 283f4cbf05Sopenharmony_ciIOEventHandler::IOEventHandler(int fd, EventId events, const EventCallback& cb) 293f4cbf05Sopenharmony_ci :prev_(nullptr), next_(nullptr), fd_(fd), events_(events), cb_(cb), enabled_(false) {} 303f4cbf05Sopenharmony_ci 313f4cbf05Sopenharmony_ciIOEventHandler::~IOEventHandler() 323f4cbf05Sopenharmony_ci{ 333f4cbf05Sopenharmony_ci if (prev_ != nullptr) { 343f4cbf05Sopenharmony_ci prev_->next_ = next_; 353f4cbf05Sopenharmony_ci } 363f4cbf05Sopenharmony_ci 373f4cbf05Sopenharmony_ci if (next_ != nullptr) { 383f4cbf05Sopenharmony_ci next_->prev_ = prev_; 393f4cbf05Sopenharmony_ci } 403f4cbf05Sopenharmony_ci 413f4cbf05Sopenharmony_ci prev_ = nullptr; 423f4cbf05Sopenharmony_ci next_ = nullptr; 433f4cbf05Sopenharmony_ci} 443f4cbf05Sopenharmony_ci 453f4cbf05Sopenharmony_cibool IOEventHandler::Start(IOEventReactor* reactor) 463f4cbf05Sopenharmony_ci{ 473f4cbf05Sopenharmony_ci UTILS_LOGD("%{public}s: Try add handler-%{public}p to reactor-%{public}p.", \ 483f4cbf05Sopenharmony_ci __FUNCTION__, reinterpret_cast<void*>(this), reinterpret_cast<void*>(reactor)); 493f4cbf05Sopenharmony_ci ErrCode res = reactor->AddHandler(this); 503f4cbf05Sopenharmony_ci if (res != EVENT_SYS_ERR_OK) { 513f4cbf05Sopenharmony_ci UTILS_LOGE("%{public}s: Try add handler failed.", __FUNCTION__); 523f4cbf05Sopenharmony_ci return false; 533f4cbf05Sopenharmony_ci } 543f4cbf05Sopenharmony_ci 553f4cbf05Sopenharmony_ci return true; 563f4cbf05Sopenharmony_ci} 573f4cbf05Sopenharmony_ci 583f4cbf05Sopenharmony_cibool IOEventHandler::Stop(IOEventReactor* reactor) 593f4cbf05Sopenharmony_ci{ 603f4cbf05Sopenharmony_ci UTILS_LOGD("%{public}s: Try remove handler-%{public}p from reactor-%{public}p.", \ 613f4cbf05Sopenharmony_ci __FUNCTION__, reinterpret_cast<void*>(this), reinterpret_cast<void*>(reactor)); 623f4cbf05Sopenharmony_ci ErrCode res = reactor->RemoveHandler(this); 633f4cbf05Sopenharmony_ci if (res != EVENT_SYS_ERR_OK) { 643f4cbf05Sopenharmony_ci UTILS_LOGE("%{public}s: Try remove handler failed.", __FUNCTION__); 653f4cbf05Sopenharmony_ci return false; 663f4cbf05Sopenharmony_ci } 673f4cbf05Sopenharmony_ci 683f4cbf05Sopenharmony_ci return true; 693f4cbf05Sopenharmony_ci} 703f4cbf05Sopenharmony_ci 713f4cbf05Sopenharmony_cibool IOEventHandler::Update(IOEventReactor* reactor) 723f4cbf05Sopenharmony_ci{ 733f4cbf05Sopenharmony_ci UTILS_LOGD("%{public}s: Try update handler-%{public}p to reactor-%{public}p.", \ 743f4cbf05Sopenharmony_ci __FUNCTION__, reinterpret_cast<void*>(this), reinterpret_cast<void*>(reactor)); 753f4cbf05Sopenharmony_ci ErrCode res = reactor->UpdateHandler(this); 763f4cbf05Sopenharmony_ci if (res != EVENT_SYS_ERR_OK) { 773f4cbf05Sopenharmony_ci UTILS_LOGE("%{public}s: Try update handler failed.", __FUNCTION__); 783f4cbf05Sopenharmony_ci return false; 793f4cbf05Sopenharmony_ci } 803f4cbf05Sopenharmony_ci 813f4cbf05Sopenharmony_ci return true; 823f4cbf05Sopenharmony_ci} 833f4cbf05Sopenharmony_ci 843f4cbf05Sopenharmony_civoid IOEventHandler::EnableRead() 853f4cbf05Sopenharmony_ci{ 863f4cbf05Sopenharmony_ci events_ |= Events::EVENT_READ; 873f4cbf05Sopenharmony_ci} 883f4cbf05Sopenharmony_ci 893f4cbf05Sopenharmony_civoid IOEventHandler::EnableWrite() 903f4cbf05Sopenharmony_ci{ 913f4cbf05Sopenharmony_ci events_ |= Events::EVENT_WRITE; 923f4cbf05Sopenharmony_ci} 933f4cbf05Sopenharmony_ci 943f4cbf05Sopenharmony_civoid IOEventHandler::DisableWrite() 953f4cbf05Sopenharmony_ci{ 963f4cbf05Sopenharmony_ci events_ &= ~Events::EVENT_WRITE; 973f4cbf05Sopenharmony_ci} 983f4cbf05Sopenharmony_civoid IOEventHandler::DisableAll() 993f4cbf05Sopenharmony_ci{ 1003f4cbf05Sopenharmony_ci events_ = Events::EVENT_NONE; 1013f4cbf05Sopenharmony_ci} 1023f4cbf05Sopenharmony_ci 1033f4cbf05Sopenharmony_ci} // Utils 1043f4cbf05Sopenharmony_ci} // OHOS