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 {
59 public class Client : IDisposable
61 private IntPtr handle;
62 private ClientCallback cb;
63 private PollCallback pollcb;
66 private Thread thread;
68 [DllImport ("avahi-client")]
69 private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
70 IntPtr userData, out int error);
72 [DllImport ("avahi-client")]
73 private static extern void avahi_client_free (IntPtr handle);
75 [DllImport ("avahi-client")]
76 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
78 [DllImport ("avahi-client")]
79 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
81 [DllImport ("avahi-client")]
82 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
84 [DllImport ("avahi-client")]
85 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
87 [DllImport ("avahi-client")]
88 private static extern ClientState avahi_client_get_state (IntPtr handle);
90 [DllImport ("avahi-client")]
91 private static extern int avahi_client_errno (IntPtr handle);
93 [DllImport ("avahi-common")]
94 private static extern IntPtr avahi_simple_poll_new ();
96 [DllImport ("avahi-common")]
97 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
99 [DllImport ("avahi-common")]
100 private static extern void avahi_simple_poll_free (IntPtr spoll);
102 [DllImport ("avahi-common")]
103 private static extern int avahi_simple_poll_iterate (IntPtr spoll, int timeout);
105 [DllImport ("avahi-common")]
106 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
108 [DllImport ("avahi-common")]
109 private static extern void avahi_simple_poll_quit (IntPtr spoll);
112 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
114 public event ClientStateHandler StateChanged;
116 internal IntPtr Handle
118 get { return handle; }
121 public string Version
125 return Utility.PtrToString (avahi_client_get_version_string (handle));
130 public string HostName
134 return Utility.PtrToString (avahi_client_get_host_name (handle));
139 public string DomainName
143 return Utility.PtrToString (avahi_client_get_domain_name (handle));
148 public string HostNameFqdn
152 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
157 public ClientState State
161 return (ClientState) avahi_client_get_state (handle);
166 internal int LastError
170 return avahi_client_errno (handle);
177 spoll = avahi_simple_poll_new ();
179 pollcb = OnPollCallback;
180 avahi_simple_poll_set_func (spoll, pollcb);
181 IntPtr poll = avahi_simple_poll_get (spoll);
182 cb = OnClientCallback;
185 handle = avahi_client_new (poll, cb, IntPtr.Zero, out error);
187 throw new ClientException (error);
189 thread = new Thread (PollLoop);
190 thread.IsBackground = true;
199 public void Dispose ()
202 if (handle != IntPtr.Zero) {
205 avahi_client_free (handle);
206 avahi_simple_poll_quit (spoll);
207 avahi_simple_poll_free (spoll);
208 handle = IntPtr.Zero;
213 internal void CheckError ()
215 int error = LastError;
218 throw new ClientException (error);
221 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
223 if (StateChanged != null)
224 StateChanged (this, state);
227 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
229 int result = poll (ufds, nfds, timeout);
230 Monitor.Enter (this);
234 private void PollLoop () {
238 if (avahi_simple_poll_iterate (spoll, -1) != 0)
242 } catch (ThreadAbortException e) {
243 } catch (Exception e) {
244 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);