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 internal enum ServerState {
61 public enum ClientState {
62 Registering = ServerState.Registering,
63 Running = ServerState.Running,
64 Collision = ServerState.Collision,
69 public enum LookupFlags {
77 public enum LookupResultFlags {
86 public class Client : IDisposable
88 private IntPtr handle;
89 private ClientCallback cb;
90 private PollCallback pollcb;
93 private Thread thread;
95 [DllImport ("avahi-client")]
96 private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
97 IntPtr userData, out int error);
99 [DllImport ("avahi-client")]
100 private static extern void avahi_client_free (IntPtr handle);
102 [DllImport ("avahi-client")]
103 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
105 [DllImport ("avahi-client")]
106 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
108 [DllImport ("avahi-client")]
109 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
111 [DllImport ("avahi-client")]
112 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
114 [DllImport ("avahi-client")]
115 private static extern ClientState avahi_client_get_state (IntPtr handle);
117 [DllImport ("avahi-client")]
118 private static extern int avahi_client_errno (IntPtr handle);
120 [DllImport ("avahi-common")]
121 private static extern IntPtr avahi_simple_poll_new ();
123 [DllImport ("avahi-common")]
124 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
126 [DllImport ("avahi-common")]
127 private static extern void avahi_simple_poll_free (IntPtr spoll);
129 [DllImport ("avahi-common")]
130 private static extern int avahi_simple_poll_loop (IntPtr spoll);
132 [DllImport ("avahi-common")]
133 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
135 [DllImport ("avahi-common")]
136 private static extern void avahi_simple_poll_quit (IntPtr spoll);
138 [DllImport ("avahi-client")]
139 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
143 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
145 public event ClientStateHandler StateChanged;
147 internal IntPtr Handle
149 get { return handle; }
152 public string Version
156 return Utility.PtrToString (avahi_client_get_version_string (handle));
161 public string HostName
165 return Utility.PtrToString (avahi_client_get_host_name (handle));
170 public string DomainName
174 return Utility.PtrToString (avahi_client_get_domain_name (handle));
179 public string HostNameFqdn
183 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
188 public ClientState State
192 return (ClientState) avahi_client_get_state (handle);
197 public uint LocalServiceCookie
201 return avahi_client_get_local_service_cookie (handle);
206 internal ErrorCode LastError
210 return (ErrorCode) avahi_client_errno (handle);
217 spoll = avahi_simple_poll_new ();
219 pollcb = OnPollCallback;
220 avahi_simple_poll_set_func (spoll, pollcb);
221 IntPtr poll = avahi_simple_poll_get (spoll);
222 cb = OnClientCallback;
225 handle = avahi_client_new (poll, cb, IntPtr.Zero, out error);
227 throw new ClientException (error);
229 thread = new Thread (PollLoop);
230 thread.IsBackground = true;
239 public void Dispose ()
241 if (handle != IntPtr.Zero) {
242 avahi_client_free (handle);
243 avahi_simple_poll_quit (spoll);
244 avahi_simple_poll_free (spoll);
245 handle = IntPtr.Zero;
249 internal void ThrowError ()
251 ErrorCode error = LastError;
253 if (error != ErrorCode.Ok)
254 throw new ClientException (error);
257 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
259 if (StateChanged != null)
260 StateChanged (this, state);
263 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
265 int result = poll (ufds, nfds, timeout);
266 Monitor.Enter (this);
270 private void PollLoop () {
273 avahi_simple_poll_loop (spoll);
275 } catch (Exception e) {
276 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);