@@ -21,13 +21,11 @@ import '../compile.dart';
2121import '../daemon.dart' ;
2222import '../device.dart' ;
2323import '../device_port_forwarder.dart' ;
24- import '../fuchsia/fuchsia_device .dart' ;
24+ import '../device_vm_service_discovery_for_attach .dart' ;
2525import '../ios/devices.dart' ;
26- import '../ios/simulators.dart' ;
2726import '../macos/macos_ipad_device.dart' ;
2827import '../mdns_discovery.dart' ;
2928import '../project.dart' ;
30- import '../protocol_discovery.dart' ;
3129import '../resident_runner.dart' ;
3230import '../run_cold.dart' ;
3331import '../run_hot.dart' ;
@@ -286,116 +284,48 @@ known, it can be explicitly provided to attach via the command-line, e.g.
286284 : null ;
287285
288286 Stream <Uri >? vmServiceUri;
289- bool usesIpv6 = ipv6! ;
287+ final bool usesIpv6 = ipv6! ;
290288 final String ipv6Loopback = InternetAddress .loopbackIPv6.address;
291289 final String ipv4Loopback = InternetAddress .loopbackIPv4.address;
292290 final String hostname = usesIpv6 ? ipv6Loopback : ipv4Loopback;
293291 final bool isWirelessIOSDevice = (device is IOSDevice ) && device.isWirelesslyConnected;
294292
295293 if ((debugPort == null && debugUri == null ) || isWirelessIOSDevice) {
296- if (device is FuchsiaDevice ) {
297- final String ? module = stringArg ('module' );
298- if (module == null ) {
299- throwToolExit ("'--module' is required for attaching to a Fuchsia device" );
300- }
301- usesIpv6 = device.ipv6;
302- FuchsiaIsolateDiscoveryProtocol ? isolateDiscoveryProtocol;
303- try {
304- isolateDiscoveryProtocol = device.getIsolateDiscoveryProtocol (module);
305- vmServiceUri = Stream <Uri >.value (await isolateDiscoveryProtocol.uri).asBroadcastStream ();
306- } on Exception {
307- isolateDiscoveryProtocol? .dispose ();
308- final List <ForwardedPort > ports = device.portForwarder.forwardedPorts.toList ();
309- for (final ForwardedPort port in ports) {
310- await device.portForwarder.unforward (port);
294+ // The device port we expect to have the debug port be listening
295+ final int ? devicePort = debugPort ?? debugUri? .port ?? deviceVmservicePort;
296+
297+ final VMServiceDiscoveryForAttach vmServiceDiscovery = device.getVMServiceDiscoveryForAttach (
298+ appId: appId,
299+ fuchsiaModule: stringArg ('module' ),
300+ filterDevicePort: devicePort,
301+ expectedHostPort: hostVmservicePort,
302+ ipv6: usesIpv6,
303+ logger: _logger,
304+ );
305+
306+ _logger.printStatus ('Waiting for a connection from Flutter on ${device .name }...' );
307+ final Status discoveryStatus = _logger.startSpinner (
308+ timeout: const Duration (seconds: 30 ),
309+ slowWarningCallback: () {
310+ // On iOS we rely on mDNS to find Dart VM Service. Remind the user to allow local network permissions on the device.
311+ if (_isIOSDevice (device)) {
312+ return 'The Dart VM Service was not discovered after 30 seconds. This is taking much longer than expected...\n\n '
313+ 'Click "Allow" to the prompt on your device asking if you would like to find and connect devices on your local network. '
314+ 'If you selected "Don\' t Allow", you can turn it on in Settings > Your App Name > Local Network. '
315+ "If you don't see your app in the Settings, uninstall the app and rerun to see the prompt again.\n " ;
311316 }
312- rethrow ;
313- }
314- } else if (_isIOSDevice (device)) {
315- // Protocol Discovery relies on logging. On iOS earlier than 13, logging is gathered using syslog.
316- // syslog is not available for iOS 13+. For iOS 13+, Protocol Discovery gathers logs from the VMService.
317- // Since we don't have access to the VMService yet, Protocol Discovery cannot be used for iOS 13+.
318- // Also, wireless devices must be found using mDNS and cannot use Protocol Discovery.
319- final bool compatibleWithProtocolDiscovery = (device is IOSDevice ) &&
320- device.majorSdkVersion < IOSDeviceLogReader .minimumUniversalLoggingSdkVersion &&
321- ! isWirelessIOSDevice;
322-
323- _logger.printStatus ('Waiting for a connection from Flutter on ${device .name }...' );
324- final Status discoveryStatus = _logger.startSpinner (
325- timeout: const Duration (seconds: 30 ),
326- slowWarningCallback: () {
327- // If relying on mDNS to find Dart VM Service, remind the user to allow local network permissions.
328- if (! compatibleWithProtocolDiscovery) {
329- return 'The Dart VM Service was not discovered after 30 seconds. This is taking much longer than expected...\n\n '
330- 'Click "Allow" to the prompt asking if you would like to find and connect devices on your local network. '
331- 'If you selected "Don\' t Allow", you can turn it on in Settings > Your App Name > Local Network. '
332- "If you don't see your app in the Settings, uninstall the app and rerun to see the prompt again.\n " ;
333- }
334-
335- return 'The Dart VM Service was not discovered after 30 seconds. This is taking much longer than expected...\n ' ;
336- },
337- );
338317
339- int ? devicePort;
340- if (debugPort != null ) {
341- devicePort = debugPort;
342- } else if (debugUri != null ) {
343- devicePort = debugUri? .port;
344- } else if (deviceVmservicePort != null ) {
345- devicePort = deviceVmservicePort;
346- }
318+ return 'The Dart VM Service was not discovered after 30 seconds. This is taking much longer than expected...\n ' ;
319+ },
320+ );
347321
348- final Future <Uri ?> mDNSDiscoveryFuture = MDnsVmServiceDiscovery .instance! .getVMServiceUriForAttach (
349- appId,
350- device,
351- usesIpv6: usesIpv6,
352- useDeviceIPAsHost: isWirelessIOSDevice,
353- deviceVmservicePort: devicePort,
354- );
322+ vmServiceUri = vmServiceDiscovery.uris;
355323
356- Future <Uri ?>? protocolDiscoveryFuture;
357- if (compatibleWithProtocolDiscovery) {
358- final ProtocolDiscovery vmServiceDiscovery = ProtocolDiscovery .vmService (
359- device.getLogReader (),
360- portForwarder: device.portForwarder,
361- ipv6: ipv6! ,
362- devicePort: devicePort,
363- hostPort: hostVmservicePort,
364- logger: _logger,
365- );
366- protocolDiscoveryFuture = vmServiceDiscovery.uri;
367- }
368-
369- final Uri ? foundUrl;
370- if (protocolDiscoveryFuture == null ) {
371- foundUrl = await mDNSDiscoveryFuture;
372- } else {
373- foundUrl = await Future .any (
374- < Future <Uri ?>> [mDNSDiscoveryFuture, protocolDiscoveryFuture]
375- );
376- }
324+ // Stop the timer once we receive the first uri.
325+ vmServiceUri = vmServiceUri.map ((Uri uri) {
377326 discoveryStatus.stop ();
378-
379- vmServiceUri = foundUrl == null
380- ? null
381- : Stream <Uri >.value (foundUrl).asBroadcastStream ();
382- }
383- // If MDNS discovery fails or we're not on iOS, fallback to ProtocolDiscovery.
384- if (vmServiceUri == null ) {
385- final ProtocolDiscovery vmServiceDiscovery =
386- ProtocolDiscovery .vmService (
387- // If it's an Android device, attaching relies on past log searching
388- // to find the service protocol.
389- await device.getLogReader (includePastLogs: device is AndroidDevice ),
390- portForwarder: device.portForwarder,
391- ipv6: ipv6! ,
392- devicePort: deviceVmservicePort,
393- hostPort: hostVmservicePort,
394- logger: _logger,
395- );
396- _logger.printStatus ('Waiting for a connection from Flutter on ${device .name }...' );
397- vmServiceUri = vmServiceDiscovery.uris;
398- }
327+ return uri;
328+ });
399329 } else {
400330 vmServiceUri = Stream <Uri >
401331 .fromFuture (
@@ -559,8 +489,7 @@ known, it can be explicitly provided to attach via the command-line, e.g.
559489 Future <void > _validateArguments () async { }
560490
561491 bool _isIOSDevice (Device device) {
562- return (device is IOSDevice ) ||
563- (device is IOSSimulator ) ||
492+ return (device.platformType == PlatformType .ios) ||
564493 (device is MacOSDesignedForIPadDevice );
565494 }
566495}
0 commit comments