blob: 942cf326e7e8b0195c9c5882cc5116bca81ec2c4 [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 Heplercb9d9572020-06-01 11:25:58 -070017#include "pw_rpc/internal/method.h"
18#include "pw_rpc/internal/packet.h"
Wyatt Hepler60fd70f2020-06-17 09:04:25 -070019#include "pw_rpc/internal/server.h"
Wyatt Heplercb9d9572020-06-01 11:25:58 -070020
21namespace pw::rpc::internal {
22
Wyatt Hepler60fd70f2020-06-17 09:04:25 -070023BaseServerWriter::BaseServerWriter(ServerCall& call)
24 : call_(call), state_(kOpen) {
25 call_.server().RegisterWriter(*this);
26}
27
Wyatt Heplercb9d9572020-06-01 11:25:58 -070028BaseServerWriter& BaseServerWriter::operator=(BaseServerWriter&& other) {
Wyatt Heplerb7643d72020-07-10 10:33:14 -070029 Finish();
30
31 state_ = other.state_;
32
33 if (other.open()) {
34 other.call_.server().RemoveWriter(other);
35 other.state_ = kClosed;
36
37 other.call_.server().RegisterWriter(*this);
38 }
39
Wyatt Hepler671946e2020-06-09 14:39:33 -070040 call_ = std::move(other.call_);
Wyatt Heplercb9d9572020-06-01 11:25:58 -070041 response_ = std::move(other.response_);
Wyatt Hepler671946e2020-06-09 14:39:33 -070042
Wyatt Heplercb9d9572020-06-01 11:25:58 -070043 return *this;
44}
45
Wyatt Hepleraf835682020-06-17 11:42:53 -070046uint32_t BaseServerWriter::method_id() const { return call_.method().id(); }
47
Wyatt Hepler712d3672020-07-13 15:52:11 -070048void BaseServerWriter::Finish(Status status) {
Wyatt Hepler671946e2020-06-09 14:39:33 -070049 if (!open()) {
50 return;
Wyatt Heplercb9d9572020-06-01 11:25:58 -070051 }
Wyatt Hepler671946e2020-06-09 14:39:33 -070052
Alexei Frolov062ed182020-09-28 16:23:06 -070053 Close();
Wyatt Hepleraf835682020-06-17 11:42:53 -070054
Wyatt Hepler712d3672020-07-13 15:52:11 -070055 // Send a control packet indicating that the stream (and RPC) has terminated.
Wyatt Hepler0f262352020-07-29 09:51:27 -070056 call_.channel().Send(Packet(PacketType::SERVER_STREAM_END,
Wyatt Hepleraf835682020-06-17 11:42:53 -070057 call_.channel().id(),
58 call_.service().id(),
Wyatt Hepler712d3672020-07-13 15:52:11 -070059 method().id(),
60 {},
61 status));
Wyatt Heplercb9d9572020-06-01 11:25:58 -070062}
63
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070064std::span<std::byte> BaseServerWriter::AcquirePayloadBuffer() {
Wyatt Heplercb9d9572020-06-01 11:25:58 -070065 if (!open()) {
66 return {};
67 }
68
Wyatt Hepler671946e2020-06-09 14:39:33 -070069 response_ = call_.channel().AcquireBuffer();
Wyatt Hepler0f262352020-07-29 09:51:27 -070070 return response_.payload(ResponsePacket());
Wyatt Heplercb9d9572020-06-01 11:25:58 -070071}
72
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070073Status BaseServerWriter::ReleasePayloadBuffer(
74 std::span<const std::byte> payload) {
Wyatt Heplercb9d9572020-06-01 11:25:58 -070075 if (!open()) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -070076 return Status::FailedPrecondition();
Wyatt Heplercb9d9572020-06-01 11:25:58 -070077 }
Wyatt Hepler0f262352020-07-29 09:51:27 -070078 return call_.channel().Send(response_, ResponsePacket(payload));
Wyatt Heplercb9d9572020-06-01 11:25:58 -070079}
80
Alexei Frolov062ed182020-09-28 16:23:06 -070081void BaseServerWriter::Close() {
82 if (!open()) {
83 return;
84 }
85
86 call_.server().RemoveWriter(*this);
87 state_ = kClosed;
88}
89
Wyatt Hepler0f262352020-07-29 09:51:27 -070090Packet BaseServerWriter::ResponsePacket(
91 std::span<const std::byte> payload) const {
92 return Packet(PacketType::RESPONSE,
Wyatt Hepler671946e2020-06-09 14:39:33 -070093 call_.channel().id(),
94 call_.service().id(),
95 method().id(),
96 payload);
Wyatt Heplercb9d9572020-06-01 11:25:58 -070097}
98
99} // namespace pw::rpc::internal