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 | ff2ca83 | 2021-09-03 17:20:40 -0700 | [diff] [blame] | 15 | #include "pw_rpc/internal/call.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 | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 18 | #include "pw_rpc/client.h" |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 19 | #include "pw_rpc/internal/endpoint.h" |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 20 | #include "pw_rpc/internal/method.h" |
Wyatt Hepler | ddfc077 | 2021-09-03 16:46:25 -0700 | [diff] [blame] | 21 | #include "pw_rpc/server.h" |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 22 | |
| 23 | namespace pw::rpc::internal { |
| 24 | |
Alexei Frolov | 86e05de | 2021-10-19 16:52:31 -0700 | [diff] [blame] | 25 | // Creates an active client-side call, assigning it a new ID. |
| 26 | Call::Call(Endpoint& client, |
| 27 | uint32_t channel_id, |
| 28 | uint32_t service_id, |
| 29 | uint32_t method_id, |
| 30 | MethodType type) |
| 31 | : Call(client, |
| 32 | client.NewCallId(), |
| 33 | channel_id, |
| 34 | service_id, |
| 35 | method_id, |
| 36 | type, |
| 37 | kClientCall) {} |
| 38 | |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 39 | Call::Call(Endpoint& endpoint_ref, |
Alexei Frolov | 86e05de | 2021-10-19 16:52:31 -0700 | [diff] [blame] | 40 | uint32_t call_id, |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 41 | uint32_t channel_id, |
| 42 | uint32_t service_id, |
| 43 | uint32_t method_id, |
| 44 | MethodType type, |
| 45 | CallType call_type) |
| 46 | : endpoint_(&endpoint_ref), |
Wyatt Hepler | 592b5a1 | 2022-02-01 17:55:21 -0800 | [diff] [blame] | 47 | channel_id_(channel_id), |
Alexei Frolov | 86e05de | 2021-10-19 16:52:31 -0700 | [diff] [blame] | 48 | id_(call_id), |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 49 | service_id_(service_id), |
| 50 | method_id_(method_id), |
Wyatt Hepler | 5a3a36b | 2021-09-08 11:15:05 -0700 | [diff] [blame] | 51 | rpc_state_(kActive), |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame] | 52 | type_(type), |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 53 | call_type_(call_type), |
Wyatt Hepler | 5a3a36b | 2021-09-08 11:15:05 -0700 | [diff] [blame] | 54 | client_stream_state_(HasClientStream(type) ? kClientStreamActive |
| 55 | : kClientStreamInactive) { |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 56 | endpoint().RegisterCall(*this); |
Wyatt Hepler | 60fd70f | 2020-06-17 09:04:25 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 59 | void Call::MoveFrom(Call& other) { |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 60 | PW_DCHECK(!active_locked()); |
Wyatt Hepler | b7643d7 | 2020-07-10 10:33:14 -0700 | [diff] [blame] | 61 | |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 62 | if (!other.active_locked()) { |
Wyatt Hepler | a215415 | 2021-10-21 08:09:30 -0700 | [diff] [blame] | 63 | return; // Nothing else to do; this call is already closed. |
| 64 | } |
Wyatt Hepler | b7643d7 | 2020-07-10 10:33:14 -0700 | [diff] [blame] | 65 | |
Wyatt Hepler | a215415 | 2021-10-21 08:09:30 -0700 | [diff] [blame] | 66 | // Copy all members from the other call. |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 67 | endpoint_ = other.endpoint_; |
Wyatt Hepler | 592b5a1 | 2022-02-01 17:55:21 -0800 | [diff] [blame] | 68 | channel_id_ = other.channel_id_; |
Alexei Frolov | 86e05de | 2021-10-19 16:52:31 -0700 | [diff] [blame] | 69 | id_ = other.id_; |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 70 | service_id_ = other.service_id_; |
| 71 | method_id_ = other.method_id_; |
| 72 | |
Wyatt Hepler | a215415 | 2021-10-21 08:09:30 -0700 | [diff] [blame] | 73 | rpc_state_ = other.rpc_state_; |
| 74 | type_ = other.type_; |
| 75 | call_type_ = other.call_type_; |
| 76 | client_stream_state_ = other.client_stream_state_; |
Wyatt Hepler | 960f5df | 2021-09-08 10:17:21 -0700 | [diff] [blame] | 77 | |
Wyatt Hepler | 01fc15b | 2021-06-10 18:15:59 -0700 | [diff] [blame] | 78 | on_error_ = std::move(other.on_error_); |
| 79 | on_next_ = std::move(other.on_next_); |
Wyatt Hepler | a215415 | 2021-10-21 08:09:30 -0700 | [diff] [blame] | 80 | |
| 81 | // Mark the other call inactive, unregister it, and register this one. |
| 82 | other.rpc_state_ = kInactive; |
| 83 | other.client_stream_state_ = kClientStreamInactive; |
| 84 | |
| 85 | endpoint().UnregisterCall(other); |
| 86 | endpoint().RegisterUniqueCall(*this); |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Wyatt Hepler | 592b5a1 | 2022-02-01 17:55:21 -0800 | [diff] [blame] | 89 | Status Call::SendPacket(PacketType type, ConstByteSpan payload, Status status) { |
Wyatt Hepler | e4ce994 | 2022-02-02 09:16:20 -0800 | [diff] [blame] | 90 | if (!active_locked()) { |
| 91 | return Status::FailedPrecondition(); |
| 92 | } |
| 93 | |
Wyatt Hepler | 592b5a1 | 2022-02-01 17:55:21 -0800 | [diff] [blame] | 94 | Channel* channel = endpoint_->GetInternalChannel(channel_id_); |
| 95 | if (channel == nullptr) { |
| 96 | return Status::Unavailable(); |
| 97 | } |
| 98 | return channel->Send(MakePacket(type, payload, status)); |
| 99 | } |
| 100 | |
Wyatt Hepler | 0c87be5 | 2022-01-13 08:33:43 -0800 | [diff] [blame] | 101 | Status Call::CloseAndSendFinalPacketLocked(PacketType type, |
| 102 | ConstByteSpan response, |
| 103 | Status status) { |
Wyatt Hepler | e4ce994 | 2022-02-02 09:16:20 -0800 | [diff] [blame] | 104 | const Status send_status = SendPacket(type, response, status); |
Wyatt Hepler | 0c87be5 | 2022-01-13 08:33:43 -0800 | [diff] [blame] | 105 | UnregisterAndMarkClosed(); |
Wyatt Hepler | e4ce994 | 2022-02-02 09:16:20 -0800 | [diff] [blame] | 106 | return send_status; |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Wyatt Hepler | af16dbb | 2022-01-27 20:15:15 -0800 | [diff] [blame] | 109 | Status Call::WriteLocked(ConstByteSpan payload) { |
Wyatt Hepler | 82db4b1 | 2021-09-23 09:10:12 -0700 | [diff] [blame] | 110 | return SendPacket(call_type_ == kServerCall ? PacketType::SERVER_STREAM |
| 111 | : PacketType::CLIENT_STREAM, |
| 112 | payload); |
| 113 | } |
| 114 | |
Wyatt Hepler | 0c87be5 | 2022-01-13 08:33:43 -0800 | [diff] [blame] | 115 | void Call::UnregisterAndMarkClosed() { |
Wyatt Hepler | b1c4799 | 2022-01-06 11:05:50 -0800 | [diff] [blame] | 116 | if (active_locked()) { |
Wyatt Hepler | a215415 | 2021-10-21 08:09:30 -0700 | [diff] [blame] | 117 | endpoint().UnregisterCall(*this); |
Wyatt Hepler | 3d57eaa | 2022-02-01 18:31:07 -0800 | [diff] [blame] | 118 | MarkClosed(); |
Wyatt Hepler | a215415 | 2021-10-21 08:09:30 -0700 | [diff] [blame] | 119 | } |
Alexei Frolov | 062ed18 | 2020-09-28 16:23:06 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Wyatt Hepler | cb9d957 | 2020-06-01 11:25:58 -0700 | [diff] [blame] | 122 | } // namespace pw::rpc::internal |