Code Monkey home page Code Monkey logo

nativerpc's Introduction

Native RPC

RPC with native remote calls declaration and arguments types check

Motivation

When we need communicate between instances of the same executable we have full calls (functions) declarations. Similarly when client and server are linked with same library which contains required calls. But we can't call function from another process directly. Usual RPC requires define call descriptors in the not native formats (SOAP, REST, DCOM, etc.) It's dictated by asymmetry client and server implementation. When both side have access to same set of native RPC calls declarations then we could generate all required boilerplate on the fly with help C++ templates.

Basic example

Server:

#include <stdlib.h>
#include <rpc.hpp>
#include <rpc_streams.hpp>
//Call declaration. Implementation located in the another module
int add(int a, int b);

int main() {
	//register add and stdlib.h exit() calls in the RPC registry
	auto myrpc = rpc::make_rpc<rpc::stream_serializer, rpc::stdin_stdout_ipc_t>(add, exit);
	//Start listening
	myrpc.listen();
	return 0;
}

Client:

#include <stdlib.h>
#include <rpc.hpp>
#include <rpc_streams.hpp>

//Call declaration. Implementation located in the another module
int add(int a, int b);
//Also implementation could be dummy stub. We need only address of the call on 
//client side and never call it:
//int add(int a, int b){return 0;}

int main() {
	//register add and stdlib.h exit() calls in the RPC registry in the same order as in server
	auto myrpc = rpc::make_rpc<rpc::stream_serializer, rpc::stdin_stdout_ipc_t>(add, exit);
	//Do addition on service side and print result
	std::cout << myrpc(add, 2, 5) << std::endl;
	//finishing server
	myrpc(exit, 0);
	return 0;
}

Calls library:

//Just do arguments addition
int add(int a, int b) {
	return a + b;
}

rpc::make_rpc makes new rpc calls wrapper. It includes registry of calls definitions, marshaller, calls invocator and service listener Seriaalizer and IPC implementation are separated from rpc itself. You are need pass particular implemetation in the rpc::make_rpc template arguments. rpc::stream_serializer is serializer based on stringstream rpc::stdin_stdout_ipc_t is simple standard streams based input/output intended to use with pipes

Demo examples

loopback.cpp - Demo based on loopback IPC implementation. RPC calls invokes locally

stdpipes.cpp - Demo based on Unix fork() and pipe() calls. After run it forks and configure pipes server stdout -> client stdin and client stdout -> server stdin;

Compilation

Requires c++14 compiler. Tested on G++ 7.3.0 and Ubuntu 18.04 For build just type:

$make

nativerpc's People

Contributors

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