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;
29 internal delegate void DomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
30 IntPtr domain, LookupResultFlags flags, IntPtr userdata);
32 public enum DomainBrowserType {
40 public struct DomainInfo
42 public int NetworkInterface;
43 public Protocol Protocol;
45 public LookupResultFlags Flags;
48 public class DomainInfoArgs : EventArgs
50 private DomainInfo domain;
52 public DomainInfo Domain
54 get { return domain; }
57 public DomainInfoArgs (DomainInfo domain)
63 public delegate void DomainInfoHandler (object o, DomainInfoArgs args);
65 public class DomainBrowser : BrowserBase, IDisposable
67 private IntPtr handle;
68 private ArrayList infos = new ArrayList ();
69 private Client client;
71 private Protocol proto;
72 private string domain;
73 private DomainBrowserType btype;
74 private LookupFlags flags;
75 private DomainBrowserCallback cb;
77 private ArrayList addListeners = new ArrayList ();
78 private ArrayList removeListeners = new ArrayList ();
80 [DllImport ("avahi-client")]
81 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
82 byte[] domain, int btype, LookupFlags flags,
83 DomainBrowserCallback cb,
86 [DllImport ("avahi-client")]
87 private static extern void avahi_domain_browser_free (IntPtr handle);
89 public event DomainInfoHandler DomainAdded
92 addListeners.Add (value);
96 addListeners.Remove (value);
101 public event DomainInfoHandler DomainRemoved
104 removeListeners.Add (value);
108 removeListeners.Remove (value);
113 public DomainInfo[] Domains
115 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
118 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
119 DomainBrowserType.Browse, LookupFlags.None) {
122 public DomainBrowser (Client client, int iface, Protocol proto, string domain,
123 DomainBrowserType btype, LookupFlags flags)
125 this.client = client;
128 this.domain = domain;
131 cb = OnDomainBrowserCallback;
139 public void Dispose ()
144 private void Start ()
146 if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
147 (addListeners.Count == 0 && removeListeners.Count == 0))
151 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto,
152 Utility.StringToBytes (domain), (int) btype, flags,
155 if (handle == IntPtr.Zero)
156 client.ThrowError ();
160 private void Stop (bool force)
162 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
163 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
165 avahi_domain_browser_free (handle);
166 handle = IntPtr.Zero;
171 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
172 IntPtr domain, LookupResultFlags flags, IntPtr userdata)
176 info.NetworkInterface = iface;
177 info.Protocol = proto;
178 info.Domain = Utility.PtrToString (domain);
182 case BrowserEvent.Added:
185 foreach (DomainInfoHandler handler in addListeners)
186 handler (this, new DomainInfoArgs (info));
188 case BrowserEvent.Removed:
191 foreach (DomainInfoHandler handler in removeListeners)
192 handler (this, new DomainInfoArgs (info));
195 EmitBrowserEvent (bevent);