timer 初步封装
This commit is contained in:
parent
59ee4ce783
commit
a229d2ba31
18
includes/net/timer.hpp
Normal file
18
includes/net/timer.hpp
Normal 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
26
src/net/timer.cc
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user