Muduo network programming example 0: Preface

Source: Internet
Author: User

Muduo Introduction
One of the purposes of writing a Muduo network library is to simplify the daily TCP network programming, so that programmers can focus on the implementation of business logic, rather than competing with the Sockets API every day. I want Muduo to reduce the occasional complexity (accidental complexity) in network programming, in the words of Brooks ).

Muduo only supports concurrent and non-blocking TCP network programming in Linux 2.6.x.

Muduo is easy to use. You do not need to derive from the specified class or overwrite the virtual function. You only need to register several callback functions to process the three and a half events mentioned above.

Take the classic echo service as an example:

1. Define the EchoServer class and do not need to be derived from any base class:

View plaincopy to clipboardprint?
# Ifndef MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H
# Define MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H
# Include
// RFC 862
Class EchoServer
{
Public:
EchoServer (muduo: net: EventLoop * loop,
Const muduo: net: InetAddress & listenAddr );
Void start ();
Private:
Void onConnection (const muduo: net: TcpConnectionPtr & conn );
Void onMessage (const muduo: net: TcpConnectionPtr & conn,
Muduo: net: Buffer * buf,
Muduo: Timestamp time );
Muduo: net: EventLoop * loop _;
Muduo: net: TcpServer server _;
};
# Endif // MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H
# Ifndef MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H
# Define MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H
# Include
// RFC 862
Class EchoServer
{
Public:
EchoServer (muduo: net: EventLoop * loop,
Const muduo: net: InetAddress & listenAddr );
Void start ();
Private:
Void onConnection (const muduo: net: TcpConnectionPtr & conn );
Void onMessage (const muduo: net: TcpConnectionPtr & conn,
Muduo: net: Buffer * buf,
Muduo: Timestamp time );
Muduo: net: EventLoop * loop _;
Muduo: net: TcpServer server _;
};
# Endif // MUDUO_EXAMPLES_SIMPLE_ECHO_ECHO_H

Register the callback function in the constructor:

View plaincopy to clipboardprint?
EchoServer: EchoServer (EventLoop * loop,
Const InetAddress & listenAddr)
: Loop _ (loop ),
Server _ (loop, listenAddr, "EchoServer ")
{
Server _. setConnectionCallback (
Boost: bind (& EchoServer: onConnection, this, _ 1 ));
Server _. setMessageCallback (
Boost: bind (& EchoServer: onMessage, this, _ 1, _ 2, _ 3 ));
}
Void EchoServer: start ()
{
Server _. start ();
}
EchoServer: EchoServer (EventLoop * loop,
Const InetAddress & listenAddr)
: Loop _ (loop ),
Server _ (loop, listenAddr, "EchoServer ")
{
Server _. setConnectionCallback (
Boost: bind (& EchoServer: onConnection, this, _ 1 ));
Server _. setMessageCallback (
Boost: bind (& EchoServer: onMessage, this, _ 1, _ 2, _ 3 ));
}
Void EchoServer: start ()
{
Server _. start ();
}

2. Implement EchoServer: onConnection () and EchoServer: onMessage ():

View plaincopy to clipboardprint?
Void EchoServer: onConnection (const TcpConnectionPtr & conn)
{
LOG_INFO <"EchoServer-" <conn-> peerAddress (). toHostPort () <"->"
<Conn-> localAddress (). toHostPort () <"is"
<(Conn-> connected ()? "UP": "DOWN ");
}
Void EchoServer: onMessage (const TcpConnectionPtr & conn,
Buffer * buf,
Timestamp time)
{
String msg (buf-> retrieveAsString ());
LOG_INFO <conn-> name () <"echo" <msg. size () <"bytes at" <time. toString ();
Conn-> send (msg );
}
Void EchoServer: onConnection (const TcpConnectionPtr & conn)
{
LOG_INFO <"EchoServer-" <conn-> peerAddress (). toHostPort () <"->"
<Conn-> localAddress (). toHostPort () <"is"
<(Conn-> connected ()? "UP": "DOWN ");
}
Void EchoServer: onMessage (const TcpConnectionPtr & conn,
Buffer * buf,
Timestamp time)
{
String msg (buf-> retrieveAsString ());
LOG_INFO <conn-> name () <"echo" <msg. size () <"bytes at" <time. toString ();
Conn-> send (msg );
}

3. Use EventLoop in main () to run the entire program:

View plaincopy to clipboardprint?
# Include "echo. h"
Using namespace muduo;
Using namespace muduo: net;
Int main ()
{
LOG_INFO <"pid =" <getpid ();
EventLoop loop;
InetAddress listenAddr (2007 );
EchoServer server (& loop, listenAddr );
Server. start ();
Loop. loop ();
}
# Include "echo. h"
Using namespace muduo;
Using namespace muduo: net;
Int main ()
{
LOG_INFO <"pid =" <getpid ();
EventLoop loop;
InetAddress listenAddr (2007 );
EchoServer server (& loop, listenAddr );
Server. start ();
Loop. loop ();
}

For the complete code, see muduo/examples/simple/echo.

 

Related Article

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.