pub struct RpcService<M, Req, Resp>where
M: RawMutex,{ /* private fields */ }Expand description
In-memory request/response synchronization for async tasks.
This type is not a wire protocol. It connects a client async task that calls
RpcService::request with a server task that calls RpcService::serve, using a
embassy_sync::blocking_mutex::Mutex and async wakers. It is suitable for no_std use when
paired with a mutex implementation appropriate for your platform (RawMutex).
§Concurrency
- Single in-flight RPC: Internal state holds at most one queued request and one response slot. Design for one active request/response pair at a time.
- Multiple clients: Several tasks may call
request; additional callers wait until the current RPC completes (including delivery ofRequestDroppedError). - Client cancellation: If the async task awaiting
Self::requestis dropped (executor cancellation), the service releases the client slot once the in-flight RPC is fully finished (including after the server responds or dropsServedRequest). Dropping the client future after the request was queued but before the server takes it removes the queued request and frees the slot immediately.
§Type parameters
M: Mutex raw type (RawMutex), e.g.CriticalSectionRawMutexon many MCUs.Req/Resp: Your message types.Reqmay borrow from the client for the duration of the call; then theRpcServicemust not outlive those borrows (often modeled with a stack-scoped service—see the crate README).
Implementations§
Source§impl<M, Req, Resp> RpcService<M, Req, Resp>where
M: RawMutex,
impl<M, Req, Resp> RpcService<M, Req, Resp>where
M: RawMutex,
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates an empty service. Safe to call in const contexts (e.g. static initializer).
Sourcepub async fn request(&self, req: Req) -> Result<Resp, RequestDroppedError>
pub async fn request(&self, req: Req) -> Result<Resp, RequestDroppedError>
Sends req and waits until the server responds or drops the ServedRequest.
If another client is already in an RPC, this call waits until that RPC fully completes
(including waking the next waiter for the client slot) before sending req.
§Errors
Returns Err(RequestDroppedError) if the server drops
ServedRequest without calling ServedRequest::respond.
§Cancellation
If this future is dropped while waiting for a response, the queued request is removed if
the server has not taken it yet; otherwise the server continues with ServedRequest and
the response is discarded when the server completes. The client slot becomes available again
after that completion (or immediately when the queued request is dropped).
Sourcepub async fn serve(&self) -> (Req, ServedRequest<'_, M, Req, Resp>)
pub async fn serve(&self) -> (Req, ServedRequest<'_, M, Req, Resp>)
Waits until a client submits a request via Self::request, then returns the request
value together with a ServedRequest used to complete or abandon the RPC.
The server must eventually call ServedRequest::respond on the handle or drop it;
dropping notifies the client with RequestDroppedError.