1 /* $Id: ServiceBrowser.cs 635 2005-09-26 03:57:30Z snorp $ */
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
24 using System.Collections;
25 using System.Runtime.InteropServices;
31 public delegate void RecordInfoHandler (object o, RecordInfoArgs args);
33 internal delegate void RecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
34 IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
35 LookupResultFlags flags, IntPtr userdata);
37 public enum RecordClass {
41 public enum RecordType {
54 public struct RecordInfo
56 public int NetworkInterface;
57 public Protocol Protocol;
59 public RecordClass Class;
60 public RecordType Type;
62 public LookupResultFlags Flags;
65 public class RecordInfoArgs : EventArgs
67 private RecordInfo record;
69 public RecordInfo Record {
70 get { return record; }
73 public RecordInfoArgs (RecordInfo record)
79 public class RecordBrowser : BrowserBase, IDisposable
81 private IntPtr handle;
82 private ArrayList infos = new ArrayList ();
83 private Client client;
85 private Protocol proto;
87 private RecordClass clazz;
88 private RecordType type;
89 private LookupFlags flags;
90 private RecordBrowserCallback cb;
92 private ArrayList addListeners = new ArrayList ();
93 private ArrayList removeListeners = new ArrayList ();
95 [DllImport ("avahi-client")]
96 private static extern IntPtr avahi_record_browser_new (IntPtr client, int iface, Protocol proto,
97 byte[] name, ushort clazz, ushort type,
98 LookupFlags flags, RecordBrowserCallback cb,
102 [DllImport ("avahi-client")]
103 private static extern void avahi_record_browser_free (IntPtr handle);
105 public event RecordInfoHandler RecordAdded
108 addListeners.Add (value);
112 addListeners.Remove (value);
117 public event RecordInfoHandler RecordRemoved
120 removeListeners.Add (value);
124 removeListeners.Remove (value);
129 public RecordInfo[] Records
131 get { return (RecordInfo[]) infos.ToArray (typeof (RecordInfo)); }
134 public RecordBrowser (Client client, string name, RecordType type) :
135 this (client, -1, Protocol.Unspecified, name, RecordClass.In, type, LookupFlags.None)
139 public RecordBrowser (Client client, int iface, Protocol proto, string name, RecordClass clazz,
140 RecordType type, LookupFlags flags)
142 this.client = client;
149 cb = OnRecordBrowserCallback;
157 public void Dispose ()
162 private void Start ()
164 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
165 (addListeners.Count == 0 && removeListeners.Count == 0))
169 handle = avahi_record_browser_new (client.Handle, iface, proto, Utility.StringToBytes (name),
170 (ushort) clazz, (ushort) type, flags, cb, IntPtr.Zero);
172 if (handle == IntPtr.Zero)
173 client.ThrowError ();
177 private void Stop (bool force)
179 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
180 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
183 avahi_record_browser_free (handle);
184 handle = IntPtr.Zero;
189 private void OnRecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
190 IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
191 LookupResultFlags flags, IntPtr userdata)
194 info.NetworkInterface = iface;
195 info.Protocol = proto;
196 info.Name = Utility.PtrToString (name);
197 info.Class = (RecordClass) clazz;
198 info.Type = (RecordType) type;
200 info.Data = new byte[size];
202 if (rdata != IntPtr.Zero) {
203 Marshal.Copy (rdata, info.Data, 0, size);
207 case BrowserEvent.Added:
210 foreach (RecordInfoHandler handler in addListeners)
211 handler (this, new RecordInfoArgs (info));
214 case BrowserEvent.Removed:
217 foreach (RecordInfoHandler handler in removeListeners)
218 handler (this, new RecordInfoArgs (info));
222 EmitBrowserEvent (bevent);