/* * SPDX-License-Identifier: LicenseRef-Unisoc-General-1.0 * Copyright 2016-2023 Unisoc (Shanghai) Technologies Co., Ltd * Licensed under the Unisoc General Software License, version 1.0 (the License); * you may not use this file except in compliance with the License. You may obtain a copy of the License at * https://www.unisoc.com/en_us/license/UNISOC_GENERAL_LICENSE_V1.0-EN_US * Software distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OF ANY KIND, either express or implied. * See the Unisoc General Software License, version 1.0 for more details. */ #include "CommonWatchManager.h" #include using namespace UDBus; CommonWatchManager *CommonWatchManager::mInstance = nullptr; CommonDispatcher *CommonWatchManager::getCommonDispatcher() { return mCommonDispatcher; } void CommonWatchManager::setCommonDispatcher(CommonDispatcher *commonDispatcher) { mCommonDispatcher = commonDispatcher; } CommonWatchManager::CommonWatchManager(CommonDispatcher *dispatcher) { this->mCommonDispatcher = dispatcher; } CommonWatch *CommonWatchManager::AddCommonWatch(int fd, int flags, CommonWatchFunc func, void *data, unsigned char pri) { if (nullptr == mInstance || nullptr == mInstance->getCommonDispatcher()) return nullptr; EventLoop *e = mInstance->getCommonDispatcher()->mMainLoop; if (nullptr == e) return nullptr; CommonWatchCallback *cb = new (nothrow) CommonWatchCallback(fd, flags, func, data); if (nullptr == cb) return nullptr; CommonWatch *watch = (CommonWatch *)mInstance->getCommonDispatcher()->AddWatch( (Watch::Internal *)cb); return watch; } void CommonWatchManager::RemoveCommonWatch(CommonWatch *watch) { if (nullptr == mInstance || nullptr == mInstance->getCommonDispatcher()) return; mInstance->getCommonDispatcher()->RemoveWatch((Watch *)watch); } int CommonWatchManager::GetFdByWatch(CommonWatch *watch) { if (watch) { return watch->GetDescriptor(); } return -1; } CommonTimeout *CommonWatchManager::AddCommonTimeout(int interval, CommonTimeOutFunc func, void *data, unsigned char pri) { if (nullptr == mInstance || nullptr == mInstance->getCommonDispatcher()) return nullptr; CommonTimeoutCallback *cb = new (nothrow) CommonTimeoutCallback(interval, func, data); if (nullptr == cb) return nullptr; CommonTimeout *timeout = reinterpret_cast( mInstance->getCommonDispatcher()->AddTimeout((CommonTimeout::Internal *)(cb))); if (nullptr == timeout) delete cb; return timeout; } CommonTimeout *CommonWatchManager::AddCommonTimeoutSecond(int interval, CommonTimeOutFunc func, void *data, unsigned char pri) { return AddCommonTimeout(interval * 1000, func, data, pri); } void CommonWatchManager::RemoveCommonTimeout(CommonTimeout *timeout) { if (nullptr == mInstance || nullptr == mInstance->getCommonDispatcher()) return; mInstance->getCommonDispatcher()->RemTimeout(reinterpret_cast(timeout)); } CommonWatchManager *CommonWatchManager::Init(CommonDispatcher *dispatcher) { if (!mInstance) { mInstance = new (nothrow) CommonWatchManager(dispatcher); } return mInstance; } void CommonWatchManager::Exit() { if (nullptr == mInstance) return; delete mInstance; mInstance = nullptr; }