Build Muduo-6 from Epoll to join EventLoop and Epoll

Source: Internet
Author: User
Tags epoll

Mini-muduo version Transfer gate
version 0.00  from Epoll build muduo-1 mini-muduo Introduction
Version 0.01  build Epoll from Muduo-2 The simplest epoll
version 0.02  build muduo-3 from Epoll to join the first class, by the way reactor
version 0.03  build Epoll from muduo-4 join channel
Version 0.04  build muduo-5 from Epoll add acceptor and tcpconnection
version 0.05  build Epoll from muduo-6 Join EventLoop and Epoll
version 0.06  build from Epoll muduo-7 join Imuduouser
Version 0.07  build Epoll from Muduo-8 Join send buffer and receive buffer
version 0.08  build muduo-9 from Epoll add onwritecomplate callback and buffer
version 0.09  Build MUDUO-10 Timer Timer
version 0.11  from the Epoll epoll build muduo-11 single-threaded reactor network model shaping
version 0.12  From Epoll build muduo-12 multithreaded Code admission
version 0.13  from Epoll build Muduo-13 reactor + ThreadPool Molding

Mini-muduo v0.05 version, complete and operational examples can be downloaded from the GitHub, using the command git checkout v0.05 can switch to this version, online Browsing this version to here This edition will include the main classes of the program, this version can be used as a milestone version, The most important modification was the addition of two classes of EventLoop and Epoll. By joining these two classes, the code logic for the program is much clearer. For a more intuitive understanding, we compare the simplest epoll examples we've described earlier (build muduo-2 the simplest epoll from Epoll) and see where the key code in the original example is running.


1 first to see EventLoop, according to the name can guess the role of this class is the event loop, in fact, this class is used to wrap the for loop, that is, the set in the epoll_wait outside of the for loop, this for loop can be said to be the core part of the entire program, The For loop waits on the epoll_wait, then iterates through each event returned, notifies the channel first, and then notifies the channel to the final event handler (in Acceptor and tcpconnection). In the previous version. The For loop is in TCPServer, and now we move it into the EventLoop loop method, with a while instead of for, acting like the previous for loop: Wait on epoll_wait, and when there is an event, callback channel. Of course, Eventloop::loop () does not call epoll_wait directly, but instead uses its wrapper class Epoll.

The function of the 2 Epoll class is to wrap the Epoll file descriptor, its most important member variable is a epoll file descriptor, and the most important two methods are poll and update. The poll method wraps the epoll_wait, waits for the occurrence of the event on the Epoll descriptor, fills the new channel into the vector after an event occurs, and the Update method wraps the Epoll_ctl to add/modify on the Epoll file descriptor/ Deletes an event. Update receives a Channel as an argument through which you can obtain the event to register (the Channel::getevents () method). All subsequent operations involving the Epoll descriptor are accomplished through the epoll of the two methods. EventLoop was supposed to include a Epoll descriptor, the loop method invokes epoll_wait through a loop, and now the Epoll descriptor is in Epoll, so eventloop only needs to include a EPOLL member variable. EventLoop only need to call Epoll in the loop::p Oll () method to get the channel list, no need to call epoll_wait directly.

3 Epoll and EventLoop should be one by one of the corresponding relationship, each eventloop has and only one epoll. In the original Muduo, in order to take into account Epoll/poll programming, the author for Io Multiplexing wrote the public parent class Poller, here I have simplified, directly implemented a Epoll packaging class Epoll and suddenly rate poll.

4 TCPServer also made the corresponding modification, in the Start method without the creation of the epoll process, there is no for loop. The Epoll creation process is moved into the Epoll constructor. The entire for loop is moved into the EventLoop, and the actual start loop is moved from the TCPServer to the main function. Another modification of tcpserver is to replace the member variable _EPOLLFD with EventLoop, because EventLoop contains a epoll, which is used to wrap the Epoll file descriptor, so this modification is easy to understand.

5 before passing EPOLLFD Place, change to eventloop*, because EventLoop and Epoll is one by one corresponding relationship, and Epoll is EPOLLFD packaging, get EventLoop is equal to get epoll descriptor.

6 channel added Methods GetEvents () and GETSOCKFD () to enable Epoll to obtain the necessary events and FD through channel.

7 added a memory leak new Epoll post-processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.