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;
30 internal delegate void ServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
31 IntPtr name, IntPtr type, IntPtr domain, LookupResultFlags flags,
34 public struct ServiceInfo
36 public int NetworkInterface;
37 public Protocol Protocol;
39 public string ServiceType;
42 public string HostName;
43 public IPAddress Address;
46 public LookupResultFlags Flags;
48 public static ServiceInfo Zero = new ServiceInfo ();
51 public class ServiceInfoArgs : EventArgs
53 private ServiceInfo service;
55 public ServiceInfo Service {
56 get { return service; }
59 public ServiceInfoArgs (ServiceInfo service)
61 this.service = service;
65 public delegate void ServiceInfoHandler (object o, ServiceInfoArgs args);
67 public class ServiceBrowser : BrowserBase, IDisposable
69 private IntPtr handle;
70 private ArrayList infos = new ArrayList ();
71 private Client client;
73 private Protocol proto;
74 private string domain;
76 private LookupFlags flags;
77 private ServiceBrowserCallback cb;
79 private ArrayList addListeners = new ArrayList ();
80 private ArrayList removeListeners = new ArrayList ();
82 [DllImport ("avahi-client")]
83 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, byte[] type,
84 byte[] domain, LookupFlags flags,
85 ServiceBrowserCallback cb,
88 [DllImport ("avahi-client")]
89 private static extern void avahi_service_browser_free (IntPtr handle);
91 public event ServiceInfoHandler ServiceAdded
94 addListeners.Add (value);
98 addListeners.Remove (value);
103 public event ServiceInfoHandler ServiceRemoved
106 removeListeners.Add (value);
110 removeListeners.Remove (value);
115 public ServiceInfo[] Services
117 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
120 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
124 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
125 type, domain, LookupFlags.None)
129 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain, LookupFlags flags)
131 this.client = client;
134 this.domain = domain;
137 cb = OnServiceBrowserCallback;
145 public void Dispose ()
150 private void Start ()
152 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
153 (addListeners.Count == 0 && removeListeners.Count == 0))
157 handle = avahi_service_browser_new (client.Handle, iface, (int) proto,
158 Utility.StringToBytes (type), Utility.StringToBytes (domain),
159 flags, cb, IntPtr.Zero);
161 if (handle == IntPtr.Zero)
162 client.ThrowError ();
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);