4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 using System.Collections;
24 using System.Runtime.InteropServices;
28 internal delegate void DomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
29 IntPtr domain, IntPtr userdata);
31 public enum DomainBrowserType {
39 public struct DomainInfo
41 public int NetworkInterface;
42 public Protocol Protocol;
46 public delegate void DomainInfoHandler (object o, DomainInfo info);
48 public class DomainBrowser : IDisposable
50 private IntPtr handle;
51 private ArrayList infos = new ArrayList ();
52 private Client client;
54 private Protocol proto;
55 private string domain;
56 private DomainBrowserType btype;
57 private DomainBrowserCallback cb;
59 private ArrayList addListeners = new ArrayList ();
60 private ArrayList removeListeners = new ArrayList ();
62 [DllImport ("avahi-client")]
63 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
64 IntPtr domain, int btype, DomainBrowserCallback cb,
67 [DllImport ("avahi-client")]
68 private static extern void avahi_domain_browser_free (IntPtr handle);
70 public event DomainInfoHandler DomainAdded
73 addListeners.Add (value);
77 addListeners.Remove (value);
82 public event DomainInfoHandler DomainRemoved
85 removeListeners.Add (value);
89 removeListeners.Remove (value);
94 public DomainInfo[] Domains
96 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
99 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
100 DomainBrowserType.Browse) {
103 public DomainBrowser (Client client, int iface, Protocol proto, string domain, DomainBrowserType btype)
105 this.client = client;
108 this.domain = domain;
110 cb = OnDomainBrowserCallback;
118 public void Dispose ()
123 private void Start ()
125 if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
126 (addListeners.Count == 0 && removeListeners.Count == 0))
130 IntPtr domainPtr = Utility.StringToPtr (domain);
131 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype,
133 Utility.Free (domainPtr);
137 private void Stop (bool force)
139 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
140 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
142 avahi_domain_browser_free (handle);
143 handle = IntPtr.Zero;
148 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
149 IntPtr domain, IntPtr userdata)
153 info.NetworkInterface = iface;
154 info.Protocol = proto;
155 info.Domain = Utility.PtrToString (domain);
159 if (bevent == BrowserEvent.Added) {
162 foreach (DomainInfoHandler handler in addListeners)
163 handler (this, info);
167 foreach (DomainInfoHandler handler in removeListeners)
168 handler (this, info);