Struct toy_rpc::server::Server [−][src]
RPC Server
const DEFAULT_RPC_PATH: &str = "_rpc_";
Implementations
impl Server
[src]
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
pub fn scope_config(cfg: &mut ServiceConfig)
[src]
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
Example
use toy_rpc::Server; use toy_rpc::macros::{export_impl, service}; use actix_web::{App, HttpServer, web}; struct FooService { } #[export_impl] impl FooService { // define some "exported" functions } #[actix::main] async fn main() -> std::io::Result<()> { let addr = "127.0.0.1:8080"; let foo_service = Arc::new(FooService { }); let server = Server::builder() .register("foo_service", service!(foo_servicem FooService)) .build(); let app_data = web::Data::new(server); HttpServer::new( move || { App::new() .service( web::scope("/rpc/") .app_data(app_data.clone()) .configure(Server::scope_config) // The line above may be replaced with line below //.configure(Server::handle_http()) // use the convenience `handle_http` ) } ) .bind(addr)? .run() .await }
pub fn handle_http() -> fn(_: &mut ServiceConfig)
[src]
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 actix_web::{App, web}; struct FooService { } #[export_impl] impl FooService { // define some "exported" functions } #[actix::main] async fn main() -> std::io::Result<()> { let addr = "127.0.0.1:8080"; let foo_service = Arc::new(FooService { }); let server = Server::builder() .register("foo_service", service!(foo_servicem FooService)) .build(); let app_data = web::Data::new(server); HttpServer::new( move || { App::new() .service(hello) .service( web::scope("/rpc/") .app_data(app_data.clone()) .configure(Server::handle_http()) // use the convenience `handle_http` ) } ) .bind(addr)? .run() .await }
impl Server
[src]
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
pub fn into_endpoint(self) -> Server<Server>
[src]
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!(foo_service, FooService)) .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(()) }
pub fn handle_http(self) -> Server<Server>
[src]
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!(foo_service, FooService)) .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(()) }
impl Server
[src]
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
pub fn into_boxed_filter(self) -> BoxedFilter<(impl Reply,)>
[src]
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!(foo_service, FooService)) .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; }
pub fn handle_http(self) -> BoxedFilter<(impl Reply,)>
[src]
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!(foo_service, FooService)) .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; }
impl Server
[src]
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
pub async fn accept(&self, listener: TcpListener) -> Result<(), Error>
[src]
async_std_runtime
or http_tide
only.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!(example_service, ExampleService)) .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
pub async fn accept_websocket(&self, listener: TcpListener) -> Result<(), Error>
[src]
async_std_runtime
or http_tide
only.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!(example_service, ExampleService)) .build(); let listener = TcpListener::bind(addr).await.unwrap(); let handle = task::spawn(async move { server.accept_websocket(listener).await.unwrap(); }); handle.await; }
pub async fn serve_conn(&self, stream: TcpStream) -> Result<(), Error>
[src]
async_std_runtime
or http_tide
only.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!(example_service, ExampleService)) .build(); let conn = TcpStream::connect(addr).await.unwrap(); let handle = task::spawn(async move { server.serve_conn(conn).await.unwrap(); }); handle.await; }
impl Server
[src]
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
pub async fn accept(&self, listener: TcpListener) -> Result<(), Error>
[src]
tokio_runtime
or http_warp
or http_actix_web
only.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!(example_service, ExampleService)) .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
pub async fn accept_websocket(&self, listener: TcpListener) -> Result<(), Error>
[src]
tokio_runtime
or http_warp
or http_actix_web
only.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!(example_service, ExampleService)) .build(); let listener = TcpListener::bind(addr).await.unwrap(); let handle = task::spawn(async move { server.accept_websocket(listener).await.unwrap(); }); handle.await; }
pub async fn serve_conn(&self, stream: TcpStream) -> Result<(), Error>
[src]
tokio_runtime
or http_warp
or http_actix_web
only.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!(example_service, ExampleService)) .build(); let conn = TcpStream::connect(addr).await.unwrap(); let handle = task::spawn(async move { server.serve_conn(conn).await.unwrap(); }); handle.await; }
impl Server
[src]
pub fn builder() -> ServerBuilder
[src]
Creates a ServerBuilder
Example
use toy_rpc::server::{ServerBuilder, Server}; let builder: ServerBuilder = Server::builder();
pub async fn serve_codec<C>(&self, codec: C) -> Result<(), Error> where
C: ServerCodec + Send + Sync,
[src]
C: ServerCodec + Send + Sync,
This is like serve_conn except that it uses a specified codec
Example
use async_std::net::TcpStream; // the default `Codec` will be used as an example use toy_rpc::codec::Codec; #[async_std::main] async fn main() { // assume the following connection can be established let stream = TcpStream::connect("127.0.0.1:8080").await.unwrap(); let codec = Codec::new(stream); let server = Server::builder() .register("example", service!(example_service, ExampleService)) .build(); // assume `ExampleService` exist let handle = task::spawn(async move { server.serve_codec(codec).await.unwrap(); }) handle.await; }
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Server
[src]
impl Send for Server
[src]
impl Sync for Server
[src]
impl Unpin for Server
[src]
impl !UnwindSafe for Server
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,