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, IntPtr userdata);
32 public struct ServiceInfo
34 public int NetworkInterface;
35 public Protocol Protocol;
37 public string ServiceType;
40 public string HostName;
41 public IPAddress Address;
45 public static ServiceInfo Zero = new ServiceInfo ();
48 public delegate void ServiceInfoHandler (object o, ServiceInfo info);
50 public class ServiceBrowser : IDisposable
52 private IntPtr handle;
53 private ArrayList infos = new ArrayList ();
54 private Client client;
56 private Protocol proto;
57 private string domain;
59 private ServiceBrowserCallback cb;
61 private ArrayList addListeners = new ArrayList ();
62 private ArrayList removeListeners = new ArrayList ();
64 [DllImport ("avahi-client")]
65 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
66 IntPtr domain, ServiceBrowserCallback cb,
69 [DllImport ("avahi-client")]
70 private static extern void avahi_service_browser_free (IntPtr handle);
72 public event ServiceInfoHandler ServiceAdded
75 addListeners.Add (value);
79 addListeners.Remove (value);
84 public event ServiceInfoHandler ServiceRemoved
87 removeListeners.Add (value);
91 removeListeners.Remove (value);
96 public ServiceInfo[] Services
98 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
101 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
105 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
110 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain)
112 this.client = client;
115 this.domain = domain;
117 cb = OnServiceBrowserCallback;
125 public void Dispose ()
130 private void Start ()
132 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
133 (addListeners.Count == 0 && removeListeners.Count == 0))
136 IntPtr domainPtr = Utility.StringToPtr (domain);
137 IntPtr typePtr = Utility.StringToPtr (type);
140 handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr,
143 Utility.Free (domainPtr);
144 Utility.Free (typePtr);
147 private void Stop (bool force)
149 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
150 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
153 avahi_service_browser_free (handle);
154 handle = IntPtr.Zero;
159 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
160 IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata)
164 info.NetworkInterface = iface;
165 info.Protocol = proto;
166 info.Domain = Utility.PtrToString (domain);
167 info.ServiceType = Utility.PtrToString (type);
168 info.Name = Utility.PtrToString (name);
169 info.HostName = null;
176 if (bevent == BrowserEvent.Added) {
179 foreach (ServiceInfoHandler handler in addListeners)
180 handler (this, info);
184 foreach (ServiceInfoHandler handler in removeListeners)
185 handler (this, info);