diff --git a/base/src/event_handler.h b/base/src/event_handler.h index eeb550f39732a58ca3f9005cc8a2cca4e51c3a20..d429c47e07b7e78a0c209fe340fa94afb061a89d 100644 --- a/base/src/event_handler.h +++ b/base/src/event_handler.h @@ -36,9 +36,10 @@ public: EventHandler(const EventHandler&) = delete; EventHandler& operator=(const EventHandler&&) = delete; EventHandler(const EventHandler&&) = delete; - ~EventHandler() {} + virtual ~EventHandler() {} int GetHandle() const { return (fd_); } + void SetHandle(int fd) { fd_ = fd; } uint32_t Events() const { return (events_); } void EnableRead(); diff --git a/base/src/event_reactor.cpp b/base/src/event_reactor.cpp index 91c6ebdada19d307a9b2fa5a8240f244fc98ebea..f04d61f99a24e896140a6111514f095f57be1ae0 100644 --- a/base/src/event_reactor.cpp +++ b/base/src/event_reactor.cpp @@ -103,7 +103,7 @@ uint32_t EventReactor::ScheduleTimer(const TimerCallback& cb, uint32_t interval, return ret; } - timerFd = handler->GetTimerFd(); + timerFd = handler->GetHandle(); timerEventHandlers_.push_back(handler); return TIMER_ERR_OK; } @@ -114,7 +114,7 @@ void EventReactor::CancelTimer(int timerFd) std::lock_guard lock(mutex_); auto itor = timerEventHandlers_.begin(); for (; itor != timerEventHandlers_.end(); ++itor) { - if ((*itor)->GetTimerFd() == timerFd) { + if ((*itor)->GetHandle() == timerFd) { (*itor)->Uninitialize(); timerEventHandlers_.erase(itor); return; diff --git a/base/src/timer_event_handler.cpp b/base/src/timer_event_handler.cpp index c710cdfabe82f3b0dbc1202bc458736e0274b9c1..c18f4f45f287c1036501299089be1008eb1cef80 100644 --- a/base/src/timer_event_handler.cpp +++ b/base/src/timer_event_handler.cpp @@ -15,7 +15,6 @@ #include "timer_event_handler.h" #include "event_reactor.h" -#include "event_handler.h" #include "common_timer_errors.h" #include "utils_log.h" @@ -31,24 +30,22 @@ static const int NANO_TO_BASE = 1000000000; constexpr int MILLI_TO_NANO = NANO_TO_BASE / MILLI_TO_BASE; TimerEventHandler::TimerEventHandler(EventReactor* p, uint32_t timeout /* ms */, bool once) - : once_(once), - timerFd_(timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC)), + : EventHandler(timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC), p), + once_(once), interval_(timeout), - reactor_(p), - handler_(new EventHandler(timerFd_, p)), callback_() { } TimerEventHandler::~TimerEventHandler() { - close(timerFd_); - timerFd_ = INVALID_TIMER_FD; + close(GetHandle()); + SetHandle(INVALID_TIMER_FD); } uint32_t TimerEventHandler::Initialize() { - if ((timerFd_ == INVALID_TIMER_FD) || (reactor_ == nullptr) || (handler_ == nullptr)) { + if ((GetHandle() == INVALID_TIMER_FD)) { UTILS_LOGE("TimerEventHandler::initialize failed."); return TIMER_ERR_INVALID_VALUE; } @@ -78,36 +75,34 @@ uint32_t TimerEventHandler::Initialize() newValue.it_interval.tv_nsec = (interval_ % MILLI_TO_BASE) * MILLI_TO_NANO; } - if (timerfd_settime(timerFd_, TFD_TIMER_ABSTIME, &newValue, nullptr) == -1) { + if (timerfd_settime(GetHandle(), TFD_TIMER_ABSTIME, &newValue, nullptr) == -1) { UTILS_LOGE("Failed in timerFd_settime"); return TIMER_ERR_DEAL_FAILED; } - handler_->SetReadCallback(std::bind(&TimerEventHandler::TimeOut, this)); - handler_->EnableRead(); + SetReadCallback(std::bind(&TimerEventHandler::TimeOut, this)); + EnableRead(); return TIMER_ERR_OK; } void TimerEventHandler::Uninitialize() { - if (handler_ != nullptr) { - handler_->DisableAll(); - } + DisableAll(); } void TimerEventHandler::TimeOut() { - if (timerFd_ == INVALID_TIMER_FD) { + if (GetHandle() == INVALID_TIMER_FD) { UTILS_LOGE("timerFd_ is invalid."); return; } uint64_t expirations = 0; - ssize_t n = ::read(timerFd_, &expirations, sizeof(expirations)); + ssize_t n = ::read(GetHandle(), &expirations, sizeof(expirations)); if (n != sizeof(expirations)) { UTILS_LOGE("epoll_loop::on_timer() reads %{public}d bytes instead of 8.", static_cast(n)); } if (callback_) { - callback_(timerFd_); + callback_(GetHandle()); } } diff --git a/base/src/timer_event_handler.h b/base/src/timer_event_handler.h index 1fd0bd69c231c84d671c9b78b57a5e76b16b99e2..86c720ae6f319527e7b897455bdaa08963724593 100644 --- a/base/src/timer_event_handler.h +++ b/base/src/timer_event_handler.h @@ -18,17 +18,17 @@ #include #include #include +#include "event_handler.h" namespace OHOS { namespace Utils { constexpr int INVALID_TIMER_FD = -1; -class EventHandler; class EventReactor; -class TimerEventHandler { +class TimerEventHandler : public EventHandler { using TimerCallback = std::function; - + public: TimerEventHandler(EventReactor* p, uint32_t timeout, bool once); ~TimerEventHandler(); @@ -44,19 +44,14 @@ public: void SetTimerCallback(const TimerCallback& callback) { callback_ = callback; } uint32_t GetInterval() const { return interval_; } - int GetTimerFd() const { return timerFd_; } private: void TimeOut(); private: bool once_; - int timerFd_; uint32_t interval_; - EventReactor* reactor_; - - std::shared_ptr handler_; - TimerCallback callback_; + TimerCallback callback_; }; } // namespace Utils