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
24 using System.Threading;
25 using System.Collections;
26 using System.Runtime.InteropServices;
30 internal enum ResolverEvent {
35 internal enum BrowserEvent {
40 internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
41 internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
43 public delegate void ClientStateHandler (object o, ClientState state);
45 public enum Protocol {
51 public enum ClientState {
60 public enum LookupFlags {
69 public enum LookupResultFlags {
76 public class Client : IDisposable
78 private IntPtr handle;
79 private ClientCallback cb;
80 private PollCallback pollcb;
83 private Thread thread;
85 [DllImport ("avahi-client")]
86 private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
87 IntPtr userData, out int error);
89 [DllImport ("avahi-client")]
90 private static extern void avahi_client_free (IntPtr handle);
92 [DllImport ("avahi-client")]
93 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
95 [DllImport ("avahi-client")]
96 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
98 [DllImport ("avahi-client")]
99 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
101 [DllImport ("avahi-client")]
102 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
104 [DllImport ("avahi-client")]
105 private static extern ClientState avahi_client_get_state (IntPtr handle);
107 [DllImport ("avahi-client")]
108 private static extern int avahi_client_errno (IntPtr handle);
110 [DllImport ("avahi-common")]
111 private static extern IntPtr avahi_simple_poll_new ();
113 [DllImport ("avahi-common")]
114 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
116 [DllImport ("avahi-common")]
117 private static extern void avahi_simple_poll_free (IntPtr spoll);
119 [DllImport ("avahi-common")]
120 private static extern int avahi_simple_poll_iterate (IntPtr spoll, int timeout);
122 [DllImport ("avahi-common")]
123 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
125 [DllImport ("avahi-common")]
126 private static extern void avahi_simple_poll_quit (IntPtr spoll);
128 [DllImport ("avahi-client")]
129 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
131 [DllImport ("avahi-client")]
132 private static extern int avahi_client_is_service_local (IntPtr client, int iface, Protocol proto,
133 IntPtr name, IntPtr type, IntPtr domain);
137 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
139 public event ClientStateHandler StateChanged;
141 internal IntPtr Handle
143 get { return handle; }
146 public string Version
150 return Utility.PtrToString (avahi_client_get_version_string (handle));
155 public string HostName
159 return Utility.PtrToString (avahi_client_get_host_name (handle));
164 public string DomainName
168 return Utility.PtrToString (avahi_client_get_domain_name (handle));
173 public string HostNameFqdn
177 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
182 public ClientState State
186 return (ClientState) avahi_client_get_state (handle);
191 public uint LocalServiceCookie
195 return avahi_client_get_local_service_cookie (handle);
200 internal int LastError
204 return avahi_client_errno (handle);
211 spoll = avahi_simple_poll_new ();
213 pollcb = OnPollCallback;
214 avahi_simple_poll_set_func (spoll, pollcb);
215 IntPtr poll = avahi_simple_poll_get (spoll);
216 cb = OnClientCallback;
219 handle = avahi_client_new (poll, cb, IntPtr.Zero, out error);
221 throw new ClientException (error);
223 thread = new Thread (PollLoop);
224 thread.IsBackground = true;
233 public void Dispose ()
236 if (handle != IntPtr.Zero) {
239 avahi_client_free (handle);
240 avahi_simple_poll_quit (spoll);
241 avahi_simple_poll_free (spoll);
242 handle = IntPtr.Zero;
247 public bool IsServiceLocal (ServiceInfo service)
249 return IsServiceLocal (service.NetworkInterface, service.Protocol, service.Name,
250 service.ServiceType, service.Domain);
253 public bool IsServiceLocal (int iface, Protocol proto, string name, string type, string domain)
255 IntPtr namePtr = Utility.StringToPtr (name);
256 IntPtr typePtr = Utility.StringToPtr (type);
257 IntPtr domainPtr = Utility.StringToPtr (domain);
259 int result = avahi_client_is_service_local (handle, iface, proto, namePtr, typePtr, domainPtr);
261 Utility.Free (namePtr);
262 Utility.Free (typePtr);
263 Utility.Free (domainPtr);
268 internal void CheckError ()
270 int error = LastError;
273 throw new ClientException (error);
276 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
278 if (StateChanged != null)
279 StateChanged (this, state);
282 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
284 int result = poll (ufds, nfds, timeout);
285 Monitor.Enter (this);
289 private void PollLoop () {
293 if (avahi_simple_poll_iterate (spoll, -1) != 0)
297 } catch (ThreadAbortException e) {
298 } catch (Exception e) {
299 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);