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;
31 internal enum ResolverEvent {
36 internal enum BrowserEvent {
44 internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
45 internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
47 public delegate void ClientStateHandler (object o, ClientStateArgs state);
49 public class ClientStateArgs : EventArgs
51 private ClientState state;
53 public ClientState State
58 public ClientStateArgs (ClientState state)
64 public enum Protocol {
70 internal enum ServerState {
77 public enum ClientState {
78 Registering = ServerState.Registering,
79 Running = ServerState.Running,
80 Collision = ServerState.Collision,
86 public enum LookupFlags {
95 public enum LookupResultFlags {
105 public enum ClientFlags {
107 IgnoreUserConfig = 1,
111 public class Client : IDisposable
113 private IntPtr handle;
114 private ClientCallback cb;
115 private PollCallback pollcb;
116 private IntPtr spoll;
118 private Thread thread;
120 [DllImport ("avahi-client")]
121 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
122 IntPtr userData, out int error);
124 [DllImport ("avahi-client")]
125 private static extern void avahi_client_free (IntPtr handle);
127 [DllImport ("avahi-client")]
128 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
130 [DllImport ("avahi-client")]
131 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
133 [DllImport ("avahi-client")]
134 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
136 [DllImport ("avahi-client")]
137 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
139 [DllImport ("avahi-client")]
140 private static extern ClientState avahi_client_get_state (IntPtr handle);
142 [DllImport ("avahi-client")]
143 private static extern int avahi_client_errno (IntPtr handle);
145 [DllImport ("avahi-common")]
146 private static extern IntPtr avahi_simple_poll_new ();
148 [DllImport ("avahi-common")]
149 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
151 [DllImport ("avahi-common")]
152 private static extern void avahi_simple_poll_free (IntPtr spoll);
154 [DllImport ("avahi-common")]
155 private static extern int avahi_simple_poll_loop (IntPtr spoll);
157 [DllImport ("avahi-common")]
158 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
160 [DllImport ("avahi-common")]
161 private static extern void avahi_simple_poll_quit (IntPtr spoll);
163 [DllImport ("avahi-client")]
164 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
166 [DllImport ("avahi-common")]
167 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
170 [DllImport ("avahi-common")]
171 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
172 IntPtr type, int type_len,
173 IntPtr domain, int domain_len);
177 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
179 public event ClientStateHandler StateChanged;
181 internal IntPtr Handle
183 get { return handle; }
186 public string Version
190 return Utility.PtrToString (avahi_client_get_version_string (handle));
195 public string HostName
199 return Utility.PtrToString (avahi_client_get_host_name (handle));
204 public string DomainName
208 return Utility.PtrToString (avahi_client_get_domain_name (handle));
213 public string HostNameFqdn
217 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
222 public ClientState State
226 return (ClientState) avahi_client_get_state (handle);
231 public uint LocalServiceCookie
235 return avahi_client_get_local_service_cookie (handle);
240 internal ErrorCode LastError
244 return (ErrorCode) avahi_client_errno (handle);
249 public Client (ClientFlags flags)
251 spoll = avahi_simple_poll_new ();
253 pollcb = OnPollCallback;
254 avahi_simple_poll_set_func (spoll, pollcb);
255 IntPtr poll = avahi_simple_poll_get (spoll);
256 cb = OnClientCallback;
259 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
261 throw new ClientException (error);
263 thread = new Thread (PollLoop);
264 thread.IsBackground = true;
268 public Client () : this (ClientFlags.None) {
276 public void Dispose ()
278 if (handle != IntPtr.Zero) {
279 avahi_client_free (handle);
280 avahi_simple_poll_quit (spoll);
281 avahi_simple_poll_free (spoll);
282 handle = IntPtr.Zero;
286 public static string JoinServiceName (string name, string type, string domain)
288 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
289 IntPtr buf = Stdlib.malloc ((ulong) len);
291 int ret = avahi_service_name_join (buf, len,
292 Utility.StringToBytes (name),
293 Utility.StringToBytes (type),
294 Utility.StringToBytes (domain));
298 return null; // FIXME, should throw exception
301 string service = Utility.PtrToString (buf);
307 public static void SplitServiceName (string service, out string name, out string type, out string domain)
311 IntPtr namePtr = Stdlib.malloc ((ulong) len);
312 IntPtr typePtr = Stdlib.malloc ((ulong) len);
313 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
315 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
319 Utility.Free (namePtr);
320 Utility.Free (typePtr);
321 Utility.Free (domainPtr);
329 name = Utility.PtrToString (namePtr);
330 type = Utility.PtrToString (typePtr);
331 domain = Utility.PtrToString (domainPtr);
333 Utility.Free (namePtr);
334 Utility.Free (typePtr);
335 Utility.Free (domainPtr);
338 internal void ThrowError ()
340 ErrorCode error = LastError;
342 if (error != ErrorCode.Ok)
343 throw new ClientException (error);
346 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
348 if (StateChanged != null)
349 StateChanged (this, new ClientStateArgs (state));
352 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
354 int result = poll (ufds, nfds, timeout);
355 Monitor.Enter (this);
359 private void PollLoop () {
362 avahi_simple_poll_loop (spoll);
364 } catch (Exception e) {
365 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);