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.