xref: /aosp_15_r20/external/openscreen/cast/streaming/rpc_messenger.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_STREAMING_RPC_MESSENGER_H_
6 #define CAST_STREAMING_RPC_MESSENGER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include "cast/streaming/remoting.pb.h"
14 #include "util/flat_map.h"
15 #include "util/weak_ptr.h"
16 
17 namespace openscreen {
18 namespace cast {
19 
20 // Processes incoming and outgoing RPC messages and links them to desired
21 // components on both end points. For outgoing messages, the messenger
22 // must send an RPC message with associated handle value. On the messagee side,
23 // the message is sent to a pre-registered component using that handle.
24 // Before RPC communication starts, both sides need to negotiate the handle
25 // value in the existing RPC communication channel using the special handles
26 // |kAcquire*Handle|.
27 //
28 // NOTE: RpcMessenger doesn't actually send RPC messages to the remote. The session
29 // messenger needs to set SendMessageCallback, and call ProcessMessageFromRemote
30 // as appropriate. The RpcMessenger then distributes each RPC message to the
31 // subscribed component.
32 class RpcMessenger {
33  public:
34   using Handle = int;
35   using ReceiveMessageCallback =
36       std::function<void(std::unique_ptr<RpcMessage>)>;
37   using SendMessageCallback = std::function<void(std::vector<uint8_t>)>;
38 
39   explicit RpcMessenger(SendMessageCallback send_message_cb);
40   RpcMessenger(const RpcMessenger&) = delete;
41   RpcMessenger(RpcMessenger&&) noexcept;
42   RpcMessenger& operator=(const RpcMessenger&) = delete;
43   RpcMessenger& operator=(RpcMessenger&&);
44   ~RpcMessenger();
45 
46   // Get unique handle value for RPC message handles.
47   Handle GetUniqueHandle();
48 
49   // Register a component to receive messages via the given
50   // ReceiveMessageCallback. |handle| is a unique handle value provided by a
51   // prior call to GetUniqueHandle() and is used to reference the component in
52   // the RPC messages. The receiver can then use it to direct an RPC message
53   // back to a specific component.
54   void RegisterMessageReceiverCallback(Handle handle,
55                                        ReceiveMessageCallback callback);
56 
57   // Allows components to unregister in order to stop receiving message.
58   void UnregisterMessageReceiverCallback(Handle handle);
59 
60   // Distributes an incoming RPC message to the registered (if any) component.
61   // The |serialized_message| should be already base64-decoded and ready for
62   // deserialization by protobuf.
63   void ProcessMessageFromRemote(const uint8_t* message,
64                                 std::size_t message_len);
65   // This overload distributes an already-deserialized message to the
66   // registered component.
67   void ProcessMessageFromRemote(std::unique_ptr<RpcMessage> message);
68 
69   // Executes the |send_message_cb_| using |rpc|.
70   void SendMessageToRemote(const RpcMessage& rpc);
71 
72   // Checks if the handle is registered for receiving messages. Test-only.
73   bool IsRegisteredForTesting(Handle handle);
74 
75   // Weak pointer creator.
76   WeakPtr<RpcMessenger> GetWeakPtr();
77 
78   // Consumers of RPCMessenger may set the send message callback post-hoc
79   // in order to simulate different scenarios.
set_send_message_cb_for_testing(SendMessageCallback cb)80   void set_send_message_cb_for_testing(SendMessageCallback cb) {
81     send_message_cb_ = std::move(cb);
82   }
83 
84   // Predefined invalid handle value for RPC message.
85   static constexpr Handle kInvalidHandle = -1;
86 
87   // Predefined handle values for RPC messages related to initialization (before
88   // the receiver handle(s) are known).
89   static constexpr Handle kAcquireRendererHandle = 0;
90   static constexpr Handle kAcquireDemuxerHandle = 1;
91 
92   // The first handle to return from GetUniqueHandle().
93   static constexpr Handle kFirstHandle = 100;
94 
95  private:
96   // Next unique handle to return from GetUniqueHandle().
97   Handle next_handle_;
98 
99   // Maps of handle values to associated MessageReceivers.
100   FlatMap<Handle, ReceiveMessageCallback> receive_callbacks_;
101 
102   // Callback that is ran to send a serialized message.
103   SendMessageCallback send_message_cb_;
104 
105   WeakPtrFactory<RpcMessenger> weak_factory_{this};
106 };
107 
108 }  // namespace cast
109 }  // namespace openscreen
110 
111 #endif  // CAST_STREAMING_RPC_MESSENGER_H_
112