]> git.meshlink.io Git - catta/blob - avahi-sharp/Client.cs
update to latest api changes
[catta] / avahi-sharp / Client.cs
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5
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.
10
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.
15
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
19   USA.
20 ***/
21
22
23 using System;
24 using System.Threading;
25 using System.Collections;
26 using System.Runtime.InteropServices;
27
28 namespace Avahi
29 {
30     internal enum ResolverEvent {
31         Found,
32         Failure
33     }
34     
35     internal enum BrowserEvent {
36         Added,
37         Removed,
38         CacheExhausted,
39         AllForNow,
40         Failure
41     }
42
43     internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
44     internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
45
46     public delegate void ClientStateHandler (object o, ClientState state);
47
48     public enum Protocol {
49         Unspecified = -1,
50         IPv4 = 0,
51         IPv6 = 1
52     }
53
54     internal enum ServerState {
55         Invalid,
56         Registering,
57         Running,
58         Collision
59     }
60     
61     public enum ClientState {
62         Registering = ServerState.Registering,
63         Running = ServerState.Running,
64         Collision = ServerState.Collision,
65         Failure = 100,
66         Connecting = 101
67     }
68
69     [Flags]
70     public enum LookupFlags {
71         None = 0,
72         UseWideArea = 1,
73         UseMulticast = 4,
74         NoAddress = 8
75     }
76
77     [Flags]
78     public enum LookupResultFlags {
79         None = 0,
80         Cached = 1,
81         WideArea = 2,
82         Multicast = 4,
83         Local = 8,
84         OurOwn = 16,
85     }
86
87     [Flags]
88     public enum ClientFlags {
89         None = 0,
90         IgnoreUserConfig = 1,
91         NoFail = 2
92     }
93     
94     public class Client : IDisposable
95     {
96         private IntPtr handle;
97         private ClientCallback cb;
98         private PollCallback pollcb;
99         private IntPtr spoll;
100
101         private Thread thread;
102
103         [DllImport ("avahi-client")]
104         private static extern IntPtr avahi_client_new (IntPtr poll, ClientFlags flags, ClientCallback handler,
105                                                        IntPtr userData, out int error);
106
107         [DllImport ("avahi-client")]
108         private static extern void avahi_client_free (IntPtr handle);
109
110         [DllImport ("avahi-client")]
111         private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
112
113         [DllImport ("avahi-client")]
114         private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
115
116         [DllImport ("avahi-client")]
117         private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
118
119         [DllImport ("avahi-client")]
120         private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
121
122         [DllImport ("avahi-client")]
123         private static extern ClientState avahi_client_get_state (IntPtr handle);
124
125         [DllImport ("avahi-client")]
126         private static extern int avahi_client_errno (IntPtr handle);
127         
128         [DllImport ("avahi-common")]
129         private static extern IntPtr avahi_simple_poll_new ();
130
131         [DllImport ("avahi-common")]
132         private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
133
134         [DllImport ("avahi-common")]
135         private static extern void avahi_simple_poll_free (IntPtr spoll);
136
137         [DllImport ("avahi-common")]
138         private static extern int avahi_simple_poll_loop (IntPtr spoll);
139
140         [DllImport ("avahi-common")]
141         private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
142
143         [DllImport ("avahi-common")]
144         private static extern void avahi_simple_poll_quit (IntPtr spoll);
145
146         [DllImport ("avahi-client")]
147         private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
148
149
150         [DllImport ("libc")]
151         private static extern int poll(IntPtr ufds, uint nfds, int timeout);
152
153         public event ClientStateHandler StateChanged;
154
155         internal IntPtr Handle
156         {
157             get { return handle; }
158         }
159         
160         public string Version
161         {
162             get {
163                 lock (this) {
164                     return Utility.PtrToString (avahi_client_get_version_string (handle));
165                 }
166             }
167         }
168
169         public string HostName
170         {
171             get {
172                 lock (this) {
173                     return Utility.PtrToString (avahi_client_get_host_name (handle));
174                 }
175             }
176         }
177
178         public string DomainName
179         {
180             get {
181                 lock (this) {
182                     return Utility.PtrToString (avahi_client_get_domain_name (handle));
183                 }
184             }
185         }
186
187         public string HostNameFqdn
188         {
189             get {
190                 lock (this) {
191                     return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
192                 }
193             }
194         }
195
196         public ClientState State
197         {
198             get {
199                 lock (this) {
200                     return (ClientState) avahi_client_get_state (handle);
201                 }
202             }
203         }
204
205         public uint LocalServiceCookie
206         {
207             get {
208                 lock (this) {
209                     return avahi_client_get_local_service_cookie (handle);
210                 }
211             }
212         }
213
214         internal ErrorCode LastError
215         {
216             get {
217                 lock (this) {
218                     return (ErrorCode) avahi_client_errno (handle);
219                 }
220             }
221         }
222
223         public Client (ClientFlags flags)
224         {
225             spoll = avahi_simple_poll_new ();
226
227             pollcb = OnPollCallback;
228             avahi_simple_poll_set_func (spoll, pollcb);
229             IntPtr poll = avahi_simple_poll_get (spoll);
230             cb = OnClientCallback;
231
232             int error;
233             handle = avahi_client_new (poll, flags, cb, IntPtr.Zero, out error);
234             if (error != 0)
235                 throw new ClientException (error);
236
237             thread = new Thread (PollLoop);
238             thread.IsBackground = true;
239             thread.Start ();
240         }
241
242         public Client () : this (ClientFlags.None) {
243         }
244
245         ~Client ()
246         {
247             Dispose ();
248         }
249
250         public void Dispose ()
251         {
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;
257             }
258         }
259
260         internal void ThrowError ()
261         {
262             ErrorCode error = LastError;
263
264             if (error != ErrorCode.Ok)
265                 throw new ClientException (error);
266         }
267         
268         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
269         {
270             if (StateChanged != null)
271                 StateChanged (this, state);
272         }
273
274         private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
275             Monitor.Exit (this);
276             int result = poll (ufds, nfds, timeout);
277             Monitor.Enter (this);
278             return result;
279         }
280
281         private void PollLoop () {
282             try {
283                 lock (this) {
284                     avahi_simple_poll_loop (spoll);
285                 }
286             } catch (Exception e) {
287                 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);
288             }
289         }
290     }
291 }