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, LookupResultFlags flags,
32 public struct ServiceTypeInfo
34 public int NetworkInterface;
35 public Protocol Protocol;
37 public string ServiceType;
38 public LookupResultFlags Flags;
41 public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfo info);
43 public class ServiceTypeBrowser : BrowserBase, IDisposable
45 private IntPtr handle;
46 private ArrayList infos = new ArrayList ();
47 private Client client;
49 private Protocol proto;
50 private string domain;
51 private LookupFlags flags;
52 private ServiceTypeBrowserCallback cb;
54 private ArrayList addListeners = new ArrayList ();
55 private ArrayList removeListeners = new ArrayList ();
57 [DllImport ("avahi-client")]
58 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
59 IntPtr domain, LookupFlags flags,
60 ServiceTypeBrowserCallback cb,
63 [DllImport ("avahi-client")]
64 private static extern void avahi_service_type_browser_free (IntPtr handle);
66 public event ServiceTypeInfoHandler ServiceTypeAdded
69 addListeners.Add (value);
73 addListeners.Remove (value);
78 public event ServiceTypeInfoHandler ServiceTypeRemoved
81 removeListeners.Add (value);
85 removeListeners.Remove (value);
90 public ServiceTypeInfo[] ServiceTypes
92 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
95 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
99 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified,
100 domain, LookupFlags.None)
104 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain, LookupFlags flags)
106 this.client = client;
109 this.domain = domain;
111 cb = OnServiceTypeBrowserCallback;
114 ~ServiceTypeBrowser ()
119 public void Dispose ()
124 private void Start ()
126 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
127 (addListeners.Count == 0 && removeListeners.Count == 0))
131 IntPtr domainPtr = Utility.StringToPtr (domain);
132 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr, flags,
134 Utility.Free (domainPtr);
138 private void Stop (bool force)
140 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
141 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
144 avahi_service_type_browser_free (handle);
145 handle = IntPtr.Zero;
150 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
151 IntPtr type, IntPtr domain, LookupResultFlags flags,
155 ServiceTypeInfo info;
156 info.NetworkInterface = iface;
157 info.Protocol = proto;
158 info.Domain = Utility.PtrToString (domain);
159 info.ServiceType = Utility.PtrToString (type);
165 case BrowserEvent.Added:
168 foreach (ServiceTypeInfoHandler handler in addListeners)
169 handler (this, info);
171 case BrowserEvent.Removed:
174 foreach (ServiceTypeInfoHandler handler in removeListeners)
175 handler (this, info);
178 EmitBrowserEvent (bevent);