[−][src]Struct toy_rpc::server::Server
RPC Server
const DEFAULT_RPC_PATH: &str = "_rpc_";
Implementations
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:8888").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; }
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]
Accepts connections on the listener 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/server_client/
for the example
pub async fn serve_conn<'_>(&'_ self, stream: TcpStream) -> Result<(), Error>
[src]
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; }
pub fn into_endpoint(self) -> Server<Server>
[src]
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:8888"; 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 scope_config(cfg: &mut ServiceConfig)
[src]
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:8888"; 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(self) -> Server<Server>
[src]
tide
and non-crate feature actix-web
only.A conevience function that calls the corresponding http handling function depending on the enabled feature flag
feature flag | function name |
---|---|
tide | into_endpoint |
actix-web | scope_config |
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:8888"; 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(()) }
pub fn handle_http() -> fn(_: &mut ServiceConfig)
[src]
actix-web
and non-crate feature tide
only.A conevience function that calls the corresponding http handling function depending on the enabled feature flag
feature flag | function name |
---|---|
tide | into_endpoint |
actix-web | scope_config |
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:8888"; 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 }
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Server
impl Send for Server
impl Sync for Server
impl Unpin for Server
impl !UnwindSafe for Server
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>,
pub fn vzip(self) -> V
impl<T> WithSubscriber for T
[src]
pub fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
[src]
S: Into<Dispatch>,