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;
29 internal delegate void ServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
30 IntPtr type, IntPtr domain, LookupResultFlags flags,
33 public struct ServiceTypeInfo
35 public int NetworkInterface;
36 public Protocol Protocol;
38 public string ServiceType;
39 public LookupResultFlags Flags;
42 public class ServiceTypeInfoArgs : EventArgs
44 private ServiceTypeInfo type;
46 public ServiceTypeInfo ServiceType
51 public ServiceTypeInfoArgs (ServiceTypeInfo type)
57 public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfoArgs args);
59 public class ServiceTypeBrowser : BrowserBase, IDisposable
61 private IntPtr handle;
62 private ArrayList infos = new ArrayList ();
63 private Client client;
65 private Protocol proto;
66 private string domain;
67 private LookupFlags flags;
68 private ServiceTypeBrowserCallback cb;
70 private ArrayList addListeners = new ArrayList ();
71 private ArrayList removeListeners = new ArrayList ();
73 [DllImport ("avahi-client")]
74 private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto,
75 byte[] domain, LookupFlags flags,
76 ServiceTypeBrowserCallback cb,
79 [DllImport ("avahi-client")]
80 private static extern void avahi_service_type_browser_free (IntPtr handle);
82 public event ServiceTypeInfoHandler ServiceTypeAdded
85 addListeners.Add (value);
89 addListeners.Remove (value);
94 public event ServiceTypeInfoHandler ServiceTypeRemoved
97 removeListeners.Add (value);
101 removeListeners.Remove (value);
106 public ServiceTypeInfo[] ServiceTypes
108 get { return (ServiceTypeInfo[]) infos.ToArray (typeof (ServiceTypeInfo)); }
111 public ServiceTypeBrowser (Client client) : this (client, client.DomainName)
115 public ServiceTypeBrowser (Client client, string domain) : this (client, -1, Protocol.Unspecified,
116 domain, LookupFlags.None)
120 public ServiceTypeBrowser (Client client, int iface, Protocol proto, string domain, LookupFlags flags)
122 this.client = client;
125 this.domain = domain;
127 cb = OnServiceTypeBrowserCallback;
130 ~ServiceTypeBrowser ()
135 public void Dispose ()
140 private void Start ()
142 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
143 (addListeners.Count == 0 && removeListeners.Count == 0))
147 handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto,
148 Utility.StringToBytes (domain), flags,
151 if (handle == IntPtr.Zero)
152 client.ThrowError ();
156 private void Stop (bool force)
158 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
159 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
162 avahi_service_type_browser_free (handle);
163 handle = IntPtr.Zero;
168 private void OnServiceTypeBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
169 IntPtr type, IntPtr domain, LookupResultFlags flags,
173 ServiceTypeInfo info;
174 info.NetworkInterface = iface;
175 info.Protocol = proto;
176 info.Domain = Utility.PtrToString (domain);
177 info.ServiceType = Utility.PtrToString (type);
181 case BrowserEvent.Added:
184 foreach (ServiceTypeInfoHandler handler in addListeners)
185 handler (this, new ServiceTypeInfoArgs (info));
187 case BrowserEvent.Removed:
190 foreach (ServiceTypeInfoHandler handler in removeListeners)
191 handler (this, new ServiceTypeInfoArgs (info));
194 EmitBrowserEvent (bevent);