AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
rpcService.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-rest-protobuf-export.hpp"
23
24#include <cstddef>
25#include <cstdint>
26#include <functional>
27#include <map>
28#include <optional>
29#include <stdexcept>
30#include <string>
31#include <string_view>
32#include <vector>
33
34#include "rest/headers.hpp"
35#include "codec/protobuf.hpp"
36#include "rest/status.hpp"
37
38namespace anch::rest {
39
81
90 class ANCH_REST_PROTOBUF_EXPORT RpcError: public std::runtime_error {
91
92 // Attributes +
93 private:
96 // Attributes -
97
98 // Constructors +
99 public:
106 RpcError(anch::rest::RpcCode code, const std::string& message = std::string());
107 // Constructors -
108
109 // Accessors +
110 public:
116 anch::rest::RpcCode getCode() const noexcept;
117 // Accessors -
118
119 };
120
128 class ANCH_REST_PROTOBUF_EXPORT RpcContext {
129
130 // Attributes +
131 private:
133 anch::rest::Headers _request;
134
136 anch::rest::Headers _response;
137 // Attributes -
138
139 // Constructors +
140 public:
147 // Constructors -
148
149 // Accessors +
150 public:
157
164 // Accessors -
165
166 };
167
173 enum class RpcFormat {
175 BINARY,
177 JSON
178 };
179
194 class ANCH_REST_PROTOBUF_EXPORT RpcService {
195
197 using Method = std::function<std::string(std::string_view, anch::rest::RpcFormat, anch::rest::RpcContext&)>;
198
199 // Attributes +
200 private:
202 std::string _name;
203
205 std::map<std::string, Method, std::less<>> _methods;
206
208 std::size_t _maxMessageSize;
209 // Attributes -
210
211 // Constructors +
212 public:
220 explicit RpcService(std::string_view name);
221 // Constructors -
222
223 // Methods +
224 public:
238 template<anch::codec::ProtobufMessage Req, anch::codec::ProtobufMessage Res, typename F>
239 RpcService& unary(std::string_view method, F&& handler);
240
254 std::string call(std::string_view method, std::string_view request, anch::rest::RpcFormat format,
255 anch::rest::RpcContext& context) const;
256
264 bool has(std::string_view method) const;
265
271 std::vector<std::string> getMethods() const;
272 // Methods -
273
274 // Accessors +
275 public:
281 const std::string& getName() const noexcept;
282
288 std::size_t getMaxMessageSize() const noexcept;
289
295 void setMaxMessageSize(std::size_t size) noexcept;
296 // Accessors -
297
298 private:
305 void add(std::string_view method, Method function);
306 // Methods -
307
308 };
309
310}
311
312namespace anch::rest::rpc {
313
323 ANCH_REST_PROTOBUF_EXPORT std::string_view connectCode(anch::rest::RpcCode code) noexcept;
324
334 ANCH_REST_PROTOBUF_EXPORT std::optional<anch::rest::RpcCode> parseConnectCode(std::string_view name) noexcept;
335
345 ANCH_REST_PROTOBUF_EXPORT anch::rest::Status httpStatus(anch::rest::RpcCode code) noexcept;
346
356 ANCH_REST_PROTOBUF_EXPORT anch::rest::RpcCode rpcCode(anch::rest::Status status) noexcept;
357
367 ANCH_REST_PROTOBUF_EXPORT std::string connectError(const anch::rest::RpcError& error);
368
379 ANCH_REST_PROTOBUF_EXPORT anch::rest::RpcError parseConnectError(anch::rest::Status status, std::string_view body);
380
391 ANCH_REST_PROTOBUF_EXPORT std::string grpcWebFrame(std::string_view data, bool trailers);
392
404 ANCH_REST_PROTOBUF_EXPORT std::string grpcWebMessage(std::string_view body);
405
416 ANCH_REST_PROTOBUF_EXPORT std::string grpcWebTrailers(anch::rest::RpcCode code, std::string_view message);
417
418}
419
420#include "rest/impl/rpcService.hpp"
Definition headers.hpp:62
Definition rpcService.hpp:128
const anch::rest::Headers & getRequestMetadata() const noexcept
RpcContext(anch::rest::Headers request=anch::rest::Headers())
anch::rest::Headers & getResponseMetadata() noexcept
Definition rpcService.hpp:90
anch::rest::RpcCode getCode() const noexcept
RpcError(anch::rest::RpcCode code, const std::string &message=std::string())
std::vector< std::string > getMethods() const
RpcService & unary(std::string_view method, F &&handler)
std::size_t getMaxMessageSize() const noexcept
std::string call(std::string_view method, std::string_view request, anch::rest::RpcFormat format, anch::rest::RpcContext &context) const
const std::string & getName() const noexcept
bool has(std::string_view method) const
RpcService(std::string_view name)
void setMaxMessageSize(std::size_t size) noexcept
AnCH REST library namespace.
Definition auth.hpp:29
RpcCode
Definition rpcService.hpp:45
@ UNAVAILABLE
Definition rpcService.hpp:75
@ ALREADY_EXISTS
Definition rpcService.hpp:59
@ INTERNAL
Definition rpcService.hpp:73
@ PERMISSION_DENIED
Definition rpcService.hpp:61
@ UNAUTHENTICATED
Definition rpcService.hpp:79
@ RESOURCE_EXHAUSTED
Definition rpcService.hpp:63
@ ABORTED
Definition rpcService.hpp:67
@ OUT_OF_RANGE
Definition rpcService.hpp:69
@ CANCELLED
Definition rpcService.hpp:49
@ UNIMPLEMENTED
Definition rpcService.hpp:71
@ DEADLINE_EXCEEDED
Definition rpcService.hpp:55
@ FAILED_PRECONDITION
Definition rpcService.hpp:65
@ INVALID_ARGUMENT
Definition rpcService.hpp:53
@ DATA_LOSS
Definition rpcService.hpp:77
RpcFormat
Definition rpcService.hpp:173
Status
Definition status.hpp:36
constexpr uint16_t code(anch::rest::Status status) noexcept
Definition status.hpp:107
AnCH framework base namespace.
Definition app.hpp:30