Kingpin is a C++ network library based on TCP/IP + epoll + pthread for the high concurrent servers and clients. Inspired by nginx / muduo.
-
Per epoll per thread and one connected socket handled only by one thread.
-
Thread pool and IO multiplexing for both server's and client's concurrency, server use thread shared mutex to avoid parallel accept syscall.
-
Avoid excessive wrapper of classes and functions to get things complicated.
1. Trying to get mutex from TPSharedData, if it's locked, thread won't block.
2. If got the mutex, IO thread register EPOLLIN for the listening socket.
3. Wait for epoll events.
4. Handle the connected sockets.
5. Get a new connected socket by accept syscall.
6. Remove EPOLLIN for the listening socket.
7. Release the mutex.
8. Handle the new connected socket.
1. Init the connected sockets by connect syscall.
2. Register the connected sockets to epoll.
3. Wait for the epoll events.
4. Handle the connected sockets.
1. Get mutex for the log buffer, if it's locked, thread will block.
2. Write log to the buffer.
3. Release the mutex.
4. Notify the corresponding backend thread.
5. Backend thread being waked up by the condition variable.
6. Backend thread trying to get the mutex, if it's locked, sleep until next time being notified.
7. If get mutex, read from the buffer.
8. Write buffer to the corresponding fd.
9. Release the mutex.
Please refer to: Quick Start
-
Linux >= 3.10.0
-
g++ >= 4.8.5
-
IOHandler.h: Virtual base class got a epoll fd, derived class shall implement handler functions for IO.
-
TPSharedData.h: Data shared among threads.
-
EpollTP.h: Thread pool, initialized by IOHandler and TPSharedData.
-
Logger.h: Thread safe logger, using backend thread for asynchronous output, line buffered.
-
Buffer.h: IO Buffer pre-allocated on heap, support nonblock socket and disk fd. NOT thread safe.
-
Context.h: Function call timeout wrapper.
-
Utils.h: Some utility functions.
-
Chinese Chess Game: Chess game server and client, also implement a high concurrency test client.
-
File Transfer: File transfer server and client, IO and exceptions are both well handled.