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, IntPtr userdata);
36 public class ServiceResolver : IDisposable
38 private IntPtr handle;
39 private ServiceInfo currentInfo;
40 private Client client;
42 private Protocol proto;
45 private string domain;
46 private Protocol aproto;
47 private ServiceResolverCallback cb;
49 private ArrayList foundListeners = new ArrayList ();
50 private ArrayList timeoutListeners = new ArrayList ();
52 [DllImport ("avahi-client")]
53 private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
54 IntPtr name, IntPtr type, IntPtr domain,
55 Protocol aproto, ServiceResolverCallback cb,
58 [DllImport ("avahi-common")]
59 private static extern IntPtr avahi_string_list_get_next (IntPtr list);
61 [DllImport ("avahi-common")]
62 private static extern IntPtr avahi_string_list_get_text (IntPtr list);
64 [DllImport ("avahi-common")]
65 private static extern int avahi_string_list_get_size (IntPtr list);
67 [DllImport ("avahi-client")]
68 private static extern void avahi_service_resolver_free (IntPtr handle);
70 public event ServiceInfoHandler Found
73 foundListeners.Add (value);
77 foundListeners.Remove (value);
82 public event EventHandler Timeout
85 timeoutListeners.Add (value);
89 timeoutListeners.Remove (value);
94 public ServiceInfo Service
96 get { return currentInfo; }
99 public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
100 Protocol.Unspecified,
102 Protocol.Unspecified)
106 public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
107 service.Protocol, service.Name,
108 service.ServiceType, service.Domain,
109 Protocol.Unspecified)
113 public ServiceResolver (Client client, int iface, Protocol proto, string name,
114 string type, string domain, Protocol aproto)
116 this.client = client;
121 this.domain = domain;
122 this.aproto = aproto;
123 cb = OnServiceResolverCallback;
131 public void Dispose ()
136 private void Start ()
138 if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
139 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
142 IntPtr namePtr = Utility.StringToPtr (name);
143 IntPtr typePtr = Utility.StringToPtr (type);
144 IntPtr domainPtr = Utility.StringToPtr (domain);
147 handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
148 aproto, cb, IntPtr.Zero);
151 Utility.Free (namePtr);
152 Utility.Free (typePtr);
153 Utility.Free (domainPtr);
156 private void Stop (bool force)
158 if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
159 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
162 avahi_service_resolver_free (handle);
163 handle = IntPtr.Zero;
168 private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
169 ResolverEvent revent, IntPtr name, IntPtr type,
170 IntPtr domain, IntPtr host, IntPtr address,
171 UInt16 port, IntPtr txt, IntPtr userdata)
174 info.NetworkInterface = iface;
175 info.Protocol = proto;
176 info.Domain = Utility.PtrToString (domain);
177 info.ServiceType = Utility.PtrToString (type);
178 info.Name = Utility.PtrToString (name);
179 info.HostName = Utility.PtrToString (host);
180 info.Address = Utility.PtrToAddress (address);
183 ArrayList txtlist = new ArrayList ();
184 for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
185 IntPtr buf = avahi_string_list_get_text (l);
186 int len = avahi_string_list_get_size (l);
188 byte[] txtbuf = new byte[len];
189 Marshal.Copy (buf, txtbuf, 0, len);
190 txtlist.Add (txtbuf);
193 info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
195 if (revent == ResolverEvent.Found) {
198 foreach (ServiceInfoHandler handler in foundListeners)
199 handler (this, info);
201 currentInfo = ServiceInfo.Zero;
203 foreach (EventHandler handler in timeoutListeners)
204 handler (this, new EventArgs ());