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
15 #include "pw_rpc/raw/internal/method.h"
16
17 #include <cstddef>
18 #include <cstring>
19
20 #include "pw_rpc/internal/packet.h"
21
22 namespace pw::rpc::internal {
23
SynchronousUnaryInvoker(const CallContext & context,const Packet & request)24 void RawMethod::SynchronousUnaryInvoker(const CallContext& context,
25 const Packet& request) {
26 RawUnaryResponder responder(context);
27 rpc_lock().unlock();
28 // TODO(hepler): Remove support for raw synchronous unary methods. Unlike
29 // synchronous Nanopb methods, they provide little value compared to
30 // asynchronous unary methods. For now, just provide a fixed buffer on the
31 // stack.
32 std::byte payload_buffer[64] = {};
33
34 StatusWithSize sws =
35 static_cast<const RawMethod&>(context.method())
36 .function_.synchronous_unary(
37 context.service(), request.payload(), std::span(payload_buffer));
38
39 responder.Finish(std::span(payload_buffer, sws.size()), sws.status())
40 .IgnoreError();
41 }
42
AsynchronousUnaryInvoker(const CallContext & context,const Packet & request)43 void RawMethod::AsynchronousUnaryInvoker(const CallContext& context,
44 const Packet& request) {
45 RawUnaryResponder responder(context);
46 rpc_lock().unlock();
47 static_cast<const RawMethod&>(context.method())
48 .function_.asynchronous_unary(
49 context.service(), request.payload(), responder);
50 }
51
ServerStreamingInvoker(const CallContext & context,const Packet & request)52 void RawMethod::ServerStreamingInvoker(const CallContext& context,
53 const Packet& request) {
54 RawServerWriter server_writer(context);
55 rpc_lock().unlock();
56 static_cast<const RawMethod&>(context.method())
57 .function_.server_streaming(
58 context.service(), request.payload(), server_writer);
59 }
60
ClientStreamingInvoker(const CallContext & context,const Packet &)61 void RawMethod::ClientStreamingInvoker(const CallContext& context,
62 const Packet&) {
63 RawServerReader reader(context);
64 rpc_lock().unlock();
65 static_cast<const RawMethod&>(context.method())
66 .function_.stream_request(context.service(), reader);
67 }
68
BidirectionalStreamingInvoker(const CallContext & context,const Packet &)69 void RawMethod::BidirectionalStreamingInvoker(const CallContext& context,
70 const Packet&) {
71 RawServerReaderWriter reader_writer(context);
72 rpc_lock().unlock();
73 static_cast<const RawMethod&>(context.method())
74 .function_.stream_request(context.service(), reader_writer);
75 }
76
77 } // namespace pw::rpc::internal
78