Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 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 | |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 21 | BaseClientCall& 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 Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 44 | void BaseClientCall::Cancel() { |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 45 | if (active()) { |
Wyatt Hepler | a921116 | 2021-06-12 15:40:11 -0700 | [diff] [blame^] | 46 | channel_->Send(NewPacket(PacketType::CANCEL)); |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 47 | } |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | std::span<std::byte> BaseClientCall::AcquirePayloadBuffer() { |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 51 | if (!active()) { |
| 52 | return {}; |
| 53 | } |
| 54 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 55 | request_ = channel_->AcquireBuffer(); |
| 56 | return request_.payload(NewPacket(PacketType::REQUEST)); |
| 57 | } |
| 58 | |
| 59 | Status BaseClientCall::ReleasePayloadBuffer( |
| 60 | std::span<const std::byte> payload) { |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 61 | if (!active()) { |
| 62 | return Status::FailedPrecondition(); |
| 63 | } |
| 64 | |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 65 | return channel_->Send(request_, NewPacket(PacketType::REQUEST, payload)); |
| 66 | } |
| 67 | |
| 68 | Packet 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 | |
| 73 | void BaseClientCall::Register() { channel_->client()->RegisterCall(*this); } |
| 74 | |
Alexei Frolov | d727622 | 2020-10-01 12:41:59 -0700 | [diff] [blame] | 75 | void BaseClientCall::Unregister() { |
| 76 | if (active()) { |
| 77 | channel_->client()->RemoveCall(*this); |
| 78 | active_ = false; |
| 79 | } |
| 80 | } |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 81 | |
| 82 | } // namespace pw::rpc::internal |