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, ClientStateArgs state);
48 public class ClientStateArgs : EventArgs
50 private ClientState state;
52 public ClientState State
57 public ClientStateArgs (ClientState state)
63 public enum Protocol {
69 internal enum ServerState {
76 public enum ClientState {
77 Registering = ServerState.Registering,
78 Running = ServerState.Running,
79 Collision = ServerState.Collision,
85 public enum LookupFlags {
93 public enum LookupResultFlags {
103 public enum ClientFlags {
105 IgnoreUserConfig = 1,
109 public class Client : IDisposable
111 private IntPtr handle;
112 private ClientCallback cb;
113 private PollCallback pollcb;
114 private IntPtr spoll;
116 private Thread thread;
118 [DllImport ("avahi-client")]
119 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
120 IntPtr userData, out int error);
122 [DllImport ("avahi-client")]
123 private static extern void avahi_client_free (IntPtr handle);
125 [DllImport ("avahi-client")]
126 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
128 [DllImport ("avahi-client")]
129 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
131 [DllImport ("avahi-client")]
132 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
134 [DllImport ("avahi-client")]
135 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
137 [DllImport ("avahi-client")]
138 private static extern ClientState avahi_client_get_state (IntPtr handle);
140 [DllImport ("avahi-client")]
141 private static extern int avahi_client_errno (IntPtr handle);
143 [DllImport ("avahi-common")]
144 private static extern IntPtr avahi_simple_poll_new ();
146 [DllImport ("avahi-common")]
147 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
149 [DllImport ("avahi-common")]
150 private static extern void avahi_simple_poll_free (IntPtr spoll);
152 [DllImport ("avahi-common")]
153 private static extern int avahi_simple_poll_loop (IntPtr spoll);
155 [DllImport ("avahi-common")]
156 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
158 [DllImport ("avahi-common")]
159 private static extern void avahi_simple_poll_quit (IntPtr spoll);
161 [DllImport ("avahi-client")]
162 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
166 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
168 public event ClientStateHandler StateChanged;
170 internal IntPtr Handle
172 get { return handle; }
175 public string Version
179 return Utility.PtrToString (avahi_client_get_version_string (handle));
184 public string HostName
188 return Utility.PtrToString (avahi_client_get_host_name (handle));
193 public string DomainName
197 return Utility.PtrToString (avahi_client_get_domain_name (handle));
202 public string HostNameFqdn
206 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
211 public ClientState State
215 return (ClientState) avahi_client_get_state (handle);
220 public uint LocalServiceCookie
224 return avahi_client_get_local_service_cookie (handle);
229 internal ErrorCode LastError
233 return (ErrorCode) avahi_client_errno (handle);
238 public Client (ClientFlags flags)
240 spoll = avahi_simple_poll_new ();
242 pollcb = OnPollCallback;
243 avahi_simple_poll_set_func (spoll, pollcb);
244 IntPtr poll = avahi_simple_poll_get (spoll);
245 cb = OnClientCallback;
248 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
250 throw new ClientException (error);
252 thread = new Thread (PollLoop);
253 thread.IsBackground = true;
257 public Client () : this (ClientFlags.None) {
265 public void Dispose ()
267 if (handle != IntPtr.Zero) {
268 avahi_client_free (handle);
269 avahi_simple_poll_quit (spoll);
270 avahi_simple_poll_free (spoll);
271 handle = IntPtr.Zero;
275 internal void ThrowError ()
277 ErrorCode error = LastError;
279 if (error != ErrorCode.Ok)
280 throw new ClientException (error);
283 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
285 if (StateChanged != null)
286 StateChanged (this, new ClientStateArgs (state));
289 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
291 int result = poll (ufds, nfds, timeout);
292 Monitor.Enter (this);
296 private void PollLoop () {
299 avahi_simple_poll_loop (spoll);
301 } catch (Exception e) {
302 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);