A TCP network library implemented in C++11.
- Clone the code and build:
$ git clone https://github.com/WineChord/chtho.git
$ cd chtho
$ make
- Run the simple echo server and client examples:
$ ./build/bin/echoserver_test 5 # thread pool size (i.e. # of sub Reactors)
$ # inside another terminal
$ ./build/bin/echoclient_test 127.0.0.1 3 # server ip and number of clients
$ # then the message of 'hello' and 'world' will bump between
$ # client and server infinitely
-
time
Timestamp
: Provide basic utilities to provide current time and its conversions.TimeZone
: Convert from Coordinated Universal Time to local time.
-
logging
Logger
andLogStream
: front end of the logging system, used directly by the user.AsyncLogging
,LogFile
andFileUtil
: back end of the logging system, asynchronously flush the data to disk and conditionally roll the log.
-
threading
MutexLock
,MutexLockGuard
,Condition
andCountDownLatch
: encapsulated synchronization utilities.Thread
andThreadPool
: thread utilities based on pthread
-
timers
Timer
andTimerID
: record timestamp and callback function.TimerQueue
: add/remove timers and find expired timers using balanced binary tree.
-
networking
Channel
: important abstraction provided for file descriptors and its relating events and corresponding callback functions.EventLoop
: core class that demonstrates the Reactor pattern.EventLoopThread
: encapsulates the eventloop in a thread, ensures 'one loop per thread'EventLoopThreadPool
: starts a main eventloop thread acting as the main Reactor (usually used to monitor the listening socket) and a bunch of other eventloop thread acting as the sub Reactors (usually used to monitor read/write events happened on the connecting sockets).- poller:
Poller
: base class providing the interface of polling, usesChannel
to manage the events and callback functions of file descritpors.Poll
: An implementation ofPoller
usingpoll(2)
.EPoll
: An implementation ofPoller
usingepoll(2)
, level-triggered. Buffer
: used byTcpConnection
to allow partial read/write.TcpConnection
: manages read/write/close/error events happened on the connecting file descriptors, usesBuffer
to read/write data.Acceptor
: created inTcpServer
, encapsulates thesocket
,bind
,listen
andaccept
steps. InsideAcceptor::listen
, it will pass the connection socket file descriptor returned byaccept
to the new connection callback function provided byTcpServer
.TcpServer
finds a thread from thread pool for this new connection fd and creates aTcpConnection
object.TcpConnection
will register the connection channel (created upon connection fd) with the poller inside the eventloop which is dispatched eariler insideTcpServer
and then starts to handle events happened on the connection channel (through the registed callback functions on the channel).TcpServer
: encapsulates aEventLoopThreadPool
andAcceptor
.Acceptor
will handlesocket
,bind
,listen
andaccept
steps.TcpServer
's main role is dispatch the new connections to threads inside eventloop thread pool. It also provides the connection callback and message callback interface to the user.Connector
: works forTcpClient
, encapsulates thesocket
andconnect
steps. Depending on the return value of::connect
, it will use a channel to detect whether the connection socket is available for writing. If it is, this channel for the socket is removed and the socket file descriptor is passed to new conection callback function provided byTcpClient
.TcpClient
will use the socket file descriptor to create a newTcpConnection
. Then theTcpConnection
will handle the reading/writing event on the connection file descriptor. (the whole process is a little similar toAcceptor
)TcpClient
: encapsulates aEventLoop
andConnector
. Similar toTcpServer
, it creates a newTcpConnection
using the file descriptor provided by theConnection
. It also provides the connection callback and message callback interface to the user.InetAddr
: encapsulates Internet address.Socket
: encapsulates socket related information.
development history: logger/logstream -> threading -> time/timer -> eventloop/thread/threadpool/poller/channel -> buffer/tcpconnection/acceptor/tcpserver -> connector/tcpclient -> logfile/asynclogging -> ???
- HTTP server
- protobuf
- protorpc
MIT License
This project learns massively from muduo.