-
Notifications
You must be signed in to change notification settings - Fork 205
server: Register raw method with connection ID #1297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
26ec635
server: Add a raw method
lexnv ef24148
server: Register raw methods, blocking or unblocking
lexnv 4f8eb03
proc-macros: Add with-context attribute
lexnv 9eba899
server: Register sync and nonblocking methods for raw API
lexnv 239cafe
examples: Add with context example
lexnv 0de6d95
core: Adjust docs for the raw method registering
lexnv 4ce51c6
proc-macros: Cargo fmt
lexnv 5f2b40a
server: Request Arc<Context> for the raw method callback
lexnv 753224a
proc-macros: Per method raw-method attribute
lexnv d526b70
examples: Add server raw method
lexnv c8ff969
tests/ui: Check correct proc-macro behavior
lexnv b4880b9
tests/ui: Negative test for async with raw methods
lexnv d2da4b0
tests/ui: Negative test for blocking with raw methods
lexnv 0dc48e9
tests/proc-macros: Ensure unique connection IDs from different clients
lexnv 0029418
tests/integration: Ensure unique connection IDs from different clients
lexnv 3a09a7e
proc-macros: Apply cargo fmt
lexnv 1565927
Register raw method as async method
lexnv f5202ef
Fix testing
lexnv 8cf1947
core: Fix documentation
lexnv bd571e1
Merge remote-tracking branch 'origin/master' into lexnv/low-level-con…
lexnv 04dfd25
server: Rename raw method to `module.register_async_with_details`
lexnv e02ba61
server: Add connection details wrapper
lexnv 7b72d8a
server: Add asyncWithDetails and connection details
lexnv 87585ad
proc-macros: Provide connection details to methods
lexnv 2c50375
Update core/src/server/rpc_module.rs
lexnv 66b6a39
server: Remove connection details builder
lexnv cfe1aec
server: Refactor `.register_async_with_details` to `.register_async_m…
lexnv 7327d8b
proc-macro: Clarify comment
lexnv 6605409
Merge remote-tracking branch 'origin/lexnv/low-level-context-api-v2' …
lexnv 650e24a
core: Doc hidden for async with details
lexnv f4d9085
Rename example
lexnv 40d2bcf
Update core/src/server/rpc_module.rs
lexnv 8185193
Merge remote-tracking branch 'origin/lexnv/low-level-context-api-v2' …
lexnv 11ef18c
core: Remove doc(hidden) from ConnectionDetails::id
lexnv 4f459ac
Update core/src/server/rpc_module.rs
niklasad1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename perhaps?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have changed it to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2019-2021 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Permission is hereby granted, free of charge, to any | ||
| // person obtaining a copy of this software and associated | ||
| // documentation files (the "Software"), to deal in the | ||
| // Software without restriction, including without | ||
| // limitation the rights to use, copy, modify, merge, | ||
| // publish, distribute, sublicense, and/or sell copies of | ||
| // the Software, and to permit persons to whom the Software | ||
| // is furnished to do so, subject to the following | ||
| // conditions: | ||
| // | ||
| // The above copyright notice and this permission notice | ||
| // shall be included in all copies or substantial portions | ||
| // of the Software. | ||
| // | ||
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
| // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
| // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
| // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
| // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
| // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
| // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| // DEALINGS IN THE SOFTWARE. | ||
|
|
||
| use std::net::SocketAddr; | ||
|
|
||
| use jsonrpsee::core::{async_trait, client::Subscription}; | ||
| use jsonrpsee::proc_macros::rpc; | ||
| use jsonrpsee::server::{PendingSubscriptionSink, Server, SubscriptionMessage}; | ||
| use jsonrpsee::types::ErrorObjectOwned; | ||
| use jsonrpsee::ws_client::WsClientBuilder; | ||
| use jsonrpsee::ConnectionId; | ||
|
|
||
| #[rpc(server, client)] | ||
| pub trait Rpc { | ||
| /// Raw method with connection ID. | ||
| #[method(name = "connectionIdMethod", raw_method)] | ||
| fn raw_method(&self, first_param: usize, second_param: u16) -> Result<usize, ErrorObjectOwned>; | ||
|
|
||
| /// Normal method call example. | ||
| #[method(name = "normalMethod")] | ||
| fn normal_method(&self, first_param: usize, second_param: u16) -> Result<usize, ErrorObjectOwned>; | ||
|
|
||
| /// Subscriptions expose the connection ID on the subscription sink. | ||
| #[subscription(name = "subscribeSync" => "sync", item = usize)] | ||
| fn sub(&self, first_param: usize); | ||
| } | ||
|
|
||
| pub struct RpcServerImpl; | ||
|
|
||
| #[async_trait] | ||
| impl RpcServer for RpcServerImpl { | ||
| fn raw_method( | ||
| &self, | ||
| connection_id: ConnectionId, | ||
| _first_param: usize, | ||
| _second_param: u16, | ||
| ) -> Result<usize, ErrorObjectOwned> { | ||
| // Return the connection ID from which this method was called. | ||
| Ok(connection_id) | ||
| } | ||
|
|
||
| fn normal_method(&self, _first_param: usize, _second_param: u16) -> Result<usize, ErrorObjectOwned> { | ||
| // The normal method does not have access to the connection ID. | ||
| Ok(usize::MAX) | ||
| } | ||
|
|
||
| fn sub(&self, pending: PendingSubscriptionSink, _first_param: usize) { | ||
| tokio::spawn(async move { | ||
| // The connection ID can be obtained before or after accepting the subscription | ||
| let pending_connection_id = pending.connection_id(); | ||
| let sink = pending.accept().await.unwrap(); | ||
| let sink_connection_id = sink.connection_id(); | ||
|
|
||
| assert_eq!(pending_connection_id, sink_connection_id); | ||
|
|
||
| let msg = SubscriptionMessage::from_json(&sink_connection_id).unwrap(); | ||
| sink.send(msg).await.unwrap(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| #[tokio::main] | ||
| async fn main() -> anyhow::Result<()> { | ||
| tracing_subscriber::FmtSubscriber::builder() | ||
| .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
| .try_init() | ||
| .expect("setting default subscriber failed"); | ||
|
|
||
| let server_addr = run_server().await?; | ||
| let url = format!("ws://{}", server_addr); | ||
|
|
||
| let client = WsClientBuilder::default().build(&url).await?; | ||
| let connection_id_first = client.raw_method(1, 2).await.unwrap(); | ||
|
|
||
| // Second call from the same connection ID. | ||
| assert_eq!(client.raw_method(1, 2).await.unwrap(), connection_id_first); | ||
|
|
||
| // Second client will increment the connection ID. | ||
| let client_second = WsClientBuilder::default().build(&url).await?; | ||
| let connection_id_second = client_second.raw_method(1, 2).await.unwrap(); | ||
| assert_ne!(connection_id_first, connection_id_second); | ||
|
|
||
| let mut sub: Subscription<usize> = RpcClient::sub(&client, 0).await.unwrap(); | ||
| assert_eq!(connection_id_first, sub.next().await.transpose().unwrap().unwrap()); | ||
|
|
||
| let mut sub: Subscription<usize> = RpcClient::sub(&client_second, 0).await.unwrap(); | ||
| assert_eq!(connection_id_second, sub.next().await.transpose().unwrap().unwrap()); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn run_server() -> anyhow::Result<SocketAddr> { | ||
| let server = Server::builder().build("127.0.0.1:0").await?; | ||
|
|
||
| let addr = server.local_addr()?; | ||
| let handle = server.start(RpcServerImpl.into_rpc()); | ||
|
|
||
| // In this example we don't care about doing shutdown so let's it run forever. | ||
| // You may use the `ServerHandle` to shut it down or manage it yourself. | ||
| tokio::spawn(handle.stopped()); | ||
|
|
||
| Ok(addr) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //! Example of using proc macro to generate working client. | ||
|
|
||
| use jsonrpsee::proc_macros::rpc; | ||
| use jsonrpsee::types::ErrorObjectOwned; | ||
|
|
||
| #[rpc(server)] | ||
| pub trait Rpc { | ||
| #[method(name = "foo")] | ||
| async fn async_method(&self, param_a: u8, param_b: String) -> Result<u16, ErrorObjectOwned>; | ||
|
|
||
| #[method(name = "bar", raw_method)] | ||
| fn sync_method(&self) -> Result<u16, ErrorObjectOwned>; | ||
| } | ||
|
|
||
| fn main() {} |
9 changes: 9 additions & 0 deletions
9
proc-macros/tests/ui/incorrect/method/method_async_raw_incompatible.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| use jsonrpsee::proc_macros::rpc; | ||
|
|
||
| #[rpc(server)] | ||
| pub trait AsyncMethodCannotBeRaw { | ||
| #[method(name = "a", raw_method)] | ||
| async fn a(&self, param: Vec<u8>); | ||
| } | ||
|
|
||
| fn main() {} |
6 changes: 6 additions & 0 deletions
6
proc-macros/tests/ui/incorrect/method/method_async_raw_incompatible.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| error: Methods must be synchronous when used with `raw_method`; use `fn` instead of `async fn` | ||
| --> tests/ui/incorrect/method/method_async_raw_incompatible.rs:5:2 | ||
| | | ||
| 5 | / #[method(name = "a", raw_method)] | ||
| 6 | | async fn a(&self, param: Vec<u8>); | ||
| | |______________________________________^ |
9 changes: 9 additions & 0 deletions
9
proc-macros/tests/ui/incorrect/method/method_blocking_raw_incompatible.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| use jsonrpsee::proc_macros::rpc; | ||
|
|
||
| #[rpc(server)] | ||
| pub trait BlockingMethodCannotBeRaw { | ||
| #[method(name = "a", blocking, raw_method)] | ||
| fn a(&self, param: Vec<u8>); | ||
| } | ||
|
|
||
| fn main() {} |
6 changes: 6 additions & 0 deletions
6
proc-macros/tests/ui/incorrect/method/method_blocking_raw_incompatible.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| error: Methods cannot be blocking when used with `raw_method`; remove `blocking` attribute or `raw_method` attribute | ||
| --> tests/ui/incorrect/method/method_blocking_raw_incompatible.rs:5:2 | ||
| | | ||
| 5 | / #[method(name = "a", blocking, raw_method)] | ||
| 6 | | fn a(&self, param: Vec<u8>); | ||
| | |________________________________^ |
4 changes: 2 additions & 2 deletions
4
proc-macros/tests/ui/incorrect/method/method_unexpected_field.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| error: Unknown argument `magic`, expected one of: `aliases`, `blocking`, `name`, `param_kind` | ||
| --> $DIR/method_unexpected_field.rs:6:25 | ||
| error: Unknown argument `magic`, expected one of: `aliases`, `blocking`, `name`, `param_kind`, `raw_method` | ||
| --> tests/ui/incorrect/method/method_unexpected_field.rs:6:25 | ||
| | | ||
| 6 | #[method(name = "foo", magic = false)] | ||
| | ^^^^^ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.