2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 using System.Collections;
23 using System.Runtime.InteropServices;
29 public delegate void RecordInfoHandler (object o, RecordInfoArgs args);
31 internal delegate void RecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
32 IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
33 LookupResultFlags flags, IntPtr userdata);
35 public enum RecordClass {
39 public enum RecordType {
52 public struct RecordInfo
54 public int NetworkInterface;
55 public Protocol Protocol;
57 public RecordClass Class;
58 public RecordType Type;
60 public LookupResultFlags Flags;
63 public class RecordInfoArgs : EventArgs
65 private RecordInfo record;
67 public RecordInfo Record {
68 get { return record; }
71 public RecordInfoArgs (RecordInfo record)
77 public class RecordBrowser : BrowserBase, IDisposable
79 private IntPtr handle;
80 private ArrayList infos = new ArrayList ();
81 private Client client;
83 private Protocol proto;
85 private RecordClass clazz;
86 private RecordType type;
87 private LookupFlags flags;
88 private RecordBrowserCallback cb;
90 private ArrayList addListeners = new ArrayList ();
91 private ArrayList removeListeners = new ArrayList ();
93 [DllImport ("avahi-client")]
94 private static extern IntPtr avahi_record_browser_new (IntPtr client, int iface, Protocol proto,
95 byte[] name, ushort clazz, ushort type,
96 LookupFlags flags, RecordBrowserCallback cb,
100 [DllImport ("avahi-client")]
101 private static extern void avahi_record_browser_free (IntPtr handle);
103 public event RecordInfoHandler RecordAdded
106 addListeners.Add (value);
110 addListeners.Remove (value);
115 public event RecordInfoHandler RecordRemoved
118 removeListeners.Add (value);
122 removeListeners.Remove (value);
127 public RecordInfo[] Records
129 get { return (RecordInfo[]) infos.ToArray (typeof (RecordInfo)); }
132 public RecordBrowser (Client client, string name, RecordType type) :
133 this (client, -1, Protocol.Unspecified, name, RecordClass.In, type, LookupFlags.None)
137 public RecordBrowser (Client client, int iface, Protocol proto, string name, RecordClass clazz,
138 RecordType type, LookupFlags flags)
140 this.client = client;
147 cb = OnRecordBrowserCallback;
155 public void Dispose ()
160 private void Start ()
162 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
163 (addListeners.Count == 0 && removeListeners.Count == 0))
167 handle = avahi_record_browser_new (client.Handle, iface, proto, Utility.StringToBytes (name),
168 (ushort) clazz, (ushort) type, flags, cb, IntPtr.Zero);
170 if (handle == IntPtr.Zero)
171 client.ThrowError ();
175 private void Stop (bool force)
177 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
178 (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
181 avahi_record_browser_free (handle);
182 handle = IntPtr.Zero;
187 private void OnRecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
188 IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
189 LookupResultFlags flags, IntPtr userdata)
192 info.NetworkInterface = iface;
193 info.Protocol = proto;
194 info.Name = Utility.PtrToString (name);
195 info.Class = (RecordClass) clazz;
196 info.Type = (RecordType) type;
198 info.Data = new byte[size];
200 if (rdata != IntPtr.Zero) {
201 Marshal.Copy (rdata, info.Data, 0, size);
205 case BrowserEvent.Added:
208 foreach (RecordInfoHandler handler in addListeners)
209 handler (this, new RecordInfoArgs (info));
212 case BrowserEvent.Removed:
215 foreach (RecordInfoHandler handler in removeListeners)
216 handler (this, new RecordInfoArgs (info));
220 EmitBrowserEvent (bevent);