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
LoggerandLogStream: front end of the logging system, used directly by the user.AsyncLogging,LogFileandFileUtil: back end of the logging system, asynchronously flush the data to disk and conditionally roll the log.
-
threading
MutexLock,MutexLockGuard,ConditionandCountDownLatch: encapsulated synchronization utilities.ThreadandThreadPool: thread utilities based on pthread
-
timers
TimerandTimerID: 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, usesChannelto manage the events and callback functions of file descritpors.Poll: An implementation ofPollerusingpoll(2).EPoll: An implementation ofPollerusingepoll(2), level-triggered. Buffer: used byTcpConnectionto allow partial read/write.TcpConnection: manages read/write/close/error events happened on the connecting file descriptors, usesBufferto read/write data.Acceptor: created inTcpServer, encapsulates thesocket,bind,listenandacceptsteps. InsideAcceptor::listen, it will pass the connection socket file descriptor returned byacceptto the new connection callback function provided byTcpServer.TcpServerfinds a thread from thread pool for this new connection fd and creates aTcpConnectionobject.TcpConnectionwill register the connection channel (created upon connection fd) with the poller inside the eventloop which is dispatched eariler insideTcpServerand then starts to handle events happened on the connection channel (through the registed callback functions on the channel).TcpServer: encapsulates aEventLoopThreadPoolandAcceptor.Acceptorwill handlesocket,bind,listenandacceptsteps.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 thesocketandconnectsteps. 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.TcpClientwill use the socket file descriptor to create a newTcpConnection. Then theTcpConnectionwill handle the reading/writing event on the connection file descriptor. (the whole process is a little similar toAcceptor)TcpClient: encapsulates aEventLoopandConnector. Similar toTcpServer, it creates a newTcpConnectionusing 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.