Git Product home page Git Product logo

jsonrpclib's Introduction

JsonRpcLib

C# DotNetCore 2.1+ Client/Server Json RPC library
Using Span<T>, Memory<T> and IO pipelines

NuGet version (Bundgaard.JsonRpcLib) license Build Status Build status

Current performance

Run the PerfTest app

  • 8 threads 1,000,000 json notify -> static class call: ~1,500,000 requests/sec
  • 8 threads 100,000 json invoke -> static class call: ~27,500 requests/sec

Test machine: 3.4 Ghz i5 3570

The Server

JsonRpc server using SocketListener class (corefxlab)

public class MyServer : JsonRpcServer
{
    readonly SocketListener _listener;
    TaskCompletionSource<int> _tcs = new TaskCompletionSource<int>();

    public MyServer(int port)
    {
        _listener = new SocketListener();
        _listener.Start(new IPEndPoint(IPAddress.Any, port));
        _listener.OnConnection(OnConnection);
    }

    private Task OnConnection(SocketConnection connection)
    {
        IClient client = AttachClient(connection.GetRemoteIp(), connection);
        return _tcs.Task;
    }

    public override void Dispose()
    {
        _tcs.TrySetCanceled();
        _listener?.Stop();
        base.Dispose();
    }
}

Start the server and register methods

const int port = 7733;
using(var server = new MyServer(port))
{
    // Bind to functions on static class
    server.Bind(typeof(Target));    

    // Bind to a delegate
    server.Bind("DelegateMethod", (Action<int,int>)( (a, b)
        => Debug.WriteLine($"DelegateMethod. a={a} b={b}") ));
}

static class Target
{
    public static int TestMethod()
    {
        Debug.WriteLine("TestMethod called");
        return 42;
    }
}

You need this little extension to get the clients IP address on the server

public static class SocketExtensions
{
    static readonly PropertyInfo s_socketProperty;
    static SocketExtensions()
    {
        s_socketProperty = typeof(SocketConnection).GetProperty("Socket",
            BindingFlags.NonPublic | BindingFlags.Instance);
    }

    public static string GetRemoteIp(this SocketConnection conn)
    {
        if (s_socketProperty?.GetValue(conn) is Socket socket)
        {
            var ipEP = socket.RemoteEndPoint as IPEndPoint;
            return ipEP.Address.MapToIPv4().ToString();
        }
        return null;
    }
}

The Client

JsonRpc client using SocketConnection class (corefxlab)

public class MyClient
{
    private readonly SocketConnection _conn;

    public static async Task<JsonRpcClient> ConnectAsync(int port)
    {
        var c = await SocketConnection.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port));
        return new JsonRpcClient(c);
    }
}

Connect client to server and call the methods

const int port = 7733;
using(var client = await MyClient.ConnectAsync(port))
{
    var result = await client.InvokeAsync<int>("TestMethod");
    await client.InvokeAsync("DelegateMethod", 44, 76);

    // Fire-and-forget 
    client.Notify("DelegateMethod", 2, 6);
}

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.