Code Monkey home page Code Monkey logo

kingpin's Introduction

Kingpin is a C++ network library based on TCP/IP + epoll + pthread for the high concurrent servers and clients. Inspired by nginx / muduo.

Features

  • 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.

Design Overview

image

EpollTPServer:

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.

EpollTPClient:

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.

image

Async Logger:

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.

Quick Start

Please refer to: Quick Start

Prerequirement

  • Linux >= 3.10.0

  • g++ >= 4.8.5

Header Files

  • 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.

Examples

  • 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.

kingpin's People

Contributors

v-and-dev avatar geniusdai avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.