RpcServer

Struct RpcServer 

Source
pub struct RpcServer<'a> { /* private fields */ }
Expand description

Server side of the shared-memory RPC protocol.

Polls the request region (offset 0) for incoming calls, dispatches them via the provided RpcHandler, and writes responses to the response region (offset 32768).

§Example

use grafos_rpc::{RpcHandler, RpcServer};
use grafos_std::error::{FabricError, Result};
use grafos_std::mem::MemBuilder;

struct Echo;
impl RpcHandler for Echo {
    fn handle(&self, _method_id: u32, payload: &[u8]) -> Result<Vec<u8>> {
        Ok(payload.to_vec())
    }
}

let lease = MemBuilder::new().min_bytes(65536).acquire()?;
let server = RpcServer::new(&lease);

// In your event loop:
// let handled = server.poll_once(&Echo)?;

Implementations§

Source§

impl<'a> RpcServer<'a>

Source

pub fn new(lease: &'a MemLease) -> Self

Create a new RPC server backed by the given memory lease.

Source

pub fn poll_once<H: RpcHandler>(&self, handler: &H) -> Result<bool>

Poll for and handle a single request.

Reads the request region. If a request with status REQUEST_READY is present:

  1. Sets status to PROCESSING.
  2. Calls handler.handle(method_id, payload).
  3. Writes RESPONSE_READY (or ERROR) to the response region.
  4. Clears the request region to EMPTY.
  5. Returns Ok(true).

If no request is pending, returns Ok(false) without side effects.

Source

pub fn serve_n<H: RpcHandler>( &self, handler: &H, max_iterations: u64, ) -> Result<u64>

Serve requests in a loop for up to max_iterations polls.

Returns the number of requests actually handled (i.e. the number of times poll_once returned Ok(true)).

This is a convenience for testing where you know a fixed number of calls will arrive. In production, call poll_once from your own event loop.

Auto Trait Implementations§

§

impl<'a> Freeze for RpcServer<'a>

§

impl<'a> !RefUnwindSafe for RpcServer<'a>

§

impl<'a> !Send for RpcServer<'a>

§

impl<'a> !Sync for RpcServer<'a>

§

impl<'a> Unpin for RpcServer<'a>

§

impl<'a> !UnwindSafe for RpcServer<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.