Muduo Overall introduction and ECHO Server process analysis

Source: Internet
Author: User
Tags epoll

Muduo is Ractor mode, the whole core is reactor; EventLoop acted as reactor. Here is the simplified class diagram structure for Muduo:

EventLoop is the loop in one thread per loop, and each thread can have only a single eventloop entity, which is responsible for dispatching IO and timer events. It uses EVENTFD to wake up asynchronously, different from the traditional use of a pair of pipe. It uses timerqueue as the timing management, Poller realizes IO multiplexing.

Poller is an abstract base class that implements the encapsulation of Pool/epoll. The derived class Pollpoller implements the encapsulation of the poll, and the derived class Epollpoller implements the Epoll encapsulation.

The channel is the selectable IO Channel, which is responsible for registering and responding to IO events. But it has no file descriptor, it is a member of Tcpconnection, Acceptor, tcpconnection, and timequeue whose life cycle is controlled by the latter.

The socket is a RAII handle that encapsulates a file descriptor and turns off FD on destruction. It is a member of Acceptor and Tcpconnection, whose life cycle is controlled by the latter. FD is owned in EventLoop, Timequeue, but not encapsulated as Scokets class.

The connector is used to initiate a TCP connection, which is a member of the TcpClient and the life cycle is controlled by the latter.

The acceptor is used to receive a TCP connection, which is a member of the TCPServer, and the life cycle is controlled by the latter.

The Muduo network library header file relationship is as follows:

Where the white bottom is visible to the user, the gray bottom is not visible to the user.

Edian.h encapsulates the conversion function between the computer byte order/network byte order, placed in the namespace sockets.

Socketsops. {h, CC} encapsulates the creation, connection, binding, listening, closing functions of the socket FD, and also encapsulates the conversion between network addresses, such as "1.2.3.4"/sockaddr_in.sin_addr.s_addr,scokaddr/sockaddr_ Transitions between.

InetAddress. {h, CC} encapsulates the InetAddress class with only one private variable struct sockaddr_in addr_, which encapsulates some of the operations of the network address. Resolve implements the domain name/hostname resolution IP.

Socket. {h, CC} encapsulates FD, which implements some of the common operations of FD. It uses the RAII technique, which is at the time of the destructor close (FD).

Let's take an echo server as an example to explain the approximate flow of MUDUO implementation reactor:
Echo Source for Muduo, in order to facilitate understanding, the package of Echoserver apart.

#include <Muduo/base/logging.H>#include <Muduo/net/tcpserver.H>#include <Muduo/net/eventloop.H>#include <Boost/bind.Hpp>//define callback functionvoidOnConnection (const Muduo:: Net:: Tcpconnectionptr&conn) {Log_info<< "Echoserver-" <<Conn -Peeraddress ().Toipport ()<< " ,"           <<Conn -LocalAddress ().Toipport ()<< "is"           <<(Conn -Connected ()? "Up":"Down");}voidOnMessage (const Muduo:: Net:: Tcpconnectionptr&Conn, Muduo:: Net:: Buffer*BUF, Muduo:: TimestampTime) {Muduo:: StringMSG (BUF -Retrieveallasstring ()); Log_info<<Conn -Name ()<< "echo" <<Msg.Size ()<< "bytes,"           << "Data received at" <<Time.ToString (); Conn -Send (msg);} int main () {Log_info<< "pid =" <<Getpid (); Muduo:: Net:: EventLoop Loop; Muduo:: Net:: InetAddressLISTENADDR ( -); Muduo:: Net:: TCPServerServer&Loop, LISTENADDR,"Echoserver"); Server.Setconnectioncallback (boost:: Bind(OnConnection, _1)); Server.Setmessagecallback (boost:: Bind(OnMessage, _1, _2, _3)); Server.Start ();Loop.Loop();return 0;}

First, two callback functions are defined, both of which have a certain format (incoming parameters and return value types), and the specific format defines the Callbacks.h. The OnConnection is called when the connection arrives, and OnMessage is called when the message arrives, (specifically how the call is explained later).

The EventLoop object and the TCPServer object are first set in the main function. When defining tcpserver, not only the port is bound, but also the pointer to the EventLoop object, a pointer to the EventLoop object is held in TCPServer, and the EventLoop pointer can only be specified at TCPServer initialization and cannot be changed after. TCPServer also binds the callback function that is called when a new connection arrives in the constructor to handle the FD returned by the accept (the binding is actually a function of acceptor binding tcpserver).

The TCPServer Object Server then sets the callback function, which is called when the connection is established and when the message arrives.

When you call Server.start (), the Threadpool_ in the server object is initialized (used for the new connection behind it), and the Listen function is then placed in the Loop Function object container (if the same thread is executed immediately, Otherwise, execute listen after the next IO event is executed). Listen encapsulated in the Acceptor class, Acceptor was created when TCPServer was created, also contained EventLoop object pointers, and the Channel class. When the Acceptor object is initialized, the channel class object is created and added to the EventLoop listener event collection, and when a connection arrives, the callback function is void Acceptor::handleread, in the callback function, Accept the new connection and call Newconnectioncallback_ (that is, tcpserver::newconnection), receive the new connection (save as tcpconnection) and set the new connection callback function , add a new connection to another eventloop, and process the receive and send in this eventloop.

The above is probably the echo server process.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Muduo Overall introduction and ECHO Server process analysis

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.