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
23 using System.Collections;
24 using System.Runtime.InteropServices;
28 internal delegate void ServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
29 IntPtr type, IntPtr domain, IntPtr userdata);
31 public struct ServiceTypeInfo
33 public int NetworkInterface;
34 public Protocol Protocol;
36 public string ServiceType;
39 public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfo info);
41 public class ServiceTypeBrowser : IDisposable
43 private IntPtr handle;
44 private ArrayList infos = new ArrayList ();
45 private Client client;
47 private Protocol proto;
48 private string domain;
50 private ArrayList addListeners = new ArrayList ();
51 private ArrayList removeListeners = new ArrayList ();
53 [DllImport ("avahi-client")]
54 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
55 IntPtr domain, ServiceTypeBrowserCallback cb,
58 [DllImport ("avahi-client")]
59 private static extern void avahi_service_type_browser_free (IntPtr handle);
61 public event ServiceTypeInfoHandler ServiceTypeAdded
64 addListeners.Add (value);
68 addListeners.Remove (value);
73 public event ServiceTypeInfoHandler ServiceTypeRemoved
76 removeListeners.Add (value);
80 removeListeners.Remove (value);
85 public ServiceTypeInfo[] ServiceTypes
87 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
90 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
94 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified, domain)
98 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain)
100 this.client = client;
103 this.domain = domain;
106 ~ServiceTypeBrowser ()
111 public void Dispose ()
116 private void Start ()
118 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
121 IntPtr domainPtr = Utility.StringToPtr (domain);
122 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr,
123 OnServiceTypeBrowserCallback, IntPtr.Zero);
124 Utility.Free (domainPtr);
127 private void Stop (bool force)
129 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
130 avahi_service_type_browser_free (handle);
131 handle = IntPtr.Zero;
135 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
136 IntPtr type, IntPtr domain, IntPtr userdata)
139 ServiceTypeInfo info;
140 info.NetworkInterface = iface;
141 info.Protocol = proto;
142 info.Domain = Utility.PtrToString (domain);
143 info.ServiceType = Utility.PtrToString (type);
147 if (bevent == BrowserEvent.Added) {
150 foreach (ServiceTypeInfoHandler handler in addListeners)
151 handler (this, info);
155 foreach (ServiceTypeInfoHandler handler in removeListeners)
156 handler (this, info);