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;
26 internal delegate void ServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
27 IntPtr type, IntPtr domain, IntPtr userdata);
29 public struct ServiceTypeInfo
31 public int NetworkInterface;
32 public Protocol Protocol;
34 public string ServiceType;
37 public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfo info);
39 public class ServiceTypeBrowser : IDisposable
41 private IntPtr handle;
42 private ArrayList infos = new ArrayList ();
43 private Client client;
45 private Protocol proto;
46 private string domain;
48 private ArrayList addListeners = new ArrayList ();
49 private ArrayList removeListeners = new ArrayList ();
51 [DllImport ("avahi-client")]
52 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
53 IntPtr domain, ServiceTypeBrowserCallback cb,
56 [DllImport ("avahi-client")]
57 private static extern void avahi_service_type_browser_free (IntPtr handle);
59 public event ServiceTypeInfoHandler ServiceTypeAdded
62 addListeners.Add (value);
66 addListeners.Remove (value);
71 public event ServiceTypeInfoHandler ServiceTypeRemoved
74 removeListeners.Add (value);
78 removeListeners.Remove (value);
83 public ServiceTypeInfo[] ServiceTypes
85 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
88 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
92 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified, domain)
96 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain)
101 this.domain = domain;
104 ~ServiceTypeBrowser ()
109 public void Dispose ()
114 private void Start ()
116 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
119 IntPtr domainPtr = Utility.StringToPtr (domain);
120 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr,
121 OnServiceTypeBrowserCallback, IntPtr.Zero);
122 Utility.Free (domainPtr);
125 private void Stop (bool force)
127 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
128 avahi_service_type_browser_free (handle);
129 handle = IntPtr.Zero;
133 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
134 IntPtr type, IntPtr domain, IntPtr userdata)
137 ServiceTypeInfo info;
138 info.NetworkInterface = iface;
139 info.Protocol = proto;
140 info.Domain = Utility.PtrToString (domain);
141 info.ServiceType = Utility.PtrToString (type);
145 if (bevent == BrowserEvent.Added) {
148 foreach (ServiceTypeInfoHandler handler in addListeners)
149 handler (this, info);
153 foreach (ServiceTypeInfoHandler handler in removeListeners)
154 handler (this, info);