25 Commits

Author SHA1 Message Date
a1e10f93ce bump
Some checks failed
Build library & run tests / build (unix) (push) Successful in 57s
Build library & run tests / build (tcp) (push) Successful in 54s
Publish library / publish (push) Failing after 21s
Build library & run tests / docs (push) Successful in 22s
2024-06-25 18:19:35 +02:00
2cf0b9abe4 Don't run doctests which would break on unix
All checks were successful
Build library & run tests / build (unix) (push) Successful in 28s
Build library & run tests / build (tcp) (push) Successful in 29s
Build library & run tests / docs (push) Successful in 30s
2024-06-25 11:48:47 +02:00
8b0f01e606 Add information about dependencies to README
Some checks failed
Build library & run tests / build (unix) (push) Failing after 48s
Build library & run tests / docs (push) Successful in 49s
Build library & run tests / build (tcp) (push) Successful in 53s
2024-06-25 11:44:25 +02:00
beda8c151d Changes in dependencies 2024-06-25 11:43:04 +02:00
84f7009ad2 Add more proper cleanup for the server
Some checks failed
Build library & run tests / build (unix) (push) Failing after 34s
Build library & run tests / build (tcp) (push) Successful in 35s
Build library & run tests / docs (push) Successful in 37s
2024-06-25 11:39:07 +02:00
267b741ac4 implement std::error::Error for the error type
All checks were successful
Build library & run tests / build (unix) (push) Successful in 33s
Build library & run tests / build (tcp) (push) Successful in 34s
Build library & run tests / docs (push) Successful in 35s
2024-06-25 00:03:29 +02:00
bffb41e8a1 implement Drop for client struct
All checks were successful
Build library & run tests / build (unix) (push) Successful in 34s
Build library & run tests / build (tcp) (push) Successful in 35s
Build library & run tests / docs (push) Successful in 37s
2024-06-24 23:44:44 +02:00
b5870e62fe Implement Display for the error enum
All checks were successful
Build library & run tests / build (unix) (push) Successful in 33s
Build library & run tests / build (tcp) (push) Successful in 35s
Build library & run tests / docs (push) Successful in 36s
2024-06-24 23:01:58 +02:00
f4d65a2c51 Sanitization improvements
Some checks failed
Build library & run tests / build (unix) (push) Successful in 19s
Build library & run tests / build (tcp) (push) Successful in 20s
Build library & run tests / docs (push) Successful in 23s
Publish library / publish (push) Failing after 26s
2024-06-24 22:32:03 +02:00
912b69ef93 Try to fix publish.yaml workflow
All checks were successful
Build library & run tests / build (unix) (push) Successful in 33s
Build library & run tests / build (tcp) (push) Successful in 34s
Build library & run tests / docs (push) Successful in 35s
2024-06-24 21:54:28 +02:00
2353c1648e Bump for mistake in deploy workflow
Some checks failed
Build library & run tests / build (unix) (push) Successful in 20s
Build library & run tests / build (tcp) (push) Successful in 21s
Build library & run tests / docs (push) Successful in 24s
Publish library / publish (push) Failing after 26s
2024-06-24 21:34:30 +02:00
e1f453fa8b Update publish.yaml to properly format token for gitea
All checks were successful
Build library & run tests / build (unix) (push) Successful in 34s
Build library & run tests / build (tcp) (push) Successful in 35s
Build library & run tests / docs (push) Successful in 36s
2024-06-24 21:33:34 +02:00
4dff84f4e7 Move rand to dev-dependencies
Some checks failed
Build library & run tests / build (tcp) (push) Successful in 20s
Build library & run tests / build (unix) (push) Successful in 19s
Build library & run tests / docs (push) Successful in 24s
Publish library / publish (push) Failing after 25s
2024-06-24 21:29:48 +02:00
4fc0359625 Lift dry run flag from publish.yaml
All checks were successful
Build library & run tests / build (tcp) (push) Successful in 34s
Build library & run tests / build (unix) (push) Successful in 33s
Build library & run tests / docs (push) Successful in 37s
2024-06-24 21:28:16 +02:00
8313209e0e Bump for testing
All checks were successful
Build library & run tests / build (unix) (push) Successful in 1m7s
Build library & run tests / build (tcp) (push) Successful in 1m10s
Build library & run tests / docs (push) Successful in 22s
Publish library / publish (push) Successful in 32s
2024-06-24 21:20:40 +02:00
91deddc1d2 Add publishing to gitea releases for the release workflow
All checks were successful
Build library & run tests / build (unix) (push) Successful in 34s
Build library & run tests / build (tcp) (push) Successful in 35s
Build library & run tests / docs (push) Successful in 35s
2024-06-24 21:20:02 +02:00
0d018d0869 Attempt to add publish workflow
All checks were successful
Build library & run tests / build (unix) (push) Successful in 34s
Build library & run tests / build (tcp) (push) Successful in 34s
Build library & run tests / docs (push) Successful in 35s
2024-06-24 21:12:14 +02:00
71adf67727 Add description and repository to manifest
All checks were successful
Build library & run tests / build (unix) (push) Successful in 57s
Build library & run tests / docs (push) Successful in 58s
Build library & run tests / build (tcp) (push) Successful in 1m1s
2024-06-24 21:04:36 +02:00
b12b382d94 Try fixing doc upload action
All checks were successful
Build library & run tests / build (unix) (push) Successful in 17s
Build library & run tests / build (tcp) (push) Successful in 19s
Build library & run tests / docs (push) Successful in 22s
2024-06-24 18:35:02 +02:00
812861640d Significantly update the documentation
Some checks failed
Build library & run tests / docs (push) Failing after 1m0s
Build library & run tests / build (unix) (push) Successful in 1m6s
Build library & run tests / build (tcp) (push) Successful in 1m7s
2024-06-24 18:26:19 +02:00
bfd4c1346f Update README.md
All checks were successful
Build library & run tests / build (unix) (push) Successful in 28s
Build library & run tests / build (tcp) (push) Successful in 30s
2024-06-24 16:58:14 +02:00
bf183a0598 Fix crash in server due to overeager parsing
All checks were successful
Build library & run tests / build (unix) (push) Successful in 29s
Build library & run tests / build (tcp) (push) Successful in 29s
2024-06-24 15:58:54 +02:00
fc570fa0bd Fix compile error on unix
Some checks failed
Build library & run tests / build (unix) (push) Failing after 27s
Build library & run tests / build (tcp) (push) Successful in 29s
2024-06-24 15:38:55 +02:00
cd2cf3346f Minor tweak in this awful pile of code
Some checks failed
Build library & run tests / build (tcp) (push) Successful in 51s
Build library & run tests / build (unix) (push) Failing after 54s
2024-06-24 15:36:29 +02:00
62262cb0fe Fully implement communication 2024-06-24 15:34:14 +02:00
10 changed files with 942 additions and 254 deletions

View File

@@ -16,3 +16,15 @@ jobs:
run: nix build .#clippy_${{ matrix.feature }} run: nix build .#clippy_${{ matrix.feature }}
- name: Build & test - name: Build & test
run: nix build .#${{ matrix.feature }} run: nix build .#${{ matrix.feature }}
docs:
runs-on: nix
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build docs
run: nix build .#doc.doc
- name: (Temporary) Upload docs
uses: actions/upload-artifact@v3
with:
name: docs
path: ./result-doc

