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_BROKER_H_ 6 #define CAST_STREAMING_RPC_BROKER_H_ 7 8 #include <vector> 9 10 #include "cast/streaming/remoting.pb.h" 11 #include "util/flat_map.h" 12 13 namespace openscreen { 14 namespace cast { 15 16 // Processes incoming and outgoing RPC messages and links them to desired 17 // components on both end points. For outgoing messages, the messager 18 // must send an RPC message with associated handle value. On the messagee side, 19 // the message is sent to a pre-registered component using that handle. 20 // Before RPC communication starts, both sides need to negotiate the handle 21 // value in the existing RPC communication channel using the special handles 22 // |kAcquire*Handle|. 23 // 24 // NOTE: RpcBroker doesn't actually send RPC messages to the remote. The session 25 // messager needs to set SendMessageCallback, and call ProcessMessageFromRemote 26 // as appropriate. The RpcBroker then distributes each RPC message to the 27 // subscribed component. 28 class RpcBroker { 29 public: 30 using Handle = int; 31 using ReceiveMessageCallback = std::function<void(const RpcMessage&)>; 32 using SendMessageCallback = std::function<void(std::vector<uint8_t>)>; 33 34 explicit RpcBroker(SendMessageCallback send_message_cb); 35 RpcBroker(const RpcBroker&) = delete; 36 RpcBroker(RpcBroker&&) noexcept; 37 RpcBroker& operator=(const RpcBroker&) = delete; 38 RpcBroker& operator=(RpcBroker&&); 39 ~RpcBroker(); 40 41 // Get unique handle value for RPC message handles. 42 Handle GetUniqueHandle(); 43 44 // Register a component to receive messages via the given 45 // ReceiveMessageCallback. |handle| is a unique handle value provided by a 46 // prior call to GetUniqueHandle() and is used to reference the component in 47 // the RPC messages. The receiver can then use it to direct an RPC message 48 // back to a specific component. 49 void RegisterMessageReceiverCallback(Handle handle, 50 ReceiveMessageCallback callback); 51 52 // Allows components to unregister in order to stop receiving message. 53 void UnregisterMessageReceiverCallback(Handle handle); 54 55 // Distributes an incoming RPC message to the registered (if any) component. 56 void ProcessMessageFromRemote(const RpcMessage& message); 57 58 // Executes the |send_message_cb_| using |message|. 59 void SendMessageToRemote(const RpcMessage& message); 60 61 // Checks if the handle is registered for receiving messages. Test-only. 62 bool IsRegisteredForTesting(Handle handle); 63 64 // Predefined invalid handle value for RPC message. 65 static constexpr Handle kInvalidHandle = -1; 66 67 // Predefined handle values for RPC messages related to initialization (before 68 // the receiver handle(s) are known). 69 static constexpr Handle kAcquireRendererHandle = 0; 70 static constexpr Handle kAcquireDemuxerHandle = 1; 71 72 // The first handle to return from GetUniqueHandle(). 73 static constexpr Handle kFirstHandle = 100; 74 75 private: 76 // Next unique handle to return from GetUniqueHandle(). 77 Handle next_handle_; 78 79 // Maps of handle values to associated MessageReceivers. 80 FlatMap<Handle, ReceiveMessageCallback> receive_callbacks_; 81 82 // Callback that is ran to send a serialized message. 83 SendMessageCallback send_message_cb_; 84 }; 85 86 } // namespace cast 87 } // namespace openscreen 88 89 #endif // CAST_STREAMING_RPC_BROKER_H_ 90