Skip to content

Commit

Permalink
uvpp: added uvIntervalStart
Browse files Browse the repository at this point in the history
  • Loading branch information
KrystianD committed Jan 24, 2022
1 parent f10e05f commit 6818409
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/uvpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(uvpp STATIC
${CMAKE_CURRENT_LIST_DIR}/src/uvpp_async.cpp
${CMAKE_CURRENT_LIST_DIR}/src/uvpp_timer.cpp
${CMAKE_CURRENT_LIST_DIR}/src/uvpp_interval.cpp
${CMAKE_CURRENT_LIST_DIR}/src/uvpp_close.cpp
${CMAKE_CURRENT_LIST_DIR}/src/uvpp_stream.cpp
${CMAKE_CURRENT_LIST_DIR}/src/uvpp_tcp.cpp
Expand Down
1 change: 1 addition & 0 deletions modules/uvpp/include/uvpp/uvpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "uvpp_stream.h"
#include "uvpp_tcp.h"
#include "uvpp_timer.h"
#include "uvpp_interval.h"
#include "uvpp_dns.h"
#include "uvpp_async.h"
#include "uvpp_channel.h"
Expand Down
33 changes: 33 additions & 0 deletions modules/uvpp/include/uvpp/uvpp_interval.h
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
48 changes: 48 additions & 0 deletions modules/uvpp/src/uvpp_interval.cpp
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

0 comments on commit 6818409

Please sign in to comment.