12a18e699SShuo Chen#include "Channel.h" 22a18e699SShuo Chen#include "EventLoop.h" 32a18e699SShuo Chen 42a18e699SShuo Chen#include <stdio.h> 52a18e699SShuo Chen#include <sys/timerfd.h> 62a18e699SShuo Chen 72a18e699SShuo Chenmuduo::EventLoop* g_loop; 82a18e699SShuo Chen 92a18e699SShuo Chenvoid timeout(muduo::Timestamp receiveTime) 102a18e699SShuo Chen{ 112a18e699SShuo Chen printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 122a18e699SShuo Chen g_loop->quit(); 132a18e699SShuo Chen} 142a18e699SShuo Chen 152a18e699SShuo Chenint main() 162a18e699SShuo Chen{ 172a18e699SShuo Chen printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 182a18e699SShuo Chen muduo::EventLoop loop; 192a18e699SShuo Chen g_loop = &loop; 202a18e699SShuo Chen 212a18e699SShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 222a18e699SShuo Chen muduo::Channel channel(&loop, timerfd); 232a18e699SShuo Chen channel.setReadCallback(timeout); 242a18e699SShuo Chen channel.enableReading(); 252a18e699SShuo Chen 262a18e699SShuo Chen struct itimerspec howlong; 272a18e699SShuo Chen bzero(&howlong, sizeof howlong); 282a18e699SShuo Chen howlong.it_value.tv_sec = 5; 292a18e699SShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 302a18e699SShuo Chen 312a18e699SShuo Chen loop.loop(); 322a18e699SShuo Chen 332a18e699SShuo Chen ::close(timerfd); 342a18e699SShuo Chen} 35