1 Star 1 Fork 0

GuaikOrg / easy-rpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

easy-rpc 是跨通信方式的Rust RPC框架,也有其他语言实现。

Rust JavaScript
WebSocket
SharedMem

WebSocket/JavaScript用于Rust和网页交互数据,共享内存(SharedMem)用于进程间通信。

优点

  • 基于MsgPack,不需要协议文件,动态解析类型
  • 通信双方可以递归地Request,类似本地的函数递归调用

缺点

easy-rpc(目前)不是异步实现,每一个会话都会占据一个线程。如果你用在有大量IO的服务端上,可能不太合适。

文档

有待完善。可以参考test里的例子,如果你用过serde系列库,应该会很容易上手。

注意事项

easy-rpc目前依赖一个git库,所以没有发布到 crates.io,使用时要通过git引用。

Cargo.toml

[package]
# ...

[dependencies]
# ...
easy-rpc = {git = 'https://github.com/metaworm/easy-rpc'}

示例

use std::sync::Arc;
use easy_rpc::*;

struct ServerService;

const MUL: u32 = 1;
easy_service! {
    ServerService(self, _ss, arg, response)

    StringMethod {
        "add" => (a: u32, b: u32) {
            a + b
        }
        "print" => (s: String) {
            println!("{}", s);
        }
    }
    IntegerMethod {
        MUL => (a: u32, b: u32) {
            a * b
        }
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    std::thread::spawn(|| {
        let mut ser = ws::bind("127.0.0.1:3333").unwrap();
        let (adaptor, _uri) = ws::accept(&mut ser).unwrap();
        Session::new(adaptor, Arc::new(ServerService)).loop_handle();
    });

    std::thread::sleep_ms(100);
    let session = Session::new(ws::connect("ws://127.0.0.1:3333")?, Arc::new(EmptyService));
    let val: u32 = session.request("add", (1, 2)).into()?;
    session.notify("print", format!("the result is {}", val));
    let val: u32 = session.request(MUL, (2, 3)).into()?;
    assert_eq!(val, 6);

    Ok(())
}
MIT License Copyright (c) 2019 metaworm Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Rust编写的RPC框架 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/GuaikOrg/easy-rpc.git
git@gitee.com:GuaikOrg/easy-rpc.git
GuaikOrg
easy-rpc
easy-rpc
master

搜索帮助