Skyost / Bonsoir

A Zeroconf library that allows you to discover network services and to broadcast your own. Based on Apple Bonjour and Android NSD.
http://bonsoir.skyost.eu/
Other
100 stars 43 forks source link

Invalid service port returned since v4 #63

Closed jonathantribouharet closed 8 months ago

jonathantribouharet commented 8 months ago

Describe the bug I've upgraded from v3 to latest version and try the github version, since the service port received is not valid. The service port is fixed, I use Bonsoir to detect my Chromecast and the service port is either 8008 or 8009.

To Reproduce Steps to reproduce the behavior:

import 'dart:async';

import 'package:bonsoir/bonsoir.dart';

import 'device.dart';

const _domain = '_googlecast._tcp';

class CastDiscoveryService {
  static final CastDiscoveryService _instance = CastDiscoveryService._();
  CastDiscoveryService._();

  factory CastDiscoveryService() {
    return _instance;
  }

  Future<List<CastDevice>> search({Duration timeout = const Duration(seconds: 5)}) async {
    final results = <CastDevice>[];

    final discovery = BonsoirDiscovery(type: _domain);
    await discovery.ready;

    discovery.eventStream!.listen((event) {
      if (event.type == BonsoirDiscoveryEventType.discoveryServiceFound) {
        event.service?.resolve(discovery.serviceResolver);
      } else if (event.type == BonsoirDiscoveryEventType.discoveryServiceResolved) {
        if (event.service == null || event.service?.attributes == null) {
          return;
        }

        final port = event.service?.port;
        final host = event.service?.toJson()['service.ip'] ?? event.service?.toJson()['service.host'];

        String name = [
          event.service?.attributes?['md'],
          event.service?.attributes?['fn'],
        ].whereType<String>().join(' - ');
        if (name.isEmpty) {
          name = event.service!.name;
        }

        if (port == null || host == null) {
          return;
        }

        results.add(
          CastDevice(
            serviceName: event.service!.name,
            name: name,
            port: port,
            host: host,
            extras: event.service!.attributes ?? {},
          ),
        );
      }
    }, onError: (error) {
      print('[CastDiscoveryService] error ${error.runtimeType} - $error');
    });

    await discovery.start();
    await Future.delayed(timeout);
    await discovery.stop();

    return results.toSet().toList();
  }
}

Expected behavior To return the right port.

Desktop (please complete the following information):

Additional context

Here the data receive with v3

{
service.name: Chromecast-Ultra-f6d7e30603081403bac91241a432ea99,
service.type: _googlecast._tcp,
service.port: 8009,
service.attributes: {ve: 05, cd: 3AB1A3FADCE0590F5307B485A9AA6EA4, rm: 6E287B10CA7872D3, ic: /setup/icon.png, ca: 201221, nf: 2, st: 1, md: Chromecast Ultra, bs: FA8FCA770C52, fn: My Cast, id: f6d7e30603081403bac91241a432ea99, rs: My Delivery KDS}, service.ip: 192.168.1.54
}

Here the data receive with v4

{
service.name: Chromecast-Ultra-f6d7e30603081403bac91241a432ea99,
service.type: _googlecast._tcp,
service.port: 18719,
service.attributes: {rm: 6E287B10CA7872D3, ve: 05, ca: 201221, md: Chromecast Ultra, bs: FA8FCA770C52, id: f6d7e30603081403bac91241a432ea99, ic: /setup/icon.png, nf: 2, cd: 3AB1A3FADCE0590F5307B485A9AA6EA4, rs: My Delivery KDS, fn: My Cast, st: 1}, service.host: f6d7e306-0308-1403-bac9-1241a432ea99.local.
}
Skyost commented 8 months ago

Wow, you're right, I don't know what's happening here.

Skyost commented 8 months ago

Confirmed. On the sample app, port 4000 becomes 40975.

milesegan commented 6 months ago

I'm seeing something similar to this in my app. The port is always coming back 0. The fix you put in should already be in 5.1.1 right?

edit: sorry I think this was a false alarm from running on the iOS simulator. When I run on-device it works.