chenshuo / muduo-tutorial Goto Github PK
View Code? Open in Web Editor NEWTutorial of Muduo network library
Tutorial of Muduo network library
Examples of Muduo network library 0) src - source file 1) bazel - build with Bazel 2) cmake-submodule - build with CMake, checking out muduo as a git submodule 3) cmake - build with CMake 2) makefile - build with Makefile 1) build with bazel cd bazel bazel build -c opt :all 2) build with CMake with git submodule git submodule update --init mkdir build cd build cmake ../cmake-submodule make Assuming Muduo is installed in $HOME/build/debug-install 3) build with CMake mkdir build cd build cmake ../cmake make # echo binary is in ./bin/ 4) build with GNU make cd makefile make # echo binary is in ./
CMakeFiles/echo.dir/home/lgh/dbbase/src/echo.cc.o: In function setLogging(char const*)': /home/lgh/dbbase/src/echo.cc:74: undefined reference to
muduo::AsyncLogging::AsyncLogging(__gnu_cxx::__versa_string<char, std::char_traits, std::allocator, __gnu_cxx::__sso_string_base> const&, long, int)'
muduo 给客户端设置 setCloseCallback 后存在连接泄露,当我onConnect的时候
void DeviceServer::MQTTServer::onConnection(const muduo::net::TcpConnectionPtr& conn)
{
// LOG_TRACE << conn->peerAddress().toIpPort() << " -> "
// << conn->localAddress().toIpPort() << " is "
// << (conn->connected() ? "UP" : "DOWN");
conn->setTcpNoDelay(true);
conn->setCloseCallback(std::bind(&MQTTServer::onClose, this, _1));
//绑定一个mqtt处理器
}
这时候会替换掉原来的handle close,这时候客户端主动关闭,服务端 并不会close 导致出现连接泄露,服务端会出现大量的close-wait
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mqtt-prox 108514 zhanglei 6u IPv4 1657576 0t0 TCP *:9500 (LISTEN)
mqtt-prox 108514 zhanglei 11u IPv4 1672517 0t0 TCP localhost:9500->localhost:44958 (CLOSE_WAIT)
mqtt-prox 108514 zhanglei 13u IPv4 1656594 0t0 TCP localhost:9500->localhost:44984 (CLOSE_WAIT)
mqtt-prox 108514 zhanglei 14u IPv4 1656662 0t0 TCP localhost:9500->localhost:44986 (CLOSE_WAIT)
我发现muduo 的Tcpserver中的connections容器在不停的增加,并没有调用removeConnection,由于removeConnection是私有的,所以我无法调用removeConnection来从stl容器中去掉这个连接
Dear Chen,
I downloaded the muduo, muduo-protorpc and muduo-tutorial into /home/ocean/MYGIT/ and compiled muduo and muduo-protorpc successfulley.
Then I modified the muduo-tutorial/cmake/CMakeLists.txt and tried to compile it but failed.
Can you kindly tell me what step was wrong?
------------------------------------------------------------------
My Os is ubuntu 14.04 LTS and basic libs were ready:
$ sudo apt-get install libboost-dev
$ sudo apt-get install libprotoc-dev protobuf-compiler
$ sudo apt-get install libprotobuf-dev cmake
$ git clone https://github.com/chenshuo/muduo.git
$ git clone https://github.com/chenshuo/muduo-protorpc.git
$ cd muduo && git checkout cpp11 && ./build.sh install
$ cd muduo-protorpc && ./build.sh
ocean@hp:~$ dpkg -S /usr/include/boost/version.hpp
libboost1.54-dev: /usr/include/boost/version.hpp
---------------------------------------------------------------------------------
Following are the error information:
ocean@hp:~/MYGIT/muduo-tutorial/cmake$ cmake .
-- /home/ocean/MYGIT/build/release-install-cpp11/include
-- /home/ocean/MYGIT/build/release-install-cpp11/lib
-- /home/ocean/MYGIT/build/release-install-cpp11/lib/libmuduo_base.a
-- /home/ocean/MYGIT/build/release-install-cpp11/lib/libmuduo_net.a
-- Boost version: 1.54.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ocean/MYGIT/muduo-tutorial/cmake
ocean@hp:~/MYGIT/muduo-tutorial/cmake$ make
[100%] Building CXX object CMakeFiles/echo.dir/home/ocean/MYGIT/muduo-tutorial/src/echo.cc.o
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc: In constructor ‘EchoServer::EchoServer(muduo::net::EventLoop*, const muduo::net::InetAddress&)’:
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:27:54: error: reference to ‘_1’ is ambiguous
boost::bind(&EchoServer::onConnection, this, _1));
^
In file included from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/Callbacks.h:16:0,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpConnection.h:16,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpServer.h:16,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:1:
/usr/include/c++/4.8/functional:1004:34: note: candidates are: const std::_Placeholder<1> std::placeholders::_1
extern const _Placeholder<1> _1;
^
In file included from /usr/include/boost/bind/bind.hpp:1742:0,
from /usr/include/boost/bind.hpp:22,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:9:
/usr/include/boost/bind/placeholders.hpp:55:15: note: boost::arg<1> {anonymous}::_1
boost::arg<1> _1;
^
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:29:51: error: reference to ‘_1’ is ambiguous
boost::bind(&EchoServer::onMessage, this, _1, _2, _3));
^
In file included from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/Callbacks.h:16:0,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpConnection.h:16,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpServer.h:16,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:1:
/usr/include/c++/4.8/functional:1004:34: note: candidates are: const std::_Placeholder<1> std::placeholders::_1
extern const _Placeholder<1> _1;
^
In file included from /usr/include/boost/bind/bind.hpp:1742:0,
from /usr/include/boost/bind.hpp:22,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:9:
/usr/include/boost/bind/placeholders.hpp:55:15: note: boost::arg<1> {anonymous}::_1
boost::arg<1> _1;
^
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:29:55: error: reference to ‘_2’ is ambiguous
boost::bind(&EchoServer::onMessage, this, _1, _2, _3));
^
In file included from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/Callbacks.h:16:0,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpConnection.h:16,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpServer.h:16,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:1:
/usr/include/c++/4.8/functional:1005:34: note: candidates are: const std::_Placeholder<2> std::placeholders::_2
extern const _Placeholder<2> _2;
^
In file included from /usr/include/boost/bind/bind.hpp:1742:0,
from /usr/include/boost/bind.hpp:22,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:9:
/usr/include/boost/bind/placeholders.hpp:56:15: note: boost::arg<2> {anonymous}::_2
boost::arg<2> _2;
^
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:29:59: error: reference to ‘_3’ is ambiguous
boost::bind(&EchoServer::onMessage, this, _1, _2, _3));
^
In file included from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/Callbacks.h:16:0,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpConnection.h:16,
from /home/ocean/MYGIT/build/release-install-cpp11/include/muduo/net/TcpServer.h:16,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:1:
/usr/include/c++/4.8/functional:1006:34: note: candidates are: const std::_Placeholder<3> std::placeholders::_3
extern const _Placeholder<3> _3;
^
In file included from /usr/include/boost/bind/bind.hpp:1742:0,
from /usr/include/boost/bind.hpp:22,
from /home/ocean/MYGIT/muduo-tutorial/src/echo.cc:9:
/usr/include/boost/bind/placeholders.hpp:57:15: note: boost::arg<3> {anonymous}::_3
boost::arg<3> _3;
^
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc: At global scope:
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:62:1: error: ‘scoped_ptr’ in namespace ‘boost’ does not name a type
boost::scoped_ptr<muduo::AsyncLogging> g_asyncLog;
^
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc: In function ‘void asyncOutput(const char*, int)’:
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:66:3: error: ‘g_asyncLog’ was not declared in this scope
g_asyncLog->append(msg, len);
^
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc: In function ‘void setLogging(const char*)’:
/home/ocean/MYGIT/muduo-tutorial/src/echo.cc:74:3: error: ‘g_asyncLog’ was not declared in this scope
g_asyncLog.reset(new muduo::AsyncLogging(::basename(name), kRollSize));
^
make[2]: *** [CMakeFiles/echo.dir/home/ocean/MYGIT/muduo-tutorial/src/echo.cc.o] 错误 1
make[1]: *** [CMakeFiles/echo.dir/all] 错误 2
make: *** [all] 错误 2
---------------------------------------------------------------------------
Following are the updates to CMakeLists.txt:
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
index 960cfa1..46dfd33 100644
--- a/cmake/CMakeLists.txt
+++ b/cmake/CMakeLists.txt
@@ -3,11 +3,13 @@ cmake_minimum_required(VERSION 2.6)
project(echo CXX)
if(NOT CMAKE_BUILD_TYPE)
- set(CMAKE_BUILD_TYPE "debug")
+ #set(CMAKE_BUILD_TYPE "debug")
+ set(CMAKE_BUILD_TYPE "release")
endif()
if(NOT MUDUO_PATH)
- set(MUDUO_PATH "/home/$ENV{USER}/build/${CMAKE_BUILD_TYPE}-install")
+ #set(MUDUO_PATH "/home/$ENV{USER}/build/${CMAKE_BUILD_TYPE}-install")
+ set(MUDUO_PATH "/home/$ENV{USER}/MYGIT/build/${CMAKE_BUILD_TYPE}-install-cpp11")
endif()
set(CXX_FLAGS
@@ -26,6 +28,7 @@ set(CXX_FLAGS
-Wshadow
-Wwrite-strings
-march=native
+ -std=c++11
# -MMD
# -std=c++0x
-rdynamic
diff --git a/makefile/Makefile b/makefile/Makefile
index 8c97c72..937e00c 100644
--- a/makefile/Makefile
+++ b/makefile/Makefile
@@ -1,10 +1,10 @@
-MUDUO_DIRECTORY ?= $(HOME)/build/debug-install
+MUDUO_DIRECTORY ?= $(HOME)/MYGIT/build/release-install-cpp11
#MUDUO_DIRECTORY ?= $(HOME)/build/install
MUDUO_INCLUDE = $(MUDUO_DIRECTORY)/include
MUDUO_LIBRARY = $(MUDUO_DIRECTORY)/lib
SRC = ../src
-CXXFLAGS = -g -O0 -Wall -Wextra -Werror \
+CXXFLAGS = -g -O0 -Wall -Wextra -Werror -std=c++11 \
-Wconversion -Wno-unused-parameter \
-Wold-style-cast -Woverloaded-virtual \
-Wpointer-arith -Wshadow -Wwrite-strings \
---------------------------------------------------------------------------
samuel@bazel$ bazel build ...
Starting local Bazel server and connecting to it...
INFO: SHA256 (https://github.com/chenshuo/muduo/archive/master.zip) = 321ce19134deed89a7d99d4a9edd51d80174d0283290716e7a5ac519b4f4042c
DEBUG: Rule 'muduo' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "321ce19134deed89a7d99d4a9edd51d80174d0283290716e7a5ac519b4f4042c"
INFO: Analysed target //:echo (12 packages loaded, 206 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_samuel/82ad9d1260b40bf0bd586e86a97b7568/external/muduo/muduo/net/BUILD.bazel:1:1: C++ compilation of rule '@muduo//muduo/net:net' failed (Exit 1) wrapped_clang failed: error executing command external/local_config_cc/wrapped_clang '-D_FORTIFY_SOURCE=1' -fstack-protector -fcolor-diagnostics -Wall -Wthread-safety -Wself-assign -fno-omit-frame-pointer -O0 -DDEBUG '-std=c++11' -iquote ... (remaining 21 argument(s) skipped)
Use --sandbox_debug to see verbose messages from the sandbox
In file included from external/muduo/muduo/net/InetAddress.cc:11:
In file included from external/muduo/muduo/base/Logging.h:10:
external/muduo/muduo/base/Timestamp.h:12:10: fatal error: 'boost/operators.hpp' file not found
#include <boost/operators.hpp>
^~~~~~~~~~~~~~~~~~~~~
1 error generated.
Target //:echo failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 9.057s, Critical Path: 0.54s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
我的macos 上是安装了boost的。
samuel@bazel$ brew info boost
boost: stable 1.67.0 (bottled), HEAD
Collection of portable C++ source libraries
https://www.boost.org/
Not installed
From: https://mirrors.aliyun.com/homebrew/homebrew-core.git/Formula/boost.rb
==> Dependencies
Optional: icu4c ✔
==> Options
--with-icu4c
Build regexp engine with icu support
--without-single
Disable building single-threading variant
--without-static
Disable building static library variant
--HEAD
Install HEAD version
==> Caveats
Building of Boost.Log is disabled because it requires newer GCC or Clang.
==> Analytics
install: 67,653 (30d), 163,931 (90d), 660,447 (365d)
install_on_request: 22,428 (30d), 52,303 (90d), 199,309 (365d)
build_error: 0 (30d)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.