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
21 using System.Collections;
22 using System.Runtime.InteropServices;
27 internal delegate void ServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
28 IntPtr type, IntPtr domain, LookupResultFlags flags,
31 public struct ServiceTypeInfo
33 public int NetworkInterface;
34 public Protocol Protocol;
36 public string ServiceType;
37 public LookupResultFlags Flags;
40 public class ServiceTypeInfoArgs : EventArgs
42 private ServiceTypeInfo type;
44 public ServiceTypeInfo ServiceType
49 public ServiceTypeInfoArgs (ServiceTypeInfo type)
55 public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfoArgs args);
57 public class ServiceTypeBrowser : BrowserBase, IDisposable
59 private IntPtr handle;
60 private ArrayList infos = new ArrayList ();
61 private Client client;
63 private Protocol proto;
64 private string domain;
65 private LookupFlags flags;
66 private ServiceTypeBrowserCallback cb;
68 private ArrayList addListeners = new ArrayList ();
69 private ArrayList removeListeners = new ArrayList ();
71 [DllImport ("avahi-client")]
72 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
73 byte[] domain, LookupFlags flags,
74 ServiceTypeBrowserCallback cb,
77 [DllImport ("avahi-client")]
78 private static extern void avahi_service_type_browser_free (IntPtr handle);
80 public event ServiceTypeInfoHandler ServiceTypeAdded
83 addListeners.Add (value);
87 addListeners.Remove (value);
92 public event ServiceTypeInfoHandler ServiceTypeRemoved
95 removeListeners.Add (value);
99 removeListeners.Remove (value);
104 public ServiceTypeInfo[] ServiceTypes
106 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
109 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
113 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified,
114 domain, LookupFlags.None)
118 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain, LookupFlags flags)
120 this.client = client;
123 this.domain = domain;
125 cb = OnServiceTypeBrowserCallback;
128 ~ServiceTypeBrowser ()
133 public void Dispose ()
138 private void Start ()
140 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
141 (addListeners.Count == 0 && removeListeners.Count == 0))
145 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto,
146 Utility.StringToBytes (domain), flags,
149 if (handle == IntPtr.Zero)
150 client.ThrowError ();
154 private void Stop (bool force)
156 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
157 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
160 avahi_service_type_browser_free (handle);
161 handle = IntPtr.Zero;
166 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
167 IntPtr type, IntPtr domain, LookupResultFlags flags,
171 ServiceTypeInfo info;
172 info.NetworkInterface = iface;
173 info.Protocol = proto;
174 info.Domain = Utility.PtrToString (domain);
175 info.ServiceType = Utility.PtrToString (type);
179 case BrowserEvent.Added:
182 foreach (ServiceTypeInfoHandler handler in addListeners)
183 handler (this, new ServiceTypeInfoArgs (info));
185 case BrowserEvent.Removed:
188 foreach (ServiceTypeInfoHandler handler in removeListeners)
189 handler (this, new ServiceTypeInfoArgs (info));
192 EmitBrowserEvent (bevent);