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;
28 internal delegate void ServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
29 IntPtr name, IntPtr type, IntPtr domain, LookupResultFlags flags,
32 public struct ServiceInfo
34 public int NetworkInterface;
35 public Protocol Protocol;
37 public string ServiceType;
40 public string HostName;
41 public IPAddress Address;
44 public LookupResultFlags Flags;
46 public static ServiceInfo Zero = new ServiceInfo ();
49 public class ServiceInfoArgs : EventArgs
51 private ServiceInfo service;
53 public ServiceInfo Service {
54 get { return service; }
57 public ServiceInfoArgs (ServiceInfo service)
59 this.service = service;
63 public delegate void ServiceInfoHandler (object o, ServiceInfoArgs args);
65 public class ServiceBrowser : BrowserBase, IDisposable
67 private IntPtr handle;
68 private ArrayList infos = new ArrayList ();
69 private Client client;
71 private Protocol proto;
72 private string domain;
74 private LookupFlags flags;
75 private ServiceBrowserCallback cb;
77 private ArrayList addListeners = new ArrayList ();
78 private ArrayList removeListeners = new ArrayList ();
80 [DllImport ("avahi-client")]
81 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, byte[] type,
82 byte[] domain, LookupFlags flags,
83 ServiceBrowserCallback cb,
86 [DllImport ("avahi-client")]
87 private static extern void avahi_service_browser_free (IntPtr handle);
89 public event ServiceInfoHandler ServiceAdded
92 addListeners.Add (value);
96 addListeners.Remove (value);
101 public event ServiceInfoHandler ServiceRemoved
104 removeListeners.Add (value);
108 removeListeners.Remove (value);
113 public ServiceInfo[] Services
115 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
118 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
122 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
123 type, domain, LookupFlags.None)
127 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain, LookupFlags flags)
129 this.client = client;
132 this.domain = domain;
135 cb = OnServiceBrowserCallback;
143 public void Dispose ()
148 private void Start ()
150 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
151 (addListeners.Count == 0 && removeListeners.Count == 0))
155 handle = avahi_service_browser_new (client.Handle, iface, (int) proto,
156 Utility.StringToBytes (type), Utility.StringToBytes (domain),
157 flags, cb, IntPtr.Zero);
159 if (handle == IntPtr.Zero)
160 client.ThrowError ();
164 private void Stop (bool force)
166 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
167 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
170 avahi_service_browser_free (handle);
171 handle = IntPtr.Zero;
176 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
177 IntPtr name, IntPtr type, IntPtr domain, LookupResultFlags flags,
182 info.NetworkInterface = iface;
183 info.Protocol = proto;
184 info.Domain = Utility.PtrToString (domain);
185 info.ServiceType = Utility.PtrToString (type);
186 info.Name = Utility.PtrToString (name);
187 info.HostName = null;
194 case BrowserEvent.Added:
197 foreach (ServiceInfoHandler handler in addListeners)
198 handler (this, new ServiceInfoArgs (info));
201 case BrowserEvent.Removed:
204 foreach (ServiceInfoHandler handler in removeListeners)
205 handler (this, new ServiceInfoArgs (info));
209 EmitBrowserEvent (bevent);