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;
58 private ArrayList addListeners = new ArrayList ();
59 private ArrayList removeListeners = new ArrayList ();
61 [DllImport ("avahi-client")]
62 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
63 IntPtr domain, int btype, DomainBrowserCallback cb,
66 [DllImport ("avahi-client")]
67 private static extern void avahi_domain_browser_free (IntPtr handle);
69 public event DomainInfoHandler DomainAdded
72 addListeners.Add (value);
76 addListeners.Remove (value);
81 public event DomainInfoHandler DomainRemoved
84 removeListeners.Add (value);
88 removeListeners.Remove (value);
93 public DomainInfo[] Domains
95 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
98 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
99 DomainBrowserType.Browse) {
102 public DomainBrowser (Client client, int iface, Protocol proto, string domain, DomainBrowserType btype)
104 this.client = client;
107 this.domain = domain;
116 public void Dispose ()
121 private void Start ()
123 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
126 IntPtr domainPtr = Utility.StringToPtr (domain);
127 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype,
128 OnDomainBrowserCallback, IntPtr.Zero);
129 Utility.Free (domainPtr);
132 private void Stop (bool force)
134 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
135 avahi_domain_browser_free (handle);
136 handle = IntPtr.Zero;
140 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
141 IntPtr domain, IntPtr userdata)
145 info.NetworkInterface = iface;
146 info.Protocol = proto;
147 info.Domain = Utility.PtrToString (domain);
151 if (bevent == BrowserEvent.Added) {
154 foreach (DomainInfoHandler handler in addListeners)
155 handler (this, info);
159 foreach (DomainInfoHandler handler in removeListeners)
160 handler (this, info);