livekit / rust-sdks

LiveKit realtime and server SDKs for Rust
https://livekit.io
Apache License 2.0
211 stars 56 forks source link

RPC implementation + FFI #461

Closed bcherry closed 4 weeks ago

bcherry commented 1 month ago

This fully implements the new RPC feature in the Rust SDK and exposes it via FFI to other SDKs.

It has one slight rough edge as there is a new FFI dance necessary for method handling. The register_rpc_method FFI cannot store the original handler, so instead it registers a wrapper handler that dispatches into the dependent SDK to actually handle the method and then get a response or error that can be sent back up the wire. So the implementation in Node/Python/Unity/etc requires both storing the handlers locally and forwarding to FFI on registerRpcMethod. Then it waits for RpcMethodInvocationEvent and invokes its stored handlers, sending the response/error in RpcMethodInvocationRequest over FFI.

This is my first time working with Rust so lmk if I've done anything particularly wrong. The RpcHandler type in particular feels very complicated but maybe that's just Rust.

Base implementation verified with a new example. FFI implementation verified with Node implementation in https://github.com/livekit/node-sdks/pull/276. Both tested locally and against cloud.

bcherry commented 1 month ago

@theomonnom I believe this is ready for re-review