]> git.meshlink.io Git - catta/blob - avahi-sharp/Client.cs
* correct the error handling in EntryGroup
[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     public enum ClientState {
55         Invalid,
56         Registering,
57         Running,
58         Collision,
59         Disconnected = 100
60     }
61
62     [Flags]
63     public enum LookupFlags {
64         None = 0,
65         UseWideArea = 1,
66         UseMulticast = 4,
67         NoAddress = 8
68     }
69
70     [Flags]
71     public enum LookupResultFlags {
72         None = 0,
73         Cached = 1,
74         WideArea = 2,
75         Multicast = 4,
76         Local = 8,
77         OurOwn = 16,
78     }
79     
80     public class Client : IDisposable
81     {
82         private IntPtr handle;
83         private ClientCallback cb;
84         private PollCallback pollcb;
85         private IntPtr spoll;
86
87         private Thread thread;
88
89         [DllImport ("avahi-client")]
90         private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
91                                                        IntPtr userData, out int error);
92
93         [DllImport ("avahi-client")]
94         private static extern void avahi_client_free (IntPtr handle);
95
96         [DllImport ("avahi-client")]
97         private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
98
99         [DllImport ("avahi-client")]
100         private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
101
102         [DllImport ("avahi-client")]
103         private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
104
105         [DllImport ("avahi-client")]
106         private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
107
108         [DllImport ("avahi-client")]
109         private static extern ClientState avahi_client_get_state (IntPtr handle);
110
111         [DllImport ("avahi-client")]
112         private static extern int avahi_client_errno (IntPtr handle);
113         
114         [DllImport ("avahi-common")]
115         private static extern IntPtr avahi_simple_poll_new ();
116
117         [DllImport ("avahi-common")]
118         private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
119
120         [DllImport ("avahi-common")]
121         private static extern void avahi_simple_poll_free (IntPtr spoll);
122
123         [DllImport ("avahi-common")]
124         private static extern int avahi_simple_poll_loop (IntPtr spoll);
125
126         [DllImport ("avahi-common")]
127         private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
128
129         [DllImport ("avahi-common")]
130         private static extern void avahi_simple_poll_quit (IntPtr spoll);
131
132         [DllImport ("avahi-client")]
133         private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
134
135
136         [DllImport ("libc")]
137         private static extern int poll(IntPtr ufds, uint nfds, int timeout);
138
139         public event ClientStateHandler StateChanged;
140
141         internal IntPtr Handle
142         {
143             get { return handle; }
144         }
145         
146         public string Version
147         {
148             get {
149                 lock (this) {
150                     return Utility.PtrToString (avahi_client_get_version_string (handle));
151                 }
152             }
153         }
154
155         public string HostName
156         {
157             get {
158                 lock (this) {
159                     return Utility.PtrToString (avahi_client_get_host_name (handle));
160                 }
161             }
162         }
163
164         public string DomainName
165         {
166             get {
167                 lock (this) {
168                     return Utility.PtrToString (avahi_client_get_domain_name (handle));
169                 }
170             }
171         }
172
173         public string HostNameFqdn
174         {
175             get {
176                 lock (this) {
177                     return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle));
178                 }
179             }
180         }
181
182         public ClientState State
183         {
184             get {
185                 lock (this) {
186                     return (ClientState) avahi_client_get_state (handle);
187                 }
188             }
189         }
190
191         public uint LocalServiceCookie
192         {
193             get {
194                 lock (this) {
195                     return avahi_client_get_local_service_cookie (handle);
196                 }
197             }
198         }
199
200         internal ErrorCode LastError
201         {
202             get {
203                 lock (this) {
204                     return (ErrorCode) avahi_client_errno (handle);
205                 }
206             }
207         }
208
209         public Client ()
210         {
211             spoll = avahi_simple_poll_new ();
212
213             pollcb = OnPollCallback;
214             avahi_simple_poll_set_func (spoll, pollcb);
215             IntPtr poll = avahi_simple_poll_get (spoll);
216             cb = OnClientCallback;
217
218             int error;
219             handle = avahi_client_new (poll, cb, IntPtr.Zero, out error);
220             if (error != 0)
221                 throw new ClientException (error);
222
223             thread = new Thread (PollLoop);
224             thread.IsBackground = true;
225             thread.Start ();
226         }
227
228         ~Client ()
229         {
230             Dispose ();
231         }
232
233         public void Dispose ()
234         {
235             if (handle != IntPtr.Zero) {
236                 avahi_client_free (handle);
237                 avahi_simple_poll_quit (spoll);
238                 avahi_simple_poll_free (spoll);
239                 handle = IntPtr.Zero;
240             }
241         }
242
243         internal void ThrowError ()
244         {
245             ErrorCode error = LastError;
246
247             if (error != ErrorCode.Ok)
248                 throw new ClientException (error);
249         }
250         
251         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
252         {
253             if (StateChanged != null)
254                 StateChanged (this, state);
255         }
256
257         private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
258             Monitor.Exit (this);
259             int result = poll (ufds, nfds, timeout);
260             Monitor.Enter (this);
261             return result;
262         }
263
264         private void PollLoop () {
265             try {
266                 lock (this) {
267                     avahi_simple_poll_loop (spoll);
268                 }
269             } catch (Exception e) {
270                 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);
271             }
272         }
273     }
274 }