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 {
94 public enum LookupResultFlags {
104 public enum ClientFlags {
106 IgnoreUserConfig = 1,
110 public class Client : IDisposable
112 private IntPtr handle;
113 private ClientCallback cb;
114 private PollCallback pollcb;
115 private IntPtr spoll;
117 private Thread thread;
119 [DllImport ("avahi-client")]
120 private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
121 IntPtr userData, out int error);
123 [DllImport ("avahi-client")]
124 private static extern void avahi_client_free (IntPtr handle);
126 [DllImport ("avahi-client")]
127 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
129 [DllImport ("avahi-client")]
130 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
132 [DllImport ("avahi-client")]
133 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
135 [DllImport ("avahi-client")]
136 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
138 [DllImport ("avahi-client")]
139 private static extern ClientState avahi_client_get_state (IntPtr handle);
141 [DllImport ("avahi-client")]
142 private static extern int avahi_client_errno (IntPtr handle);
144 [DllImport ("avahi-common")]
145 private static extern IntPtr avahi_simple_poll_new ();
147 [DllImport ("avahi-common")]
148 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
150 [DllImport ("avahi-common")]
151 private static extern void avahi_simple_poll_free (IntPtr spoll);
153 [DllImport ("avahi-common")]
154 private static extern int avahi_simple_poll_loop (IntPtr spoll);
156 [DllImport ("avahi-common")]
157 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
159 [DllImport ("avahi-common")]
160 private static extern void avahi_simple_poll_quit (IntPtr spoll);
162 [DllImport ("avahi-client")]
163 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
165 [DllImport ("avahi-common")]
166 private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type,
169 [DllImport ("avahi-common")]
170 private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len,
171 IntPtr type, int type_len,
172 IntPtr domain, int domain_len);
176 private static extern int poll(IntPtr ufds, uint nfds, int timeout);
178 public event ClientStateHandler StateChanged;
180 internal IntPtr Handle
182 get { return handle; }
185 public string Version
189 return Utility.PtrToString (avahi_client_get_version_string (handle));
194 public string HostName
198 return Utility.PtrToString (avahi_client_get_host_name (handle));
203 public string DomainName
207 return Utility.PtrToString (avahi_client_get_domain_name (handle));
212 public string HostNameFqdn
216 return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
221 public ClientState State
225 return (ClientState) avahi_client_get_state (handle);
230 public uint LocalServiceCookie
234 return avahi_client_get_local_service_cookie (handle);
239 internal ErrorCode LastError
243 return (ErrorCode) avahi_client_errno (handle);
248 public Client (ClientFlags flags)
250 spoll = avahi_simple_poll_new ();
252 pollcb = OnPollCallback;
253 avahi_simple_poll_set_func (spoll, pollcb);
254 IntPtr poll = avahi_simple_poll_get (spoll);
255 cb = OnClientCallback;
258 handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
260 throw new ClientException (error);
262 thread = new Thread (PollLoop);
263 thread.IsBackground = true;
267 public Client () : this (ClientFlags.None) {
275 public void Dispose ()
277 if (handle != IntPtr.Zero) {
278 avahi_client_free (handle);
279 avahi_simple_poll_quit (spoll);
280 avahi_simple_poll_free (spoll);
281 handle = IntPtr.Zero;
285 public static string JoinServiceName (string name, string type, string domain)
287 int len = 4 * (name.Length + type.Length + domain.Length) + 4;
288 IntPtr buf = Stdlib.malloc ((ulong) len);
290 int ret = avahi_service_name_join (buf, len,
291 Utility.StringToBytes (name),
292 Utility.StringToBytes (type),
293 Utility.StringToBytes (domain));
297 return null; // FIXME, should throw exception
300 string service = Utility.PtrToString (buf);
306 public static void SplitServiceName (string service, out string name, out string type, out string domain)
310 IntPtr namePtr = Stdlib.malloc ((ulong) len);
311 IntPtr typePtr = Stdlib.malloc ((ulong) len);
312 IntPtr domainPtr = Stdlib.malloc ((ulong) len);
314 int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len,
318 Utility.Free (namePtr);
319 Utility.Free (typePtr);
320 Utility.Free (domainPtr);
328 name = Utility.PtrToString (namePtr);
329 type = Utility.PtrToString (typePtr);
330 domain = Utility.PtrToString (domainPtr);
332 Utility.Free (namePtr);
333 Utility.Free (typePtr);
334 Utility.Free (domainPtr);
337 internal void ThrowError ()
339 ErrorCode error = LastError;
341 if (error != ErrorCode.Ok)
342 throw new ClientException (error);
345 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
347 if (StateChanged != null)
348 StateChanged (this, new ClientStateArgs (state));
351 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
353 int result = poll (ufds, nfds, timeout);
354 Monitor.Enter (this);
358 private void PollLoop () {
361 avahi_simple_poll_loop (spoll);
363 } catch (Exception e) {
364 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);