Description

A vibe.d RPC library based on TCP connections

Package Information

Version0.1.2 (2017-Apr-17)
Repository https://github.com/jpf91/vibe-rpcchannel
LicenseBSL-1.0
CopyrightCopyright © 2017, Johannes Pfau
AuthorsJohannes Pfau
Registered byJohannes Pfau
Sub packages
vibe-rpcchannel:noise
Noise transport for RPC library
vibe-rpcchannel:docpkg
Fake package used to build documentation
Dependencies

vibe-d

eventsystem

Installation

To use this package, put the following dependency into your project's dependencies section:

dub.json
dub.sdl

Readme

vibe-rpcchannel

Coverage Status Build Status

This implements a simple D-only RPC mechanism for vibe.D. The library is designed to be generic and work with any transport stream. Easy to use wrappers are available for unencrypted TCP streams and vibe-noisestream encrypted noise channels.

The API documentation is available here.

Features

  • Events allow for server->client communication callbacks
  • Overloading for functions
  • API enables easy management of sessions and authentication
  • API is flexible enough to allow for reverse mode: The machine connecting to a remote machine can be the RPC server. This is useful for NATed machines.

Limitations:

vibe-rpcchannel is more of a method to quickly build client<->server messaging protocols than a general purpose RPC server. This means especially that only one RPC call can be processed at a time. Also there's only limited Exception handling: All server side exceptions map to an RPCException at the client. Use error return values instead.

A simple TCP server/client example

import vibe.d, vibe.rpcchannel;

abstract class API
{
    int request(int a);
    string request(string a);
    
    Event!(int) progress;
    Event!() timer;

    void startTimer();
    void stopTimer();


    @ignoreRPC void ignoredFunction() {};
    @ignoreRPC void ignoredEvent();
}

class APIServer : API
{
    bool _timerStop = false;

    static APIServer startSession(TCPConnection info)
    {
        // Use info for authentication
        return new APIServer();
    }
    
    override int request(int a)
    {
        progress.emit(1);
        progress.emit(2);
        progress.emit(3);
        return a;
    }

    override string request(string a)
    {
        progress.emit(4);
        return a;
    }

    override void startTimer()
    {
        runTask(()
        {
            while (!_timerStop)
            {
                timer.emit();
                sleep(100.msecs);
            }
        });
    }
    
    override void stopTimer()
    {
        _timerStop = true;
    }
}

void main()
{
    void clientMain()
    {
        auto client = clientTCP!(API)("localhost", 8030);
        client.progress ~= (int a) {writeln("Progress event: ", a);};
        client.timer ~= () {writeln("Timer event");};
        assert(client.request(42) == 42);
        assert(client.request("hello") == "hello");
        client.startTimer();
        client.stopTimer();
        client.closeTCP();
    }

    auto server = serveTCP!(APIServer, API)(8030);
    runTask(&clientMain);
    runEventLoop();
}

Available versions

0.1.2 0.1.1 0.1.0 ~master