Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
/ JsonRpcLib Public archive

C# DotNetCore 2.1 Client/Server Json RPC library

License

Notifications You must be signed in to change notification settings

jbdk/JsonRpcLib

Repository files navigation

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);
}