blob: 2560ab87978e232b958b1fdb232327e767e5f72c [file] [log] [blame]
Alexei Frolov4d2adde2020-08-04 10:19:24 -07001// 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
19namespace pw::rpc::internal {
20
Alexei Frolov2d737bc2021-04-27 23:03:09 -070021BaseClientCall& BaseClientCall::operator=(BaseClientCall&& other) {
22 // If the current client call is active, it must be unregistered from the
23 // client as it will no longer be alive after assignment.
24 Unregister();
25
26 active_ = other.active_;
27
28 if (other.active()) {
29 // If the call being assigned is active, replace it in the client's list
30 // with a reference to the current object.
31 other.Unregister();
32 other.channel_->client()->RegisterCall(*this);
33 }
34
35 channel_ = other.channel_;
36 service_id_ = other.service_id_;
37 method_id_ = other.method_id_;
38 request_ = std::move(other.request_);
39 handler_ = other.handler_;
40
41 return *this;
42}
43
Alexei Frolov4d2adde2020-08-04 10:19:24 -070044void BaseClientCall::Cancel() {
Alexei Frolovd7276222020-10-01 12:41:59 -070045 if (active()) {
Wyatt Heplera9211162021-06-12 15:40:11 -070046 channel_->Send(NewPacket(PacketType::CANCEL));
Alexei Frolovd7276222020-10-01 12:41:59 -070047 }
Alexei Frolov4d2adde2020-08-04 10:19:24 -070048}
49
50std::span<std::byte> BaseClientCall::AcquirePayloadBuffer() {
Alexei Frolovd7276222020-10-01 12:41:59 -070051 if (!active()) {
52 return {};
53 }
54
Alexei Frolov4d2adde2020-08-04 10:19:24 -070055 request_ = channel_->AcquireBuffer();
56 return request_.payload(NewPacket(PacketType::REQUEST));
57}
58
59Status BaseClientCall::ReleasePayloadBuffer(
60 std::span<const std::byte> payload) {
Alexei Frolovd7276222020-10-01 12:41:59 -070061 if (!active()) {
62 return Status::FailedPrecondition();
63 }
64
Alexei Frolov4d2adde2020-08-04 10:19:24 -070065 return channel_->Send(request_, NewPacket(PacketType::REQUEST, payload));
66}
67
68Packet BaseClientCall::NewPacket(PacketType type,
69 std::span<const std::byte> payload) const {
70 return Packet(type, channel_->id(), service_id_, method_id_, payload);
71}
72
73void BaseClientCall::Register() { channel_->client()->RegisterCall(*this); }
74
Alexei Frolovd7276222020-10-01 12:41:59 -070075void BaseClientCall::Unregister() {
76 if (active()) {
77 channel_->client()->RemoveCall(*this);
78 active_ = false;
79 }
80}
Alexei Frolov4d2adde2020-08-04 10:19:24 -070081
82} // namespace pw::rpc::internal