Skip to main content

workflow_rpc/server/protocol/
mod.rs

1//!
2//! Protocol module containing protocol handlers in charge
3//! of incoming and outgoing message serialization and
4//! RPC method and notification dispatch.
5//!
6
7pub mod borsh;
8pub mod serde_json;
9
10use crate::imports::*;
11use crate::server::Interface;
12pub use crate::server::result::Result;
13use workflow_websocket::server::{Message, Result as WebSocketResult, WebSocketSink};
14
15pub use self::borsh::BorshProtocol;
16pub use self::serde_json::JsonProtocol;
17
18/// Base trait for [`BorshProtocol`] and [`JsonProtocol`] protocol handlers
19#[async_trait]
20pub trait ProtocolHandler<ServerContext, ConnectionContext, Ops>:
21    DowncastSync + Sized + Send + Sync
22where
23    Ops: OpsT,
24    ServerContext: Clone + Send + Sync + 'static,
25    ConnectionContext: Clone + Send + Sync + 'static,
26{
27    /// Construct a protocol handler backed by the given RPC method/notification interface.
28    fn new(methods: Arc<Interface<ServerContext, ConnectionContext, Ops>>) -> Self
29    where
30        Self: Sized;
31
32    /// Return the wire encoding (`Borsh` or `JSON`) implemented by this handler.
33    fn encoding(&self) -> Encoding;
34
35    /// Decode an incoming WebSocket message, dispatch it to the matching method
36    /// or notification handler, and send any response back through the sink.
37    async fn handle_message(
38        &self,
39        connection_ctx: ConnectionContext,
40        message: Message,
41        sink: &WebSocketSink,
42    ) -> WebSocketResult<()>;
43
44    /// Serialize a server-initiated notification for the given operation and
45    /// message into an outgoing WebSocket message.
46    fn serialize_notification_message<Msg>(
47        &self,
48        op: Ops,
49        msg: Msg,
50    ) -> Result<tungstenite::Message>
51    where
52        Msg: BorshSerialize + Serialize + Send + Sync + 'static;
53}