3 using System.Collections;
4 using System.Runtime.InteropServices;
8 internal delegate void ServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
9 IntPtr name, IntPtr type, IntPtr domain, IntPtr userdata);
11 public struct ServiceInfo
13 public int NetworkInterface;
14 public Protocol Protocol;
16 public string ServiceType;
20 public IPAddress Address;
24 public static ServiceInfo Zero = new ServiceInfo ();
27 public delegate void ServiceInfoHandler (object o, ServiceInfo info);
29 public class ServiceBrowser : IDisposable
31 private IntPtr handle;
32 private ArrayList infos = new ArrayList ();
33 private Client client;
35 private Protocol proto;
36 private string domain;
39 private ArrayList addListeners = new ArrayList ();
40 private ArrayList removeListeners = new ArrayList ();
42 [DllImport ("avahi-client")]
43 private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type,
44 IntPtr domain, ServiceBrowserCallback cb,
47 [DllImport ("avahi-client")]
48 private static extern void avahi_service_browser_free (IntPtr handle);
50 public event ServiceInfoHandler ServiceAdded
53 addListeners.Add (value);
57 addListeners.Remove (value);
62 public event ServiceInfoHandler ServiceRemoved
65 removeListeners.Add (value);
69 removeListeners.Remove (value);
74 public ServiceInfo[] Services
76 get { return (ServiceInfo[]) infos.ToArray (typeof (ServiceInfo)); }
79 public ServiceBrowser (Client client, string type) : this (client, type, client.DomainName)
83 public ServiceBrowser (Client client, string type, string domain) : this (client, -1, Protocol.Unspecified,
88 public ServiceBrowser (Client client, int iface, Protocol proto, string type, string domain)
104 public void Dispose ()
109 private void Start ()
111 if (handle != IntPtr.Zero || (addListeners.Count == 0 && removeListeners.Count == 0))
114 IntPtr domainPtr = Utility.StringToPtr (domain);
115 IntPtr typePtr = Utility.StringToPtr (type);
116 handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr,
117 OnServiceBrowserCallback, IntPtr.Zero);
118 Utility.Free (domainPtr);
119 Utility.Free (typePtr);
122 private void Stop (bool force)
124 if (handle != IntPtr.Zero && (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
125 avahi_service_browser_free (handle);
126 handle = IntPtr.Zero;
130 private void OnServiceBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
131 IntPtr name, IntPtr type, IntPtr domain, 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);
147 if (bevent == BrowserEvent.Added) {
150 foreach (ServiceInfoHandler handler in addListeners)
151 handler (this, info);
155 foreach (ServiceInfoHandler handler in removeListeners)
156 handler (this, info);