Giter Club home page Giter Club logo

moustique's Introduction

Moustique

Moustique is a tiny C++14 library (~180 LOC) providing an easy to use interface to non-blocking network IO on linux.

Implementation details

Moustique relies on linux epoll for non blocking IO and on boost::context for resuming/suspending handlers. The source code is rather small, fell free to dive into moustique.hh if you want to know more about the implementation.

No dynamic allocations, no buffering is done by moustique internally. The read/write callbacks are simply forwarding your buffer to the read/write syscalls. The only overhead is the context switching of boost::context [1].

Dependencies: boost::context (-lboost_context), C++14. Licence: MIT

TCP echo example

#include "moustique.hh"

int main()
{
    moustique_listen(1234, // Port number
                     SOCK_STREAM, // TCP socket
                     2, // number of threads
         [] (int fd, auto read, auto write) {

           printf("new connection: %i\n", fd);

           char buf[1024];
           int received;
           
           while (received = read(buf, sizeof(buf))) // Suspend until new bytes
                                                     // are available for reading.

             if (!write(buf, received)) // Suspend until the socket is ready for a write.
               break;

           printf("end of connection: %i\n", fd);
         }
    );
}

Compilation

g++/clang++ echo.cc -lboost_context -lpthread -DNDEBUG -O3

[1] https://www.boost.org/doc/libs/1_66_0/libs/context/doc/html/context/performance.html

moustique's People

Contributors

matt-42 avatar nanxiao avatar

Watchers

 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.