View File

@@ -0,0 +1,23 @@
name: Publish library
on:
push:
branches:
- master
tags:
- v*
workflow_dispatch:
jobs:
publish:
runs-on: nix
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Publish to Gitea Cargo registry
run: nix develop -c cargo publish --token "Bearer ${{ secrets.GITHUB_TOKEN }}" --index sparse+https://git.colon-three.com/api/packages/kodi/cargo/
- name: Publish to crates.io
run: nix develop -c cargo publish --token ${{ secrets.CRATESIO_TOKEN }}
- name: Publish to Gitea Releases
uses: akkuman/gitea-release-action@v1
with:
draft: true

197
Cargo.lock generated
View File

@@ -17,6 +17,86 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "async-stream"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51"
dependencies = [
"async-stream-impl",
"futures-core",
"pin-project-lite",
]
[[package]]
name = "async-stream-impl"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.73" version = "0.3.73"
@@ -65,10 +145,18 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]] [[package]]
name = "eagle" name = "eagle"
version = "0.2.0" version = "0.3.0"
dependencies = [ dependencies = [
"env_logger",
"log",
"proc-macro2", "proc-macro2",
"quote", "quote",
"rand", "rand",
@@ -76,8 +164,38 @@ dependencies = [
"serde", "serde",
"syn", "syn",
"tokio", "tokio",
"tokio-test",
] ]
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]]
name = "futures-core"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.15" version = "0.2.15"
@@ -101,12 +219,30 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.155" version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.4" version = "2.7.4"
@@ -212,6 +348,35 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "regex"
version = "1.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]] [[package]]
name = "ron" name = "ron"
version = "0.8.1" version = "0.8.1"
@@ -299,12 +464,42 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "tokio-stream"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "tokio-test"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7"
dependencies = [
"async-stream",
"bytes",
"futures-core",
"tokio",
"tokio-stream",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"

View File

@@ -1,27 +1,33 @@
[package] [package]
name = "eagle" name = "eagle"
version = "0.2.0" version = "0.3.0"
edition = "2021" description = "A simple library for creating RPC protocols."
repository = "https://git.colon-three.com/kodi/eagle"
authors = ["KodiCraft <kodi@kdcf.me>"]
license = "AGPL-3.0" license = "AGPL-3.0"
publish = ["gitea"] edition = "2021"
resolver = "2" resolver = "2"
[features] [features]
default = ["tcp"] default = ["tcp", "log"]
tcp = ["tokio/net"] tcp = []
unix = ["tokio/net"] unix = []
log = []
[dependencies] [dependencies]
proc-macro2 = "1.0.85" proc-macro2 = "1.0.85"
quote = "1.0.36" quote = "1.0.36"
rand = "0.8.5"
ron = "0.8.1"
serde = { version = "1.0.203", features = ["serde_derive"] }
syn = "2.0.66" syn = "2.0.66"
tokio = { version = "1.38.0", features = ["sync", "io-util"] }
[dev-dependencies] [dev-dependencies]
# tokio = { version = "1.38.0", features = ["sync", "io-util"] }
ron = "0.8.1"
serde = { version = "1.0.203", features = ["serde_derive"] }
rand = "0.8.5"
tokio = { version = "1.38.0", features = ["sync", "rt-multi-thread", "macros", "time", "io-util", "net"] } tokio = { version = "1.38.0", features = ["sync", "rt-multi-thread", "macros", "time", "io-util", "net"] }
tokio-test = "0.4.4"
env_logger = { version = "0.11.3" }
log = { version = "0.4.21" }
[lib] [lib]
proc-macro = true proc-macro = true

View File

@@ -1,58 +1,89 @@
# Eagle # Eagle
## Disclaimer ## Stability
Eagle is still in development and not currently usable. The current state is barely a proof of concept. Eagle is still in early development. Performance is not ideal, the interface is likely to change and the documentation is not final. Basic functionality is fully implemented and works as expected.
## What is Eagle? ## What is Eagle?
Eagle is a library designed to make "full-stack" applications with Rust. It allows you to define a communication protocol Eagle is a library which allows you to easily build an [RPC](https://en.wikipedia.org/wiki/Remote_procedure_call) protocol.
based on simple "questions" and "answers" which can be implemented as simple functions. From the perspective of the client It uses a macro to generate the required communication code and makes adding new functions easy and quick. Eagle is designed to work specifically with [`tokio`](https://crates.io/crates/tokio) and uses [`serde`](https://crates.io/crates/serde) for formatting data.
(which sends "questions") the protocol is simply a set of async functions on a struct. From the perspective of the server
(which sends "answers") the protocol is a trait which it implements on any struct of its choice. Please note that since `eagle` is a pure proc-macro library, you must manually add compatible versions of `tokio`, `serde`, `ron` and optionally `log` to your dependencies.
## Using Eagle ## Using Eagle
The way that `eagle` is designed to be used is inside a shared dependency between your "server" and your "client". Both of these should be in a workspace. Create a `shared` crate which both components should depend on. Inside this crate, you can The way that `eagle` is designed to be used is inside a shared dependency between your "server" and your "client". Both of these should be in a workspace. Create a `shared` crate which both components should depend on, this crate should have `eagle` as a dependency. By default `eagle` uses TCP for communication, but you may disable default features and enable the `unix` feature on `eagle` to use unix sockets instead.
define your protocol as an enum:
```rs Inside this crate, you can define your protocol as an enum:
```rust
use eagle::Protocol; use eagle::Protocol;
use serde::{Serialize, Deserialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct ExampleStruct {
a: i32,
b: i32
}
#[derive(Protocol)] #[derive(Protocol)]
pub enum TestProtocol { pub enum Example {
Addition((i32, i32), i32), Addition((i32, i32), i32),
SomeKindOfQuestion(String, i32) StructuredDataAlsoWorks(ExampleStruct, ()),
SetState(i32, i32),
GetState((), i32)
} }
``` ```
In your server, you will be able to implement this protocol for any struct (and in the future register it for communication): Each variant describes one of the functions that the client can call, the first field on a variant represents the arguments that the client can send and the second field represents the return value. In the example above, the `addition` function would take in two [`i32`]s and return another [`i32`]. Any data passed this way must implement [`Clone`] as well as [`serde::Serialize`] and [`serde::Deserialize`].
```rs The [`Protocol`] macro will create a number of exports in your shared crate. You will be able to import them by name in your client and server.
use shared::TestProtocolServer;
pub struct Server; Once your protocol is defined, you can implement it on your server. To do so, you must first implement a handler for your
impl TestProtocolServer for Server { protocol. A handler must implement [`Clone`] as well as the `ServerHandler` trait for your protocol. For the above example:
fn addition(&mut self, a: i32, b: i32) -> i32 {
```rust
struct ExampleHandler {
state: i32
}
impl ExampleServerHandler for ExampleHandler {
async fn addition(&mut self, a: i32, b: i32) -> i32 {
a + b a + b
} }
fn some_kind_of_question(&mut self, question: String) -> i32 { async fn get_state(&mut self) -> i32 {
42 self.state
}
async fn set_state(&mut self, state: i32) -> i32 {
self.state = state;
self.state
} }
} }
``` ```
In your client, you can use an instance of the client struct to query the server: Your handler can now be used by the server. You can easily bind your server to a socket with:
```rs ```rust
use shared::TestProtocolClient; use shared::ExampleServer;
#[tokio::main] let handler = ExampleHandler { state: 0 };
async fn main() { let server_task = ExampleServer::bind(handler, "127.0.0.1:1234").await;
let client = TestProtocolClient::new(); // Or, if you're using the 'unix' feature...
assert_eq!(client.addition(2, 2).await, 4); let server_task = ExampleServer::bind(handler, "/tmp/sock").await;
}
```
Note that bind is an asynchronous function which should never return, you must put it in a separate task. Once bound, the server will await for connections and start responding to queries.
On the client, all you need to do is to use your protocol's `Client` to connect and you can start making requests.
```rust
use shared::ExampleClient;
let client = ExampleClient::connect("127.0.0.1:1234").await.unwrap();
assert_eq!(client.addition(5, 2), 7);
``` ```
## License ## License

View File

@@ -20,6 +20,12 @@
doCheck = true; doCheck = true;
mode = "test"; mode = "test";
}; };
doc = naersk-lib.buildPackage {
src = ./.;
doDoc = true;
mode = "test";
cargoDocOptions = x: x ++ ["--no-deps"];
};
unix = naersk-lib.buildPackage { unix = naersk-lib.buildPackage {
src = ./.; src = ./.;
doCheck = true; doCheck = true;
@@ -49,6 +55,7 @@
}; };
devShell = with pkgs; mkShell { devShell = with pkgs; mkShell {
nativeBuildInputs = with pkgs; [rustc cargo rustfmt pre-commit clippy cargo-expand]; nativeBuildInputs = with pkgs; [rustc cargo rustfmt pre-commit clippy cargo-expand];
RUST_LOG = "debug";
}; };
}); });
} }

