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, LookupResultFlags flags, IntPtr userdata);
31 public enum DomainBrowserType {
39 public struct DomainInfo
41 public int NetworkInterface;
42 public Protocol Protocol;
44 public LookupResultFlags Flags;
47 public delegate void DomainInfoHandler (object o, DomainInfo info);
49 public class DomainBrowser : BrowserBase, IDisposable
51 private IntPtr handle;
52 private ArrayList infos = new ArrayList ();
53 private Client client;
55 private Protocol proto;
56 private string domain;
57 private DomainBrowserType btype;
58 private LookupFlags flags;
59 private DomainBrowserCallback cb;
61 private ArrayList addListeners = new ArrayList ();
62 private ArrayList removeListeners = new ArrayList ();
64 [DllImport ("avahi-client")]
65 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
66 IntPtr domain, int btype, LookupFlags flags,
67 DomainBrowserCallback cb,
70 [DllImport ("avahi-client")]
71 private static extern void avahi_domain_browser_free (IntPtr handle);
73 public event DomainInfoHandler DomainAdded
76 addListeners.Add (value);
80 addListeners.Remove (value);
85 public event DomainInfoHandler DomainRemoved
88 removeListeners.Add (value);
92 removeListeners.Remove (value);
97 public DomainInfo[] Domains
99 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
102 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
103 DomainBrowserType.Browse, LookupFlags.None) {
106 public DomainBrowser (Client client, int iface, Protocol proto, string domain,
107 DomainBrowserType btype, LookupFlags flags)
109 this.client = client;
112 this.domain = domain;
115 cb = OnDomainBrowserCallback;
123 public void Dispose ()
128 private void Start ()
130 if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
131 (addListeners.Count == 0 && removeListeners.Count == 0))
135 IntPtr domainPtr = Utility.StringToPtr (domain);
136 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype, flags,
138 Utility.Free (domainPtr);
142 private void Stop (bool force)
144 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
145 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
147 avahi_domain_browser_free (handle);
148 handle = IntPtr.Zero;
153 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
154 IntPtr domain, LookupResultFlags flags, IntPtr userdata)
158 info.NetworkInterface = iface;
159 info.Protocol = proto;
160 info.Domain = Utility.PtrToString (domain);
166 case BrowserEvent.Added:
169 foreach (DomainInfoHandler handler in addListeners)
170 handler (this, info);
172 case BrowserEvent.Removed:
175 foreach (DomainInfoHandler handler in removeListeners)
176 handler (this, info);
179 EmitBrowserEvent (bevent);