Skip to content

Commit 80efe0d

Browse files
authored
RSDK-9556 Forward declarations for grpc Client stuff (#341)
1 parent 5b87e89 commit 80efe0d

File tree

9 files changed

+128
-65
lines changed

9 files changed

+128
-65
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ if ((VIAMCPPSDK_GRPCXX_VERSION VERSION_GREATER 1.51.1) AND (VIAMCPPSDK_PROTOC_VE
430430
set(VIAMCPPSDK_BUF_REMOTE_PLUGIN_SUPPORTED 1)
431431
endif()
432432

433+
if (VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.32.0)
434+
set(VIAMCPPSDK_GRPCXX_LEGACY_CLIENT_FWD 1)
435+
endif()
436+
433437
include(FetchContent)
434438

435439
FetchContent_Declare(

src/viam/sdk/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ endif()
2929
message(WARNING "api proto label is ${VIAMCPPSDK_API_PROTO_LABEL}")
3030
configure_file(common/private/version_metadata.hpp.in common/private/version_metadata.hpp @ONLY)
3131

32+
# Configure the grpc client forward declarations file
33+
configure_file(common/grpc_client_fwd.hpp.in common/grpc_client_fwd.hpp)
3234

3335
# Set compile and link options based on arguments
3436
if (VIAMCPPSDK_USE_WALL_WERROR)
@@ -137,6 +139,7 @@ target_sources(viamsdk
137139
PUBLIC FILE_SET viamsdk_public_includes TYPE HEADERS
138140
BASE_DIRS
139141
../..
142+
${CMAKE_CURRENT_BINARY_DIR}/../..
140143
FILES
141144
../../viam/sdk/common/client_helper.hpp
142145
../../viam/sdk/common/exception.hpp
@@ -189,6 +192,7 @@ target_sources(viamsdk
189192
../../viam/sdk/spatialmath/geometry.hpp
190193
../../viam/sdk/spatialmath/orientation.hpp
191194
../../viam/sdk/spatialmath/orientation_types.hpp
195+
${CMAKE_CURRENT_BINARY_DIR}/../../viam/sdk/common/grpc_client_fwd.hpp
192196
)
193197

194198
set_target_properties(
@@ -207,7 +211,6 @@ target_include_directories(viamsdk
207211
"$<INSTALL_INTERFACE:${VIAMCPPSDK_GRPC_INCLUDE_DIRS}>"
208212
"$<BUILD_INTERFACE:${VIAMCPPSDK_PROTOBUF_INCLUDE_DIRS}>"
209213
"$<INSTALL_INTERFACE:${VIAMCPPSDK_PROTOBUF_INCLUDE_DIRS}>"
210-
PRIVATE
211214
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../..>"
212215
)
213216

src/viam/sdk/common/client_helper.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
#include <cstdlib>
44

5+
#include <grpcpp/client_context.h>
6+
57
#include <boost/log/trivial.hpp>
68

9+
#include <viam/sdk/common/private/version_metadata.hpp>
10+
711
namespace viam {
812
namespace sdk {
913

@@ -16,5 +20,37 @@ namespace client_helper_details {
1620
}
1721

1822
} // namespace client_helper_details
23+
24+
ClientContext::ClientContext() : wrapped_context_(std::make_unique<GrpcClientContext>()) {
25+
set_client_ctx_authority_();
26+
add_viam_client_version_();
27+
}
28+
29+
ClientContext::~ClientContext() = default;
30+
31+
ClientContext::operator const GrpcClientContext*() const {
32+
return wrapped_context_.get();
33+
}
34+
35+
ClientContext::operator GrpcClientContext*() {
36+
return wrapped_context_.get();
37+
}
38+
39+
void ClientContext::try_cancel() {
40+
wrapped_context_->TryCancel();
41+
}
42+
43+
void ClientContext::set_debug_key(const std::string& debug_key) {
44+
wrapped_context_->AddMetadata("dtname", debug_key);
45+
}
46+
47+
void ClientContext::set_client_ctx_authority_() {
48+
wrapped_context_->set_authority("viam-placeholder");
49+
}
50+
51+
void ClientContext::add_viam_client_version_() {
52+
wrapped_context_->AddMetadata("viam_client", impl::k_version);
53+
}
54+
1955
} // namespace sdk
2056
} // namespace viam

src/viam/sdk/common/client_helper.hpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#pragma once
22

3-
#include <grpcpp/client_context.h>
4-
#include <grpcpp/support/sync_stream.h>
5-
63
#include <viam/sdk/common/exception.hpp>
4+
#include <viam/sdk/common/grpc_client_fwd.hpp>
75
#include <viam/sdk/common/private/utils.hpp>
86
#include <viam/sdk/common/proto_value.hpp>
9-
#include <viam/sdk/common/utils.hpp>
107

118
namespace grpc {
129

@@ -23,16 +20,39 @@ namespace client_helper_details {
2320

2421
} // namespace client_helper_details
2522

23+
// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
24+
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
25+
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
26+
// to avoid runtime issues.
27+
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
28+
class ClientContext {
29+
public:
30+
ClientContext();
31+
~ClientContext();
32+
33+
void try_cancel();
34+
35+
operator GrpcClientContext*();
36+
operator const GrpcClientContext*() const;
37+
38+
void set_debug_key(const std::string& debug_key);
39+
40+
private:
41+
void set_client_ctx_authority_();
42+
void add_viam_client_version_();
43+
std::unique_ptr<GrpcClientContext> wrapped_context_;
44+
};
45+
2646
// Method type for a gRPC call that returns a response message type.
2747
template <typename StubType, typename RequestType, typename ResponseType>
28-
using SyncMethodType = ::grpc::Status (StubType::*)(::grpc::ClientContext*,
48+
using SyncMethodType = ::grpc::Status (StubType::*)(GrpcClientContext*,
2949
const RequestType&,
3050
ResponseType*);
3151

3252
// Method type for a gRPC call that returns a stream of response message type.
3353
template <typename StubType, typename RequestType, typename ResponseType>
34-
using StreamingMethodType = std::unique_ptr<::grpc::ClientReaderInterface<ResponseType>> (
35-
StubType::*)(::grpc::ClientContext*, const RequestType&);
54+
using StreamingMethodType = std::unique_ptr<GrpcClientReaderInterface<ResponseType>> (StubType::*)(
55+
GrpcClientContext*, const RequestType&);
3656

3757
template <typename ClientType,
3858
typename StubType,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
#cmakedefine VIAMCPPSDK_GRPCXX_LEGACY_CLIENT_FWD
4+
5+
#ifndef VIAMCPPSDK_GRPCXX_LEGACY_CLIENT_FWD
6+
7+
// Forward declaration file for grpc ClientContext and ClientReaderInterface<T>.
8+
// This file provides includes for recent (>= 1.32.0) versions of grpc.
9+
10+
namespace grpc {
11+
12+
class ClientContext;
13+
14+
template <typename>
15+
class ClientReaderInterface;
16+
17+
} // namespace grpc
18+
19+
namespace viam {
20+
namespace sdk {
21+
22+
using GrpcClientContext = ::grpc::ClientContext;
23+
24+
template <typename T>
25+
using GrpcClientReaderInterface = ::grpc::ClientReaderInterface<T>;
26+
27+
} // namespace sdk
28+
} // namespace viam
29+
30+
#else
31+
32+
// Forward declaration file for grpc ClientContext and ClientReaderInterface<T>.
33+
// This file provides includes for the oldest supported versions of grpc (< 1.32.0).
34+
35+
namespace grpc_impl {
36+
37+
class ClientContext;
38+
39+
template <typename>
40+
class ClientReaderInterface;
41+
42+
} // namespace grpc_impl
43+
44+
namespace viam {
45+
namespace sdk {
46+
47+
using GrpcClientContext = ::grpc_impl::ClientContext;
48+
49+
template <typename T>
50+
using GrpcClientReaderInterface = ::grpc_impl::ClientReaderInterface<T>;
51+
52+
} // namespace sdk
53+
} // namespace viam
54+
55+
#endif

src/viam/sdk/common/utils.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <viam/api/common/v1/common.pb.h>
1919

2020
#include <viam/sdk/common/private/utils.hpp>
21-
#include <viam/sdk/common/private/version_metadata.hpp>
2221
#include <viam/sdk/components/component.hpp>
2322
#include <viam/sdk/registry/registry.hpp>
2423

@@ -151,37 +150,6 @@ ProtoStruct with_debug_entry(ProtoStruct&& map) {
151150
return map;
152151
}
153152

154-
ClientContext::ClientContext() : wrapped_context_(std::make_unique<grpc::ClientContext>()) {
155-
set_client_ctx_authority_();
156-
add_viam_client_version_();
157-
}
158-
159-
ClientContext::~ClientContext() = default;
160-
161-
ClientContext::operator const grpc::ClientContext*() const {
162-
return wrapped_context_.get();
163-
}
164-
165-
ClientContext::operator grpc::ClientContext*() {
166-
return wrapped_context_.get();
167-
}
168-
169-
void ClientContext::try_cancel() {
170-
wrapped_context_->TryCancel();
171-
}
172-
173-
void ClientContext::set_debug_key(const std::string& debug_key) {
174-
wrapped_context_->AddMetadata("dtname", debug_key);
175-
}
176-
177-
void ClientContext::set_client_ctx_authority_() {
178-
wrapped_context_->set_authority("viam-placeholder");
179-
}
180-
181-
void ClientContext::add_viam_client_version_() {
182-
wrapped_context_->AddMetadata("viam_client", impl::k_version);
183-
}
184-
185153
bool from_dm_from_extra(const ProtoStruct& extra) {
186154
auto pos = extra.find("fromDataManagement");
187155
if (pos != extra.end()) {

src/viam/sdk/common/utils.hpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <memory>
44

55
#include <boost/optional/optional.hpp>
6-
#include <grpcpp/client_context.h>
76

87
#include <viam/sdk/common/proto_value.hpp>
98
#include <viam/sdk/components/component.hpp>
@@ -82,29 +81,6 @@ struct from_proto<common::v1::ResponseMetadata> {
8281
std::vector<unsigned char> string_to_bytes(std::string const& s);
8382
std::string bytes_to_string(std::vector<unsigned char> const& b);
8483

85-
// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
86-
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a
87-
// grpc ClientContext that allows us to make any necessary modifications to authority or else where
88-
// to avoid runtime issues.
89-
// For more details, see https://viam.atlassian.net/browse/RSDK-5194.
90-
class ClientContext {
91-
public:
92-
ClientContext();
93-
~ClientContext();
94-
95-
void try_cancel();
96-
97-
operator grpc::ClientContext*();
98-
operator const grpc::ClientContext*() const;
99-
100-
void set_debug_key(const std::string& debug_key);
101-
102-
private:
103-
void set_client_ctx_authority_();
104-
void add_viam_client_version_();
105-
std::unique_ptr<grpc::ClientContext> wrapped_context_;
106-
};
107-
10884
/// @brief Given a fully qualified resource name, returns remote name (or "" if no remote name
10985
/// exists) and short name
11086
std::pair<std::string, std::string> long_name_to_remote_and_short(const std::string& long_name);

src/viam/sdk/robot/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <viam/api/robot/v1/robot.grpc.pb.h>
2020
#include <viam/api/robot/v1/robot.pb.h>
2121

22+
#include <viam/sdk/common/client_helper.hpp>
2223
#include <viam/sdk/common/private/repeated_ptr_convert.hpp>
2324
#include <viam/sdk/common/proto_value.hpp>
2425
#include <viam/sdk/common/utils.hpp>

src/viam/sdk/services/private/mlmodel_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#include <grpcpp/channel.h>
1818

19-
#include <viam/sdk/common/utils.hpp>
19+
#include <viam/sdk/common/client_helper.hpp>
2020
#include <viam/sdk/services/private/mlmodel.hpp>
2121

2222
#include <viam/sdk/common/exception.hpp>

0 commit comments

Comments
 (0)