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(span(response).first(result.size()), result.status())
39 .IgnoreError();
40 }
41
AllocateReaderWriterId()42 BenchmarkService::ReaderWriterId BenchmarkService::AllocateReaderWriterId() {
43 return next_reader_writer_id_++;
44 }
45
BidirectionalEcho(RawServerReaderWriter & new_reader_writer)46 void BenchmarkService::BidirectionalEcho(
47 RawServerReaderWriter& new_reader_writer) {
48 auto id = AllocateReaderWriterId();
49
50 struct Captures {
51 BenchmarkService* self;
52 ReaderWriterId id;
53 };
54
55 auto captures = std::make_unique<Captures>(Captures{.self = this, .id = id});
56 new_reader_writer.set_on_next(
57 [context = std::move(captures)](ConstByteSpan request) {
58 auto& reader_writers = context->self->reader_writers_;
59 auto rw_id = context->id;
60 auto reader_writer = reader_writers.find(rw_id);
61 if (reader_writer == reader_writers.end()) {
62 return;
63 }
64 Status status = reader_writer->second.Write(request);
65 if (!status.ok()) {
66 reader_writer->second.Finish(status)
67 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
68 reader_writers.erase(rw_id);
69 }
70 });
71 reader_writers_.insert({id, std::move(new_reader_writer)});
72 }
73
74 } // namespace pw::rpc
75