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 class DomainInfoArgs : EventArgs
49 private DomainInfo domain;
51 public DomainInfo Domain
53 get { return domain; }
56 public DomainInfoArgs (DomainInfo domain)
62 public delegate void DomainInfoHandler (object o, DomainInfoArgs args);
64 public class DomainBrowser : BrowserBase, IDisposable
66 private IntPtr handle;
67 private ArrayList infos = new ArrayList ();
68 private Client client;
70 private Protocol proto;
71 private string domain;
72 private DomainBrowserType btype;
73 private LookupFlags flags;
74 private DomainBrowserCallback cb;
76 private ArrayList addListeners = new ArrayList ();
77 private ArrayList removeListeners = new ArrayList ();
79 [DllImport ("avahi-client")]
80 private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto,
81 IntPtr domain, int btype, LookupFlags flags,
82 DomainBrowserCallback cb,
85 [DllImport ("avahi-client")]
86 private static extern void avahi_domain_browser_free (IntPtr handle);
88 public event DomainInfoHandler DomainAdded
91 addListeners.Add (value);
95 addListeners.Remove (value);
100 public event DomainInfoHandler DomainRemoved
103 removeListeners.Add (value);
107 removeListeners.Remove (value);
112 public DomainInfo[] Domains
114 get { return (DomainInfo[]) infos.ToArray (typeof (DomainInfo)); }
117 public DomainBrowser (Client client) : this (client, -1, Protocol.Unspecified, client.DomainName,
118 DomainBrowserType.Browse, LookupFlags.None) {
121 public DomainBrowser (Client client, int iface, Protocol proto, string domain,
122 DomainBrowserType btype, LookupFlags flags)
124 this.client = client;
127 this.domain = domain;
130 cb = OnDomainBrowserCallback;
138 public void Dispose ()
143 private void Start ()
145 if (client.Handle == IntPtr.Zero && handle != IntPtr.Zero ||
146 (addListeners.Count == 0 && removeListeners.Count == 0))
150 IntPtr domainPtr = Utility.StringToPtr (domain);
151 handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype, flags,
153 Utility.Free (domainPtr);
157 private void Stop (bool force)
159 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
160 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
162 avahi_domain_browser_free (handle);
163 handle = IntPtr.Zero;
168 private void OnDomainBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
169 IntPtr domain, LookupResultFlags flags, IntPtr userdata)
173 info.NetworkInterface = iface;
174 info.Protocol = proto;
175 info.Domain = Utility.PtrToString (domain);
179 case BrowserEvent.Added:
182 foreach (DomainInfoHandler handler in addListeners)
183 handler (this, new DomainInfoArgs (info));
185 case BrowserEvent.Removed:
188 foreach (DomainInfoHandler handler in removeListeners)
189 handler (this, new DomainInfoArgs (info));
192 EmitBrowserEvent (bevent);