1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstddef> 17 #include <span> 18 19 #include "pw_bytes/span.h" 20 #include "pw_rpc/internal/base_client_call.h" 21 #include "pw_rpc/internal/channel.h" 22 23 namespace pw::rpc { 24 25 class Client { 26 public: 27 // Creates a client that uses a set of RPC channels. Channels can be shared 28 // between a client and a server, but not between multiple clients. Client(std::span<Channel> channels)29 constexpr Client(std::span<Channel> channels) 30 : channels_(static_cast<internal::Channel*>(channels.data()), 31 channels.size()) { 32 for (Channel& channel : channels_) { 33 channel.set_client(this); 34 }; 35 } 36 37 // Processes an incoming RPC packet. The packet may be an RPC response or a 38 // control packet, the result of which is processed in this function. Returns 39 // whether the packet was able to be processed: 40 // 41 // OK - The packet was processed by the client. 42 // DATA_LOSS - Failed to decode the packet. 43 // INVALID_ARGUMENT - The packet is intended for a server, not a client. 44 // NOT_FOUND - The packet belongs to an unknown RPC call. 45 // UNIMPLEMENTED - Received a type of packet that the client doesn't know 46 // how to handle. 47 // 48 Status ProcessPacket(ConstByteSpan data); 49 active_calls()50 size_t active_calls() const { return calls_.size(); } 51 52 private: 53 friend class internal::BaseClientCall; 54 55 Status RegisterCall(internal::BaseClientCall& call); RemoveCall(const internal::BaseClientCall & call)56 void RemoveCall(const internal::BaseClientCall& call) { calls_.remove(call); } 57 58 std::span<internal::Channel> channels_; 59 IntrusiveList<internal::BaseClientCall> calls_; 60 }; 61 62 } // namespace pw::rpc 63