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;
27 internal delegate void DomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
28 IntPtr domain, LookupResultFlags flags, IntPtr userdata);
30 public enum DomainBrowserType {
38 public struct DomainInfo
40 public int NetworkInterface;
41 public Protocol Protocol;
43 public LookupResultFlags Flags;
46 public class DomainInfoArgs : EventArgs
48 private DomainInfo domain;
50 public DomainInfo Domain
52 get { return domain; }
55 public DomainInfoArgs (DomainInfo domain)
61 public delegate void DomainInfoHandler (object o, DomainInfoArgs args);
63 public class DomainBrowser : BrowserBase, IDisposable
65 private IntPtr handle;
66 private ArrayList infos = new ArrayList ();
67 private Client client;
69 private Protocol proto;
70 private string domain;
71 private DomainBrowserType btype;
72 private LookupFlags flags;
73 private DomainBrowserCallback cb;
75 private ArrayList addListeners = new ArrayList ();
76 private ArrayList removeListeners = new ArrayList ();
78 [DllImport ("avahi-client")]
79 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
80 byte[] domain, int btype, LookupFlags flags,
81 DomainBrowserCallback cb,
84 [DllImport ("avahi-client")]
85 private static extern void avahi_domain_browser_free (IntPtr handle);
87 public event DomainInfoHandler DomainAdded
90 addListeners.Add (value);
94 addListeners.Remove (value);
99 public event DomainInfoHandler DomainRemoved
102 removeListeners.Add (value);
106 removeListeners.Remove (value);
111 public DomainInfo[] Domains
113 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
116 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
117 DomainBrowserType.Browse, LookupFlags.None) {
120 public DomainBrowser (Client client, int iface, Protocol proto, string domain,
121 DomainBrowserType btype, LookupFlags flags)
123 this.client = client;
126 this.domain = domain;
129 cb = OnDomainBrowserCallback;
137 public void Dispose ()
142 private void Start ()
144 if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
145 (addListeners.Count == 0 && removeListeners.Count == 0))
149 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto,
150 Utility.StringToBytes (domain), (int) btype, flags,
153 if (handle == IntPtr.Zero)
154 client.ThrowError ();
158 private void Stop (bool force)
160 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
161 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
163 avahi_domain_browser_free (handle);
164 handle = IntPtr.Zero;
169 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
170 IntPtr domain, LookupResultFlags flags, IntPtr userdata)
174 info.NetworkInterface = iface;
175 info.Protocol = proto;
176 info.Domain = Utility.PtrToString (domain);
180 case BrowserEvent.Added:
183 foreach (DomainInfoHandler handler in addListeners)
184 handler (this, new DomainInfoArgs (info));
186 case BrowserEvent.Removed:
189 foreach (DomainInfoHandler handler in removeListeners)
190 handler (this, new DomainInfoArgs (info));
193 EmitBrowserEvent (bevent);