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;
25 using System.Runtime.InteropServices;
31 internal delegate void ServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
32 ResolverEvent revent, IntPtr name, IntPtr type,
33 IntPtr domain, IntPtr host, IntPtr address,
34 UInt16 port, IntPtr txt, LookupResultFlags flags,
37 public class ServiceResolver : IDisposable
39 private IntPtr handle;
40 private ServiceInfo currentInfo;
41 private Client client;
43 private Protocol proto;
46 private string domain;
47 private Protocol aproto;
48 private LookupFlags flags;
49 private ServiceResolverCallback cb;
51 private ArrayList foundListeners = new ArrayList ();
52 private ArrayList timeoutListeners = new ArrayList ();
54 [DllImport ("avahi-client")]
55 private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
56 IntPtr name, IntPtr type, IntPtr domain,
57 Protocol aproto, LookupFlags flags,
58 ServiceResolverCallback cb,
61 [DllImport ("avahi-common")]
62 private static extern IntPtr avahi_string_list_get_next (IntPtr list);
64 [DllImport ("avahi-common")]
65 private static extern IntPtr avahi_string_list_get_text (IntPtr list);
67 [DllImport ("avahi-common")]
68 private static extern int avahi_string_list_get_size (IntPtr list);
70 [DllImport ("avahi-client")]
71 private static extern void avahi_service_resolver_free (IntPtr handle);
73 public event ServiceInfoHandler Found
76 foundListeners.Add (value);
80 foundListeners.Remove (value);
85 public event EventHandler Timeout
88 timeoutListeners.Add (value);
92 timeoutListeners.Remove (value);
97 public ServiceInfo Service
99 get { return currentInfo; }
102 public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
103 Protocol.Unspecified,
105 Protocol.Unspecified)
109 public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
110 service.Protocol, service.Name,
111 service.ServiceType, service.Domain,
112 Protocol.Unspecified)
116 public ServiceResolver (Client client, int iface, Protocol proto, string name,
117 string type, string domain, Protocol aproto)
119 this.client = client;
124 this.domain = domain;
125 this.aproto = aproto;
126 cb = OnServiceResolverCallback;
134 public void Dispose ()
139 private void Start ()
141 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
142 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
145 IntPtr namePtr = Utility.StringToPtr (name);
146 IntPtr typePtr = Utility.StringToPtr (type);
147 IntPtr domainPtr = Utility.StringToPtr (domain);
150 handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
151 aproto, flags, cb, IntPtr.Zero);
154 Utility.Free (namePtr);
155 Utility.Free (typePtr);
156 Utility.Free (domainPtr);
159 private void Stop (bool force)
161 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
162 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
165 avahi_service_resolver_free (handle);
166 handle = IntPtr.Zero;
171 private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
172 ResolverEvent revent, IntPtr name, IntPtr type,
173 IntPtr domain, IntPtr host, IntPtr address,
174 UInt16 port, IntPtr txt, LookupResultFlags flags,
178 info.NetworkInterface = iface;
179 info.Protocol = proto;
180 info.Domain = Utility.PtrToString (domain);
181 info.ServiceType = Utility.PtrToString (type);
182 info.Name = Utility.PtrToString (name);
183 info.HostName = Utility.PtrToString (host);
184 info.Address = Utility.PtrToAddress (address);
187 ArrayList txtlist = new ArrayList ();
188 for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
189 IntPtr buf = avahi_string_list_get_text (l);
190 int len = avahi_string_list_get_size (l);
192 byte[] txtbuf = new byte[len];
193 Marshal.Copy (buf, txtbuf, 0, len);
194 txtlist.Add (txtbuf);
197 info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
200 if (revent == ResolverEvent.Found) {
203 foreach (ServiceInfoHandler handler in foundListeners)
204 handler (this, info);
206 currentInfo = ServiceInfo.Zero;
208 foreach (EventHandler handler in timeoutListeners)
209 handler (this, new EventArgs ());