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 {
43 internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
44 internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
46 public delegate void ClientStateHandler (object o, ClientState state);
48 public enum Protocol {
54 public enum ClientState {
63 public enum LookupFlags {
71 public enum LookupResultFlags {
80 public class Client : IDisposable
82 private IntPtr handle;
83 private ClientCallback cb;
84 private PollCallback pollcb;
87 private Thread thread;
89 [DllImport ("avahi-client")]
90 private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
91 IntPtr userData, out int error);
93 [DllImport ("avahi-client")]
94 private static extern void avahi_client_free (IntPtr handle);
96 [DllImport ("avahi-client")]
97 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
99 [DllImport ("avahi-client")]
100 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
102 [DllImport ("avahi-client")]
103 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
105 [DllImport ("avahi-client")]
106 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
108 [DllImport ("avahi-client")]
109 private static extern ClientState avahi_client_get_state (IntPtr handle);
111 [DllImport ("avahi-client")]
112 private static extern int avahi_client_errno (IntPtr handle);
114 [DllImport ("avahi-common")]
115 private static extern IntPtr avahi_simple_poll_new ();
117 [DllImport ("avahi-common")]
118 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
120 [DllImport ("avahi-common")]
121 private static extern void avahi_simple_poll_free (IntPtr spoll);
123 [DllImport ("avahi-common")]
124 private static extern int avahi_simple_poll_loop (IntPtr spoll);
126 [DllImport ("avahi-common")]
127 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
129 [DllImport ("avahi-common")]
130 private static extern void avahi_simple_poll_quit (IntPtr spoll);
132 [DllImport ("avahi-client")]
133 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
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 ErrorCode LastError
204 return (ErrorCode) 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 ()
235 if (handle != IntPtr.Zero) {
236 avahi_client_free (handle);
237 avahi_simple_poll_quit (spoll);
238 avahi_simple_poll_free (spoll);
239 handle = IntPtr.Zero;
243 internal void ThrowError ()
245 ErrorCode error = LastError;
247 if (error != ErrorCode.Ok)
248 throw new ClientException (error);
251 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
253 if (StateChanged != null)
254 StateChanged (this, state);
257 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
259 int result = poll (ufds, nfds, timeout);
260 Monitor.Enter (this);
264 private void PollLoop () {
267 avahi_simple_poll_loop (spoll);
269 } catch (Exception e) {
270 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);