[][src]Struct toy_rpc::server::Server

pub struct Server { /* fields omitted */ }

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]

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]

This is supported on crate feature 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]

This is supported on crate feature 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]

This is supported on crate feature 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 flagfunction name
tideinto_endpoint
actix-webscope_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]

This is supported on crate feature 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 flagfunction name
tideinto_endpoint
actix-webscope_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

impl Clone for Server[src]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

impl<T> WithSubscriber for T[src]