A multi-thread TCP network library using C++11.
We can setup a simple http server based on this TCP library.
- resources are in
./root
- execuatble is built from
./flute/net/http/tests/HttpServer_test.cc
. Http callbacks are defined here.
# build the project
sh ./build_flute.sh
# HttpServer is one of the execuatbles
./build/debug_cpp11/bin/HttpServer_test 8 3 0 1
args
- Num of threads in the sub-reactor poll
- LogLevel. 3 means
ERROR
- Copy Mode. 0 means
sendflie
(zerocopy) - Chunk size(KB) of the copier
- Macro-based multi-thread logging
- FIFO Buffer for byte-oriented IO
- Pooler for IO multiplexing
- Main-sub Reactor, in one-reactor-per-thread manner
- ZeroCopier for efficient file transfer
- HTTP Server
Common
- Buffer
- LogLine
- CurrentThread
- Mutex, Condition
- Thread
- Timestamp
- ZeroCopier
Network
- Socket
- Channel
- Pooler
- Reactor
- TimerQueue
- Acceptor
- TCPServer
- HttpServer
Based on Google C++ Style
- naming
- Files:
Reactor.h
,Reactor.cc
- variable:
offset
,tcp_conn_ptr
- class:
Reactor
,ZeroCopier
- member variables:
m_socket_fd
,m_mutex
,m_is_writing
- funtions:
to_write()
,send_one_chunk()
- global variable:
g_conn_counter
- enums:
kZeroCopy
- macros:
LOG_INFO
- namespace:
flute
- Files:
- Header:
#define FLUTE_NET_REACTOR
- using namespace
flute
contains all of the definitions.std
to access standard library.- anonymous to hide details
Resource acquisition is initialization is a programming idiom used in several object-oriented, statically-typed programming languages to describe a particular language behavior.
In RAII, holding a resource is a class invariant, and is tied to object lifetime.
- scoped variable
MutexGuard lock;
for critial resources
- combined with std::shared_ptr, to automatically call deconstructors and free the resources.
Thread
: pthread idZeroCopier
: source file fdTCPConnection
: socket fdTimerQueue
: timer fdReactorPool
: Reactors
- Locust is recommened for scalable loadtest.
- FlameGraph can help to trace stack and identify the bottleneck of the system.
sudo ./flamegraph.sh
- muduo
- Unix NetWork Programming
- Linux 多线程服务端编程:使用 muduo C++网络库