Skip to content

Commit 4183152

Browse files
committed
initial connection options for cpp
1 parent 117de35 commit 4183152

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/viam/sdk/rpc/dial.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,18 @@ void DialOptions::set_entity(boost::optional<std::string> entity) {
6060
auth_entity_ = std::move(entity);
6161
}
6262

63+
void DialOptions::set_initial_connection_attempts(int attempts) {
64+
initial_connection_attempts_ = attempts;
65+
}
66+
6367
void DialOptions::set_timeout(std::chrono::duration<float> timeout) {
6468
timeout_ = std::move(timeout);
6569
}
6670

71+
void DialOptions::set_initial_connection_attempt_timeout(std::chrono::duration<float> timeout) {
72+
initial_connection_attempt_timeout_ = std::move(timeout);
73+
}
74+
6775
const boost::optional<std::string>& DialOptions::entity() const {
6876
return auth_entity_;
6977
}
@@ -72,10 +80,18 @@ const boost::optional<Credentials>& DialOptions::credentials() const {
7280
return credentials_;
7381
}
7482

83+
int DialOptions::initial_connection_attempts() const {
84+
return initial_connection_attempts_;
85+
}
86+
7587
const std::chrono::duration<float>& DialOptions::timeout() const {
7688
return timeout_;
7789
}
7890

91+
const std::chrono::duration<float>& DialOptions::initial_connection_attempt_timeout() const {
92+
return initial_connection_attempt_timeout_;
93+
}
94+
7995
void DialOptions::set_allow_insecure_downgrade(bool allow) {
8096
allow_insecure_downgrade_ = allow;
8197
}
@@ -84,6 +100,34 @@ bool DialOptions::allows_insecure_downgrade() const {
84100
return allow_insecure_downgrade_;
85101
}
86102

103+
std::shared_ptr<ViamChannel> ViamChannel::dial(const char* uri,
104+
const boost::optional<DialOptions>& options,
105+
bool initial_attempt) {
106+
if (!initial_attempt) {
107+
return dial(std::move(uri), options);
108+
}
109+
110+
DialOptions opts = options.get_value_or(DialOptions());
111+
auto timeout = opts.timeout();
112+
auto attempts_remaining = opts.initial_connection_attempts();
113+
if (attempts_remaining == 0) {
114+
attempts_remaining = -1;
115+
}
116+
opts.set_timeout(opts.initial_connection_attempt_timeout());
117+
118+
while (attempts_remaining > 0) {
119+
try {
120+
auto connection = dial(uri, opts);
121+
opts.set_timeout(timeout);
122+
return connection;
123+
} catch (...) {
124+
attempts_remaining -= 1;
125+
}
126+
}
127+
128+
throw Exception(ErrorCondition::k_connection, "Unable to establish connection");
129+
}
130+
87131
std::shared_ptr<ViamChannel> ViamChannel::dial(const char* uri,
88132
const boost::optional<DialOptions>& options) {
89133
void* ptr = init_rust_runtime();

src/viam/sdk/rpc/dial.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ class ViamChannel {
1616
public:
1717
void close();
1818
ViamChannel(std::shared_ptr<GrpcChannel> channel, const char* path, void* runtime);
19+
1920
static std::shared_ptr<ViamChannel> dial(const char* uri,
2021
const boost::optional<DialOptions>& options);
2122

23+
// allows for specifying that this dial is an initial connection attempt, indicating that
24+
// initial connection options should be used.
25+
static std::shared_ptr<ViamChannel> dial(const char* uri,
26+
const boost::optional<DialOptions>& options,
27+
bool initial_attempt);
28+
2229
const std::shared_ptr<GrpcChannel>& channel() const;
2330

2431
private:
@@ -49,11 +56,15 @@ class DialOptions {
4956
const boost::optional<std::string>& entity() const;
5057
bool allows_insecure_downgrade() const;
5158
const std::chrono::duration<float>& timeout() const;
59+
int initial_connection_attempts() const;
60+
const std::chrono::duration<float>& initial_connection_attempt_timeout() const;
5261

5362
void set_entity(boost::optional<std::string> entity);
5463
void set_credentials(boost::optional<Credentials> creds);
5564
void set_allow_insecure_downgrade(bool allow);
5665
void set_timeout(std::chrono::duration<float> timeout);
66+
void set_initial_connection_attempts(int attempts);
67+
void set_initial_connection_attempt_timeout(std::chrono::duration<float> timeout);
5768

5869
private:
5970
// TODO (RSDK-917): We currently don't provide a flag for disabling webRTC, instead relying on a
@@ -72,6 +83,14 @@ class DialOptions {
7283
/// @brief Duration before the dial connection times out
7384
/// Set to 20sec to match _defaultOfferDeadline in goutils/rpc/wrtc_call_queue.go
7485
std::chrono::duration<float> timeout_{20};
86+
87+
/// @brief Number of attempts to make when initially connecting to a robot
88+
/// If set to 0 or a negative integer, will attempt to reconnect forever.
89+
int initial_connection_attempts_ = 3;
90+
91+
/// @brief Timeout of connection attempts when initially dialing a robot
92+
/// Defaults to 20sec to match the default timeout duration
93+
std::chrono::duration<float> initial_connection_attempt_timeout_{20};
7594
};
7695

7796
class Options {

0 commit comments

Comments
 (0)