test3.cc revision 354280cf
1354280cfSShuo Chen#include "Channel.h" 2354280cfSShuo Chen#include "EventLoop.h" 3354280cfSShuo Chen 4354280cfSShuo Chen#include <stdio.h> 5354280cfSShuo Chen#include <sys/timerfd.h> 6354280cfSShuo Chen 7354280cfSShuo Chenmuduo::EventLoop* g_loop; 8354280cfSShuo Chen 9354280cfSShuo Chenvoid timeout(muduo::Timestamp receiveTime) 10354280cfSShuo Chen{ 11354280cfSShuo Chen printf("%s Timeout!\n", receiveTime.toFormattedString().c_str()); 12354280cfSShuo Chen g_loop->quit(); 13354280cfSShuo Chen} 14354280cfSShuo Chen 15354280cfSShuo Chenint main() 16354280cfSShuo Chen{ 17354280cfSShuo Chen printf("%s started\n", muduo::Timestamp::now().toFormattedString().c_str()); 18354280cfSShuo Chen muduo::EventLoop loop; 19354280cfSShuo Chen g_loop = &loop; 20354280cfSShuo Chen 21354280cfSShuo Chen int timerfd = ::timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); 22354280cfSShuo Chen muduo::Channel channel(&loop, timerfd); 23354280cfSShuo Chen channel.setReadCallback(timeout); 24354280cfSShuo Chen channel.enableReading(); 25354280cfSShuo Chen 26354280cfSShuo Chen struct itimerspec howlong; 27354280cfSShuo Chen bzero(&howlong, sizeof howlong); 28354280cfSShuo Chen howlong.it_value.tv_sec = 5; 29354280cfSShuo Chen ::timerfd_settime(timerfd, 0, &howlong, NULL); 30354280cfSShuo Chen 31354280cfSShuo Chen loop.loop(); 32354280cfSShuo Chen 33354280cfSShuo Chen ::close(timerfd); 34354280cfSShuo Chen} 35