#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);
        }
    }
}