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

pub struct Server { /* fields omitted */ }

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]

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

This is supported on 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 flagfunction name
http_tideinto_endpoint
http_actix_webscope_config
http_warpinto_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]

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

This is supported on crate feature 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 flagfunction name
http_tideinto_endpoint
http_actix_webscope_config
http_warpinto_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]

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

This is supported on crate feature 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 flagfunction name
http_tideinto_endpoint
http_actix_webscope_config
http_warpinto_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]

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

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

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

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

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

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

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

impl Clone for Server[src]

Auto Trait Implementations

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>,