29 Commits

Author SHA1 Message Date
62282eacd5 Fix compile error due to attempting to derive incorrect trait
All checks were successful
Build library & run tests / build (unix) (push) Successful in 1m27s
Build library & run tests / build (tcp) (push) Successful in 1m28s
Build library & run tests / docs (push) Successful in 1m28s
2024-07-05 13:13:23 +02:00
a5c975d113 Attempt to change the way errors are created
Some checks failed
Build library & run tests / docs (push) Failing after 2m31s
Build library & run tests / build (tcp) (push) Failing after 2m41s
Build library & run tests / build (unix) (push) Failing after 2m42s
2024-07-05 13:09:31 +02:00
ef1faf5bd9 Barely better error reporting 2024-06-26 11:28:14 +02:00
d47f62cdbb Attempt to deal with task/memory leaks
All checks were successful
Build library & run tests / docs (push) Successful in 2m20s
Build library & run tests / build (unix) (push) Successful in 2m23s
Build library & run tests / build (tcp) (push) Successful in 2m27s
2024-06-26 11:16:20 +02:00
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
11 changed files with 1681 additions and 262 deletions

2
.cargo/config.toml Normal file
View File

@@ -0,0 +1,2 @@
[build]
rustflags = ["--cfg", "tokio_unstable"]

View File

@@ -16,3 +16,15 @@ jobs:
run: nix build .#clippy_${{ matrix.feature }}
- name: Build & test
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

