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,
70 public enum LookupFlags {
78 public enum LookupResultFlags {
88 public enum ClientFlags {
94 public class Client : IDisposable
96 private IntPtr handle;
97 private ClientCallback cb;
98 private PollCallback pollcb;
101 private Thread thread;
103 [DllImport ("avahi-client")]
104 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
105 IntPtr userData, out int error);
107 [DllImport ("avahi-client")]
108 private static extern void avahi_client_free (IntPtr handle);
110 [DllImport ("avahi-client")]
111 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
113 [DllImport ("avahi-client")]
114 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
116 [DllImport ("avahi-client")]
117 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
119 [DllImport ("avahi-client")]
120 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
122 [DllImport ("avahi-client")]
123 private static extern ClientState avahi_client_get_state (IntPtr handle);
125 [DllImport ("avahi-client")]
126 private static extern int avahi_client_errno (IntPtr handle);
128 [DllImport ("avahi-common")]
129 private static extern IntPtr avahi_simple_poll_new ();
131 [DllImport ("avahi-common")]
132 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
134 [DllImport ("avahi-common")]
135 private static extern void avahi_simple_poll_free (IntPtr spoll);
137 [DllImport ("avahi-common")]
138 private static extern int avahi_simple_poll_loop (IntPtr spoll);
140 [DllImport ("avahi-common")]
141 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
143 [DllImport ("avahi-common")]
144 private static extern void avahi_simple_poll_quit (IntPtr spoll);
146 [DllImport ("avahi-client")]
147 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
151 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
153 public event ClientStateHandler StateChanged;
155 internal IntPtr Handle
157 get { return handle; }
160 public string Version
164 return Utility.PtrToString (avahi_client_get_version_string (handle));
169 public string HostName
173 return Utility.PtrToString (avahi_client_get_host_name (handle));
178 public string DomainName
182 return Utility.PtrToString (avahi_client_get_domain_name (handle));
187 public string HostNameFqdn
191 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
196 public ClientState State
200 return (ClientState) avahi_client_get_state (handle);
205 public uint LocalServiceCookie
209 return avahi_client_get_local_service_cookie (handle);
214 internal ErrorCode LastError
218 return (ErrorCode) avahi_client_errno (handle);
223 public Client (ClientFlags flags)
225 spoll = avahi_simple_poll_new ();
227 pollcb = OnPollCallback;
228 avahi_simple_poll_set_func (spoll, pollcb);
229 IntPtr poll = avahi_simple_poll_get (spoll);
230 cb = OnClientCallback;
233 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
235 throw new ClientException (error);
237 thread = new Thread (PollLoop);
238 thread.IsBackground = true;
242 public Client () : this (ClientFlags.None) {
250 public void Dispose ()
252 if (handle != IntPtr.Zero) {
253 avahi_client_free (handle);
254 avahi_simple_poll_quit (spoll);
255 avahi_simple_poll_free (spoll);
256 handle = IntPtr.Zero;
260 internal void ThrowError ()
262 ErrorCode error = LastError;
264 if (error != ErrorCode.Ok)
265 throw new ClientException (error);
268 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
270 if (StateChanged != null)
271 StateChanged (this, state);
274 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
276 int result = poll (ufds, nfds, timeout);
277 Monitor.Enter (this);
281 private void PollLoop () {
284 avahi_simple_poll_loop (spoll);
286 } catch (Exception e) {
287 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);