GithubHelp home page GithubHelp logo

deepgrace / snp Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 42 KB

Structured Network Programming with Sender / Receiver in Modern C++♦️

License: Boost Software License 1.0

CMake 1.02% Shell 0.22% C++ 98.76%
netowrk programming receiver sender snp concurrency asynchronous c-plus-plus cpp asio

snp's Introduction

snp LICENSE Language Platform

Structured Network Programming

Overview

#define BOOST_ASIO_HAS_IO_URING
#define BOOST_ASIO_DISABLE_EPOLL

#include <iostream>
#include <snp.hpp>
#include <unifex/just.hpp>
#include <unifex/then.hpp>
#include <unifex/let_value.hpp>
#include <unifex/upon_error.hpp>

namespace net = boost::asio;

using tcp = net::ip::tcp;
using socket_t = tcp::socket;

using error_code_t = boost::system::error_code;
using results_type = tcp::resolver::results_type;

int main(int argc, char* argv[])
{
    net::io_context ioc;
    socket_t socket(ioc); 

    std::string port = "https";
    std::string host = "isocpp.org";

    snp::async_resolve(ioc, host, port) 
    | unifex::then([&](results_type endpoints)
      {
          return snp::async_connect(socket, endpoints);
      })
    | unifex::let_value([&]<typename T>(T&& t)
      {
          t | unifex::then([&](tcp::endpoint e)
            {
                std::cout << "connected to " << e.address() << " " << e.port() << std::endl;
            })
          | unifex::upon_error([]<typename Error>(Error error)
            {
                if constexpr(std::is_same_v<Error, error_code_t>)
                    std::cout << "async_connect: " << error.message() << std::endl;
            })
          | snp::start_detached();

          return unifex::just();
      })
    | unifex::upon_error([]<typename Error>(Error error)
      {
          if constexpr(std::is_same_v<Error, error_code_t>)
              std::cout << "async_resolve: " << error.message() << std::endl;
      })
    | snp::start_detached();

    ioc.run();

    return 0;
}

Introduction

snp is a network programming library, which is header-only, extensible and modern C++ oriented.
It's built on top off the unifex and boost, it's inspired by the C++ Sender / Receiver async programming model.
snp enables you to do network programming with tcp and websocket protocols in an asynchronous and structured manner.

snp provides the following sender factories:

  • async_accept
  • async_close
  • async_connect
  • async_handshake
  • async_read
  • async_read_some
  • async_read_some_at
  • async_resolve
  • async_wait
  • async_wait_until
  • async_write
  • async_write_some
  • async_write_some_at

snp provides the following sender consumer:

  • start_detached

snp provides the following scheduler type:

  • asio_context

snp provides the following scheduler algorithms:

  • now
  • schedule
  • schedule_at
  • schedule_after

Prerequsites

boost
uring
unifex

By default, the example is using the io_uring backend, it's disabled by default in Boost.Asio.
This backend may be used for all I/O objects, including sockets, timers, and posix descriptors.
To disable the io_uring backend, just comment out the following lines in code under the example directory:

#define BOOST_ASIO_HAS_IO_URING
#define BOOST_ASIO_DISABLE_EPOLL

Compiler requirements

The library relies on a C++20 compiler and standard library

More specifically, snp requires a compiler/standard library supporting the following C++20 features (non-exhaustively):

  • concepts
  • lambda templates
  • All the C++20 type traits from the <type_traits> header

Building

snp is header-only. To use it just add the necessary #include line to your source files, like this:

#include <snp.hpp>

To build the example with cmake, cd to the root of the project and setup the build directory:

mkdir build
cd build
cmake ..

Make and install the executables:

make -j4
make install

The executables are now located at the bin directory of the root of the project.
The example can also be built with the script build.sh, just run it, the executables will be put at the /tmp directory.

Full example

Please see example.

License

snp is licensed as Boost Software License 1.0.

snp's People

Contributors

deepgrace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 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.