1 // Copyright 2021 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/benchmark.h"
16
17 #include <algorithm>
18
19 #include "pw_rpc/internal/config.h"
20
21 namespace pw::rpc {
22 namespace {
23
CopyBuffer(ConstByteSpan input,ByteSpan output)24 StatusWithSize CopyBuffer(ConstByteSpan input, ByteSpan output) {
25 if (input.size() > output.size()) {
26 return pw::StatusWithSize::ResourceExhausted();
27 }
28 std::copy(input.begin(), input.end(), output.begin());
29 return pw::StatusWithSize(input.size());
30 }
31
32 } // namespace
33
UnaryEcho(ConstByteSpan request,RawUnaryResponder & responder)34 void BenchmarkService::UnaryEcho(ConstByteSpan request,
35 RawUnaryResponder& responder) {
36 std::byte response[32];
37 StatusWithSize result = CopyBuffer(request, response);
38 responder.Finish(std::span(response).first(result.size()), result.status())
39 .IgnoreError();
40 }
41
BidirectionalEcho(RawServerReaderWriter & new_reader_writer)42 void BenchmarkService::BidirectionalEcho(
43 RawServerReaderWriter& new_reader_writer) {
44 reader_writer_ = std::move(new_reader_writer);
45
46 reader_writer_.set_on_next([this](ConstByteSpan request) {
47 Status status = reader_writer_.Write(request);
48 if (!status.ok()) {
49 reader_writer_.Finish(status)
50 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
51 }
52 });
53 }
54
55 } // namespace pw::rpc
56