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
24 using System.Collections;
25 using System.Runtime.InteropServices;
29 internal delegate void ServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
30 IntPtr name, IntPtr type, IntPtr domain, LookupResultFlags flags,
33 public struct ServiceInfo
35 public int NetworkInterface;
36 public Protocol Protocol;
38 public string ServiceType;
41 public string HostName;
42 public IPAddress Address;
45 public LookupResultFlags Flags;
47 public static ServiceInfo Zero = new ServiceInfo ();
50 public delegate void ServiceInfoHandler (object o, ServiceInfo info);
52 public class ServiceBrowser : BrowserBase, IDisposable
54 private IntPtr handle;
55 private ArrayList infos = new ArrayList ();
56 private Client client;
58 private Protocol proto;
59 private string domain;
61 private LookupFlags flags;
62 private ServiceBrowserCallback cb;
64 private ArrayList addListeners = new ArrayList ();
65 private ArrayList removeListeners = new ArrayList ();
67 [DllImport ("avahi-client")]
68 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
69 IntPtr domain, LookupFlags flags,
70 ServiceBrowserCallback cb,
73 [DllImport ("avahi-client")]
74 private static extern void avahi_service_browser_free (IntPtr handle);
76 public event ServiceInfoHandler ServiceAdded
79 addListeners.Add (value);
83 addListeners.Remove (value);
88 public event ServiceInfoHandler ServiceRemoved
91 removeListeners.Add (value);
95 removeListeners.Remove (value);
100 public ServiceInfo[] Services
102 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
105 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
109 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
110 type, domain, LookupFlags.None)
114 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain, LookupFlags flags)
116 this.client = client;
119 this.domain = domain;
122 cb = OnServiceBrowserCallback;
130 public void Dispose ()
135 private void Start ()
137 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
138 (addListeners.Count == 0 && removeListeners.Count == 0))
141 IntPtr domainPtr = Utility.StringToPtr (domain);
142 IntPtr typePtr = Utility.StringToPtr (type);
145 handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr, flags,
148 Utility.Free (domainPtr);
149 Utility.Free (typePtr);
152 private void Stop (bool force)
154 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
155 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
158 avahi_service_browser_free (handle);
159 handle = IntPtr.Zero;
164 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
165 IntPtr name, IntPtr type, IntPtr domain, LookupResultFlags flags,
170 info.NetworkInterface = iface;
171 info.Protocol = proto;
172 info.Domain = Utility.PtrToString (domain);
173 info.ServiceType = Utility.PtrToString (type);
174 info.Name = Utility.PtrToString (name);
175 info.HostName = null;
184 case BrowserEvent.Added:
187 foreach (ServiceInfoHandler handler in addListeners)
188 handler (this, info);
191 case BrowserEvent.Removed:
194 foreach (ServiceInfoHandler handler in removeListeners)
195 handler (this, info);
199 EmitBrowserEvent (bevent);