├── .gitignore ├── .gitmodules ├── README ├── bazel ├── .gitignore ├── BUILD.bazel ├── WORKSPACE └── echo.cc ├── cmake-submodule └── CMakeLists.txt ├── cmake └── CMakeLists.txt ├── makefile └── Makefile └── src └── echo.cc /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "cmake-submodule/muduo"] 2 | path = cmake-submodule/muduo 3 | url = https://github.com/chenshuo/muduo.git 4 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Examples of Muduo network library 2 | 3 | 0) src - source file 4 | 1) bazel - build with Bazel 5 | 2) cmake-submodule - build with CMake, checking out muduo as a git submodule 6 | 3) cmake - build with CMake 7 | 2) makefile - build with Makefile 8 | 9 | 1) build with bazel 10 | cd bazel 11 | bazel build -c opt :all 12 | 13 | 2) build with CMake with git submodule 14 | git submodule update --init 15 | mkdir build 16 | cd build 17 | cmake ../cmake-submodule 18 | make 19 | 20 | Assuming Muduo is installed in $HOME/build/debug-install 21 | 22 | 3) build with CMake 23 | mkdir build 24 | cd build 25 | cmake ../cmake 26 | make 27 | # echo binary is in ./bin/ 28 | 29 | 4) build with GNU make 30 | cd makefile 31 | make 32 | # echo binary is in ./ 33 | -------------------------------------------------------------------------------- /bazel/.gitignore: -------------------------------------------------------------------------------- 1 | bazel-* 2 | -------------------------------------------------------------------------------- /bazel/BUILD.bazel: -------------------------------------------------------------------------------- 1 | cc_binary( 2 | name = "echo", 3 | srcs = ["echo.cc"], 4 | deps = [ 5 | "@muduo//muduo/net", 6 | ], 7 | ) 8 | -------------------------------------------------------------------------------- /bazel/WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "com_chenshuo_muduo_tutorial") 2 | 3 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 4 | 5 | # muduo 6 | git_repository( 7 | name = "muduo", 8 | commit = "0edc4c9ddfaeecbede71229673eefc48bcb6fe8c", 9 | remote = "https://github.com/chenshuo/muduo", 10 | shallow_since = "1567382400 -0700", #unix timestamp 11 | ) 12 | -------------------------------------------------------------------------------- /bazel/echo.cc: -------------------------------------------------------------------------------- 1 | ../src/echo.cc -------------------------------------------------------------------------------- /cmake-submodule/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | 3 | project(echo CXX) 4 | 5 | if(NOT CMAKE_BUILD_TYPE) 6 | set(CMAKE_BUILD_TYPE "Release") 7 | endif() 8 | 9 | add_subdirectory(muduo) 10 | include_directories("muduo") 11 | 12 | add_executable(echo ../src/echo.cc) 13 | target_link_libraries(echo muduo_net) 14 | target_link_libraries(echo muduo_base) 15 | target_link_libraries(echo pthread rt) 16 | 17 | -------------------------------------------------------------------------------- /cmake/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | project(echo CXX) 4 | 5 | if(NOT CMAKE_BUILD_TYPE) 6 | set(CMAKE_BUILD_TYPE "debug") 7 | endif() 8 | 9 | if(NOT MUDUO_PATH) 10 | set(MUDUO_PATH "/home/$ENV{USER}/build/${CMAKE_BUILD_TYPE}-install") 11 | endif() 12 | 13 | set(CXX_FLAGS 14 | -g 15 | # -DVALGRIND 16 | # -DMUDUO_STD_STRING 17 | -Wall 18 | -Wextra 19 | # -m32 20 | -Werror 21 | -Wconversion 22 | -Wno-unused-parameter 23 | -Wold-style-cast 24 | -Woverloaded-virtual 25 | -Wpointer-arith 26 | -Wshadow 27 | -Wwrite-strings 28 | -march=native 29 | # -MMD 30 | # -std=c++0x 31 | -rdynamic 32 | ) 33 | string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}") 34 | 35 | set(CMAKE_CXX_COMPILER "g++") 36 | set(CMAKE_CXX_FLAGS_DEBUG "-O0") 37 | set(CMAKE_CXX_FLAGS_RELEASE "-O2 -finline-limit=1000 -DNDEBUG") 38 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) 39 | set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) 40 | 41 | find_path(Muduo_INCLUDE_DIR muduo "${MUDUO_PATH}/include") 42 | find_path(Muduo_LIBRARY_DIR libmuduo_net.a "${MUDUO_PATH}/lib") 43 | set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${Muduo_LIBRARY_DIR}) 44 | message(STATUS ${Muduo_INCLUDE_DIR}) 45 | message(STATUS ${Muduo_LIBRARY_DIR}) 46 | 47 | include_directories(${Muduo_INCLUDE_DIR}) 48 | find_library(muduo_base muduo_base) 49 | find_library(muduo_net muduo_net) 50 | message(STATUS ${muduo_base}) 51 | message(STATUS ${muduo_net}) 52 | 53 | find_package(Boost REQUIRED) 54 | include_directories(${Boost_INCLUDE_DIRS}) 55 | 56 | include_directories(${PROJECT_SOURCE_DIR}) 57 | 58 | add_executable(echo ../src/echo.cc) 59 | target_link_libraries(echo ${muduo_net}) 60 | target_link_libraries(echo ${muduo_base}) 61 | target_link_libraries(echo pthread rt) 62 | 63 | 64 | -------------------------------------------------------------------------------- /makefile/Makefile: -------------------------------------------------------------------------------- 1 | MUDUO_DIRECTORY ?= $(HOME)/build/debug-install 2 | #MUDUO_DIRECTORY ?= $(HOME)/build/install 3 | MUDUO_INCLUDE = $(MUDUO_DIRECTORY)/include 4 | MUDUO_LIBRARY = $(MUDUO_DIRECTORY)/lib 5 | SRC = ../src 6 | 7 | CXXFLAGS = -g -O0 -Wall -Wextra -Werror \ 8 | -Wconversion -Wno-unused-parameter \ 9 | -Wold-style-cast -Woverloaded-virtual \ 10 | -Wpointer-arith -Wshadow -Wwrite-strings \ 11 | -march=native -rdynamic \ 12 | -I$(MUDUO_INCLUDE) 13 | 14 | LDFLAGS = -L$(MUDUO_LIBRARY) -lmuduo_net -lmuduo_base -lpthread -lrt 15 | 16 | all: echo 17 | clean: 18 | rm -f echo core 19 | 20 | echo: $(SRC)/echo.cc 21 | g++ $(CXXFLAGS) -o $@ $^ $(LDFLAGS) 22 | 23 | .PHONY: all clean 24 | -------------------------------------------------------------------------------- /src/echo.cc: -------------------------------------------------------------------------------- 1 | #include "muduo/net/TcpServer.h" 2 | 3 | #include "muduo/base/AsyncLogging.h" 4 | #include "muduo/base/Logging.h" 5 | #include "muduo/base/Thread.h" 6 | #include "muduo/net/EventLoop.h" 7 | #include "muduo/net/InetAddress.h" 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | using namespace muduo; 16 | using namespace muduo::net; 17 | 18 | class EchoServer 19 | { 20 | public: 21 | EchoServer(EventLoop* loop, const InetAddress& listenAddr) 22 | : loop_(loop), 23 | server_(loop, listenAddr, "EchoServer") 24 | { 25 | server_.setConnectionCallback( 26 | std::bind(&EchoServer::onConnection, this, _1)); 27 | server_.setMessageCallback( 28 | std::bind(&EchoServer::onMessage, this, _1, _2, _3)); 29 | } 30 | 31 | void start() 32 | { 33 | server_.start(); 34 | } 35 | 36 | private: 37 | void onConnection(const TcpConnectionPtr& conn); 38 | 39 | void onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time); 40 | 41 | EventLoop* loop_; 42 | TcpServer server_; 43 | }; 44 | 45 | void EchoServer::onConnection(const TcpConnectionPtr& conn) 46 | { 47 | LOG_TRACE << conn->peerAddress().toIpPort() << " -> " 48 | << conn->localAddress().toIpPort() << " is " 49 | << (conn->connected() ? "UP" : "DOWN"); 50 | } 51 | 52 | void EchoServer::onMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp time) 53 | { 54 | string msg(buf->retrieveAllAsString()); 55 | LOG_TRACE << conn->name() << " recv " << msg.size() << " bytes at " << time.toString(); 56 | conn->send(msg); 57 | } 58 | 59 | int kRollSize = 500*1000*1000; 60 | 61 | std::unique_ptr g_asyncLog; 62 | 63 | void asyncOutput(const char* msg, int len) 64 | { 65 | g_asyncLog->append(msg, len); 66 | } 67 | 68 | void setLogging(const char* argv0) 69 | { 70 | muduo::Logger::setOutput(asyncOutput); 71 | char name[256]; 72 | strncpy(name, argv0, 256); 73 | g_asyncLog.reset(new muduo::AsyncLogging(::basename(name), kRollSize)); 74 | g_asyncLog->start(); 75 | } 76 | 77 | int main(int argc, char* argv[]) 78 | { 79 | setLogging(argv[0]); 80 | 81 | LOG_INFO << "pid = " << getpid() << ", tid = " << CurrentThread::tid(); 82 | EventLoop loop; 83 | InetAddress listenAddr(2007); 84 | EchoServer server(&loop, listenAddr); 85 | 86 | server.start(); 87 | 88 | loop.loop(); 89 | } 90 | 91 | --------------------------------------------------------------------------------