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,
104105ViamChannel 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
185190const std::shared_ptr<grpc::Channel>& ViamChannel::channel () const {
0 commit comments