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;
49 private ServiceTypeBrowserCallback cb;
51 private ArrayList addListeners = new ArrayList ();
52 private ArrayList removeListeners = new ArrayList ();
54 [DllImport ("avahi-client")]
55 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
56 IntPtr domain, ServiceTypeBrowserCallback cb,
59 [DllImport ("avahi-client")]
60 private static extern void avahi_service_type_browser_free (IntPtr handle);
62 public event ServiceTypeInfoHandler ServiceTypeAdded
65 addListeners.Add (value);
69 addListeners.Remove (value);
74 public event ServiceTypeInfoHandler ServiceTypeRemoved
77 removeListeners.Add (value);
81 removeListeners.Remove (value);
86 public ServiceTypeInfo[] ServiceTypes
88 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
91 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
95 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified, domain)
99 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain)
101 this.client = client;
104 this.domain = domain;
105 cb = OnServiceTypeBrowserCallback;
108 ~ServiceTypeBrowser ()
113 public void Dispose ()
118 private void Start ()
120 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
123 IntPtr domainPtr = Utility.StringToPtr (domain);
124 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr,
126 Utility.Free (domainPtr);
129 private void Stop (bool force)
131 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
132 avahi_service_type_browser_free (handle);
133 handle = IntPtr.Zero;
137 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
138 IntPtr type, IntPtr domain, IntPtr userdata)
141 ServiceTypeInfo info;
142 info.NetworkInterface = iface;
143 info.Protocol = proto;
144 info.Domain = Utility.PtrToString (domain);
145 info.ServiceType = Utility.PtrToString (type);
149 if (bevent == BrowserEvent.Added) {
152 foreach (ServiceTypeInfoHandler handler in addListeners)
153 handler (this, info);
157 foreach (ServiceTypeInfoHandler handler in removeListeners)
158 handler (this, info);