-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <uv.h> | ||
|
||
#include <cstdint> | ||
#include <functional> | ||
#include <memory> | ||
|
||
namespace uvpp { | ||
using IntervalCallback = std::function<void()>; | ||
|
||
class IntervalHandle { | ||
void* handle; | ||
IntervalCallback callback; | ||
|
||
IntervalHandle(const IntervalHandle&) = delete; | ||
IntervalHandle& operator=(const IntervalHandle&) = delete; | ||
IntervalHandle(IntervalHandle&&) = delete; | ||
IntervalHandle& operator=(IntervalHandle&&) = delete; | ||
|
||
public: | ||
IntervalHandle(void* handle, IntervalCallback callback) : handle(handle), callback(callback) {} | ||
|
||
void cancel(); | ||
|
||
bool isRunning(); | ||
|
||
friend void intervalCb(uv_timer_t* uvHandle); | ||
}; | ||
|
||
std::shared_ptr<IntervalHandle> uvIntervalStart(uint64_t intervalMs, uint64_t delayMs, IntervalCallback cb, | ||
uv_loop_t* loop = nullptr); | ||
} // namespace uvpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <uvpp/uvpp_interval.h> | ||
|
||
namespace uvpp { | ||
static void uvCloseDelete(void* handle) { | ||
uv_close((uv_handle_t*)handle, [](uv_handle_t* handleInner) { delete (uv_timer_t*)handleInner; }); | ||
} | ||
|
||
using TimerDataHandleSPtr = std::shared_ptr<IntervalHandle>; | ||
|
||
void intervalCb(uv_timer_t* uvHandle) { (*(TimerDataHandleSPtr*)uvHandle->data)->callback(); } | ||
|
||
void IntervalHandle::cancel() { | ||
if (this->handle == nullptr) return; | ||
|
||
uv_timer_t* uvHandle = (uv_timer_t*)this->handle; | ||
|
||
TimerDataHandleSPtr* intervalHandleInner = (TimerDataHandleSPtr*)uvHandle->data; | ||
delete intervalHandleInner; | ||
|
||
uv_timer_stop(uvHandle); | ||
uvCloseDelete(uvHandle); | ||
|
||
this->handle = nullptr; | ||
} | ||
|
||
bool IntervalHandle::isRunning() { | ||
if (this->handle == nullptr) return false; | ||
|
||
uv_timer_t* uvHandle = (uv_timer_t*)this->handle; | ||
return uv_is_active((uv_handle_t*)uvHandle); | ||
} | ||
|
||
std::shared_ptr<IntervalHandle> uvIntervalStart(uint64_t intervalMs, uint64_t delayMs, IntervalCallback cb, | ||
uv_loop_t* loop) { | ||
if (loop == nullptr) loop = uv_default_loop(); | ||
|
||
uv_timer_t* uvHandle = new uv_timer_t(); | ||
|
||
std::shared_ptr<IntervalHandle> intervalHandle = std::make_shared<IntervalHandle>(uvHandle, std::move(cb)); | ||
|
||
uvHandle->data = new TimerDataHandleSPtr(intervalHandle); | ||
|
||
uv_timer_init(loop, uvHandle); | ||
uv_timer_start(uvHandle, intervalCb, delayMs, intervalMs); | ||
|
||
return intervalHandle; | ||
} | ||
} // namespace uvpp |