![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /usr/local/lib/node_modules/mediasoup/worker/src/handles/ |
Upload File : |
#define MS_CLASS "Timer" // #define MS_LOG_DEV_LEVEL 3 #include "handles/Timer.hpp" #include "DepLibUV.hpp" #include "Logger.hpp" #include "MediaSoupErrors.hpp" /* Static methods for UV callbacks. */ inline static void onTimer(uv_timer_t* handle) { static_cast<Timer*>(handle->data)->OnUvTimer(); } inline static void onClose(uv_handle_t* handle) { delete handle; } /* Instance methods. */ Timer::Timer(Listener* listener) : listener(listener) { MS_TRACE(); this->uvHandle = new uv_timer_t; this->uvHandle->data = static_cast<void*>(this); int err = uv_timer_init(DepLibUV::GetLoop(), this->uvHandle); if (err != 0) { delete this->uvHandle; this->uvHandle = nullptr; MS_THROW_ERROR("uv_timer_init() failed: %s", uv_strerror(err)); } } Timer::~Timer() { MS_TRACE(); if (!this->closed) Close(); } void Timer::Close() { MS_TRACE(); if (this->closed) return; this->closed = true; uv_close(reinterpret_cast<uv_handle_t*>(this->uvHandle), static_cast<uv_close_cb>(onClose)); } void Timer::Start(uint64_t timeout, uint64_t repeat) { MS_TRACE(); if (this->closed) MS_THROW_ERROR("closed"); this->timeout = timeout; this->repeat = repeat; if (uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) != 0) Stop(); int err = uv_timer_start(this->uvHandle, static_cast<uv_timer_cb>(onTimer), timeout, repeat); if (err != 0) MS_THROW_ERROR("uv_timer_start() failed: %s", uv_strerror(err)); } void Timer::Stop() { MS_TRACE(); if (this->closed) MS_THROW_ERROR("closed"); int err = uv_timer_stop(this->uvHandle); if (err != 0) MS_THROW_ERROR("uv_timer_stop() failed: %s", uv_strerror(err)); } void Timer::Reset() { MS_TRACE(); if (this->closed) MS_THROW_ERROR("closed"); if (uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) == 0) return; if (this->repeat == 0u) return; int err = uv_timer_start(this->uvHandle, static_cast<uv_timer_cb>(onTimer), this->repeat, this->repeat); if (err != 0) MS_THROW_ERROR("uv_timer_start() failed: %s", uv_strerror(err)); } void Timer::Restart() { MS_TRACE(); if (this->closed) MS_THROW_ERROR("closed"); if (uv_is_active(reinterpret_cast<uv_handle_t*>(this->uvHandle)) != 0) Stop(); int err = uv_timer_start(this->uvHandle, static_cast<uv_timer_cb>(onTimer), this->timeout, this->repeat); if (err != 0) MS_THROW_ERROR("uv_timer_start() failed: %s", uv_strerror(err)); } inline void Timer::OnUvTimer() { MS_TRACE(); // Notify the listener. this->listener->OnTimer(this); }