Coder Social home page Coder Social logo

07101994 / zeroconf Goto Github PK

View Code? Open in Web Editor NEW

This project forked from novotnyllc/zeroconf

0.0 2.0 0.0 1.77 MB

Bonjour support for .NET Core, .NET 4.5, Windows Phone 8, 8.1, Xamarin iOS and Android, and Windows Store apps

License: Microsoft Public License

C# 99.43% PowerShell 0.57%

zeroconf's Introduction

Zeroconf

Bonjour/mDNS discovery support for .NET v4.6.1, UWP, Xamarin & .NET Standard 2.0

The core logic is implemented primarily .NET Standard 2.0. Due to networking APIs being platform-specific on earlier platforms, a platform-specific version is required. Just make sure that you also install the NuGet to your main app and you'll be all set.

Installation

The easiest way to get started is to use the NuGet package.

Install-Package Zeroconf

Current Build Status: Build Status

Usage

There's are two methods with a few optional parameters:

using Zeroconf;
public async Task ProbeForNetworkPrinters()
{
    IReadOnlyList<IZeroconfHost> results = await
        ZeroconfResolver.ResolveAsync("_printer._tcp.local.");
}

public async Task EnumerateAllServicesFromAllHosts()
{
    ILookup<string, string> domains = await ZeroconfResolver.BrowseDomainsAsync();            
    var responses = await ZeroconfResolver.ResolveAsync(domains.Select(g => g.Key));            
    foreach (var resp in responses)
        Console.WriteLine(resp);
}

The ResolveAsync method has one required and several optional parameters. The method signature is as follows:

Task<IReadOnlyList<IZeroconfHost>> ResolveAsync(string protocol, TimeSpan scanTime = default(TimeSpan), int retries = 2, int retryDelayMilliseconds = 2000, Action<IZeroconfHost> callback = null, CancellationToken cancellationToken = default(CancellationToken));

The BrowseDomainsAsync method has the same set of optional parameters. The method signature is:

Task<ILookup<string, string>> BrowseDomainsAsync(TimeSpan scanTime = default (TimeSpan), int retryDelayMilliseconds = 2000, Action<string, string> callback = null, CancellationToken cancellationToken = default (CancellationToken))

What you get back from the Browse is a lookup, by service name, of a group that contains every host offering that service. Thst most common use would be in the example above, passing in all keys (services) to the Resolve method. Otherwise, you can also see what hosts are offering which services as well.

IObservable

Starting in v2.5, there are two additional methods that return IObservable's instead of Tasks. These methods are otherwise identical to the *Async versions but are more suitable for some usages.

Parameters

Parameter Name Default Value Notes
protocol Service to query. Almost always must end with .local.
scanTime 2 seconds Amount of time to listen for responses
retries 2 Number of times to attempt to bind to the socket. Binding may fail if another app is currently using it.
retryDelayMilliseconds 2000 Delay between retries
callback null If provided, called per IZeroconfigHost as they are processed. This can be used to stream data back prior to call completion.
cancellationToken CancellationToken.None Optional use of task cancellation

Notes

The ResolveAsync method is thread-safe, however all calls to it are serialized as only one can be in-progress at a time.

Xamarin.Android 4.x Linker bug

There is currently a bug on Xamarin.Android 4.x that incorrectly strips out internal Socket methods. This has been fixed for the Xamarin.Android 5.0 series. As a workaround on 4.x, entering System; in to the Ignore Assemblies field in the Project Options->Build->Android Build page will fix the problem.

Android

You must call the WifiManager.MulticastLock manager Aquire and Release before/after you call the Zeroconf methods. Previous versions (prior to 2.7 did this internally, now it requires the caller to do it).

Something like thisl

// Somewhere early
var wifi = (WifiManager)ApplicationContext.GetSystemService(Context.WifiService);
var mlock = wifi.CreateMulticastLock("Zeroconf lock");

---
// Later, before you call Zeroconf
try
{
  mlock.Acquire();

  // Call Zeroconf
  ZeroconfResolver....
}
finally
{
  mlock.Release();
}

You'll also need to specify the correct permsision like this:

[assembly: UsesPermission(Android.Manifest.Permission.ChangeWifiMulticastState)]

UWP

You'll need to have the following permissions on your manifest depending on what networks you're trying to scan:

  • Private Networks (Client & Server)
  • Internet (Client & Server)

Credits

This library was made possible through the efforts of the following projects:

zeroconf's People

Contributors

ghuntley avatar joj avatar jorgensigvardsson avatar kakone avatar kobush avatar lakritzator avatar marcus-l avatar queequac avatar saldoukhov avatar sparksteam avatar zone117x avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.