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;
48 private ArrayList foundListeners = new ArrayList ();
49 private ArrayList timeoutListeners = new ArrayList ();
51 [DllImport ("avahi-client")]
52 private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
53 IntPtr name, IntPtr type, IntPtr domain,
54 Protocol aproto, ServiceResolverCallback cb,
57 [DllImport ("avahi-client")]
58 private static extern void avahi_service_resolver_free (IntPtr handle);
60 public event ServiceInfoHandler Found
63 foundListeners.Add (value);
67 foundListeners.Remove (value);
72 public event EventHandler Timeout
75 timeoutListeners.Add (value);
79 timeoutListeners.Remove (value);
84 public ServiceInfo Service
86 get { return currentInfo; }
89 public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
96 public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
97 service.Protocol, service.Name,
98 service.ServiceType, service.Domain,
103 public ServiceResolver (Client client, int iface, Protocol proto, string name,
104 string type, string domain, Protocol aproto)
106 this.client = client;
111 this.domain = domain;
112 this.aproto = aproto;
122 public void Dispose ()
127 private void Start ()
129 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
132 IntPtr namePtr = Utility.StringToPtr (name);
133 IntPtr typePtr = Utility.StringToPtr (type);
134 IntPtr domainPtr = Utility.StringToPtr (domain);
135 handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
136 aproto, OnServiceResolverCallback, IntPtr.Zero);
137 Utility.Free (namePtr);
138 Utility.Free (typePtr);
139 Utility.Free (domainPtr);
142 private void Stop (bool force)
144 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
145 avahi_service_resolver_free (handle);
146 handle = IntPtr.Zero;
150 private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
151 ResolverEvent revent, IntPtr name, IntPtr type,
152 IntPtr domain, IntPtr host, IntPtr address,
153 UInt16 port, IntPtr txt, IntPtr userdata)
156 info.NetworkInterface = iface;
157 info.Protocol = proto;
158 info.Domain = Utility.PtrToString (domain);
159 info.ServiceType = Utility.PtrToString (type);
160 info.Name = Utility.PtrToString (name);
161 info.Host = Utility.PtrToString (host);
162 info.Address = Utility.PtrToAddress (address);
166 if (revent == ResolverEvent.Found) {
169 foreach (ServiceInfoHandler handler in foundListeners)
170 handler (this, info);
172 currentInfo = ServiceInfo.Zero;
174 foreach (EventHandler handler in timeoutListeners)
175 handler (this, new EventArgs ());