Code Monkey home page Code Monkey logo

varlink-cpp's Introduction

C++17 implementation of the Varlink protocol

This is an implementation of Varlink in C++17 built on top of asio. It's not yet feature complete, but in general it's working.

It is using nlohmann/json as data format and provides a std::function service callback interface. I consider it work in progress still, API may change.

Basic usage

include(VarlinkWrapper)
# Generate C++ header
varlink_wrapper(org.example.more.varlink)

# Add to taret
add_executable(example [...] org.example.more.varlink.hpp)

Server

#include <varlink/server.hpp>
#include "org.example.more.varlink.hpp"

...

{
// varlink_server class provides encapsulation for an async_server and a varlink_service
// and resolves varlink-uris to the corresponding asio endpoints
varlink::net::io_context ctx{};
auto varlink_srv = varlink::varlink_server(ctx, "unix:/tmp/example.varlink",
                {"vendor", "product", "version", "url"});

// interfaces can only be added once and callbacks can't be changed
varlink_srv.add_interface(org_example_more_varlink, varlink::callback_map{
    // varlink_callback is a macro containing the callback parameter list
    {"Ping", [] varlink_callback { send_reply({{"pong", parameters["ping"]}}, /* continues = */ false); }}
});

varlink_srv.async_serve_forever();
ctx.run();
}

Client (sync):

#include <varlink/client.hpp>

...

{
// Connect on creation
varlink::net::io_context ctx{};
auto client = varlink::varlink_client{ctx, "unix:/tmp/example.varlink"};

// .call returns a json object of the result ["parameters"]
auto reply = client.call("org.example.more.Ping", R"({"ping":"Test"})"_json);
// all parameters are validated by the server
assert(reply["pong"].get<std::string>() == "Test");

// .call_more returns a callable object
auto more = client.call_more("org.example.more.TestMore", json{{"n","1"}});
reply = more();
}

Client (async):

#include <varlink/client.hpp>

...

{
// Connect on creation
varlink::net::io_context ctx{};
auto client = varlink::varlink_client{ctx, "unix:/tmp/example.varlink"};

json reply{};
// async_call (currently) captures the message by value
client.async_call(varlink::varlink_message("org.example.more.Ping", R"({"ping":"Test"})"_json),
                  [&](auto ec, const json& rep){
    reply = rep;
});

ctx.run();
assert(reply["pong"].get<std::string>() == "Test");
}

varlink-cpp's People

Contributors

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