Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -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 | |
Wyatt Hepler | 74e7ab6 | 2021-06-15 19:44:32 -0700 | [diff] [blame] | 15 | #include "pw_rpc/internal/responder.h" |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 16 | |
Wyatt Hepler | f298de4 | 2021-03-19 15:06:36 -0700 | [diff] [blame] | 17 | #include "pw_assert/check.h" |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 18 | #include "pw_rpc/internal/method.h" |
| 19 | #include "pw_rpc/internal/packet.h" |
Wyatt Hepler | 60fd70f | 2020-06-17 09:04:25 -0700 | [diff] [blame] | 20 | #include "pw_rpc/internal/server.h" |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 21 | |
| 22 | namespace pw::rpc::internal { |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
Wyatt Hepler | 29332d9 | 2021-09-02 07:49:53 -0700 | [diff] [blame] | 25 | Packet ResponsePacket(const CallContext& call, |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 26 | std::span<const std::byte> payload, |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 27 | Status status) { |
| 28 | return Packet(PacketType::RESPONSE, |
| 29 | call.channel().id(), |
| 30 | call.service().id(), |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 31 | call.method().id(), |
| 32 | payload, |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 33 | status); |
| 34 | } |
| 35 | |
Wyatt Hepler | 29332d9 | 2021-09-02 07:49:53 -0700 | [diff] [blame] | 36 | Packet StreamPacket(const CallContext& call, |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 37 | std::span<const std::byte> payload) { |
| 38 | return Packet(PacketType::SERVER_STREAM, |
| 39 | call.channel().id(), |
| 40 | call.service().id(), |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 41 | call.method().id(), |
Wyatt Hepler | 5ba8064 | 2021-06-18 12:56:17 -0700 | [diff] [blame] | 42 | payload); |
| 43 | } |
| 44 | |
| 45 | } // namespace |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 46 | |
Wyatt Hepler | 29332d9 | 2021-09-02 07:49:53 -0700 | [diff] [blame] | 47 | Responder::Responder(const CallContext& call, MethodType type) |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 48 | : call_(call), |
| 49 | rpc_state_(kOpen), |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 50 | type_(type), |
| 51 | client_stream_state_(HasClientStream(type) ? kClientStreamOpen |
| 52 | : kClientStreamClosed) { |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 53 | call_.server().RegisterResponder(*this); |
Wyatt Hepler | 60fd70f | 2020-06-17 09:04:25 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 56 | Responder& Responder::operator=(Responder&& other) { |
Wyatt Hepler | 68daa72 | 2021-07-15 17:10:15 -0700 | [diff] [blame] | 57 | // If this RPC was running, complete it before moving in the other RPC. |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 58 | CloseAndSendResponse(OkStatus()).IgnoreError(); |
Wyatt Hepler | b7643d7 | 2020-07-10 10:33:14 -0700 | [diff] [blame] | 59 | |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 60 | // Move the state variables, which may change when the other client closes. |
| 61 | rpc_state_ = other.rpc_state_; |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 62 | type_ = other.type_; |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 63 | client_stream_state_ = other.client_stream_state_; |
Wyatt Hepler | b7643d7 | 2020-07-10 10:33:14 -0700 | [diff] [blame] | 64 | |
| 65 | if (other.open()) { |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 66 | other.Close(); |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 67 | other.call_.server().RegisterResponder(*this); |
Wyatt Hepler | b7643d7 | 2020-07-10 10:33:14 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 70 | // Move the rest of the member variables. |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 71 | call_ = std::move(other.call_); |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 72 | response_ = std::move(other.response_); |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 73 | |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 74 | on_error_ = std::move(other.on_error_); |
| 75 | on_next_ = std::move(other.on_next_); |
Wyatt Hepler | 379b2fc | 2021-06-22 22:54:33 -0700 | [diff] [blame] | 76 | |
| 77 | #if PW_RPC_CLIENT_STREAM_END_CALLBACK |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 78 | on_client_stream_end_ = std::move(other.on_client_stream_end_); |
Wyatt Hepler | 379b2fc | 2021-06-22 22:54:33 -0700 | [diff] [blame] | 79 | #endif // PW_RPC_CLIENT_STREAM_END_CALLBACK |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 80 | |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 81 | return *this; |
| 82 | } |
| 83 | |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 84 | uint32_t Responder::method_id() const { return call_.method().id(); } |
Wyatt Hepler | af83568 | 2020-06-17 11:42:53 -0700 | [diff] [blame] | 85 | |
Wyatt Hepler | 59b37f7 | 2021-06-15 16:23:44 -0700 | [diff] [blame] | 86 | Status Responder::CloseAndSendResponse(std::span<const std::byte> response, |
| 87 | Status status) { |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 88 | if (!open()) { |
Wyatt Hepler | d08e582 | 2021-02-18 17:53:38 -0800 | [diff] [blame] | 89 | return Status::FailedPrecondition(); |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 90 | } |
Wyatt Hepler | 671946e | 2020-06-09 14:39:33 -0700 | [diff] [blame] | 91 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 92 | Status packet_status; |
Wyatt Hepler | 59b37f7 | 2021-06-15 16:23:44 -0700 | [diff] [blame] | 93 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 94 | // Acquire a buffer to use for the outgoing packet if none is available. |
| 95 | if (response_.empty()) { |
| 96 | response_ = call_.channel().AcquireBuffer(); |
Alexei Frolov | b3701c2 | 2020-11-04 11:30:47 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 99 | // Send a packet indicating that the RPC has terminated and optionally |
| 100 | // containing the final payload. |
| 101 | packet_status = |
| 102 | call_.channel().Send(response_, ResponsePacket(call_, response, status)); |
| 103 | |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 104 | Close(); |
Wyatt Hepler | af83568 | 2020-06-17 11:42:53 -0700 | [diff] [blame] | 105 | |
Wyatt Hepler | 59b37f7 | 2021-06-15 16:23:44 -0700 | [diff] [blame] | 106 | return packet_status; |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 109 | std::span<std::byte> Responder::AcquirePayloadBuffer() { |
Wyatt Hepler | d08e582 | 2021-02-18 17:53:38 -0800 | [diff] [blame] | 110 | PW_DCHECK(open()); |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 111 | |
Alexei Frolov | b3701c2 | 2020-11-04 11:30:47 -0800 | [diff] [blame] | 112 | // Only allow having one active buffer at a time. |
| 113 | if (response_.empty()) { |
| 114 | response_ = call_.channel().AcquireBuffer(); |
| 115 | } |
| 116 | |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 117 | return response_.payload(StreamPacket(call_, {})); |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 120 | Status Responder::SendPayloadBufferClientStream( |
| 121 | std::span<const std::byte> payload) { |
Wyatt Hepler | d08e582 | 2021-02-18 17:53:38 -0800 | [diff] [blame] | 122 | PW_DCHECK(open()); |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 123 | return call_.channel().Send(response_, StreamPacket(call_, payload)); |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 126 | void Responder::ReleasePayloadBuffer() { |
Wyatt Hepler | d08e582 | 2021-02-18 17:53:38 -0800 | [diff] [blame] | 127 | PW_DCHECK(open()); |
Alexei Frolov | 909bdfa | 2020-11-03 16:21:47 -0800 | [diff] [blame] | 128 | call_.channel().Release(response_); |
Alexei Frolov | 909bdfa | 2020-11-03 16:21:47 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 131 | void Responder::Close() { |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 132 | PW_DCHECK(open()); |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 133 | |
Wyatt Hepler | 5180fe2 | 2021-06-15 19:32:08 -0700 | [diff] [blame] | 134 | call_.server().RemoveResponder(*this); |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 135 | rpc_state_ = kClosed; |
| 136 | client_stream_state_ = kClientStreamClosed; |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 139 | } // namespace pw::rpc::internal |