19
19
#include " absl/strings/str_format.h"
20
20
#include " absl/strings/string_view.h"
21
21
#include " absl/synchronization/mutex.h"
22
+ #include " absl/time/time.h"
22
23
#include " internal/flags/nearby_flags.h"
23
24
#include " internal/platform/byte_array.h"
24
25
#include " internal/platform/cancellation_flag.h"
@@ -97,9 +98,6 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
97
98
return nullptr ;
98
99
}
99
100
100
- HostName host_name{winrt::to_hstring (ipv4_address)};
101
- winrt::hstring service_name{winrt::to_hstring (port)};
102
-
103
101
// Try connecting to the service up to wifi_hotspot_max_connection_retries,
104
102
// because it may fail first time if DHCP procedure is not finished yet.
105
103
int64_t wifi_hotspot_max_connection_retries =
@@ -120,14 +118,17 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
120
118
<< " , connection interval=" << wifi_hotspot_retry_interval_millis
121
119
<< " ms, connection timeout="
122
120
<< wifi_hotspot_client_socket_connect_timeout_millis << " ms" ;
123
- for (int i = 0 ; i < wifi_hotspot_max_connection_retries; i++) {
124
- try {
125
- StreamSocket socket{};
126
- // Listener to connect cancellation.
127
- std::unique_ptr<CancellationFlagListener>
128
- connection_cancellation_listener = nullptr ;
121
+
122
+ if (NearbyFlags::GetInstance ().GetBoolFlag (
123
+ nearby::platform::config_package_nearby::nearby_platform_feature::
124
+ kEnableBlockingSocket )) {
125
+ LOG (INFO) << " Connect to service " << ipv4_address << " :" << port;
126
+ for (int i = 0 ; i < wifi_hotspot_max_connection_retries; ++i) {
127
+ auto wifi_hotspot_socket = std::make_unique<WifiHotspotSocket>();
129
128
130
129
// setup cancel listener
130
+ std::unique_ptr<CancellationFlagListener>
131
+ connection_cancellation_listener = nullptr ;
131
132
if (cancellation_flag != nullptr ) {
132
133
if (cancellation_flag->Cancelled ()) {
133
134
LOG (INFO) << " connect has been cancelled to service " << ipv4_address
@@ -137,57 +138,100 @@ std::unique_ptr<api::WifiHotspotSocket> WifiHotspotMedium::ConnectToService(
137
138
138
139
connection_cancellation_listener =
139
140
std::make_unique<nearby::CancellationFlagListener>(
140
- cancellation_flag, [socket]() {
141
+ cancellation_flag, [socket = wifi_hotspot_socket. get () ]() {
141
142
LOG (WARNING) << " connect is closed due to it is cancelled." ;
142
- socket. Close ();
143
+ socket-> Close ();
143
144
});
144
145
}
145
146
146
- if (FeatureFlags::GetInstance ().GetFlags ().enable_connection_timeout ) {
147
- connection_timeout_ = scheduled_executor_.Schedule (
148
- [socket]() {
149
- LOG (WARNING) << " connect is closed due to timeout." ;
150
- socket.Close ();
151
- },
152
- absl::Milliseconds (
153
- wifi_hotspot_client_socket_connect_timeout_millis));
147
+ bool result = wifi_hotspot_socket->Connect (ipv4_address, port);
148
+ if (!result) {
149
+ LOG (WARNING) << " reconnect to service at " << (i + 1 ) << " th times" ;
150
+ Sleep (wifi_hotspot_retry_interval_millis);
151
+ continue ;
154
152
}
155
153
156
- socket.ConnectAsync (host_name, service_name).get ();
154
+ LOG (INFO) << " connected to remote service " << ipv4_address << " :"
155
+ << port;
156
+ return wifi_hotspot_socket;
157
+ }
158
+
159
+ LOG (ERROR) << " Failed to connect to service " << ipv4_address << " :"
160
+ << port;
161
+ return nullptr ;
162
+ } else {
163
+ HostName host_name{winrt::to_hstring (ipv4_address)};
164
+ winrt::hstring service_name{winrt::to_hstring (port)};
165
+
166
+ for (int i = 0 ; i < wifi_hotspot_max_connection_retries; i++) {
167
+ try {
168
+ StreamSocket socket{};
169
+ // Listener to connect cancellation.
170
+ std::unique_ptr<CancellationFlagListener>
171
+ connection_cancellation_listener = nullptr ;
172
+
173
+ // setup cancel listener
174
+ if (cancellation_flag != nullptr ) {
175
+ if (cancellation_flag->Cancelled ()) {
176
+ LOG (INFO) << " connect has been cancelled to service "
177
+ << ipv4_address << " :" << port;
178
+ return nullptr ;
179
+ }
180
+
181
+ connection_cancellation_listener =
182
+ std::make_unique<nearby::CancellationFlagListener>(
183
+ cancellation_flag, [socket]() {
184
+ LOG (WARNING) << " connect is closed due to it is cancelled." ;
185
+ socket.Close ();
186
+ });
187
+ }
188
+
189
+ if (FeatureFlags::GetInstance ().GetFlags ().enable_connection_timeout ) {
190
+ connection_timeout_ = scheduled_executor_.Schedule (
191
+ [socket]() {
192
+ LOG (WARNING) << " connect is closed due to timeout." ;
193
+ socket.Close ();
194
+ },
195
+ absl::Milliseconds (
196
+ wifi_hotspot_client_socket_connect_timeout_millis));
197
+ }
198
+
199
+ socket.ConnectAsync (host_name, service_name).get ();
200
+
201
+ if (connection_timeout_ != nullptr ) {
202
+ connection_timeout_->Cancel ();
203
+ connection_timeout_ = nullptr ;
204
+ }
205
+
206
+ auto wifi_hotspot_socket = std::make_unique<WifiHotspotSocket>(socket);
207
+
208
+ LOG (INFO) << " connected to remote service " << ipv4_address << " :"
209
+ << port;
210
+ return wifi_hotspot_socket;
211
+ } catch (std::exception exception ) {
212
+ LOG (ERROR) << " failed to connect remote service " << ipv4_address << " :"
213
+ << port << " for the " << i + 1
214
+ << " time. Exception: " << exception .what ();
215
+ } catch (const winrt::hresult_error& error) {
216
+ LOG (ERROR) << " failed to connect remote service " << ipv4_address << " :"
217
+ << port << " for the " << i + 1
218
+ << " time. WinRT exception: " << error.code () << " : "
219
+ << winrt::to_string (error.message ());
220
+ } catch (...) {
221
+ LOG (ERROR) << " failed to connect remote service " << ipv4_address << " :"
222
+ << port << " for the " << i + 1
223
+ << " time due to unknown reason." ;
224
+ }
157
225
158
226
if (connection_timeout_ != nullptr ) {
159
227
connection_timeout_->Cancel ();
160
228
connection_timeout_ = nullptr ;
161
229
}
162
230
163
- auto wifi_hotspot_socket = std::make_unique<WifiHotspotSocket>(socket);
164
-
165
- LOG (INFO) << " connected to remote service " << ipv4_address << " :"
166
- << port;
167
- return wifi_hotspot_socket;
168
- } catch (std::exception exception ) {
169
- LOG (ERROR) << " failed to connect remote service " << ipv4_address << " :"
170
- << port << " for the " << i + 1
171
- << " time. Exception: " << exception .what ();
172
- } catch (const winrt::hresult_error& error) {
173
- LOG (ERROR) << " failed to connect remote service " << ipv4_address << " :"
174
- << port << " for the " << i + 1
175
- << " time. WinRT exception: " << error.code () << " : "
176
- << winrt::to_string (error.message ());
177
- } catch (...) {
178
- LOG (ERROR) << " failed to connect remote service " << ipv4_address << " :"
179
- << port << " for the " << i + 1
180
- << " time due to unknown reason." ;
181
- }
182
-
183
- if (connection_timeout_ != nullptr ) {
184
- connection_timeout_->Cancel ();
185
- connection_timeout_ = nullptr ;
231
+ Sleep (wifi_hotspot_retry_interval_millis);
186
232
}
187
-
188
- Sleep (wifi_hotspot_retry_interval_millis);
233
+ return nullptr ;
189
234
}
190
- return nullptr ;
191
235
}
192
236
193
237
std::unique_ptr<api::WifiHotspotServerSocket>
@@ -401,10 +445,9 @@ fire_and_forget WifiHotspotMedium::OnConnectionRequested(
401
445
// We found new connection request comes in during hotspot transfer. In this
402
446
// case, we should create a new WiFiDirectDevice for it. It will cause
403
447
// transfer failure if replace the old WiFiDirectDevice with it.
404
- auto wifi_direct_device =
405
- WiFiDirectDevice::FromIdAsync (
406
- connection_request.DeviceInformation ().Id ())
407
- .get ();
448
+ auto wifi_direct_device = WiFiDirectDevice::FromIdAsync (
449
+ connection_request.DeviceInformation ().Id ())
450
+ .get ();
408
451
wifi_direct_devices_.push_back (wifi_direct_device);
409
452
LOG (INFO) << " Registered the device " << winrt::to_string (device_name)
410
453
<< " in WLAN-AutoConfig" ;
0 commit comments