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
15 #include "pw_rpc/internal/base_client_call.h"
16
17 #include "pw_rpc/client.h"
18
19 namespace pw::rpc::internal {
20
Cancel()21 void BaseClientCall::Cancel() {
22 if (active()) {
23 channel_->Send(NewPacket(PacketType::CANCEL_SERVER_STREAM));
24 }
25 }
26
AcquirePayloadBuffer()27 std::span<std::byte> BaseClientCall::AcquirePayloadBuffer() {
28 if (!active()) {
29 return {};
30 }
31
32 request_ = channel_->AcquireBuffer();
33 return request_.payload(NewPacket(PacketType::REQUEST));
34 }
35
ReleasePayloadBuffer(std::span<const std::byte> payload)36 Status BaseClientCall::ReleasePayloadBuffer(
37 std::span<const std::byte> payload) {
38 if (!active()) {
39 return Status::FailedPrecondition();
40 }
41
42 return channel_->Send(request_, NewPacket(PacketType::REQUEST, payload));
43 }
44
NewPacket(PacketType type,std::span<const std::byte> payload) const45 Packet BaseClientCall::NewPacket(PacketType type,
46 std::span<const std::byte> payload) const {
47 return Packet(type, channel_->id(), service_id_, method_id_, payload);
48 }
49
Register()50 void BaseClientCall::Register() { channel_->client()->RegisterCall(*this); }
51
Unregister()52 void BaseClientCall::Unregister() {
53 if (active()) {
54 channel_->client()->RemoveCall(*this);
55 active_ = false;
56 }
57 }
58
59 } // namespace pw::rpc::internal
60