atm0s_media_server_cluster/define/
rpc.rs

1use std::fmt::Debug;
2
3pub mod connector;
4pub mod gateway;
5pub mod general;
6pub mod sip;
7pub mod webrtc;
8pub mod whep;
9pub mod whip;
10
11pub trait RpcRequest: Send {
12    fn cmd(&self) -> &str;
13    /// Parse into param, if cannot it auto reply with DESERIALIZE_ERROR
14    fn parse<P: for<'a> TryFrom<&'a [u8]> + Send + 'static, R: Into<Vec<u8>> + Send + 'static>(self) -> Option<Box<dyn RpcReqRes<P, R>>>;
15    /// Answer error
16    fn error(self, err: &str);
17}
18
19pub trait RpcReqRes<Param, Res>: Debug + Send {
20    fn param(&self) -> &Param;
21    fn answer(&self, res: Result<Res, &str>);
22}
23
24impl<P: PartialEq, R> Eq for Box<dyn RpcReqRes<P, R>> {}
25
26impl<P: PartialEq, R> PartialEq for Box<dyn RpcReqRes<P, R>> {
27    fn eq(&self, other: &Self) -> bool {
28        self.param().eq(other.param())
29    }
30}
31
32#[async_trait::async_trait]
33pub trait RpcEndpoint<Req: RpcRequest, Emitter: RpcEmitter> {
34    fn emitter(&mut self) -> Emitter;
35    async fn recv(&mut self) -> Option<Req>;
36}
37
38#[derive(Debug, PartialEq, Eq, Clone)]
39pub enum RpcError {
40    Timeout,
41    LocalQueueError,
42    RemoteQueueError,
43    DeserializeError,
44    RuntimeError(String),
45}
46
47#[async_trait::async_trait]
48pub trait RpcEmitter: Clone {
49    fn emit<E: Into<Vec<u8>>>(&self, service: u8, node: Option<u32>, cmd: &str, event: E);
50    async fn request<Req: Into<Vec<u8>> + Send, Res: for<'a> TryFrom<&'a [u8]> + Send>(&self, service: u8, node: Option<u32>, cmd: &str, req: Req, timeout_ms: u64) -> Result<Res, RpcError>;
51}
52
53pub const RPC_WEBRTC_CONNECT: &str = "RPC_WEBRTC_CONNECT";
54pub const RPC_WEBRTC_ICE: &str = "RPC_WEBRTC_ICE";
55pub const RPC_WEBRTC_PATCH: &str = "RPC_WEBRTC_PATCH";
56pub const RPC_MEDIA_ENDPOINT_CLOSE: &str = "RPC_MEDIA_ENDPOINT_CLOSE";
57pub const RPC_WHIP_CONNECT: &str = "RPC_WHIP_CONNECT";
58pub const RPC_WHEP_CONNECT: &str = "RPC_WHEP_CONNECT";
59pub const RPC_SIP_INVITE_OUTGOING_CLIENT: &str = "RPC_SIP_INVITE_OUTGOING_CLIENT";
60pub const RPC_SIP_INVITE_OUTGOING_SERVER: &str = "RPC_SIP_INVITE_OUTGOING_SERVER";
61pub const RPC_SIP_INCOMING_REGISTER_REQUEST: &str = "RPC_SIP_INCOMING_REGISTER_REQUEST";
62pub const RPC_SIP_INCOMING_REGISTER_RESPONSE: &str = "RPC_SIP_INCOMING_REGISTER_RESPONSE";
63pub const RPC_SIP_INCOMING_SIP_INVITE_REQUEST: &str = "RPC_SIP_INCOMING_SIP_INVITE_REQUEST";
64pub const RPC_SIP_INCOMING_SIP_INVITE_RESPONSE: &str = "RPC_SIP_INCOMING_SIP_INVITE_RESPONSE";
65
66pub const RPC_NODE_PING: &str = "RPC_NODE_PING";
67pub const RPC_NODE_HEALTHCHECK: &str = "RPC_NODE_HEALTHCHECK";
68
69pub const RPC_MEDIA_ENDPOINT_LOG: &str = "RPC_MEDIA_ENDPOINT_LOG";