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 class ServiceTypeInfoArgs : EventArgs
43 private ServiceTypeInfo type;
45 public ServiceTypeInfo ServiceType
50 public ServiceTypeInfoArgs (ServiceTypeInfo type)
56 public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfoArgs args);
58 public class ServiceTypeBrowser : BrowserBase, IDisposable
60 private IntPtr handle;
61 private ArrayList infos = new ArrayList ();
62 private Client client;
64 private Protocol proto;
65 private string domain;
66 private LookupFlags flags;
67 private ServiceTypeBrowserCallback cb;
69 private ArrayList addListeners = new ArrayList ();
70 private ArrayList removeListeners = new ArrayList ();
72 [DllImport ("avahi-client")]
73 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
74 IntPtr domain, LookupFlags flags,
75 ServiceTypeBrowserCallback cb,
78 [DllImport ("avahi-client")]
79 private static extern void avahi_service_type_browser_free (IntPtr handle);
81 public event ServiceTypeInfoHandler ServiceTypeAdded
84 addListeners.Add (value);
88 addListeners.Remove (value);
93 public event ServiceTypeInfoHandler ServiceTypeRemoved
96 removeListeners.Add (value);
100 removeListeners.Remove (value);
105 public ServiceTypeInfo[] ServiceTypes
107 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
110 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
114 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified,
115 domain, LookupFlags.None)
119 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain, LookupFlags flags)
121 this.client = client;
124 this.domain = domain;
126 cb = OnServiceTypeBrowserCallback;
129 ~ServiceTypeBrowser ()
134 public void Dispose ()
139 private void Start ()
141 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
142 (addListeners.Count == 0 && removeListeners.Count == 0))
146 IntPtr domainPtr = Utility.StringToPtr (domain);
147 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr, flags,
149 Utility.Free (domainPtr);
153 private void Stop (bool force)
155 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
156 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
159 avahi_service_type_browser_free (handle);
160 handle = IntPtr.Zero;
165 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
166 IntPtr type, IntPtr domain, LookupResultFlags flags,
170 ServiceTypeInfo info;
171 info.NetworkInterface = iface;
172 info.Protocol = proto;
173 info.Domain = Utility.PtrToString (domain);
174 info.ServiceType = Utility.PtrToString (type);
178 case BrowserEvent.Added:
181 foreach (ServiceTypeInfoHandler handler in addListeners)
182 handler (this, new ServiceTypeInfoArgs (info));
184 case BrowserEvent.Removed:
187 foreach (ServiceTypeInfoHandler handler in removeListeners)
188 handler (this, new ServiceTypeInfoArgs (info));
191 EmitBrowserEvent (bevent);