2 using System.Collections;
4 using System.Runtime.InteropServices;
10 internal delegate void ServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
11 ResolverEvent revent, IntPtr name, IntPtr type,
12 IntPtr domain, IntPtr host, IntPtr address,
13 UInt16 port, IntPtr txt, IntPtr userdata);
15 public class ServiceResolver : IDisposable
17 private IntPtr handle;
18 private ServiceInfo currentInfo;
19 private Client client;
21 private Protocol proto;
24 private string domain;
25 private Protocol aproto;
27 private ArrayList foundListeners = new ArrayList ();
28 private ArrayList timeoutListeners = new ArrayList ();
30 [DllImport ("avahi-client")]
31 private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
32 IntPtr name, IntPtr type, IntPtr domain,
33 Protocol aproto, ServiceResolverCallback cb,
36 [DllImport ("avahi-client")]
37 private static extern void avahi_service_resolver_free (IntPtr handle);
39 public event ServiceInfoHandler Found
42 foundListeners.Add (value);
46 foundListeners.Remove (value);
51 public event EventHandler Timeout
54 timeoutListeners.Add (value);
58 timeoutListeners.Remove (value);
63 public ServiceInfo Service
65 get { return currentInfo; }
68 public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
75 public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
76 service.Protocol, service.Name,
77 service.ServiceType, service.Domain,
82 public ServiceResolver (Client client, int iface, Protocol proto, string name,
83 string type, string domain, Protocol aproto)
101 public void Dispose ()
106 private void Start ()
108 if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
111 IntPtr namePtr = Utility.StringToPtr (name);
112 IntPtr typePtr = Utility.StringToPtr (type);
113 IntPtr domainPtr = Utility.StringToPtr (domain);
114 handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
115 aproto, OnServiceResolverCallback, IntPtr.Zero);
116 Utility.Free (namePtr);
117 Utility.Free (typePtr);
118 Utility.Free (domainPtr);
121 private void Stop (bool force)
123 if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
124 avahi_service_resolver_free (handle);
125 handle = IntPtr.Zero;
129 private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
130 ResolverEvent revent, IntPtr name, IntPtr type,
131 IntPtr domain, IntPtr host, IntPtr address,
132 UInt16 port, IntPtr txt, IntPtr userdata)
135 info.NetworkInterface = iface;
136 info.Protocol = proto;
137 info.Domain = Utility.PtrToString (domain);
138 info.ServiceType = Utility.PtrToString (type);
139 info.Name = Utility.PtrToString (name);
140 info.Host = Utility.PtrToString (host);
141 info.Address = Utility.PtrToAddress (address);
145 if (revent == ResolverEvent.Found) {
148 foreach (ServiceInfoHandler handler in foundListeners)
149 handler (this, info);
151 currentInfo = ServiceInfo.Zero;
153 foreach (EventHandler handler in timeoutListeners)
154 handler (this, new EventArgs ());