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