diff options
Diffstat (limited to 'avahi-sharp/DomainBrowser.cs')
-rw-r--r-- | avahi-sharp/DomainBrowser.cs | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/avahi-sharp/DomainBrowser.cs b/avahi-sharp/DomainBrowser.cs new file mode 100644 index 0000000..deed9b8 --- /dev/null +++ b/avahi-sharp/DomainBrowser.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections; +using System.Runtime.InteropServices; + +namespace Avahi +{ + internal delegate void DomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent, + IntPtr domain, IntPtr userdata); + + public enum DomainBrowserType { + Register, + RegisterDefault, + Browse, + BrowseDefault, + BrowseLegacy + } + + public struct DomainInfo + { + public int NetworkInterface; + public Protocol Protocol; + public string Domain; + } + + public delegate void DomainInfoHandler (object o, DomainInfo info); + + public class DomainBrowser : IDisposable + { + private IntPtr handle; + private ArrayList infos = new ArrayList (); + private Client client; + private int iface; + private Protocol proto; + private string domain; + private DomainBrowserType btype; + + private ArrayList addListeners = new ArrayList (); + private ArrayList removeListeners = new ArrayList (); + + [DllImport ("avahi-client")] + private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto, + IntPtr domain, int btype, DomainBrowserCallback cb, + IntPtr userdata); + + [DllImport ("avahi-client")] + private static extern void avahi_domain_browser_free (IntPtr handle); + + public event DomainInfoHandler DomainAdded + { + add { + addListeners.Add (value); + Start (); + } + remove { + addListeners.Remove (value); + Stop (false); + } + } + + public event DomainInfoHandler DomainRemoved + { + add { + removeListeners.Add (value); + Start (); + } + remove { + removeListeners.Remove (value); + Stop (false); + } + } + + public DomainInfo[] Domains + { + get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); } + } + + public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName, + DomainBrowserType.Browse) { + } + + public DomainBrowser (Client client, int iface, Protocol proto, string domain, DomainBrowserType btype) + { + this.client = client; + this.iface = iface; + this.proto = proto; + this.domain = domain; + this.btype = btype; + } + + ~DomainBrowser () + { + Dispose (); + } + + public void Dispose () + { + Stop (true); + } + + private void Start () + { + if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0)) + return; + + IntPtr domainPtr = Utility.StringToPtr (domain); + handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype, + OnDomainBrowserCallback, IntPtr.Zero); + Utility.Free (domainPtr); + } + + private void Stop (bool force) + { + if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) { + avahi_domain_browser_free (handle); + handle = IntPtr.Zero; + } + } + + private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent, + IntPtr domain, IntPtr userdata) + { + + DomainInfo info; + info.NetworkInterface = iface; + info.Protocol = proto; + info.Domain = Utility.PtrToString (domain); + + infos.Add (info); + + if (bevent == BrowserEvent.Added) { + infos.Add (info); + + foreach (DomainInfoHandler handler in addListeners) + handler (this, info); + } else { + infos.Remove (info); + + foreach (DomainInfoHandler handler in removeListeners) + handler (this, info); + } + } + } +} |