blob: 9d4423d5039a415b606ea0578166ed893a1d7f68 [file] [log] [blame]
Wyatt Heplercb9d9572020-06-01 11:25:58 -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_server_writer.h"
16
Wyatt Heplerd08e5822021-02-18 17:53:38 -080017#include "pw_assert/assert.h"
Wyatt Heplercb9d9572020-06-01 11:25:58 -070018#include "pw_rpc/internal/method.h"
19#include "pw_rpc/internal/packet.h"
Wyatt Hepler60fd70f2020-06-17 09:04:25 -070020#include "pw_rpc/internal/server.h"
Wyatt Heplercb9d9572020-06-01 11:25:58 -070021
22namespace pw::rpc::internal {
23
Wyatt Hepler60fd70f2020-06-17 09:04:25 -070024BaseServerWriter::BaseServerWriter(ServerCall& call)
25 : call_(call), state_(kOpen) {
26 call_.server().RegisterWriter(*this);
27}
28
Wyatt Heplercb9d9572020-06-01 11:25:58 -070029BaseServerWriter& BaseServerWriter::operator=(BaseServerWriter&& other) {
Wyatt Heplerb7643d72020-07-10 10:33:14 -070030 Finish();
31
32 state_ = other.state_;
33
34 if (other.open()) {
35 other.call_.server().RemoveWriter(other);
36 other.state_ = kClosed;
37
38 other.call_.server().RegisterWriter(*this);
39 }
40
Wyatt Hepler671946e2020-06-09 14:39:33 -070041 call_ = std::move(other.call_);
Wyatt Heplercb9d9572020-06-01 11:25:58 -070042 response_ = std::move(other.response_);
Wyatt Hepler671946e2020-06-09 14:39:33 -070043
Wyatt Heplercb9d9572020-06-01 11:25:58 -070044 return *this;
45}
46
Wyatt Hepleraf835682020-06-17 11:42:53 -070047uint32_t BaseServerWriter::method_id() const { return call_.method().id(); }
48
Wyatt Heplerd08e5822021-02-18 17:53:38 -080049Status BaseServerWriter::Finish(Status status) {
Wyatt Hepler671946e2020-06-09 14:39:33 -070050 if (!open()) {
Wyatt Heplerd08e5822021-02-18 17:53:38 -080051 return Status::FailedPrecondition();
Wyatt Heplercb9d9572020-06-01 11:25:58 -070052 }
Wyatt Hepler671946e2020-06-09 14:39:33 -070053
Alexei Frolovb3701c22020-11-04 11:30:47 -080054 // If the ServerWriter implementer or user forgets to release an acquired
55 // buffer before finishing, release it here.
56 if (!response_.empty()) {
57 ReleasePayloadBuffer();
58 }
59
Alexei Frolov062ed182020-09-28 16:23:06 -070060 Close();
Wyatt Hepleraf835682020-06-17 11:42:53 -070061
Wyatt Hepler712d3672020-07-13 15:52:11 -070062 // Send a control packet indicating that the stream (and RPC) has terminated.
Wyatt Heplerd08e5822021-02-18 17:53:38 -080063 return call_.channel().Send(Packet(PacketType::SERVER_STREAM_END,
64 call_.channel().id(),
65 call_.service().id(),
66 method().id(),
67 {},
68 status));
Wyatt Heplercb9d9572020-06-01 11:25:58 -070069}
70
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070071std::span<std::byte> BaseServerWriter::AcquirePayloadBuffer() {
Wyatt Heplerd08e5822021-02-18 17:53:38 -080072 PW_DCHECK(open());
Wyatt Heplercb9d9572020-06-01 11:25:58 -070073
Alexei Frolovb3701c22020-11-04 11:30:47 -080074 // Only allow having one active buffer at a time.
75 if (response_.empty()) {
76 response_ = call_.channel().AcquireBuffer();
77 }
78
Wyatt Hepler0f262352020-07-29 09:51:27 -070079 return response_.payload(ResponsePacket());
Wyatt Heplercb9d9572020-06-01 11:25:58 -070080}
81
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070082Status BaseServerWriter::ReleasePayloadBuffer(
83 std::span<const std::byte> payload) {
Wyatt Heplerd08e5822021-02-18 17:53:38 -080084 PW_DCHECK(open());
Wyatt Hepler0f262352020-07-29 09:51:27 -070085 return call_.channel().Send(response_, ResponsePacket(payload));
Wyatt Heplercb9d9572020-06-01 11:25:58 -070086}
87
Alexei Frolov909bdfa2020-11-03 16:21:47 -080088Status BaseServerWriter::ReleasePayloadBuffer() {
Wyatt Heplerd08e5822021-02-18 17:53:38 -080089 PW_DCHECK(open());
Alexei Frolov909bdfa2020-11-03 16:21:47 -080090 call_.channel().Release(response_);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080091 return OkStatus();
Alexei Frolov909bdfa2020-11-03 16:21:47 -080092}
93
Alexei Frolov062ed182020-09-28 16:23:06 -070094void BaseServerWriter::Close() {
95 if (!open()) {
96 return;
97 }
98
99 call_.server().RemoveWriter(*this);
100 state_ = kClosed;
101}
102
Wyatt Hepler0f262352020-07-29 09:51:27 -0700103Packet BaseServerWriter::ResponsePacket(
104 std::span<const std::byte> payload) const {
105 return Packet(PacketType::RESPONSE,
Wyatt Hepler671946e2020-06-09 14:39:33 -0700106 call_.channel().id(),
107 call_.service().id(),
108 method().id(),
109 payload);
Wyatt Heplercb9d9572020-06-01 11:25:58 -0700110}
111
112} // namespace pw::rpc::internal