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 (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
141 IntPtr namePtr = Utility.StringToPtr (name);
142 IntPtr typePtr = Utility.StringToPtr (type);
143 IntPtr domainPtr = Utility.StringToPtr (domain);
144 handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
145 aproto, cb, IntPtr.Zero);
146 Utility.Free (namePtr);
147 Utility.Free (typePtr);
148 Utility.Free (domainPtr);
151 private void Stop (bool force)
153 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
154 avahi_service_resolver_free (handle);
155 handle = IntPtr.Zero;
159 private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
160 ResolverEvent revent, IntPtr name, IntPtr type,
161 IntPtr domain, IntPtr host, IntPtr address,
162 UInt16 port, IntPtr txt, IntPtr userdata)
165 info.NetworkInterface = iface;
166 info.Protocol = proto;
167 info.Domain = Utility.PtrToString (domain);
168 info.ServiceType = Utility.PtrToString (type);
169 info.Name = Utility.PtrToString (name);
170 info.HostName = Utility.PtrToString (host);
171 info.Address = Utility.PtrToAddress (address);
174 ArrayList txtlist = new ArrayList ();
175 for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
176 IntPtr buf = avahi_string_list_get_text (l);
177 int len = avahi_string_list_get_size (l);
179 byte[] txtbuf = new byte[len];
180 Marshal.Copy (buf, txtbuf, 0, len);
181 txtlist.Add (txtbuf);
184 info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
186 if (revent == ResolverEvent.Found) {
189 foreach (ServiceInfoHandler handler in foundListeners)
190 handler (this, info);
192 currentInfo = ServiceInfo.Zero;
194 foreach (EventHandler handler in timeoutListeners)
195 handler (this, new EventArgs ());