1.muduo库简介
muduo是由Google大佬陈硕开发,是一个基于非阻塞IO和事件驱动的现代C++网络库,原生支持one loop per thread这种IO模型,该库只支持Linux系统,网上大佬对其褒贬不一,作为小白用来学习就无可厚非了。
git仓库:https://github.com/chenshuo/muduo
2.准备事项
- muduo采用Cmake为build system,安装如下:
$ sudo apt-get install cmake
- muduo依赖于Boost,安装如下:
$ sudo apt-get install libboost-dev libboost-test-dev
- curl、c-ares DNS、Google Protobuf这3个库非必须安装,如果安装了cmake会多编译一些示例,安装如下:
$ sudo apt-get install libcur14-openssl-dev libc-ares-dev
$ sudo apt-get install protobuf-compiler libprotobuf-dev
3.编译
- 下载muduo-master解压后,执行编译脚本
// 切换路径
$ cd muduo-master/
//编译muduo库,默认release版,生成build文件夹
$ ./build.sh -j2
// 将muduo头文件和库文件安装到release-install目录下的include和lib文件夹
$ ./build.sh install
// 将头文件拷贝到系统路径
$ cd build/release-install-cpp11/include/
$ cp -r muduo/ /usr/local/include/
// 将库文件拷贝到系统路径
$ cd build/release-install-cpp11/lib/
$ cp * /usr/local/lib/
4.测试demo
- EchoServer.h
#include <muduo/net/TcpServer.h>
#include <muduo/base/Logging.h>
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_;
};
- EchoServer.cpp
#include "EchoServer.h"
#include <boost/bind/bind.hpp>
using namespace boost::placeholders;
EchoServer::EchoServer(muduo::net::EventLoop* loop,const muduo::net::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::onConnection(const muduo::net::TcpConnectionPtr& conn)
{
LOG_INFO << "EchoServer - " << conn->peerAddress().toIpPort() << " -> "
<< conn->localAddress().toIpPort() << " is "
<< (conn->connected()? "UP" : "DOWN");
}
void EchoServer::onMessage(const muduo::net::TcpConnectionPtr& conn,
muduo::net::Buffer* buf,
muduo::Timestamp time)
{
muduo::string msg(buf->retrieveAllAsString());
LOG_INFO << conn->name() << "echo - " << msg.size() << " bytes, "
<< " data received at " << time.toString();
conn->send(msg);
}
void EchoServer::start()
{
server_.start();
}
- test.cpp
#include "EchoServer.h"
#include <muduo/net/EventLoop.h>
#include <muduo/base/Logging.h>
using namespace muduo;
using namespace muduo::net;
int main()
{
LOG_INFO << "pid = "<< getpid();
muduo::net::EventLoop loop;
muduo::net::InetAddress listenAddr(2007);
EchoServer server(&loop,listenAddr);
server.start();
loop.loop();
}
- 在编译的时候出现好多未定义的错误,最后排查是因为编译的时候链接库的顺序有要求,编译如下:
$ g++ EchoServer.cpp test.cpp -o test -lmuduo_base -lmuduo_net -lpthread
错误消息如下:
5.测试
执行test.out程序启动服务端,再通过终端模拟客户端建立连接,在客户端发送消息会同时接收服务端回复的相同消息。