环境变量里加export MUDUO_LOG_TRACE=1使其打印出最多的日志
看这个库需要boost基础,这个专栏地址参考价值比较大
EchoServer_unittest.cc代码如下
EventLoop loop;
InetAddress listenAddr(2000, false, ipv6);
EchoServer server(&loop, listenAddr);
server.start();
loop.loop();
1、EventLoop构造如下
EventLoop::EventLoop()
: looping_(false),
quit_(false),
eventHandling_(false),
callingPendingFunctors_(false),
iteration_(0),
threadId_(CurrentThread::tid()),
poller_(Poller::newDefaultPoller(this)),
timerQueue_(new TimerQueue(this)),
wakeupFd_(createEventfd()),
wakeupChannel_(new Channel(this, wakeupFd_)),
currentActiveChannel_(NULL)
{
LOG_DEBUG << "EventLoop created " << this << " in thread " << threadId_;
LOG_TRACE << "wakeupFd_ = " << wakeupFd_ << " in thread " << threadId_;
以上这里poller_(Poller::newDefaultPoller(this)),new了个epoll对象代码如下
EPollPoller::EPollPoller(EventLoop* loop)
: Poller(loop),
epollfd_(::epoll_create1(EPOLL_CLOEXEC)),
events_(kInitEventListSize)
使用新API接口:epoll_create1创建了个epollfd_
接着在epollfd_下挂了关注三个fd
timerQueue_(new TimerQueue(this)),这里创建了个timerfd,并弄成了Channel
wakeupFd_(createEventfd()),
wakeupChannel_(new Channel(this, wakeupFd_)),
wakeupFd_ notify里用的 唤醒 EventLoop 的 是baseloop?待后面研究
EchoServer server(&loop, listenAddr);这句构造函数如下
TcpServer::TcpServer(EventLoop* loop,
const InetAddress& listenAddr,
const string& nameArg,
Option option)
: loop_(CHECK_NOTNULL(loop)),
ipPort_(listenAddr.toIpPort()),
name_(nameArg),
acceptor_(new Acceptor(loop, listenAddr, option == kReusePort)),
threadPool_(new EventLoopThreadPool(loop, name_)),
connectionCallback_(defaultConnectionCallback),
messageCallback_(defaultMessageCallback),
nextConnId_(1)
创建了Acceptor 和 EventLoopThreadPool
Acceptor::Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport)
: loop_(loop),
acceptSocket_(sockets::createNonblockingOrDie(listenAddr.family())),
acceptChannel_(loop, acceptSocket_.fd()),
listenning_(false),
idleFd_(::open("/dev/null", O_RDONLY | O_CLOEXEC))
{
assert(idleFd_ >= 0);
LOG_TRACE << "acceptSocket_.fd = " << acceptSocket_.fd();
Acceptor这里就创建了acceptSocket_.fd
loop_是base pool threadPool_是根据setThreadNum设置才有的
接着 server.start();代码如下启动监听
void TcpServer::start()
{
if (started_.getAndSet(1) == 0)
{
threadPool_->start(threadInitCallback_);
assert(!acceptor_->listenning());
loop_->runInLoop(
boost::bind(&Acceptor::listen, get_pointer(acceptor_)));
}
}
Acceptor::listen里有acceptChannel_.enableReading();这句 ,类似Channel的enable系列里面都有 update 就最终在EPollPoller.cc文件里
TRACE updateChannel fd = 6 events = 3 index = -1 - EPollPoller.cc:113
20170830 12:39:39.678981Z 5875 TRACE update epoll_ctl op = ADD fd = 6 event = { 6: IN PRI } - EPollPoller.cc:181
实现效果了
接着 void EventLoop::loop()就 pollReturnTime_ = poller_->poll(kPollTimeMs, &activeChannels_); poll在这个地方了
日志如下
[root@localhost bin]# ./echoserver_unittest
20170830 12:39:39.675537Z 5875 INFO pid = 5875, tid = 5875 - EchoServer_unittest.cc:73
20170830 12:39:39.675809Z 5875 INFO sizeof TcpConnection = 236 - EchoServer_unittest.cc:74
20170830 12:39:39.675863Z 5875 TRACE EPollPoller epollfd_ = 3 - EPollPoller.cc:45
20170830 12:39:39.675891Z 5875 TRACE Channel fd_ = 4 - Channel.cc:35
20170830 12:39:39.675904Z 5875 TRACE Channel loop_ = 0xBFD1F1E0 - Channel.cc:36
20170830 12:39:39.675910Z 5875 TRACE TimerQueue timerfd_ = 4 - TimerQueue.cc:102
20170830 12:39:39.675918Z 5875 TRACE updateChannel fd = 4 events = 3 index = -1 - EPollPoller.cc:113
20170830 12:39:39.676131Z 5875 TRACE update epoll_ctl op = ADD fd = 4 event = { 4: IN PRI } - EPollPoller.cc:181
20170830 12:39:39.676167Z 5875 TRACE Channel fd_ = 5 - Channel.cc:35
20170830 12:39:39.676174Z 5875 TRACE Channel loop_ = 0xBFD1F1E0 - Channel.cc:36
20170830 12:39:39.676181Z 5875 DEBUG EventLoop EventLoop created 0xBFD1F1E0 in thread 5875 - EventLoop.cc:76
20170830 12:39:39.676192Z 5875 TRACE EventLoop wakeupFd_ = 5 in thread 5875 - EventLoop.cc:77
20170830 12:39:39.676205Z 5875 TRACE updateChannel fd = 5 events = 3 index = -1 - EPollPoller.cc:113
20170830 12:39:39.676219Z 5875 TRACE update epoll_ctl op = ADD fd = 5 event = { 5: IN PRI } - EPollPoller.cc:181
20170830 12:39:39.676279Z 5875 TRACE Channel fd_ = 6 - Channel.cc:35
20170830 12:39:39.676287Z 5875 TRACE Channel loop_ = 0xBFD1F1E0 - Channel.cc:36
20170830 12:39:39.678890Z 5875 TRACE Acceptor acceptSocket_.fd = 6 - Acceptor.cc:34
20170830 12:39:39.678955Z 5875 TRACE updateChannel fd = 6 events = 3 index = -1 - EPollPoller.cc:113
20170830 12:39:39.678981Z 5875 TRACE update epoll_ctl op = ADD fd = 6 event = { 6: IN PRI } - EPollPoller.cc:181
20170830 12:39:39.678993Z 5875 TRACE loop EventLoop 0xBFD1F1E0 start looping - EventLoop.cc:109
20170830 12:39:39.678999Z 5875 TRACE poll fd total count 3 - EPollPoller.cc:59
20170830 12:39:49.679514Z 5875 TRACE poll nothing happended - EPollPoller.cc:77
20170830 12:39:49.679567Z 5875 TRACE poll fd total count 3 - EPollPoller.cc:59
20170830 12:39:59.694012Z 5875 TRACE poll nothing happended - EPollPoller.cc:77
20170830 12:39:59.694126Z 5875 TRACE poll fd total count 3 - EPollPoller.cc:59
20170830 12:40:09.709135Z 5875 TRACE poll nothing happended - EPollPoller.cc:77
20170830 12:40:09.709186Z 5875 TRACE poll fd total count 3 - EPollPoller.cc:59
20170830 12:40:19.713299Z 5875 TRACE poll nothing happended - EPollPoller.cc:77
20170830 12:40:19.713349Z 5875 TRACE poll fd total count 3 - EPollPoller.cc:59