Skip to content

Commit 44d8601

Browse files
committed
use ifdef for direct dial
1 parent fde961c commit 44d8601

File tree

6 files changed

+35
-17
lines changed

6 files changed

+35
-17
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ endif()
457457

458458
if (VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.32.0)
459459
set(VIAMCPPSDK_GRPCXX_LEGACY_FWD 1)
460+
elseif(VIAMCPPSDK_GRPCXX_VERSION VERSION_LESS 1.43.0)
461+
set(VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL 1)
460462
endif()
461463

462464
include(FetchContent)

src/viam/sdk/common/grpc_fwd.hpp.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
#cmakedefine VIAMCPPSDK_GRPCXX_LEGACY_FWD
44

5+
// Preprocessor definition for direct dial support in grpc.
6+
// For grpc >= 1.43.0 it is possible to dial directly over grpc. For older versions,
7+
// `VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL` is defined.
8+
// The default behavior is for the variable to be undefined, providing the behavior for recent
9+
// versions. If you are experiencing compilation errors in an installed version of the SDK it may be
10+
// due to a mismatch with the configured version of this header and the grpc version found by the
11+
// compiler. You may wish to comment/uncomment the define above as needed, or add the definition
12+
// with `-D` to the compiler.
13+
#cmakedefine VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL
14+
515
// Forward declaration file grpc client and server types.
616
// This file provides includes for recent (>= 1.32.0) versions of grpc or older
717
// versions, depending on if `VIAMCPPSDK_GRPCXX_LEGACY_FWD` is defined.

src/viam/sdk/rpc/dial.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <grpc/grpc.h>
1111
#include <grpcpp/channel.h>
1212
#include <grpcpp/create_channel.h>
13+
#include <grpcpp/grpcpp.h>
1314
#include <grpcpp/security/credentials.h>
1415

1516
#include <viam/api/proto/rpc/v1/auth.grpc.pb.h>
@@ -104,8 +105,15 @@ ViamChannel ViamChannel::dial_initial(const char* uri,
104105
ViamChannel ViamChannel::dial(const char* uri, const boost::optional<DialOptions>& options) {
105106
const DialOptions opts = options.get_value_or(DialOptions());
106107

108+
// If this flag is passed, try to dial directly through grpc if possible.
109+
// If grpc is too old to do a direct dial, we fall back to the rust version below even if
110+
// disable_webrtc was pased. This is consistent with a hoped-for future semantic where rust
111+
// would get the disable_webrtc flag as well rather than deducing it from the URI format, which
112+
// is what it currently does.
107113
if (opts.disable_webrtc) {
114+
#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL
108115
return dial_direct(uri, opts);
116+
#endif
109117
}
110118

111119
const std::chrono::duration<float> float_timeout = opts.timeout;
@@ -140,7 +148,11 @@ ViamChannel ViamChannel::dial(const char* uri, const boost::optional<DialOptions
140148
ptr);
141149
}
142150

143-
void ViamChannel::set_bearer_token(const char* uri, const DialOptions& opts) {
151+
ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) {
152+
#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL
153+
// TODO: if we ever drop older grpc support the logic below might make sense as a
154+
// set_bearer_token helper function, but for now it just proliferates the ifdef messiness so
155+
// we'll leave it inline
144156
auto channel_for_auth = sdk::impl::create_viam_auth_channel(uri);
145157
using namespace proto::rpc::v1;
146158

@@ -164,22 +176,15 @@ void ViamChannel::set_bearer_token(const char* uri, const DialOptions& opts) {
164176

165177
Instance::current(Instance::Creation::open_existing).impl_->direct_dial_token =
166178
resp.access_token();
167-
}
168-
169-
ViamChannel ViamChannel::dial_direct(const char* uri, const DialOptions& opts) {
170-
ViamChannel::set_bearer_token(uri, opts);
171-
172-
#ifndef VIAMCPPSDK_GRPCXX_LEGACY_FWD
173-
namespace grpc_experimental = grpc::experimental;
174179

175-
#else
176-
namespace grpc_experimental = grpc_impl::experimental;
177-
#endif
178-
179-
grpc_experimental::TlsChannelCredentialsOptions c_opts;
180+
grpc::experimental::TlsChannelCredentialsOptions c_opts;
180181
c_opts.set_check_call_host(false);
181-
auto creds = grpc_experimental::TlsCredentials(c_opts);
182+
auto creds = grpc::experimental::TlsCredentials(c_opts);
182183
return ViamChannel(sdk::impl::create_viam_grpc_channel(uri, creds));
184+
#else
185+
throw std::logic_error("Tried to call dial_direct on unsupported grpc version " +
186+
grpc::Version());
187+
#endif
183188
}
184189

185190
const std::shared_ptr<grpc::Channel>& ViamChannel::channel() const {

src/viam/sdk/rpc/dial.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class ViamChannel {
5050
private:
5151
struct impl;
5252

53-
// Sets the global bearer token used in client requests for direct dialing over gRPC.
54-
static void set_bearer_token(const char* uri, const DialOptions& opts);
55-
5653
static ViamChannel dial_direct(const char* uri, const DialOptions& opts);
5754

5855
std::shared_ptr<GrpcChannel> channel_;

src/viam/sdk/rpc/private/viam_grpc_channel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ namespace viam {
99
namespace sdk {
1010
namespace impl {
1111

12+
#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL
1213
std::shared_ptr<grpc::Channel> create_viam_auth_channel(const std::string& address) {
1314
grpc::experimental::TlsChannelCredentialsOptions opts;
1415
opts.set_check_call_host(false);
1516
auto tls_creds = grpc::experimental::TlsCredentials(opts);
1617

1718
return grpc::CreateChannel(address, tls_creds);
1819
}
20+
#endif
1921

2022
std::shared_ptr<grpc::Channel> create_viam_grpc_channel(
2123
const grpc::string& target, const std::shared_ptr<grpc::ChannelCredentials>& credentials) {

src/viam/sdk/rpc/private/viam_grpc_channel.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ namespace impl {
1414
std::shared_ptr<grpc::Channel> create_viam_grpc_channel(
1515
const grpc::string& target, const std::shared_ptr<grpc::ChannelCredentials>& credentials);
1616

17+
#ifndef VIAMCPPSDK_GRPCXX_NO_DIRECT_DIAL
1718
/// @brief Like grpc::CreateChannel, but for the express purpose of returning a channel for making
1819
/// an AuthService request.
1920
std::shared_ptr<grpc::Channel> create_viam_auth_channel(const std::string& address);
21+
#endif
2022

2123
} // namespace impl
2224
} // namespace sdk

0 commit comments

Comments
 (0)