920
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,27 +1,35 @@
[package]
name = "eagle"
version = "0.2.0"
edition = "2021"
version = "0.3.0"
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"
publish = ["gitea"]
edition = "2021"
resolver = "2"
[features]
default = ["tcp"]
tcp = ["tokio/net"]
unix = ["tokio/net"]
default = ["tcp", "log"]
tcp = []
unix = []
log = []
[dependencies]
proc-macro2 = "1.0.85"
quote = "1.0.36"
rand = "0.8.5"
ron = "0.8.1"
serde = { version = "1.0.203", features = ["serde_derive"] }
syn = "2.0.66"
tokio = { version = "1.38.0", features = ["sync", "io-util"] }
[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-test = "0.4.4"
env_logger = { version = "0.11.3" }
log = { version = "0.4.21" }
console-subscriber = "0.3.0"
thiserror = "1.0.61"
[lib]
proc-macro = true

View File

@@ -1,58 +1,89 @@
# 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?
Eagle is a library designed to make "full-stack" applications with Rust. It allows you to define a communication protocol
based on simple "questions" and "answers" which can be implemented as simple functions. From the perspective of the client
(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.
Eagle is a library which allows you to easily build an [RPC](https://en.wikipedia.org/wiki/Remote_procedure_call) protocol.
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.
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
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
define your protocol as an enum:
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.
```rs
Inside this crate, you can define your protocol as an enum:
```rust
use eagle::Protocol;
use serde::{Serialize, Deserialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct ExampleStruct {
a: i32,
b: i32
}
#[derive(Protocol)]
pub enum TestProtocol {
pub enum Example {
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
use shared::TestProtocolServer;
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.
pub struct Server;
impl TestProtocolServer for Server {
fn addition(&mut self, a: i32, b: i32) -> i32 {
Once your protocol is defined, you can implement it on your server. To do so, you must first implement a handler for your
protocol. A handler must implement [`Clone`] as well as the `ServerHandler` trait for your protocol. For the above example:
```rust
struct ExampleHandler {
state: i32
}
impl ExampleServerHandler for ExampleHandler {
async fn addition(&mut self, a: i32, b: i32) -> i32 {
a + b
}
fn some_kind_of_question(&mut self, question: String) -> i32 {
42
async fn get_state(&mut self) -> i32 {
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
use shared::TestProtocolClient;
```rust
use shared::ExampleServer;
#[tokio::main]
async fn main() {
let client = TestProtocolClient::new();
assert_eq!(client.addition(2, 2).await, 4);
}
let handler = ExampleHandler { state: 0 };
let server_task = ExampleServer::bind(handler, "127.0.0.1:1234").await;
// Or, if you're using the 'unix' feature...
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

View File

@@ -14,12 +14,29 @@
naersk-lib = pkgs.callPackage naersk {};
in
{
packages = {
packages = rec {
all = pkgs.stdenv.mkDerivation rec {
name = "all";
buildInputs = [
default
doc
unix
tcp
clippy_unix
clippy_tcp
];
};
default = naersk-lib.buildPackage {
src = ./.;
doCheck = true;
mode = "test";
};
doc = naersk-lib.buildPackage {
src = ./.;
doDoc = true;
mode = "test";
cargoDocOptions = x: x ++ ["--no-deps"];
};
unix = naersk-lib.buildPackage {
src = ./.;
doCheck = true;
@@ -49,6 +66,7 @@
};
devShell = with pkgs; mkShell {
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
This program is free software: you can redistribute it and/or modify
@@ -15,6 +15,133 @@ 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/>.
*/
//! # 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;
//! # });
//! ```
//! Once bound, the server will begin listening for incoming connections and
//! queries.
//!
//! 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);
//! # });
//! ```
#![warn(missing_docs)]
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::{parse2, spanned::Spanned, DeriveInput, Field, Ident};
@@ -26,6 +153,33 @@ compile_error!("You must enable either the 'tcp' or 'unix' feature");
#[cfg(all(feature = "unix", not(unix)))]
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)]
pub fn derive_protocol_derive(input: TokenStream) -> TokenStream {
let expanded = derive_protocol(input.into());
@@ -34,6 +188,33 @@ pub fn derive_protocol_derive(input: TokenStream) -> TokenStream {
fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
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
let enum_ = match &input.data {
syn::Data::Enum(e) => e,
@@ -50,7 +231,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
let query_enum_name = format_ident!("__{}Query", name);
let queries_struct_name = format_ident!("__{}Queries", 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 client_struct_name = format_ident!("{}Client", name);
@@ -68,6 +249,29 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
let mut query_set_answer = Vec::new();
let mut query_get_answer = Vec::new();
// TODO: This should just be regular code!
let join_handle_guard_name = format_ident!("__{}JoinHandleGuard", name);
let join_handle_guard = quote! {
struct #join_handle_guard_name<T: ::std::fmt::Debug>(::tokio::task::JoinHandle<T>);
impl<T: ::std::fmt::Debug> From<::tokio::task::JoinHandle<T>> for #join_handle_guard_name<T> {
fn from(handle: ::tokio::task::JoinHandle<T>) -> Self {
#debug("Creating join handle guard for task {:?}", handle);
Self(handle)
}
}
impl #join_handle_guard_name<()> {
pub fn abort(self) {
self.0.abort();
}
}
impl<T: ::std::fmt::Debug> Drop for #join_handle_guard_name<T> {
fn drop(&mut self) {
#debug("Dropping join handle guard for task {:?}", self.0);
self.0.abort();
}
}
};
for variant in &enum_.variants {
// Every variant must have 2 fields
// The first field is the question (serverbound), the second field is the answer (clientbound)
@@ -102,70 +306,78 @@ 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
query_set_answer.push(quote! {
#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."),
},
});
// There is a function that must be implemented to get the answer from the query enum
query_get_answer.push(quote! {
#query_enum_name::#var_name(_, answer) => match answer {
Some(answer) => Some(#answer_enum_name::#var_name(answer.clone())),
None => None
::std::option::Option::Some(answer) => ::std::option::Option::Some(#answer_enum_name::#var_name(answer.clone())),
::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
server_handler.push(quote! {
#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;
return #answer_enum_name::#var_name(answer);
},
});
// The function that the server needs to implement
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
client_impl.push(quote! {
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 answer = self.recv_until(nonce).await?;
match 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<>
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
let error_enum = quote! {
#[derive(Debug)]
#[derive(::std::fmt::Debug, ::std::clone::Clone, ::thiserror::Error)]
#vis enum #error_enum_name {
SendError(tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>),
#[error("Failed to send query: {0}")]
SendError(::tokio::sync::mpsc::error::SendError<(u64, #question_enum_name)>),
#[error("Connection to sender thread closed")]
Closed,
}
};
// Create enums for the types of messages the server and client will use
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 {
#(#server_enum), *
#(#server_enum), *,
Ready
}
};
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 {
#(#client_enum), *
}
};
// Create an enum to represent the queries the client has sent
let query_enum = quote! {
#[derive(Clone, Debug)]
#[derive(::std::clone::Clone, ::std::fmt::Debug)]
#vis enum #query_enum_name {
#(#query_enum), *
}
@@ -175,7 +387,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
#(#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 {
#(#query_get_answer)*
}
@@ -191,17 +403,17 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
};
#[cfg(feature = "tcp")]
let stream_type = quote! { tokio::net::TcpStream };
let stream_type = quote! { ::tokio::net::TcpStream };
#[cfg(feature = "tcp")]
let stream_addr_trait = quote! { tokio::net::ToSocketAddrs };
let stream_addr_trait = quote! { ::tokio::net::ToSocketAddrs };
#[cfg(feature = "tcp")]
let listener_type = quote! { tokio::net::TcpListener };
let listener_type = quote! { ::tokio::net::TcpListener };
#[cfg(feature = "unix")]
let stream_type = quote! { tokio::net::UnixStream };
let stream_type = quote! { ::tokio::net::UnixStream };
#[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")]
let listener_type = quote! { tokio::net::UnixListener };
let listener_type = quote! { ::tokio::net::UnixListener };
// Create a trait which the server will have to implement
let server_trait = quote! {
@@ -220,18 +432,43 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
let listener = #listener_type::bind(addr.as_ref())?;
};
let sc_struct = quote! {
#vis struct #server_connection_struct_name<H: #server_trait_name> {
handler: ::std::sync::Arc<tokio::sync::Mutex<H>>,
stream: #stream_type,
#[derive(Clone)]
#vis struct #server_connection_struct_name<H: #server_trait_name + ::std::marker::Send + ::std::clone::Clone + 'static> {
handler: ::std::sync::Arc<::tokio::sync::Mutex<H>>,
tasks: ::std::sync::Arc<::tokio::sync::Mutex<::std::vec::Vec<#join_handle_guard_name<()>>>>,
}
impl<H: #server_trait_name> #server_connection_struct_name<H> {
pub async fn bind<S: #stream_addr_trait>(handler: H, addr: S) -> Result<Self, std::io::Error> {
impl<H: #server_trait_name + ::std::marker::Send + std::clone::Clone + 'static> #server_connection_struct_name<H> {
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.into());
sc
}
pub async fn accept_connections<A: #stream_addr_trait>(
&self,
addr: A,
) -> ::std::result::Result<(), ::std::io::Error> {
#listener_statement
#info("Listening for clients on {:?}", listener.local_addr()?);
loop {
let (stream, _) = listener.accept().await?;
Ok(Self {
handler: ::std::sync::Arc::new(tokio::sync::Mutex::new(handler)),
stream,
})
#info("Accepted connection from {:?}", stream.peer_addr()?);
let self_clone = self.clone();
let run_task = ::tokio::spawn(async move {
self_clone.run(stream).await;
});
self.tasks.lock().await.push(run_task.into());
}
}
async fn handle(&self, question: #question_enum_name) -> #answer_enum_name {
@@ -240,35 +477,44 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
}
}
async fn send(&mut self, nonce: u64, answer: #answer_enum_name) {
use tokio::io::AsyncWriteExt;
async fn send(&self, stream: &mut #stream_type, nonce: u64, answer: #answer_enum_name) {
use ::tokio::io::AsyncWriteExt;
let serialized = ron::ser::to_string(&(nonce, answer)).expect("Failed to serialize response!");
let len = serialized.len() as u32;
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 response!");
#debug("Sending `{}`", serialized);
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) {
use tokio::io::AsyncWriteExt;
use tokio::io::AsyncReadExt;
let mut buf = Vec::with_capacity(1024);
async fn run(&self, mut stream: #stream_type) {
use ::tokio::io::AsyncWriteExt;
use ::tokio::io::AsyncReadExt;
let mut buf = ::std::vec::Vec::with_capacity(1024);
self.send(&mut stream, 0, #answer_enum_name::Ready).await;
loop {
tokio::select! {
Ok(_) = self.stream.readable() => {
match self.stream.try_read(&mut buf) {
Ok(0) => break, // Stream closed
Ok(n) => {
// TODO: This doesn't cope with partial reads, we will handle that later
::tokio::select! {
::std::result::Result::Ok(_) = stream.readable() => {
let mut read_buf = [0; 1024];
match stream.try_read(&mut read_buf) {
::std::result::Result::Ok(0) => break, // Stream closed
::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 serialized = std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string");
let question: (u64, #question_enum_name) = ron::de::from_str(serialized).expect("Failed to deserialize response!");
// TODO: This should ideally be done in a separate task but that's not
// necessary for now
let answer = self.handle(question.1).await;
self.send(question.0, answer).await;
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 (nonce, question): (u64, #question_enum_name) = ::ron::de::from_str(serialized).expect("Failed to deserialize query!");
let answer = self.handle(question).await;
self.send(&mut stream, nonce, answer).await;
buf.drain(0..(4 + len as usize));
} else {
break;
}
}
},
Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; },
Err(e) => eprintln!("Error reading from stream: {:?}", e),
::std::result::Result::Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; },
::std::result::Result::Err(e) => ::std::eprintln!("Error reading from stream: {:?}", e),
}
}
}
@@ -293,21 +539,21 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
}
pub fn insert(&self, nonce: u64, query: #query_enum_name) {
self.queries.lock().unwrap().insert(nonce, query);
self.queries.lock().expect("Could not insert new query; Mutex has been poisoned.").insert(nonce, query);
}
pub fn get(&self, nonce: &u64) -> Option<#query_enum_name> {
self.queries.lock().unwrap().get(nonce).cloned()
pub fn get(&self, nonce: &u64) -> ::std::option::Option<#query_enum_name> {
self.queries.lock().expect("Could not get query; Mutex has been poisoned.").get(nonce).cloned()
}
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().expect("Could not set answer to query; Mutex has been poisoned.").get_mut(&nonce) {
query.set_answer(answer);
}
}
pub fn len(&self) -> usize {
self.queries.lock().unwrap().len()
self.queries.lock().expect("Could not get query count; Mutex has been poisoned.").len()
}
}
};
@@ -315,48 +561,70 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// Create a struct to handle the connection from the client to the server
let cc_struct = quote! {
struct #client_connection_struct_name {
to_send: tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>,
received: tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>,
to_send: ::tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>,
received: ::tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>,
ready: ::std::sync::Arc<tokio::sync::Notify>,
stream: #stream_type,
}
impl #client_connection_struct_name {
pub fn new(
to_send: tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>,
received: tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>,
to_send: ::tokio::sync::mpsc::Receiver<(u64, #question_enum_name)>,
received: ::tokio::sync::mpsc::Sender<(u64, #answer_enum_name)>,
ready: ::std::sync::Arc<::tokio::sync::Notify>,
stream: #stream_type,
) -> Self {
Self {
to_send,
received,
ready,
stream,
}
}
pub async fn run(mut self) {
use tokio::io::AsyncWriteExt;
use tokio::io::AsyncReadExt;
let mut buf = Vec::with_capacity(1024);
use ::tokio::io::AsyncWriteExt;
use ::tokio::io::AsyncReadExt;
let mut buf = ::std::vec::Vec::with_capacity(1024);
loop {
tokio::select! {
Some(msg) = self.to_send.recv() => {
::tokio::select! {
::std::option::Option::Some(msg) = self.to_send.recv() => {
let serialized = ron::ser::to_string(&msg).expect("Failed to serialize query!");
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(serialized.as_bytes()).await.expect("Failed to write query!");
},
Ok(_) = self.stream.readable() => {
match self.stream.try_read(&mut buf) {
Ok(0) => break, // Stream closed
Ok(n) => {
// TODO: This doesn't cope with partial reads, we will handle that later
::std::result::Result::Ok(_) = self.stream.readable() => {
let mut read_buf = [0; 1024];
match self.stream.try_read(&mut read_buf) {
::std::result::Result::Ok(0) => { break; },
::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"));
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 response: (u64, #answer_enum_name) = ron::de::from_str(serialized).expect("Failed to deserialize response!");
self.received.send(response).await.expect("Failed to send response!");
buf.clear();
if let #answer_enum_name::Ready = response.1 {
#debug("Received ready signal");
self.ready.notify_one();
} else {
match self.received.send(response).await {
::std::result::Result::Ok(_) => {},
::std::result::Result::Err(e) => {
#error("Failed to send received answer to Client: {:?}", e);
},
Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; },
Err(e) => eprintln!("Error reading from stream: {:?}", e),
};
}
buf.drain(0..(4 + len as usize));
} else {
break;
}
}
},
::std::result::Result::Err(ref e) if e.kind() == ::std::io::ErrorKind::WouldBlock => { continue; },
::std::result::Result::Err(e) => eprintln!("Error reading from stream: {:?}", e),
}
}
}
@@ -367,75 +635,85 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
// Create a struct which the client will use to communicate
let client_recv_queue_wrapper = format_ident!("__{}RecvQueueWrapper", name);
let client_struct = quote! {
#[derive(Clone)]
#[derive(::std::clone::Clone)]
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 {
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 {
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
}
}
#[derive(Clone)]
#vis struct #client_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,
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<#join_handle_guard_name<()>>>,
}
impl #client_struct_name {
pub fn new(send_queue: tokio::sync::mpsc::Sender<(u64, #question_enum_name)>,
recv_queue: tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>,
connection_task: Option<::std::sync::Arc<tokio::task::JoinHandle<()>>>) -> Self {
pub fn new(send_queue: ::tokio::sync::mpsc::Sender<(u64, #question_enum_name)>,
recv_queue: ::tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>,
connection_task: ::std::option::Option<::std::sync::Arc<#join_handle_guard_name<()>>>,
ready_notify: ::std::sync::Arc<tokio::sync::Notify>) -> Self {
Self {
queries: #queries_struct_name::new(),
recv_queue: #client_recv_queue_wrapper::new(recv_queue),
ready: ::std::sync::Arc::new(false.into()),
ready_notify,
send_queue,
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 (send_queue, to_send) = 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 connection_task = tokio::spawn(connection.run());
Ok(Self::new(send_queue, recv_queue, Some(::std::sync::Arc::new(connection_task))))
let (send_queue, to_send) = ::tokio::sync::mpsc::channel(16);
let (to_recv, recv_queue) = ::tokio::sync::mpsc::channel(16);
let ready_notify = ::std::sync::Arc::new(tokio::sync::Notify::new());
let connection = #client_connection_struct_name::new(to_send, to_recv, ready_notify.clone(), stream);
let connection_task = ::tokio::spawn(connection.run());
Ok(Self::new(send_queue, recv_queue, ::std::option::Option::Some(::std::sync::Arc::new(connection_task.into())), ready_notify))
}
pub fn close(self) {
if let Some(task) = self.connection_task {
task.abort();
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;
}
}
async fn send(&self, query: #question_enum_name) -> Result<u64, #error_enum_name> {
let nonce = self.queries.len() as u64;
let res = self.send_queue.send((nonce, query.clone())).await;
match res {
Ok(_) => {
::std::result::Result::Ok(_) => {
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 {
// Check if we've received the answer for the query we're looking for
if let Some(query) = self.queries.get(&id) {
if let Some(answer) = query.get_answer() {
if let ::std::option::Option::Some(query) = self.queries.get(&id) {
if let ::std::option::Option::Some(answer) = query.get_answer() {
#info("Found answer for query {}", id);
return Ok(answer);
}
}
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());
}
None => return Err(#error_enum_name::Closed),
::std::option::Option::None => return ::std::result::Result::Err(#error_enum_name::Closed),
};
}
}
@@ -444,6 +722,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
};
let expanded = quote! {
#join_handle_guard // TODO: This should just be regular code and not in the macro!
#error_enum
#answer_enum
#question_enum

143
tests/client.rs Normal file
View File

@@ -0,0 +1,143 @@
/*
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 LOG_INIT: Once = Once::new();
static CONSOLE_INIT: Once = Once::new();
pub fn init_logger() {
LOG_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();
});
}
pub fn init_console() {
CONSOLE_INIT.call_once(|| {
console_subscriber::init();
});
}
#[derive(Protocol)]
enum TestProtocol {
Addition((i32, i32), i32),
SomeKindOfQuestion(String, i32),
ThisRespondsWithAString(i32, String),
Void((), ()),
}
#[tokio::test]
async fn client() {
init_logger();
init_console();
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();
init_console();
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();
}

100
tests/full.rs Normal file
View File

@@ -0,0 +1,100 @@
/*
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;
use tokio::net::TcpStream;
static LOG_INIT: Once = Once::new();
static CONSOLE_INIT: Once = Once::new();
pub fn init_logger() {
LOG_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();
});
}
pub fn init_console() {
CONSOLE_INIT.call_once(|| {
console_subscriber::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();
init_console();
#[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();
}

View File

@@ -1,112 +1 @@
/*
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();
}
mod client;