View File

@@ -1,5 +1,5 @@
/* /*
Eagle - A library for easy communication in full-stack Rust applications Eagle - A simple library for RPC in Rust
Copyright (c) 2024 KodiCraft Copyright (c) 2024 KodiCraft
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
@@ -15,6 +15,138 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
//! # Eagle - A simple library for RPC in Rust
//!
//! <div class="warning">Eagle is still in early development. This documentation is subject to change and may not be entirely accurate.</div>
//!
//! Eagle is a library for building RPC protocols in Rust. It uses a macro
//! to transform your protocol definition into the necessary code to allow
//! communication between a server and a client.
//!
//! Eagle uses [`tokio`](https://tokio.rs) as its async runtime and
//! [`ron`](https://crates.io/crates/ron) for serialization.
//!
//! ## Usage
//! `eagle` is designed to be used to create your own protocol crate. We
//! recommend creating a new cargo workspace for your project with a shared
//! crate which will contain your protocol definition and individual crates
//! for the server and client.
//!
//! In your shared crate, you can define your protocol using the [`Protocol`]
//! derive macro. This will generate the necessary code for the server and
//! client to communicate.
//!
//! ```rust
//! use eagle::Protocol;
//!
//! #[derive(Protocol)]
//! pub enum Example {
//! Add((i32, i32), i32),
//! Length(String, usize),
//! /* ... */
//! }
//! ```
//!
//! The [`Protocol`] derive macro will generate all the necessary code, including
//! your handler trait, your server struct, and your client struct.
//!
//! On your server, you will need to implement the handler trait. This trait
//! describes the functions that the client can request from the server.
//!
//! ```rust
//! # use eagle::Protocol;
//! # #[derive(Protocol)]
//! # pub enum Example {
//! # Add((i32, i32), i32),
//! # Length(String, usize),
//! # /* ... */
//! # }
//! #
//! #[derive(Clone)]
//! pub struct Handler;
//! impl ExampleServerHandler for Handler {
//! async fn add(&mut self, a: i32, b: i32) -> i32 {
//! a + b
//! }
//! async fn length(&mut self, s: String) -> usize {
//! s.len()
//! }
//! /* ... */
//! }
//! ```
//!
//! To start the server, you simply need to use the generated server struct and
//! pass it your handler.
//!
//! ```no_run
//! # use eagle::Protocol;
//! # #[derive(Protocol)]
//! # pub enum Example {
//! # Add((i32, i32), i32),
//! # Length(String, usize),
//! # /* ... */
//! # }
//! #
//! # #[derive(Clone)]
//! # pub struct Handler;
//! # impl ExampleServerHandler for Handler {
//! # async fn add(&mut self, a: i32, b: i32) -> i32 {
//! # a + b
//! # }
//! # async fn length(&mut self, s: String) -> usize {
//! # s.len()
//! # }
//! # }
//! #
//! # tokio_test::block_on(async {
//! let handler = Handler;
//! let address = "127.0.0.1:12345"; // Or, if using the 'unix' feature, "/tmp/eagle.sock"
//! let server = ExampleServer::bind(handler, address).await;
//! server.close().await;
//! # });
//! ```
//! Once bound, the server will begin listening for incoming connections and
//! queries. **You must remember to use the `close` method to shut down the server.**
//!
//! On the client side, you can simply use the generated client struct to connect
//! to the server and begin sending queries.
//!
//! ```no_run
//! # use eagle::Protocol;
//! # #[derive(Protocol)]
//! # pub enum Example {
//! # Add((i32, i32), i32),
//! # Length(String, usize),
//! # /* ... */
//! # }
//! #
//! # #[derive(Clone)]
//! # pub struct Handler;
//! # impl ExampleServerHandler for Handler {
//! # async fn add(&mut self, a: i32, b: i32) -> i32 {
//! # a + b
//! # }
//! # async fn length(&mut self, s: String) -> usize {
//! # s.len()
//! # }
//! # }
//! #
//! # tokio_test::block_on(async {
//! # let handler = Handler;
//! let address = "127.0.0.1:12345"; // Or, if using the 'unix' feature, "/tmp/eagle.sock"
//! # let server = ExampleServer::bind(handler, address).await;
//! # tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; // Wait for the server to start
//! let client = ExampleClient::connect(address).await.unwrap();
//! assert_eq!(client.add(2, 5).await.unwrap(), 7);
//! # server.close().await;
//! # });
//! ```
//!
//! The client can be closed by calling the `close` method on the client struct.
//! This will abort the connection.
#![warn(missing_docs)]
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use syn::{parse2, spanned::Spanned, DeriveInput, Field, Ident}; use syn::{parse2, spanned::Spanned, DeriveInput, Field, Ident};
@@ -26,6 +158,33 @@ compile_error!("You must enable either the 'tcp' or 'unix' feature");
#[cfg(all(feature = "unix", not(unix)))] #[cfg(all(feature = "unix", not(unix)))]
compile_error!("The 'unix' feature requires compiling for a unix target"); compile_error!("The 'unix' feature requires compiling for a unix target");
/// Generate all the necessary RPC code for a protocol from an enum describing it.
///
/// This macro will generate various enums and structs to enable communication
/// between a server and a client. The following items will be generated, where {}
/// is the name of the protocol enum:
/// - `{}ServerHandler` - A trait that the server must implement to handle queries
/// - `{}Server` - A struct that the server uses to communicate with clients
/// - `{}Client` - A struct that the client uses to communicate with a server
///
/// Each variant of the passed enum represents a query that the client can send to the
/// server. The first field of each variant is the question (serverbound), the second field
/// is the answer (clientbound). You may use tuples to represent sending multiple arguments and
/// you may use the unit type `()` to represent no arguments. Only data types which implement
/// [`Clone`], [`serde::Serialize`], and [`serde::Deserialize`] can be used.
///
/// For more information on how to use the generated code, see the [crate-level documentation](index.html).
///
/// # Example
/// ```rust
/// use eagle::Protocol;
///
/// #[derive(Protocol)]
/// pub enum Example {
/// Add((i32, i32), i32),
/// Length(String, usize),
/// }
/// ```
#[proc_macro_derive(Protocol)] #[proc_macro_derive(Protocol)]
pub fn derive_protocol_derive(input: TokenStream) -> TokenStream { pub fn derive_protocol_derive(input: TokenStream) -> TokenStream {
let expanded = derive_protocol(input.into()); let expanded = derive_protocol(input.into());
@@ -34,6 +193,33 @@ pub fn derive_protocol_derive(input: TokenStream) -> TokenStream {
fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream { fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let input = parse2::<DeriveInput>(input).unwrap(); let input = parse2::<DeriveInput>(input).unwrap();
// TODO: These logs should be filterable in some way
#[cfg(feature = "log")]
#[allow(unused_variables)]
let debug = quote! { ::log::debug! };
#[cfg(feature = "log")]
#[allow(unused_variables)]
let info = quote! { ::log::info! };
#[cfg(feature = "log")]
#[allow(unused_variables)]
let warn = quote! { ::log::warn! };
#[cfg(feature = "log")]
#[allow(unused_variables)]
let error = quote! { ::log::error! };
#[cfg(not(feature = "log"))]
#[allow(unused_variables)]
let debug = quote! { ::std::eprintln! };
#[cfg(not(feature = "log"))]
#[allow(unused_variables)]
let info = quote! { ::std::eprintln! };
#[cfg(not(feature = "log"))]
#[allow(unused_variables)]
let warn = quote! { ::std::eprintln! };
#[cfg(not(feature = "log"))]
#[allow(unused_variables)]
let error = quote! { ::std::eprintln! };
// Must be on an enum // Must be on an enum
let enum_ = match &input.data { let enum_ = match &input.data {
syn::Data::Enum(e) => e, syn::Data::Enum(e) => e,
@@ -50,7 +236,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
let query_enum_name = format_ident!("__{}Query", name); let query_enum_name = format_ident!("__{}Query", name);
let queries_struct_name = format_ident!("__{}Queries", name); let queries_struct_name = format_ident!("__{}Queries", name);
let client_connection_struct_name = format_ident!("__{}Connection", name); let client_connection_struct_name = format_ident!("__{}Connection", name);
let server_trait_name = format_ident!("{}ServerTrait", name); let server_trait_name = format_ident!("{}ServerHandler", name);
let server_connection_struct_name = format_ident!("{}Server", name); let server_connection_struct_name = format_ident!("{}Server", name);
let client_struct_name = format_ident!("{}Client", name); let client_struct_name = format_ident!("{}Client", name);
@@ -102,70 +288,104 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// There is a function that must be implemented to set the answer in the query enum // There is a function that must be implemented to set the answer in the query enum
query_set_answer.push(quote! { query_set_answer.push(quote! {
#query_enum_name::#var_name(question, answer_opt) => match answer { #query_enum_name::#var_name(question, answer_opt) => match answer {
#answer_enum_name::#var_name(answer) => {answer_opt.replace(answer);}, #answer_enum_name::#var_name(answer) => {
#debug("Setting answer for query {}", stringify!(#var_name));
answer_opt.replace(answer);
},
_ => panic!("The answer for this query is not the correct type."), _ => panic!("The answer for this query is not the correct type."),
}, },
}); });
// There is a function that must be implemented to get the answer from the query enum // There is a function that must be implemented to get the answer from the query enum
query_get_answer.push(quote! { query_get_answer.push(quote! {
#query_enum_name::#var_name(_, answer) => match answer { #query_enum_name::#var_name(_, answer) => match answer {
Some(answer) => Some(#answer_enum_name::#var_name(answer.clone())), ::std::option::Option::Some(answer) => ::std::option::Option::Some(#answer_enum_name::#var_name(answer.clone())),
None => None ::std::option::Option::None => ::std::option::Option::None
}, },
}); });
// There is a function that the server uses to call the appropriate function when receiving a query // There is a function that the server uses to call the appropriate function when receiving a query
server_handler.push(quote! { server_handler.push(quote! {
#question_enum_name::#var_name(#question_tuple_args) => { #question_enum_name::#var_name(#question_tuple_args) => {
#info("Received query {}", stringify!(#var_name));
let answer = self.handler.lock().await.#var_name(#question_handler_args).await; let answer = self.handler.lock().await.#var_name(#question_handler_args).await;
return #answer_enum_name::#var_name(answer); return #answer_enum_name::#var_name(answer);
}, },
}); });
// The function that the server needs to implement // The function that the server needs to implement
server_trait.push(quote! { server_trait.push(quote! {
async fn #var_name(&mut self, #question_args) -> #answer_type; fn #var_name(&mut self, #question_args) -> impl ::std::future::Future<Output = #answer_type> + Send;
}); });
// The function that the client uses to communicate // The function that the client uses to communicate
client_impl.push(quote! { client_impl.push(quote! {
pub async fn #var_name(&self, #question_args) -> Result<#answer_type, #error_enum_name> { pub async fn #var_name(&self, #question_args) -> Result<#answer_type, #error_enum_name> {
#info("Sending query {}", stringify!(#var_name));
let nonce = self.send(#question_enum_name::#var_name(#question_tuple_args)).await?; let nonce = self.send(#question_enum_name::#var_name(#question_tuple_args)).await?;
let answer = self.recv_until(nonce).await?; let answer = self.recv_until(nonce).await?;
match answer { match answer {
#answer_enum_name::#var_name(answer) => Ok(answer), #answer_enum_name::#var_name(answer) => Ok(answer),
_ => panic!("The answer for this query is not the correct type."), _ => ::std::panic!("The answer for this query is not the correct type."),
} }
} }
}); });
// The query enum is the same as the source enum, but the second field is always wrapped in a Option<> // The query enum is the same as the source enum, but the second field is always wrapped in a Option<>
query_enum.push(quote! { query_enum.push(quote! {
#var_name(#question_field, Option<#answer_type>) #var_name(#question_field, ::std::option::Option<#answer_type>)
}); });
} }
// Create an error and result type for sending messages // Create an error and result type for sending messages
let error_enum = quote! { let error_enum = quote! {
#[derive(Debug)] #[derive(::std::fmt::Debug)]
#vis enum #error_enum_name { #vis enum #error_enum_name {
SendError(tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>), SendError(::tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>),
Closed, Closed,
} }
impl ::std::fmt::Display for #error_enum_name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match self {
#error_enum_name::SendError(e) => write!(f, "Failed to send query: {}", e),
#error_enum_name::Closed => write!(f, "Connection closed"),
}
}
}
impl ::std::error::Error for #error_enum_name {
fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> {
match self {
#error_enum_name::SendError(e) => ::std::option::Option::Some(e),
#error_enum_name::Closed => ::std::option::Option::None,
}
}
fn description(&self) -> &str {
match self {
#error_enum_name::SendError(_) => "Failed to send query",
#error_enum_name::Closed => "Connection closed",
}
}
fn cause(&self) -> ::std::option::Option<&dyn ::std::error::Error> {
match self {
#error_enum_name::SendError(e) => ::std::option::Option::Some(e),
#error_enum_name::Closed => ::std::option::Option::None,
}
}
}
}; };
// Create enums for the types of messages the server and client will use // Create enums for the types of messages the server and client will use
let answer_enum = quote! { let answer_enum = quote! {
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(::serde::Serialize, ::serde::Deserialize, ::std::clone::Clone, ::std::fmt::Debug)]
#vis enum #answer_enum_name { #vis enum #answer_enum_name {
#(#server_enum), * #(#server_enum), *,
Ready
} }
}; };
let question_enum = quote! { let question_enum = quote! {
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[derive(::serde::Serialize, ::serde::Deserialize, ::std::clone::Clone, ::std::fmt::Debug)]
#vis enum #question_enum_name { #vis enum #question_enum_name {
#(#client_enum), * #(#client_enum), *
} }
}; };
// Create an enum to represent the queries the client has sent // Create an enum to represent the queries the client has sent
let query_enum = quote! { let query_enum = quote! {
#[derive(Clone, Debug)] #[derive(::std::clone::Clone, ::std::fmt::Debug)]
#vis enum #query_enum_name { #vis enum #query_enum_name {
#(#query_enum), * #(#query_enum), *
} }
@@ -175,7 +395,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
#(#query_set_answer)* #(#query_set_answer)*
}; };
} }
pub fn get_answer(&self) -> Option<#answer_enum_name> { pub fn get_answer(&self) -> ::std::option::Option<#answer_enum_name> {
match self { match self {
#(#query_get_answer)* #(#query_get_answer)*
} }
@@ -191,17 +411,17 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
}; };
#[cfg(feature = "tcp")] #[cfg(feature = "tcp")]
let stream_type = quote! { tokio::net::TcpStream }; let stream_type = quote! { ::tokio::net::TcpStream };
#[cfg(feature = "tcp")] #[cfg(feature = "tcp")]
let stream_addr_trait = quote! { tokio::net::ToSocketAddrs }; let stream_addr_trait = quote! { ::tokio::net::ToSocketAddrs };
#[cfg(feature = "tcp")] #[cfg(feature = "tcp")]
let listener_type = quote! { tokio::net::TcpListener }; let listener_type = quote! { ::tokio::net::TcpListener };
#[cfg(feature = "unix")] #[cfg(feature = "unix")]
let stream_type = quote! { tokio::net::UnixStream }; let stream_type = quote! { ::tokio::net::UnixStream };
#[cfg(feature = "unix")] #[cfg(feature = "unix")]
let stream_addr_trait = quote! { std::convert::AsRef<std::path::Path> }; let stream_addr_trait = quote! { ::std::convert::AsRef<std::path::Path> };
#[cfg(feature = "unix")] #[cfg(feature = "unix")]
let listener_type = quote! { tokio::net::UnixListener }; let listener_type = quote! { ::tokio::net::UnixListener };
// Create a trait which the server will have to implement // Create a trait which the server will have to implement
let server_trait = quote! { let server_trait = quote! {
@@ -220,18 +440,49 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
let listener = #listener_type::bind(addr.as_ref())?; let listener = #listener_type::bind(addr.as_ref())?;
}; };
let sc_struct = quote! { let sc_struct = quote! {
#vis struct #server_connection_struct_name<H: #server_trait_name> { #[derive(Clone)]
handler: ::std::sync::Arc<tokio::sync::Mutex<H>>, #vis struct #server_connection_struct_name<H: #server_trait_name + ::std::marker::Send + ::std::clone::Clone + 'static> {
stream: #stream_type, handler: ::std::sync::Arc<::tokio::sync::Mutex<H>>,
tasks: ::std::sync::Arc<::tokio::sync::Mutex<::std::vec::Vec<tokio::task::JoinHandle<()>>>>,
} }
impl<H: #server_trait_name> #server_connection_struct_name<H> { impl<H: #server_trait_name + ::std::marker::Send + std::clone::Clone + 'static> #server_connection_struct_name<H> {
pub async fn bind<S: #stream_addr_trait>(handler: H, addr: S) -> Result<Self, std::io::Error> { pub async fn bind<A: #stream_addr_trait + ::std::marker::Send + ::std::fmt::Display + 'static>(handler: H, addr: A) -> Self {
#info("Binding server to address {}", addr);
let handler = ::std::sync::Arc::new(::tokio::sync::Mutex::new(handler));
let tasks = ::std::sync::Arc::new(::tokio::sync::Mutex::new(::std::vec::Vec::new()));
let sc = Self {
handler,
tasks,
};
let sc_clone = sc.clone();
let acc_task = ::tokio::spawn(async move {
sc_clone.accept_connections(addr).await.expect("Failed to accept connections!");
});
sc.tasks.lock().await.push(acc_task);
sc
}
pub async fn close(self) {
#info("Closing server");
for task in self.tasks.lock().await.drain(..) {
task.abort();
}
}
pub async fn accept_connections<A: #stream_addr_trait>(
&self,
addr: A,
) -> ::std::result::Result<(), ::std::io::Error> {
#listener_statement #listener_statement
loop {
let (stream, _) = listener.accept().await?; let (stream, _) = listener.accept().await?;
Ok(Self { #info("Accepted connection from {:?}", stream.peer_addr()?);
handler: ::std::sync::Arc::new(tokio::sync::Mutex::new(handler)), let self_clone = self.clone();
stream, let run_task = ::tokio::spawn(async move {
}) self_clone.run(stream).await;
});
self.tasks.lock().await.push(run_task);
}
} }
async fn handle(&self, question: #question_enum_name) -> #answer_enum_name { async fn handle(&self, question: #question_enum_name) -> #answer_enum_name {
@@ -240,35 +491,44 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
} }
} }
async fn send(&mut self, nonce: u64, answer: #answer_enum_name) { async fn send(&self, stream: &mut #stream_type, nonce: u64, answer: #answer_enum_name) {
use tokio::io::AsyncWriteExt; use ::tokio::io::AsyncWriteExt;
let serialized = ron::ser::to_string(&(nonce, answer)).expect("Failed to serialize response!"); let serialized = ron::ser::to_string(&(nonce, answer)).expect("Failed to serialize response!");
let len = serialized.len() as u32; let len = serialized.len() as u32;
self.stream.write_all(&len.to_le_bytes()).await.expect("Failed to write length!"); #debug("Sending `{}`", serialized);
self.stream.write_all(serialized.as_bytes()).await.expect("Failed to write response!"); stream.write_all(&len.to_le_bytes()).await.expect("Failed to write length!");
stream.write_all(serialized.as_bytes()).await.expect("Failed to write response!");
} }
async fn run(mut self) { async fn run(&self, mut stream: #stream_type) {
use tokio::io::AsyncWriteExt; use ::tokio::io::AsyncWriteExt;
use tokio::io::AsyncReadExt; use ::tokio::io::AsyncReadExt;
let mut buf = Vec::with_capacity(1024); let mut buf = ::std::vec::Vec::with_capacity(1024);
self.send(&mut stream, 0, #answer_enum_name::Ready).await;
loop { loop {
tokio::select! { ::tokio::select! {
Ok(_) = self.stream.readable() => { ::std::result::Result::Ok(_) = stream.readable() => {
match self.stream.try_read(&mut buf) { let mut read_buf = [0; 1024];
Ok(0) => break, // Stream closed match stream.try_read(&mut read_buf) {
Ok(n) => { ::std::result::Result::Ok(0) => break, // Stream closed
// TODO: This doesn't cope with partial reads, we will handle that later ::std::result::Result::Ok(n) => {
#debug("Received {} bytes (server)", n);
buf.extend_from_slice(&read_buf[..n]);
while buf.len() >= 4 {
let len = u32::from_le_bytes(buf[..4].try_into().expect("Failed to convert bytes to u32")); let len = u32::from_le_bytes(buf[..4].try_into().expect("Failed to convert bytes to u32"));
let serialized = std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string"); if buf.len() >= (4 + len as usize) {
let question: (u64, #question_enum_name) = ron::de::from_str(serialized).expect("Failed to deserialize response!"); let serialized = ::std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string");
// TODO: This should ideally be done in a separate task but that's not let (nonce, question): (u64, #question_enum_name) = ::ron::de::from_str(serialized).expect("Failed to deserialize query!");
// necessary for now let answer = self.handle(question).await;
let answer = self.handle(question.1).await; self.send(&mut stream, nonce, answer).await;
self.send(question.0, answer).await; buf.drain(0..(4 + len as usize));
} else {
break;
}
}
}, },
Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; }, ::std::result::Result::Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; },
Err(e) => eprintln!("Error reading from stream: {:?}", e), ::std::result::Result::Err(e) => ::std::eprintln!("Error reading from stream: {:?}", e),
} }
} }
} }
@@ -296,12 +556,12 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
self.queries.lock().unwrap().insert(nonce, query); self.queries.lock().unwrap().insert(nonce, query);
} }
pub fn get(&self, nonce: &u64) -> Option<#query_enum_name> { pub fn get(&self, nonce: &u64) -> ::std::option::Option<#query_enum_name> {
self.queries.lock().unwrap().get(nonce).cloned() self.queries.lock().unwrap().get(nonce).cloned()
} }
pub fn set_answer(&self, nonce: u64, answer: #answer_enum_name) { pub fn set_answer(&self, nonce: u64, answer: #answer_enum_name) {
if let Some(query) = self.queries.lock().unwrap().get_mut(&nonce) { if let ::std::option::Option::Some(query) = self.queries.lock().unwrap().get_mut(&nonce) {
query.set_answer(answer); query.set_answer(answer);
} }
} }
@@ -315,48 +575,65 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// Create a struct to handle the connection from the client to the server // Create a struct to handle the connection from the client to the server
let cc_struct = quote! { let cc_struct = quote! {
struct #client_connection_struct_name { struct #client_connection_struct_name {
to_send: tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>, to_send: ::tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>,
received: tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>, received: ::tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>,
ready: ::std::sync::Arc<tokio::sync::Notify>,
stream: #stream_type, stream: #stream_type,
} }
impl #client_connection_struct_name { impl #client_connection_struct_name {
pub fn new( pub fn new(
to_send: tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>, to_send: ::tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>,
received: tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>, received: ::tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>,
ready: ::std::sync::Arc<::tokio::sync::Notify>,
stream: #stream_type, stream: #stream_type,
) -> Self { ) -> Self {
Self { Self {
to_send, to_send,
received, received,
ready,
stream, stream,
} }
} }
pub async fn run(mut self) { pub async fn run(mut self) {
use tokio::io::AsyncWriteExt; use ::tokio::io::AsyncWriteExt;
use tokio::io::AsyncReadExt; use ::tokio::io::AsyncReadExt;
let mut buf = Vec::with_capacity(1024); let mut buf = ::std::vec::Vec::with_capacity(1024);
loop { loop {
tokio::select! { ::tokio::select! {
Some(msg) = self.to_send.recv() => { ::std::option::Option::Some(msg) = self.to_send.recv() => {
let serialized = ron::ser::to_string(&msg).expect("Failed to serialize query!"); let serialized = ron::ser::to_string(&msg).expect("Failed to serialize query!");
let len = serialized.len() as u32; let len = serialized.len() as u32;
#debug("Sending `{}`", serialized);
self.stream.write_all(&len.to_le_bytes()).await.expect("Failed to write length!"); self.stream.write_all(&len.to_le_bytes()).await.expect("Failed to write length!");
self.stream.write_all(serialized.as_bytes()).await.expect("Failed to write query!"); self.stream.write_all(serialized.as_bytes()).await.expect("Failed to write query!");
}, },
Ok(_) = self.stream.readable() => { ::std::result::Result::Ok(_) = self.stream.readable() => {
match self.stream.try_read(&mut buf) { let mut read_buf = [0; 1024];
Ok(0) => break, // Stream closed match self.stream.try_read(&mut read_buf) {
Ok(n) => { ::std::result::Result::Ok(0) => { break; },
// TODO: This doesn't cope with partial reads, we will handle that later ::std::result::Result::Ok(n) => {
#debug("Received {} bytes (client)", n);
buf.extend_from_slice(&read_buf[..n]);
while buf.len() >= 4 {
let len = u32::from_le_bytes(buf[..4].try_into().expect("Failed to convert bytes to u32")); let len = u32::from_le_bytes(buf[..4].try_into().expect("Failed to convert bytes to u32"));
if buf.len() >= (4 + len as usize) {
let serialized = std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string"); let serialized = std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string");
let response: (u64, #answer_enum_name) = ron::de::from_str(serialized).expect("Failed to deserialize response!"); let response: (u64, #answer_enum_name) = ron::de::from_str(serialized).expect("Failed to deserialize response!");
if let #answer_enum_name::Ready = response.1 {
#debug("Received ready signal");
self.ready.notify_one();
} else {
self.received.send(response).await.expect("Failed to send response!"); self.received.send(response).await.expect("Failed to send response!");
buf.clear(); }
buf.drain(0..(4 + len as usize));
} else {
break;
}
}
}, },
Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; }, ::std::result::Result::Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; },
Err(e) => eprintln!("Error reading from stream: {:?}", e), ::std::result::Result::Err(e) => eprintln!("Error reading from stream: {:?}", e),
} }
} }
} }
@@ -367,80 +644,100 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// Create a struct which the client will use to communicate // Create a struct which the client will use to communicate
let client_recv_queue_wrapper = format_ident!("__{}RecvQueueWrapper", name); let client_recv_queue_wrapper = format_ident!("__{}RecvQueueWrapper", name);
let client_struct = quote! { let client_struct = quote! {
#[derive(Clone)] #[derive(::std::clone::Clone)]
struct #client_recv_queue_wrapper { struct #client_recv_queue_wrapper {
recv_queue: ::std::sync::Arc<::tokio::sync::Mutex<tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>>>, recv_queue: ::std::sync::Arc<::tokio::sync::Mutex<::tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>>>,
} }
impl #client_recv_queue_wrapper { impl #client_recv_queue_wrapper {
fn new(recv_queue: tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>) -> Self { fn new(recv_queue: ::tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>) -> Self {
Self { Self {
recv_queue: ::std::sync::Arc::new(::tokio::sync::Mutex::new(recv_queue)), recv_queue: ::std::sync::Arc::new(::tokio::sync::Mutex::new(recv_queue)),
} }
} }
async fn recv(&self) -> Option<(u64, #answer_enum_name)> { async fn recv(&self) -> ::std::option::Option<(u64, #answer_enum_name)> {
self.recv_queue.lock().await.recv().await self.recv_queue.lock().await.recv().await
} }
} }
#[derive(Clone)] #[derive(Clone)]
#vis struct #client_struct_name { #vis struct #client_struct_name {
queries: #queries_struct_name, queries: #queries_struct_name,
send_queue: tokio::sync::mpsc::Sender<(u64, #question_enum_name)>, send_queue: ::tokio::sync::mpsc::Sender<(u64, #question_enum_name)>,
recv_queue: #client_recv_queue_wrapper, recv_queue: #client_recv_queue_wrapper,
connection_task: Option<::std::sync::Arc<tokio::task::JoinHandle<()>>>, ready: ::std::sync::Arc<tokio::sync::Mutex<bool>>,
ready_notify: ::std::sync::Arc<tokio::sync::Notify>,
connection_task: ::std::option::Option<::std::sync::Arc<tokio::task::JoinHandle<()>>>,
} }
impl #client_struct_name { impl #client_struct_name {
pub fn new(send_queue: tokio::sync::mpsc::Sender<(u64, #question_enum_name)>, pub fn new(send_queue: ::tokio::sync::mpsc::Sender<(u64, #question_enum_name)>,
recv_queue: tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>, recv_queue: ::tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>,
connection_task: Option<::std::sync::Arc<tokio::task::JoinHandle<()>>>) -> Self { connection_task: ::std::option::Option<::std::sync::Arc<tokio::task::JoinHandle<()>>>,
ready_notify: ::std::sync::Arc<tokio::sync::Notify>) -> Self {
Self { Self {
queries: #queries_struct_name::new(), queries: #queries_struct_name::new(),
recv_queue: #client_recv_queue_wrapper::new(recv_queue), recv_queue: #client_recv_queue_wrapper::new(recv_queue),
ready: ::std::sync::Arc::new(false.into()),
ready_notify,
send_queue, send_queue,
connection_task, connection_task,
} }
} }
pub async fn connect<A: #stream_addr_trait>(addr: A) -> Result<Self, std::io::Error> { pub async fn connect<A: #stream_addr_trait + ::std::fmt::Display>(addr: A) -> Result<Self, std::io::Error> {
#info("Connecting to server at address {}", addr);
let stream = #stream_type::connect(addr).await?; let stream = #stream_type::connect(addr).await?;
let (send_queue, to_send) = tokio::sync::mpsc::channel(16); let (send_queue, to_send) = ::tokio::sync::mpsc::channel(16);
let (to_recv, recv_queue) = tokio::sync::mpsc::channel(16); let (to_recv, recv_queue) = ::tokio::sync::mpsc::channel(16);
let connection = #client_connection_struct_name::new(to_send, to_recv, stream); let ready_notify = ::std::sync::Arc::new(tokio::sync::Notify::new());
let connection_task = tokio::spawn(connection.run()); let connection = #client_connection_struct_name::new(to_send, to_recv, ready_notify.clone(), stream);
Ok(Self::new(send_queue, recv_queue, Some(::std::sync::Arc::new(connection_task)))) let connection_task = ::tokio::spawn(connection.run());
Ok(Self::new(send_queue, recv_queue, ::std::option::Option::Some(::std::sync::Arc::new(connection_task)), ready_notify))
} }
pub fn close(self) { pub fn close(&mut self) {
if let Some(task) = self.connection_task { if let ::std::option::Option::Some(task) = self.connection_task.take() {
task.abort(); task.abort();
} }
} }
async fn send(&self, query: #question_enum_name) -> Result<u64, #error_enum_name> { async fn send(&self, query: #question_enum_name) -> ::std::result::Result<u64, #error_enum_name> {
// Wait until the connection is ready
if !*self.ready.lock().await {
self.ready_notify.notified().await;
let mut ready = self.ready.lock().await;
*ready = true;
}
let nonce = self.queries.len() as u64; let nonce = self.queries.len() as u64;
let res = self.send_queue.send((nonce, query.clone())).await; let res = self.send_queue.send((nonce, query.clone())).await;
match res { match res {
Ok(_) => { ::std::result::Result::Ok(_) => {
self.queries.insert(nonce, query.into()); self.queries.insert(nonce, query.into());
Ok(nonce) ::std::result::Result::Ok(nonce)
} }
Err(e) => Err(#error_enum_name::SendError(e)), ::std::result::Result::Err(e) => ::std::result::Result::Err(#error_enum_name::SendError(e)),
} }
} }
async fn recv_until(&self, id: u64) -> Result<#answer_enum_name, #error_enum_name> { async fn recv_until(&self, id: u64) -> ::std::result::Result<#answer_enum_name, #error_enum_name> {
loop { loop {
// Check if we've received the answer for the query we're looking for // Check if we've received the answer for the query we're looking for
if let Some(query) = self.queries.get(&id) { if let ::std::option::Option::Some(query) = self.queries.get(&id) {
if let Some(answer) = query.get_answer() { if let ::std::option::Option::Some(answer) = query.get_answer() {
#info("Found answer for query {}", id);
return Ok(answer); return Ok(answer);
} }
} }
match self.recv_queue.recv().await { match self.recv_queue.recv().await {
Some((nonce, answer)) => { ::std::option::Option::Some((nonce, answer)) => {
#info("Received answer for query {}", nonce);
self.queries.set_answer(nonce, answer.clone()); self.queries.set_answer(nonce, answer.clone());
} }
None => return Err(#error_enum_name::Closed), ::std::option::Option::None => return ::std::result::Result::Err(#error_enum_name::Closed),
}; };
} }
} }
#(#client_impl)* #(#client_impl)*
} }
impl ::std::ops::Drop for #client_struct_name {
fn drop(&mut self) {
self.close();
}
}
}; };
let expanded = quote! { let expanded = quote! {

135
tests/client.rs Normal file
View File

@@ -0,0 +1,135 @@
/*
Eagle - A simple library for RPC in Rust
Copyright (c) 2024 KodiCraft
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use eagle::Protocol;
use env_logger::{Builder, Env};
use std::sync::Arc;
use std::sync::Once;
use tokio::sync::{
mpsc::{self, Receiver, Sender},
Notify,
};
static INIT: Once = Once::new();
pub fn init_logger() {
INIT.call_once(|| {
let env = Env::default()
.filter_or("RUST_LOG", "info")
.write_style_or("LOG_STYLE", "always");
Builder::from_env(env).format_timestamp_nanos().init();
});
}
#[derive(Protocol)]
enum TestProtocol {
Addition((i32, i32), i32),
SomeKindOfQuestion(String, i32),
ThisRespondsWithAString(i32, String),
Void((), ()),
}
#[tokio::test]
async fn client() {
init_logger();
let (qtx, qrx) = mpsc::channel(16);
let (atx, arx) = mpsc::channel(16);
let ready_notify = Arc::new(Notify::new());
let client = TestProtocolClient::new(qtx, arx, None, ready_notify.clone());
ready_notify.notify_one();
let server = tokio::spawn(server_loop(qrx, atx));
let result = client.addition(2, 5).await.unwrap();
assert_eq!(result, 7);
let result = client
.some_kind_of_question("Hello, world!".to_string())
.await
.unwrap();
assert_eq!(result, "Hello, world!".len() as i32);
let result = client.this_responds_with_a_string(42).await.unwrap();
assert_eq!(result, "The number is 42");
client.void().await.unwrap();
server.abort();
}
async fn server_loop(
mut qrx: Receiver<(u64, __TestProtocolQuestion)>,
atx: Sender<(u64, __TestProtocolAnswer)>,
) {
loop {
if let Some((nonce, q)) = qrx.recv().await {
match q {
__TestProtocolQuestion::addition((a, b)) => {
atx.send((nonce, __TestProtocolAnswer::addition(a + b)))
.await
.unwrap();
}
__TestProtocolQuestion::some_kind_of_question(s) => {
atx.send((
nonce,
__TestProtocolAnswer::some_kind_of_question(s.len() as i32),
))
.await
.unwrap();
}
__TestProtocolQuestion::this_responds_with_a_string(i) => {
atx.send((
nonce,
__TestProtocolAnswer::this_responds_with_a_string(format!(
"The number is {}",
i
)),
))
.await
.unwrap();
}
__TestProtocolQuestion::void(()) => {
println!("Received void question");
atx.send((nonce, __TestProtocolAnswer::void(())))
.await
.unwrap();
}
}
}
}
}
#[tokio::test]
async fn heavy_async() {
init_logger();
let (qtx, qrx) = mpsc::channel(16);
let (atx, arx) = mpsc::channel(16);
let ready_notify = Arc::new(Notify::new());
let client = TestProtocolClient::new(qtx, arx, None, ready_notify.clone());
ready_notify.notify_one();
let server = tokio::spawn(server_loop(qrx, atx));
let mut tasks = Vec::new();
for i in 0..100 {
let client = client.clone();
tasks.push(tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(
rand::random::<u64>() % 100,
))
.await;
let result = client.addition(i, i).await.unwrap();
assert_eq!(result, i + i);
}));
}
for task in tasks {
task.await.unwrap();
}
server.abort();
}

93
tests/full.rs Normal file
View File

@@ -0,0 +1,93 @@
/*
Eagle - A simple library for RPC in Rust
Copyright (c) 2024 KodiCraft
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use eagle::Protocol;
use env_logger::{Builder, Env};
use std::sync::Once;
static INIT: Once = Once::new();
pub fn init_logger() {
INIT.call_once(|| {
let env = Env::default()
.filter_or("RUST_LOG", "info")
.write_style_or("LOG_STYLE", "always");
Builder::from_env(env).format_timestamp_nanos().init();
});
}
#[derive(Protocol)]
enum TestProtocol {
Addition((i32, i32), i32),
SomeKindOfQuestion(String, i32),
ThisRespondsWithAString(i32, String),
Void((), ()),
}
#[derive(Clone)]
struct TrivialServer;
impl TestProtocolServerHandler for TrivialServer {
async fn addition(&mut self, a: i32, b: i32) -> i32 {
a + b
}
async fn some_kind_of_question(&mut self, s: String) -> i32 {
s.len() as i32
}
async fn this_responds_with_a_string(&mut self, i: i32) -> String {
format!("The number is {}", i)
}
async fn void(&mut self) {}
}
struct Cleanup {
address: String,
}
impl Drop for Cleanup {
fn drop(&mut self) {
std::fs::remove_file(&self.address).unwrap();
}
}
#[tokio::test]
async fn e2e() {
init_logger();
#[cfg(feature = "unix")]
let address = format!("/tmp/eagle-test-{}.sock", rand::random::<u64>());
#[cfg(feature = "unix")]
let _cleanup = Cleanup {
address: address.clone(),
};
#[cfg(feature = "tcp")]
let address = format!("127.0.0.1:{}", 10000 + rand::random::<u64>() % 1000);
let server = TestProtocolServer::bind(TrivialServer, address.clone()).await;
tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; // Wait for the server to start
let client = TestProtocolClient::connect(address).await.unwrap();
assert_eq!(client.addition(2, 5).await.unwrap(), 7);
assert_eq!(
client
.some_kind_of_question("Hello, world!".to_string())
.await
.unwrap(),
"Hello, world!".len() as i32
);
assert_eq!(
client.this_responds_with_a_string(42).await.unwrap(),
"The number is 42"
);
client.void().await.unwrap();
server.close().await;
}

View File

@@ -1,112 +1 @@
/* mod client;
Eagle - A library for easy communication in full-stack Rust applications
Copyright (c) 2024 KodiCraft
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use eagle::Protocol;
use tokio::sync::mpsc::{self, Receiver, Sender};
#[derive(Protocol)]
enum TestProtocol {
Addition((i32, i32), i32),
SomeKindOfQuestion(String, i32),
ThisRespondsWithAString(i32, String),
Void((), ()),
}
#[tokio::test]
async fn main() {
let (qtx, qrx) = mpsc::channel(16);
let (atx, arx) = mpsc::channel(16);
let client = TestProtocolClient::new(qtx, arx, None);
let server = tokio::spawn(server_loop(qrx, atx));
let result = client.addition(2, 5).await.unwrap();
assert_eq!(result, 7);
let result = client
.some_kind_of_question("Hello, world!".to_string())
.await
.unwrap();
assert_eq!(result, "Hello, world!".len() as i32);
let result = client.this_responds_with_a_string(42).await.unwrap();
assert_eq!(result, "The number is 42");
client.void().await.unwrap();
server.abort();
}
async fn server_loop(
mut qrx: Receiver<(u64, __TestProtocolQuestion)>,
atx: Sender<(u64, __TestProtocolAnswer)>,
) {
loop {
if let Some((nonce, q)) = qrx.recv().await {
match q {
__TestProtocolQuestion::addition((a, b)) => {
atx.send((nonce, __TestProtocolAnswer::addition(a + b)))
.await
.unwrap();
}
__TestProtocolQuestion::some_kind_of_question(s) => {
atx.send((
nonce,
__TestProtocolAnswer::some_kind_of_question(s.len() as i32),
))
.await
.unwrap();
}
__TestProtocolQuestion::this_responds_with_a_string(i) => {
atx.send((
nonce,
__TestProtocolAnswer::this_responds_with_a_string(format!(
"The number is {}",
i
)),
))
.await
.unwrap();
}
__TestProtocolQuestion::void(()) => {
println!("Received void question");
atx.send((nonce, __TestProtocolAnswer::void(())))
.await
.unwrap();
}
}
}
}
}
#[tokio::test]
async fn heavy_async() {
let (qtx, qrx) = mpsc::channel(16);
let (atx, arx) = mpsc::channel(16);
let client = TestProtocolClient::new(qtx, arx, None);
let server = tokio::spawn(server_loop(qrx, atx));
let mut tasks = Vec::new();
for i in 0..100 {
let client = client.clone();
tasks.push(tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_millis(
rand::random::<u64>() % 100,
))
.await;
let result = client.addition(i, i).await.unwrap();
assert_eq!(result, i + i);
}));
}
for task in tasks {
task.await.unwrap();
}
server.abort();
}