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 {
37 internal enum BrowserEvent {
46 internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
47 internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
49 public delegate void ClientStateHandler (object o, ClientState state);
51 public enum Protocol {
57 public enum ClientState {
66 public enum LookupFlags {
75 public enum LookupResultFlags {
82 public class Client : IDisposable
84 private IntPtr handle;
85 private ClientCallback cb;
86 private PollCallback pollcb;
89 private Thread thread;
91 [DllImport ("avahi-client")]
92 private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
93 IntPtr userData, out int error);
95 [DllImport ("avahi-client")]
96 private static extern void avahi_client_free (IntPtr handle);
98 [DllImport ("avahi-client")]
99 private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
101 [DllImport ("avahi-client")]
102 private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
104 [DllImport ("avahi-client")]
105 private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
107 [DllImport ("avahi-client")]
108 private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
110 [DllImport ("avahi-client")]
111 private static extern ClientState avahi_client_get_state (IntPtr handle);
113 [DllImport ("avahi-client")]
114 private static extern int avahi_client_errno (IntPtr handle);
116 [DllImport ("avahi-common")]
117 private static extern IntPtr avahi_simple_poll_new ();
119 [DllImport ("avahi-common")]
120 private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
122 [DllImport ("avahi-common")]
123 private static extern void avahi_simple_poll_free (IntPtr spoll);
125 [DllImport ("avahi-common")]
126 private static extern int avahi_simple_poll_iterate (IntPtr spoll, int timeout);
128 [DllImport ("avahi-common")]
129 private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
131 [DllImport ("avahi-common")]
132 private static extern void avahi_simple_poll_quit (IntPtr spoll);
134 [DllImport ("avahi-client")]
135 private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
137 [DllImport ("avahi-client")]
138 private static extern int avahi_client_is_service_local (IntPtr client, int iface, Protocol proto,
139 IntPtr name, IntPtr type, IntPtr domain);
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 int LastError
210 return 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 ()
242 if (handle != IntPtr.Zero) {
245 avahi_client_free (handle);
246 avahi_simple_poll_quit (spoll);
247 avahi_simple_poll_free (spoll);
248 handle = IntPtr.Zero;
253 public bool IsServiceLocal (ServiceInfo service)
255 return IsServiceLocal (service.NetworkInterface, service.Protocol, service.Name,
256 service.ServiceType, service.Domain);
259 public bool IsServiceLocal (int iface, Protocol proto, string name, string type, string domain)
261 IntPtr namePtr = Utility.StringToPtr (name);
262 IntPtr typePtr = Utility.StringToPtr (type);
263 IntPtr domainPtr = Utility.StringToPtr (domain);
265 int result = avahi_client_is_service_local (handle, iface, proto, namePtr, typePtr, domainPtr);
267 Utility.Free (namePtr);
268 Utility.Free (typePtr);
269 Utility.Free (domainPtr);
274 internal void CheckError ()
276 int error = LastError;
279 throw new ClientException (error);
282 private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
284 if (StateChanged != null)
285 StateChanged (this, state);
288 private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
290 int result = poll (ufds, nfds, timeout);
291 Monitor.Enter (this);
295 private void PollLoop () {
299 if (avahi_simple_poll_iterate (spoll, -1) != 0)
303 } catch (ThreadAbortException e) {
304 } catch (Exception e) {
305 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);