@@ -61,6 +61,13 @@ class DialOptions {
61
61
/// Whether the connection was made using mDNS
62
62
bool _usingMdns = false ;
63
63
64
+ /// The number of connection attempts to make when first dialing. If set to zero or a negative
65
+ /// integer, will attempt to reconnect forever.
66
+ int initialConnectionAttempts = 3 ;
67
+
68
+ // The timeout to use for initial connection attempts.
69
+ Duration initialConnectionAttemptTimeout = const Duration (seconds: 10 );
70
+
64
71
/// Timeout is the timeout for dial.
65
72
Duration timeout = const Duration (seconds: 10 );
66
73
@@ -123,6 +130,35 @@ class DialWebRtcOptions {
123
130
String ? signalingAccessToken;
124
131
}
125
132
133
+ /// {@category Viam SDK}
134
+ /// Initial connection to a robot at the provided address with the given options, allowing for specifying of initial connection attempt count and timeout
135
+ Future <ClientChannelBase > dialInitial (String address, DialOptions ? options, String Function () sessionCallback) async {
136
+ final opts = options ?? DialOptions ();
137
+
138
+ int numAttempts = opts.initialConnectionAttempts;
139
+ if (numAttempts == 0 ) {
140
+ numAttempts = - 1 ;
141
+ }
142
+
143
+ final timeout = opts.timeout;
144
+ opts.timeout = opts.initialConnectionAttemptTimeout;
145
+
146
+ while (numAttempts != 0 ) {
147
+ try {
148
+ final channel = await dial (address, opts, sessionCallback);
149
+ opts.timeout = timeout;
150
+ return channel;
151
+ } catch (e) {
152
+ numAttempts -= 1 ;
153
+ if (numAttempts == 0 ) {
154
+ rethrow ;
155
+ }
156
+ }
157
+ }
158
+
159
+ throw Exception ('unreachable' );
160
+ }
161
+
126
162
/// {@category Viam SDK}
127
163
/// Connect to a robot at the provided address with the given options
128
164
Future <ClientChannelBase > dial (String address, DialOptions ? options, String Function () sessionCallback) async {
0 commit comments