Giter VIP home page Giter VIP logo

elecho.jsonrpc's Introduction

EleCho.JsonRpc

基于 JSON 的简单 RPC 库.
Simple JSON based RPC library.

通过阅读此项目的代码, 你可以学到: 动态代理.
By reading the code of this project, you can learn: Dynamic proxy.

传输 / Transmission

--> 包头(四字节整数) + {"Method":"方法名","Arg":["参数"]}
<-- 包头(四字节整数) + {"Ret":"返回值","RefRet":["引用返回值"],"Err":"错误信息"}
--> header(four-byte integer) + {"Method":"method name","Arg":["arguments"]}
<-- header(four-byte integer) + {"Ret":"return value","RefRet":["reference returns"],"Err":"error message"}

注: 当方法正确响应返回值时, Err 字段应该为 null
Note: The Err field should be null when the method responds correctly with the return value

使用 / Usage

该库可以在 System.IO.Stream 上使用
This library can be used on System.IO.Stream

定义公共的接口(Define the public interface):

public interface Commands
{
    public void WriteLine(string message);
    public int Add(int a, int b);
    public int Add114514(ref int num);
}

服务端对接口的实现(Server implementation of the interface):

internal class CommandsImpl : Commands
{
    public int Add(int a, int b) => a + b;
    public int Add114514(ref int num) => num += 114514;
    public void WriteLine(string message) => Console.WriteLine("Server print: " + message);
}

服务端监听 TCP (Server listening on TCP):

int port = 11451;

TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));      // 监听指定端口 / listen on specified port
listener.Start();

CommandsImpl serverCommands = new CommandsImpl();                                 // 创建公用的指令调用实例 / Create a common command call instance
List<RpcServer<Commands>> rpcs = new List<RpcServer<Commands>>();                 // 保存所有客户端 RPC 引用 / Save all client RPC references

Console.WriteLine($"Listening {port}");

while (true)
{
    TcpClient client = await listener.AcceptTcpClientAsync();                     // 接受一个客户端 / Accept a client
    rpcs.Add(new RpcServer<Commands>(client.GetStream(), serverCommands));        // 创建并保存 RPC 实例 / Create and save an RPC instance
}

客户端连接并调用远程函数(The client connects and calls the remote function):

Console.Write("Addr: ");
var addr = Console.ReadLine()!;                         // 用户输入地址 / User enters the address

TcpClient client = new TcpClient();
client.Connect(IPEndPoint.Parse(addr));                 // 连接到服务器 / Connect to server

RpcClient<Commands> rpc =
    new RpcClient<Commands>(client.GetStream());        // 创建 RPC 客户端实例 / Create an RPC client instance

int num = 10;
rpc.Remote.Add114514(ref num);

if (num == 114524)
    Console.WriteLine("带 ref 参数的 RPC 调用成功");

while (true)
{
    var input = Console.ReadLine();
    if (input == null)
        break;

    rpc.Remote.WriteLine(input);                        // 调用服务端 WriteLine 方法 / Call the server WriteLine method
}

客户端控制台(Client console):
Addr: 127.0.0.1:11451
带 ref 参数的 RPC 调用成功
hello
this message is from client

服务端控制台:
Listening 11451
Server print: hello
Server print: this message is from client

elecho.jsonrpc's People

Contributors

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