pub struct WshRpcEngine { /* private fields */ }Expand description
Core RPC engine: handles incoming RPC requests, dispatches to registered command handlers, and manages request/response lifecycle.
Port of Go’s WshRpc from pkg/wshutil/wshrpc.go.
Implementations§
Source§impl WshRpcEngine
impl WshRpcEngine
Sourcepub fn new() -> (Arc<Self>, UnboundedReceiver<RpcMessage>)
pub fn new() -> (Arc<Self>, UnboundedReceiver<RpcMessage>)
Create a new RPC engine. Returns the engine and a receiver for outgoing messages.
Sourcepub fn register_handler(&self, command: &str, handler: CommandHandler)
pub fn register_handler(&self, command: &str, handler: CommandHandler)
Register a call handler (single request → single response).
Drops any typed binding previously recorded for command: the last
registration wins for the handler map, so it must win for the schema
too, or schema_json() would advertise a request/response pair the
live handler no longer implements.
Sourcepub fn register_typed<Req, Resp, F, Fut>(
&self,
command: &'static str,
handler: F,
)
pub fn register_typed<Req, Resp, F, Fut>( &self, command: &'static str, handler: F, )
Register a call handler from typed request/response values, and
record the pair in this engine’s RpcSchema.
Behaviourally identical to Self::register_handler: the closure is
wrapped in exactly the deserialize → run → serialize steps every
hand-written handler already performs, so migrating one is not
supposed to change what goes over the wire. What it adds is that the
Req/Resp pairing becomes data the process can dump, which is
the prerequisite for generating the frontend’s bindings instead of
hand-maintaining them — see schema.rs and
docs/specs/SPEC_RPC_BINDINGS_CODEGEN_2026_09_07.md.
Two deliberate differences from the untyped path, both matching what the existing handlers do anyway:
- Deserialization failure is reported as
Err, with the command name and serde’s message — the sameformat!("{cmd}: {e}")shape the hand-written handlers use. - A response is always sent (
Ok(Some(..))). Handlers that must answer with nothing at all keep usingregister_handler;Resp = ()serializes tonull, which is a response, not silence.
Serialization failure is also an Err rather than a panic: a
response type whose Serialize fails is a bug, but it is not worth
taking the connection down for.
Both types must derive ts_rs::TS: the schema records each as
TypeScript, which is what the generated frontend stubs
(frontend/app/store/rpc-api/generated.ts) are built from. A typed
command whose types have no TypeScript would be one the frontend has
to describe by hand again.
Sourcepub fn register_stream_handler(&self, command: &str, handler: StreamHandler)
pub fn register_stream_handler(&self, command: &str, handler: StreamHandler)
Register a streaming handler (single request → stream of responses).
Sourcepub fn set_auth_token(&self, token: &str)
pub fn set_auth_token(&self, token: &str)
Set the authentication token.
Sourcepub fn get_auth_token(&self) -> String
pub fn get_auth_token(&self) -> String
Get the authentication token.
Sourcepub fn set_rpc_context(&self, ctx: RpcContext)
pub fn set_rpc_context(&self, ctx: RpcContext)
Set the RPC context.
Sourcepub fn set_dispatch_guard(&self, guard: DispatchGuard)
pub fn set_dispatch_guard(&self, guard: DispatchGuard)
Guard every command this connection calls: guard runs before the handler and a refusal is the answer.
Sourcepub fn check_dispatch(&self, command: &str, data: &Value) -> Result<(), String>
pub fn check_dispatch(&self, command: &str, data: &Value) -> Result<(), String>
Whether this connection may call command (also asked for the websocket’s own input commands, which
do not go through Self::handle_message).
Sourcepub fn rpc_context(&self) -> RpcContext
pub fn rpc_context(&self) -> RpcContext
The context handlers get for this connection’s next call.
Sourcepub fn mark_operator_proven(&self) -> bool
pub fn mark_operator_proven(&self) -> bool
Record that the CEF host (or, on a headless srv, a confirmed local
browser session) vouched for this connection as the person’s window
(server::operator_proof). Refused, returning false, once the
connection has registered as an agent. Self::set_rpc_context
replaces the whole context, so registering afterwards drops the proof.
Sourcepub fn mark_operator_proven_for(&self, person: &str, until: u64) -> bool
pub fn mark_operator_proven_for(&self, person: &str, until: u64) -> bool
Record that a registered external host vouched for this connection as person’s window until the
unix second until (server::external_host). Refused, returning false, once the connection has
registered as an agent; registering afterwards drops the proof, as for the CEF host’s.
Sourcepub fn clear_operator_proof(&self)
pub fn clear_operator_proof(&self)
Drop any operator proof this connection holds (an external host’s lease that was ended).
Sourcepub fn set_gateway_conn(&self, id: String)
pub fn set_gateway_conn(&self, id: String)
Remember the id an external host gave this connection (set once, when it connects).
Sourcepub fn gateway_conn(&self) -> Option<String>
pub fn gateway_conn(&self) -> Option<String>
The id an external host gave this connection, if it came through one and srv verified it.
Sourcepub fn handle_message(self: &Arc<Self>, msg: RpcMessage)
pub fn handle_message(self: &Arc<Self>, msg: RpcMessage)
Process an incoming message (from the transport layer).
Sourcepub async fn send_command(
self: &Arc<Self>,
command: &str,
data: Value,
opts: &RpcOpts,
) -> Result<Value, String>
pub async fn send_command( self: &Arc<Self>, command: &str, data: Value, opts: &RpcOpts, ) -> Result<Value, String>
Send an RPC command and wait for a single response.
Sourcepub fn send_request(
self: &Arc<Self>,
command: &str,
data: Value,
opts: &RpcOpts,
) -> Result<RpcRequestHandler, String>
pub fn send_request( self: &Arc<Self>, command: &str, data: Value, opts: &RpcOpts, ) -> Result<RpcRequestHandler, String>
Send an RPC command and get a request handler for streaming responses.
Sourcepub fn send_command_no_response(&self, command: &str, data: Value, route: &str)
pub fn send_command_no_response(&self, command: &str, data: Value, route: &str)
Send a fire-and-forget command (no response expected).
Auto Trait Implementations§
impl !Freeze for WshRpcEngine
impl RefUnwindSafe for WshRpcEngine
impl Send for WshRpcEngine
impl Sync for WshRpcEngine
impl Unpin for WshRpcEngine
impl UnsafeUnpin for WshRpcEngine
impl UnwindSafe for WshRpcEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more