165c497a3SShuo Chen#include "Channel.h" 265c497a3SShuo Chen#include "EventLoop.h" 365c497a3SShuo Chen 465c497a3SShuo Chen#include <stdio.h> 565c497a3SShuo Chen#include <sys/timerfd.h> 665c497a3SShuo Chen 765c497a3SShuo Chenmuduo::EventLoop* g_loop; 865c497a3SShuo Chen 9714cd85fSShuo Chenvoid timeout(muduo::Timestamp receiveTime) 1065c497a3SShuo Chen{ 11714cd85fSShuo Chen printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 1265c497a3SShuo Chen g_loop->quit(); 1365c497a3SShuo Chen} 1465c497a3SShuo Chen 1565c497a3SShuo Chenint main() 1665c497a3SShuo Chen{ 17714cd85fSShuo Chen printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 1865c497a3SShuo Chen muduo::EventLoop loop; 1965c497a3SShuo Chen g_loop = &loop; 2065c497a3SShuo Chen 2165c497a3SShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 2265c497a3SShuo Chen muduo::Channel channel(&loop, timerfd); 2365c497a3SShuo Chen channel.setReadCallback(timeout); 2465c497a3SShuo Chen channel.enableReading(); 2565c497a3SShuo Chen 2665c497a3SShuo Chen struct itimerspec howlong; 2765c497a3SShuo Chen bzero(&howlong, sizeof howlong); 2865c497a3SShuo Chen howlong.it_value.tv_sec = 5; 2965c497a3SShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 3065c497a3SShuo Chen 3165c497a3SShuo Chen loop.loop(); 3265c497a3SShuo Chen 3365c497a3SShuo Chen ::close(timerfd); 3465c497a3SShuo Chen} 35