timer 初步封装

This commit is contained in:
yhy 2024-12-25 19:38:36 +08:00
parent 59ee4ce783
commit a229d2ba31
2 changed files with 44 additions and 0 deletions

18
includes/net/timer.hpp Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include "fd_event.hpp"
#include "reactor.hpp"
namespace tinyrpc {
class Timer : FdEvent {
public:
Timer(Reactor::Task cb = FdEvent::m_default_callback);
~Timer();
private:
// TODO .... 完善 Timer 类
};
}

26
src/net/timer.cc Normal file
View File

@ -0,0 +1,26 @@
#include "timer.hpp"
#include "fd_event.hpp"
#include "logger.hpp"
#include <cstring>
#include <sys/timerfd.h>
#include <unistd.h>
namespace tinyrpc {
Timer::Timer(Reactor::Task cb /* = FdEvent::m_default_callback */) {
m_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if(m_fd == -1) {
logger() << "timerfd_create ret -1 err:" << strerror(errno);
exit(-1);
}
addListenEvent(IOEvent::READ);
setReadCallback(cb);
}
Timer::~Timer() {
int ret = close(m_fd);
if(ret == -1) {
logger() << "close ret -1 err:" << strerror(errno);
// exit(-1);
}
}
}