1e254a845SShuo Chen#include "Channel.h" 2e254a845SShuo Chen#include "EventLoop.h" 3e254a845SShuo Chen 4e254a845SShuo Chen#include <stdio.h> 5e254a845SShuo Chen#include <sys/timerfd.h> 6e254a845SShuo Chen 7e254a845SShuo Chenmuduo::EventLoop* g_loop; 8e254a845SShuo Chen 9e254a845SShuo Chenvoid timeout(muduo::Timestamp receiveTime) 10e254a845SShuo Chen{ 11e254a845SShuo Chen printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 12e254a845SShuo Chen g_loop->quit(); 13e254a845SShuo Chen} 14e254a845SShuo Chen 15e254a845SShuo Chenint main() 16e254a845SShuo Chen{ 17e254a845SShuo Chen printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 18e254a845SShuo Chen muduo::EventLoop loop; 19e254a845SShuo Chen g_loop = &loop; 20e254a845SShuo Chen 21e254a845SShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 22e254a845SShuo Chen muduo::Channel channel(&loop, timerfd); 23e254a845SShuo Chen channel.setReadCallback(timeout); 24e254a845SShuo Chen channel.enableReading(); 25e254a845SShuo Chen 26e254a845SShuo Chen struct itimerspec howlong; 27e254a845SShuo Chen bzero(&howlong, sizeof howlong); 28e254a845SShuo Chen howlong.it_value.tv_sec = 5; 29e254a845SShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 30e254a845SShuo Chen 31e254a845SShuo Chen loop.loop(); 32e254a845SShuo Chen 33e254a845SShuo Chen ::close(timerfd); 34e254a845SShuo Chen} 35