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