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;
30 public delegate void RecordInfoHandler (object o, RecordInfoArgs args);
32 internal delegate void RecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
33 IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
34 LookupResultFlags flags, IntPtr userdata);
36 public enum RecordClass {
40 public enum RecordType {
53 public struct RecordInfo
55 public int NetworkInterface;
56 public Protocol Protocol;
58 public RecordClass Class;
59 public RecordType Type;
61 public LookupResultFlags Flags;
64 public class RecordInfoArgs : EventArgs
66 private RecordInfo record;
68 public RecordInfo Record {
69 get { return record; }
72 public RecordInfoArgs (RecordInfo record)
78 public class RecordBrowser : BrowserBase, IDisposable
80 private IntPtr handle;
81 private ArrayList infos = new ArrayList ();
82 private Client client;
84 private Protocol proto;
86 private RecordClass clazz;
87 private RecordType type;
88 private LookupFlags flags;
89 private RecordBrowserCallback cb;
91 private ArrayList addListeners = new ArrayList ();
92 private ArrayList removeListeners = new ArrayList ();
94 [DllImport ("avahi-client")]
95 private static extern IntPtr avahi_record_browser_new (IntPtr client, int iface, Protocol proto,
96 IntPtr name, ushort clazz, ushort type,
97 LookupFlags flags, RecordBrowserCallback cb,
101 [DllImport ("avahi-client")]
102 private static extern void avahi_record_browser_free (IntPtr handle);
104 public event RecordInfoHandler RecordAdded
107 addListeners.Add (value);
111 addListeners.Remove (value);
116 public event RecordInfoHandler RecordRemoved
119 removeListeners.Add (value);
123 removeListeners.Remove (value);
128 public RecordInfo[] Records
130 get { return (RecordInfo[]) infos.ToArray (typeof (RecordInfo)); }
133 public RecordBrowser (Client client, string name, RecordType type) :
134 this (client, -1, Protocol.Unspecified, name, RecordClass.In, type, LookupFlags.None)
138 public RecordBrowser (Client client, int iface, Protocol proto, string name, RecordClass clazz,
139 RecordType type, LookupFlags flags)
141 this.client = client;
148 cb = OnRecordBrowserCallback;
156 public void Dispose ()
161 private void Start ()
163 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
164 (addListeners.Count == 0 && removeListeners.Count == 0))
167 IntPtr namePtr = Utility.StringToPtr (name);
170 handle = avahi_record_browser_new (client.Handle, iface, proto, namePtr, (ushort) clazz, (ushort) type,
171 flags, cb, IntPtr.Zero);
174 Utility.Free (namePtr);
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);