Struct toy_rpc::server::Server [−][src]
pub struct Server { /* fields omitted */ }
Expand description
RPC Server
const DEFAULT_RPC_PATH: &str = "_rpc_";
Implementations
This is supported on crate feature http_actix_web
only.
http_actix_web
only.Configuration for integration with an actix-web scope.
A convenient funciont “handle_http” may be used to achieve the same thing
with the actix-web
feature turned on.
The DEFAULT_RPC_PATH
will be appended to the end of the scope’s path.
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
This is supported on crate feature http_actix_web
and non-crate feature http_tide
and non-crate feature http_warp
only.
http_actix_web
and non-crate feature http_tide
and non-crate feature http_warp
only.A conevience function that calls the corresponding http handling function depending on the enabled feature flag
feature flag | function name |
---|---|
http_tide | into_endpoint |
http_actix_web | scope_config |
http_warp | into_boxed_filter |
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
The following impl block is controlled by feature flag. It is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
This is supported on crate feature http_tide
only.
http_tide
only.Creates a tide::Endpoint
that handles http connections.
A convienient function handle_http
can be used to achieve the same thing
with tide
feature turned on
The endpoint will be created with DEFAULT_RPC_PATH
appended to the
end of the nested tide
endpoint.
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use toy_rpc::server::Server; use toy_rpc::macros::{export_impl, service}; use async_std::sync::Arc; struct FooService { } #[export_impl] impl FooService { // define some "exported" functions } #[async_std::main] async fn main() -> tide::Result<()> { let addr = "127.0.0.1:8080"; let foo_service = Arc::new(FooService { }); let server = Server::builder() .register(foo_service) .build(); let mut app = tide::new(); // If a network path were to be supplied, // the network path must end with a slash "/" app.at("/rpc/").nest(server.into_endpoint()); // `handle_http` is a conenient function that calls `into_endpoint` // with the `tide` feature turned on //app.at("/rpc/").nest(server.handle_http()); app.listen(addr).await?; Ok(()) }
This is supported on crate feature http_tide
and non-crate feature http_actix_web
and non-crate feature http_warp
only.
http_tide
and non-crate feature http_actix_web
and non-crate feature http_warp
only.A conevience function that calls the corresponding http handling function depending on the enabled feature flag
feature flag | function name |
---|---|
http_tide | into_endpoint |
http_actix_web | scope_config |
http_warp | into_boxed_filter |
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use toy_rpc::server::Server; use toy_rpc::macros::{export_impl, service}; use async_std::sync::Arc; struct FooService { } #[export_impl] impl FooService { // define some "exported" functions } #[async_std::main] async fn main() -> tide::Result<()> { let addr = "127.0.0.1:8080"; let foo_service = Arc::new(FooService { }); let server = Server::builder() .register(foo_service) .build(); let mut app = tide::new(); // If a network path were to be supplied, // the network path must end with a slash "/" // `handle_http` is a conenience function that calls `into_endpoint` // with the `tide` feature turned on app.at("/rpc/").nest(server.handle_http()); app.listen(addr).await?; Ok(()) }
The following impl block is controlled by feature flag. It is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
This is supported on crate feature http_warp
only.
http_warp
only.Consumes Server
and returns a warp::filters::BoxedFilter
which can be chained with warp
filters
Example
use toy_rpc::Server; use toy_rpc::macros::{export_impl, service}; use std::sync::Arc; struct FooService { } #[export_impl] impl FooService { // define some "exported" functions here } #[tokio::main] async fn main() { env_logger::init(); let foo_service = Arc::new(FooService { }); let server = Server::builder() .register(foo_service) .build(); let routes = warp::path("rpc") .and(server.handle_http()); // RPC will be served at "ws://127.0.0.1/rpc/_rpc_" warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; }
This is supported on crate feature http_warp
and non-crate feature http_actix_web
and non-crate feature http_tide
only.
http_warp
and non-crate feature http_actix_web
and non-crate feature http_tide
only.A conevience function that calls the corresponding http handling function depending on the enabled feature flag
feature flag | function name |
---|---|
http_tide | into_endpoint |
http_actix_web | scope_config |
http_warp | into_boxed_filter |
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use toy_rpc::Server; use toy_rpc::macros::{export_impl, service}; use std::sync::Arc; struct FooService { } #[export_impl] impl FooService { // define some "exported" functions here } #[tokio::main] async fn main() { env_logger::init(); let foo_service = Arc::new(FooService { }); let server = Server::builder() .register(foo_service) .build(); let routes = warp::path("rpc") .and(server.handle_http()); // RPC will be served at "ws://127.0.0.1/rpc/_rpc_" warp::serve(routes).run(([127, 0, 0, 1], 8080)).await; }
The following impl block is controlled by feature flag. It is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Accepts connections on an async_std::net::TcpListner
and serves requests to default
server for each incoming connection.
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use async_std::net::TcpListener; async fn main() { // assume `ExampleService` exist let example_service = ExampleService {}; let server = Server::builder() .register(example_service) .build(); let listener = TcpListener::bind(addr).await.unwrap(); let handle = task::spawn(async move { server.accept(listener).await.unwrap(); }); handle.await; }
See toy-rpc/examples/rap_tcp/
for the example
Similar to accept
. This will accept connections on an async_std::net::TcpListner
and serves
requests using WebSocket transport protocol and the default codec.
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use async_std::net::TcpListener; async fn main() { // assume `ExampleService` exist let example_service = ExampleService {}; let server = Server::builder() .register(example_service) .build(); let listener = TcpListener::bind(addr).await.unwrap(); let handle = task::spawn(async move { server.accept_websocket(listener).await.unwrap(); }); handle.await; }
Serves a single connection using the default codec
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use async_std::net::TcpStream; async fn main() { // assume `ExampleService` exist let example_service = ExampleService {}; let server = Server::builder() .register(example_service) .build(); let conn = TcpStream::connect(addr).await.unwrap(); let handle = task::spawn(async move { server.serve_conn(conn).await.unwrap(); }); handle.await; }
pub async fn serve_codec<C>(&self, codec: C) -> Result<(), Error> where
C: SplittableServerCodec + Send + Sync + 'static,
[src]
pub async fn serve_codec<C>(&self, codec: C) -> Result<(), Error> where
C: SplittableServerCodec + Send + Sync + 'static,
[src]This is like serve_conn except that it uses a specified codec
Example
let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap(); let codec = Codec::new(stream); let server = Server::builder() .register(example_service) .build(); // assume `ExampleService` exist let handle = task::spawn(async move { server.serve_codec(codec).await.unwrap(); }) handle.await;
The following impl block is controlled by feature flag. It is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Accepts connections on an tokio::net::TcpListener
and serves requests to default
server for each incoming connection
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use async_std::net::TcpListener; async fn main() { // assume `ExampleService` exist let example_service = ExampleService {}; let server = Server::builder() .register(example_service) .build(); let listener = TcpListener::bind(addr).await.unwrap(); let handle = task::spawn(async move { server.accept(listener).await.unwrap(); }); handle.await; }
See toy-rpc/examples/tokio_tcp/
for the example
Similar to accept
. This will accept connections on a tokio::net::TcpListener
and serves
requests using WebSocket transport protocol and the default codec.
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use async_std::net::TcpListener; async fn main() { // assume `ExampleService` exist let example_service = ExampleService {}; let server = Server::builder() .register(example_service) .build(); let listener = TcpListener::bind(addr).await.unwrap(); let handle = task::spawn(async move { server.accept_websocket(listener).await.unwrap(); }); handle.await; }
Serves a single connection using the default codec
This is enabled if and only if exactly one of the the following feature flag is turned on
serde_bincode
serde_json
serde_cbor
serde_rmp
Example
use async_std::net::TcpStream; async fn main() { // assume `ExampleService` exist let example_service = ExampleService {}; let server = Server::builder() .register(example_service) .build(); let conn = TcpStream::connect(addr).await.unwrap(); let handle = task::spawn(async move { server.serve_conn(conn).await.unwrap(); }); handle.await; }
pub async fn serve_codec<C>(&self, codec: C) -> Result<(), Error> where
C: SplittableServerCodec + Send + Sync + 'static,
[src]
pub async fn serve_codec<C>(&self, codec: C) -> Result<(), Error> where
C: SplittableServerCodec + Send + Sync + 'static,
[src]This is like serve_conn except that it uses a specified codec
Example
let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap(); let codec = Codec::new(stream); let server = Server::builder() .register(example_service) .build(); // assume `ExampleService` exist let handle = task::spawn(async move { server.serve_codec(codec).await.unwrap(); }) handle.await;
Creates a ServerBuilder
Example
use toy_rpc::server::{ServerBuilder, Server}; let builder: ServerBuilder = Server::builder();
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Server
impl !UnwindSafe for Server
Blanket Implementations
Mutably borrows from an owned value. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
type Output = T
type Output = T
Should always be Self
pub fn vzip(self) -> V