2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 using System.Collections;
22 using System.Runtime.InteropServices;
26 internal delegate void DomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
27 IntPtr domain, IntPtr userdata);
29 public enum DomainBrowserType {
37 public struct DomainInfo
39 public int NetworkInterface;
40 public Protocol Protocol;
44 public delegate void DomainInfoHandler (object o, DomainInfo info);
46 public class DomainBrowser : IDisposable
48 private IntPtr handle;
49 private ArrayList infos = new ArrayList ();
50 private Client client;
52 private Protocol proto;
53 private string domain;
54 private DomainBrowserType btype;
56 private ArrayList addListeners = new ArrayList ();
57 private ArrayList removeListeners = new ArrayList ();
59 [DllImport ("avahi-client")]
60 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
61 IntPtr domain, int btype, DomainBrowserCallback cb,
64 [DllImport ("avahi-client")]
65 private static extern void avahi_domain_browser_free (IntPtr handle);
67 public event DomainInfoHandler DomainAdded
70 addListeners.Add (value);
74 addListeners.Remove (value);
79 public event DomainInfoHandler DomainRemoved
82 removeListeners.Add (value);
86 removeListeners.Remove (value);
91 public DomainInfo[] Domains
93 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
96 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
97 DomainBrowserType.Browse) {
100 public DomainBrowser (Client client, int iface, Protocol proto, string domain, DomainBrowserType btype)
102 this.client = client;
105 this.domain = domain;
114 public void Dispose ()
119 private void Start ()
121 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
124 IntPtr domainPtr = Utility.StringToPtr (domain);
125 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype,
126 OnDomainBrowserCallback, IntPtr.Zero);
127 Utility.Free (domainPtr);
130 private void Stop (bool force)
132 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
133 avahi_domain_browser_free (handle);
134 handle = IntPtr.Zero;
138 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
139 IntPtr domain, IntPtr userdata)
143 info.NetworkInterface = iface;
144 info.Protocol = proto;
145 info.Domain = Utility.PtrToString (domain);
149 if (bevent == BrowserEvent.Added) {
152 foreach (DomainInfoHandler handler in addListeners)
153 handler (this, info);
157 foreach (DomainInfoHandler handler in removeListeners)
158 handler (this, info);