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 class ServiceInfoArgs : EventArgs
52 private ServiceInfo service;
54 public ServiceInfo Service {
55 get { return service; }
58 public ServiceInfoArgs (ServiceInfo service)
60 this.service = service;
64 public delegate void ServiceInfoHandler (object o, ServiceInfoArgs args);
66 public class ServiceBrowser : BrowserBase, IDisposable
68 private IntPtr handle;
69 private ArrayList infos = new ArrayList ();
70 private Client client;
72 private Protocol proto;
73 private string domain;
75 private LookupFlags flags;
76 private ServiceBrowserCallback cb;
78 private ArrayList addListeners = new ArrayList ();
79 private ArrayList removeListeners = new ArrayList ();
81 [DllImport ("avahi-client")]
82 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
83 IntPtr domain, LookupFlags flags,
84 ServiceBrowserCallback cb,
87 [DllImport ("avahi-client")]
88 private static extern void avahi_service_browser_free (IntPtr handle);
90 public event ServiceInfoHandler ServiceAdded
93 addListeners.Add (value);
97 addListeners.Remove (value);
102 public event ServiceInfoHandler ServiceRemoved
105 removeListeners.Add (value);
109 removeListeners.Remove (value);
114 public ServiceInfo[] Services
116 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
119 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
123 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
124 type, domain, LookupFlags.None)
128 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain, LookupFlags flags)
130 this.client = client;
133 this.domain = domain;
136 cb = OnServiceBrowserCallback;
144 public void Dispose ()
149 private void Start ()
151 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
152 (addListeners.Count == 0 && removeListeners.Count == 0))
155 IntPtr domainPtr = Utility.StringToPtr (domain);
156 IntPtr typePtr = Utility.StringToPtr (type);
159 handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr, flags,
162 Utility.Free (domainPtr);
163 Utility.Free (typePtr);
166 private void Stop (bool force)
168 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
169 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
172 avahi_service_browser_free (handle);
173 handle = IntPtr.Zero;
178 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
179 IntPtr name, IntPtr type, IntPtr domain, LookupResultFlags flags,
184 info.NetworkInterface = iface;
185 info.Protocol = proto;
186 info.Domain = Utility.PtrToString (domain);
187 info.ServiceType = Utility.PtrToString (type);
188 info.Name = Utility.PtrToString (name);
189 info.HostName = null;
196 case BrowserEvent.Added:
199 foreach (ServiceInfoHandler handler in addListeners)
200 handler (this, new ServiceInfoArgs (info));
203 case BrowserEvent.Removed:
206 foreach (ServiceInfoHandler handler in removeListeners)
207 handler (this, new ServiceInfoArgs (info));
211 EmitBrowserEvent (bevent);