RpcServer

Struct RpcServer 

Source
pub struct RpcServer<C: Codec = BincodeCodec> { /* private fields */ }

Implementations§

Source§

impl RpcServer<BincodeCodec>

Source

pub fn new() -> Self

Examples found in repository?
examples/rpc_client_server.rs (line 58)
51async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
52    println!("[Server] Starting RPC server");
53
54    let config = SharedMemoryConfig::default();
55    let transport = SharedMemoryFrameTransport::create_server(SERVICE_NAME, config)?;
56    let channel = Arc::new(MessageChannelAdapter::new(transport));
57
58    let server = RpcServer::new();
59
60    server.register_typed("add", |req: AddRequest| async move {
61        println!("[Server] add({}, {})", req.a, req.b);
62        Ok(AddResponse {
63            result: req.a + req.b,
64        })
65    });
66
67    server.register_typed("echo", |req: EchoRequest| async move {
68        println!("[Server] echo(\"{}\")", req.message);
69        let len = req.message.len();
70        Ok(EchoResponse {
71            message: req.message,
72            length: len,
73        })
74    });
75
76    println!("[Server] Registered {} handlers", server.handler_count());
77    println!("[Server] Waiting for requests\n");
78
79    server.serve(channel).await?;
80
81    Ok(())
82}
Source§

impl<C: Codec + Clone + Default + 'static> RpcServer<C>

Source

pub fn with_codec(codec: C) -> Self

Source

pub fn register(&self, handler: Arc<dyn Handler<C>>)

Source

pub fn register_fn<F, Fut>(&self, method: impl Into<String>, func: F)
where F: Fn(Message<C>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Message<C>>> + Send + 'static,

Source

pub fn register_typed<Req, Resp, F, Fut>( &self, method: impl Into<String>, func: F, )
where Req: for<'de> Deserialize<'de> + Send + Sync + 'static, Resp: Serialize + Send + Sync + 'static, F: Fn(Req) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Resp>> + Send + 'static,

Examples found in repository?
examples/rpc_client_server.rs (lines 60-65)
51async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
52    println!("[Server] Starting RPC server");
53
54    let config = SharedMemoryConfig::default();
55    let transport = SharedMemoryFrameTransport::create_server(SERVICE_NAME, config)?;
56    let channel = Arc::new(MessageChannelAdapter::new(transport));
57
58    let server = RpcServer::new();
59
60    server.register_typed("add", |req: AddRequest| async move {
61        println!("[Server] add({}, {})", req.a, req.b);
62        Ok(AddResponse {
63            result: req.a + req.b,
64        })
65    });
66
67    server.register_typed("echo", |req: EchoRequest| async move {
68        println!("[Server] echo(\"{}\")", req.message);
69        let len = req.message.len();
70        Ok(EchoResponse {
71            message: req.message,
72            length: len,
73        })
74    });
75
76    println!("[Server] Registered {} handlers", server.handler_count());
77    println!("[Server] Waiting for requests\n");
78
79    server.serve(channel).await?;
80
81    Ok(())
82}
Source

pub fn register_stream<Req, Item, F, S>( &self, method: impl Into<String>, func: F, )
where Req: for<'de> Deserialize<'de> + Send + Sync + 'static, Item: Serialize + Send + Sync + 'static, S: Stream<Item = Result<Item>> + Send + 'static, F: Fn(Req) -> S + Send + Sync + 'static,

Source

pub fn register_stream_fn<F, Fut>(&self, method: impl Into<String>, func: F)
where F: Fn(Message<C>, ServerStreamSender<C>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Source

pub async fn handle_message<T: MessageChannel<C>>( &self, message: Message<C>, transport: &T, ) -> Option<Message<C>>

Source

pub async fn serve<T: MessageChannel<C>>(&self, transport: Arc<T>) -> Result<()>

Examples found in repository?
examples/rpc_client_server.rs (line 79)
51async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
52    println!("[Server] Starting RPC server");
53
54    let config = SharedMemoryConfig::default();
55    let transport = SharedMemoryFrameTransport::create_server(SERVICE_NAME, config)?;
56    let channel = Arc::new(MessageChannelAdapter::new(transport));
57
58    let server = RpcServer::new();
59
60    server.register_typed("add", |req: AddRequest| async move {
61        println!("[Server] add({}, {})", req.a, req.b);
62        Ok(AddResponse {
63            result: req.a + req.b,
64        })
65    });
66
67    server.register_typed("echo", |req: EchoRequest| async move {
68        println!("[Server] echo(\"{}\")", req.message);
69        let len = req.message.len();
70        Ok(EchoResponse {
71            message: req.message,
72            length: len,
73        })
74    });
75
76    println!("[Server] Registered {} handlers", server.handler_count());
77    println!("[Server] Waiting for requests\n");
78
79    server.serve(channel).await?;
80
81    Ok(())
82}
Source

pub fn spawn_handler<T: MessageChannel<C> + 'static>( &self, transport: T, ) -> ServerHandle

Source

pub fn handler_count(&self) -> usize

Examples found in repository?
examples/rpc_client_server.rs (line 76)
51async fn run_server() -> Result<(), Box<dyn std::error::Error>> {
52    println!("[Server] Starting RPC server");
53
54    let config = SharedMemoryConfig::default();
55    let transport = SharedMemoryFrameTransport::create_server(SERVICE_NAME, config)?;
56    let channel = Arc::new(MessageChannelAdapter::new(transport));
57
58    let server = RpcServer::new();
59
60    server.register_typed("add", |req: AddRequest| async move {
61        println!("[Server] add({}, {})", req.a, req.b);
62        Ok(AddResponse {
63            result: req.a + req.b,
64        })
65    });
66
67    server.register_typed("echo", |req: EchoRequest| async move {
68        println!("[Server] echo(\"{}\")", req.message);
69        let len = req.message.len();
70        Ok(EchoResponse {
71            message: req.message,
72            length: len,
73        })
74    });
75
76    println!("[Server] Registered {} handlers", server.handler_count());
77    println!("[Server] Waiting for requests\n");
78
79    server.serve(channel).await?;
80
81    Ok(())
82}

Trait Implementations§

Source§

impl Default for RpcServer<BincodeCodec>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<C> Freeze for RpcServer<C>
where C: Freeze,

§

impl<C = BincodeCodec> !RefUnwindSafe for RpcServer<C>

§

impl<C> Send for RpcServer<C>

§

impl<C> Sync for RpcServer<C>

§

impl<C> Unpin for RpcServer<C>
where C: Unpin,

§

impl<C = BincodeCodec> !UnwindSafe for RpcServer<C>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V