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 (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
135 IntPtr domainPtr = Utility.StringToPtr (domain);
136 IntPtr typePtr = Utility.StringToPtr (type);
137 handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr,
139 Utility.Free (domainPtr);
140 Utility.Free (typePtr);
143 private void Stop (bool force)
145 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
146 avahi_service_browser_free (handle);
147 handle = IntPtr.Zero;
151 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
152 IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata)
156 info.NetworkInterface = iface;
157 info.Protocol = proto;
158 info.Domain = Utility.PtrToString (domain);
159 info.ServiceType = Utility.PtrToString (type);
160 info.Name = Utility.PtrToString (name);
161 info.HostName = null;
168 if (bevent == BrowserEvent.Added) {
171 foreach (ServiceInfoHandler handler in addListeners)
172 handler (this, info);
176 foreach (ServiceInfoHandler handler in removeListeners)
177 handler (this, info);