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
22 using System.Collections;
23 using System.Runtime.InteropServices;
27 internal delegate void ServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
28 IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata);
30 public struct ServiceInfo
32 public int NetworkInterface;
33 public Protocol Protocol;
35 public string ServiceType;
39 public IPAddress Address;
43 public static ServiceInfo Zero = new ServiceInfo ();
46 public delegate void ServiceInfoHandler (object o, ServiceInfo info);
48 public class ServiceBrowser : IDisposable
50 private IntPtr handle;
51 private ArrayList infos = new ArrayList ();
52 private Client client;
54 private Protocol proto;
55 private string domain;
58 private ArrayList addListeners = new ArrayList ();
59 private ArrayList removeListeners = new ArrayList ();
61 [DllImport ("avahi-client")]
62 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
63 IntPtr domain, ServiceBrowserCallback cb,
66 [DllImport ("avahi-client")]
67 private static extern void avahi_service_browser_free (IntPtr handle);
69 public event ServiceInfoHandler ServiceAdded
72 addListeners.Add (value);
76 addListeners.Remove (value);
81 public event ServiceInfoHandler ServiceRemoved
84 removeListeners.Add (value);
88 removeListeners.Remove (value);
93 public ServiceInfo[] Services
95 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
98 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
102 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
107 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain)
109 this.client = client;
112 this.domain = domain;
123 public void Dispose ()
128 private void Start ()
130 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
133 IntPtr domainPtr = Utility.StringToPtr (domain);
134 IntPtr typePtr = Utility.StringToPtr (type);
135 handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr,
136 OnServiceBrowserCallback, IntPtr.Zero);
137 Utility.Free (domainPtr);
138 Utility.Free (typePtr);
141 private void Stop (bool force)
143 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
144 avahi_service_browser_free (handle);
145 handle = IntPtr.Zero;
149 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
150 IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata)
154 info.NetworkInterface = iface;
155 info.Protocol = proto;
156 info.Domain = Utility.PtrToString (domain);
157 info.ServiceType = Utility.PtrToString (type);
158 info.Name = Utility.PtrToString (name);
166 if (bevent == BrowserEvent.Added) {
169 foreach (ServiceInfoHandler handler in addListeners)
170 handler (this, info);
174 foreach (ServiceInfoHandler handler in removeListeners)
175 handler (this, info);