]> git.meshlink.io Git - catta/blob - avahi-sharp/Client.cs
actually define the flags correctly
[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         Timeout
33     }
34     
35     internal enum BrowserEvent {
36         Added,
37         Removed
38     }
39
40     internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
41     internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
42
43     public delegate void ClientStateHandler (object o, ClientState state);
44
45     public enum Protocol {
46         Unspecified = -1,
47         IPv4 = 0,
48         IPv6 = 1
49     }
50     
51     public enum ClientState {
52         Invalid,
53         Registering,
54         Running,
55         Collision,
56         Disconnected = 100
57     }
58
59     [Flags]
60     public enum LookupFlags {
61         None = 0,
62         UseWideArea = 1,
63         UseMulticast = 2,
64         NoTxt = 4,
65         NoAddress = 8
66     }
67
68     [Flags]
69     public enum LookupResultFlags {
70         None = 0,
71         Cached = 1,
72         WideArea = 2,
73         Multicast = 4
74     }
75     
76     public class Client : IDisposable
77     {
78         private IntPtr handle;
79         private ClientCallback cb;
80         private PollCallback pollcb;
81         private IntPtr spoll;
82
83         private Thread thread;
84
85         [DllImport ("avahi-client")]
86         private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
87                                                        IntPtr userData, out int error);
88
89         [DllImport ("avahi-client")]
90         private static extern void avahi_client_free (IntPtr handle);
91
92         [DllImport ("avahi-client")]
93         private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
94
95         [DllImport ("avahi-client")]
96         private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
97
98         [DllImport ("avahi-client")]
99         private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
100
101         [DllImport ("avahi-client")]
102         private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
103
104         [DllImport ("avahi-client")]
105         private static extern ClientState avahi_client_get_state (IntPtr handle);
106
107         [DllImport ("avahi-client")]
108         private static extern int avahi_client_errno (IntPtr handle);
109         
110         [DllImport ("avahi-common")]
111         private static extern IntPtr avahi_simple_poll_new ();
112
113         [DllImport ("avahi-common")]
114         private static extern IntPtr avahi_simple_poll_get (IntPtr spoll);
115
116         [DllImport ("avahi-common")]
117         private static extern void avahi_simple_poll_free (IntPtr spoll);
118
119         [DllImport ("avahi-common")]
120         private static extern int avahi_simple_poll_iterate (IntPtr spoll, int timeout);
121
122         [DllImport ("avahi-common")]
123         private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb);
124
125         [DllImport ("avahi-common")]
126         private static extern void avahi_simple_poll_quit (IntPtr spoll);
127
128         [DllImport ("avahi-client")]
129         private static extern uint avahi_client_get_local_service_cookie (IntPtr client);
130
131         [DllImport ("avahi-client")]
132         private static extern int avahi_client_is_service_local (IntPtr client, int iface, Protocol proto,
133                                                                  IntPtr name, IntPtr type, IntPtr domain);
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 int LastError
201         {
202             get {
203                 lock (this) {
204                     return 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             lock (this) {
236                 if (handle != IntPtr.Zero) {
237                     thread.Abort ();
238
239                     avahi_client_free (handle);
240                     avahi_simple_poll_quit (spoll);
241                     avahi_simple_poll_free (spoll);
242                     handle = IntPtr.Zero;
243                 }
244             }
245         }
246
247         public bool IsServiceLocal (ServiceInfo service)
248         {
249             return IsServiceLocal (service.NetworkInterface, service.Protocol, service.Name,
250                                    service.ServiceType, service.Domain);
251         }
252
253         public bool IsServiceLocal (int iface, Protocol proto, string name, string type, string domain)
254         {
255             IntPtr namePtr = Utility.StringToPtr (name);
256             IntPtr typePtr = Utility.StringToPtr (type);
257             IntPtr domainPtr = Utility.StringToPtr (domain);
258             
259             int result = avahi_client_is_service_local (handle, iface, proto, namePtr, typePtr, domainPtr);
260
261             Utility.Free (namePtr);
262             Utility.Free (typePtr);
263             Utility.Free (domainPtr);
264
265             return result == 1;
266         }
267
268         internal void CheckError ()
269         {
270             int error = LastError;
271
272             if (error != 0)
273                 throw new ClientException (error);
274         }
275         
276         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
277         {
278             if (StateChanged != null)
279                 StateChanged (this, state);
280         }
281
282         private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
283             Monitor.Exit (this);
284             int result = poll (ufds, nfds, timeout);
285             Monitor.Enter (this);
286             return result;
287         }
288
289         private void PollLoop () {
290             try {
291                 lock (this) {
292                     while (true) {
293                         if (avahi_simple_poll_iterate (spoll, -1) != 0)
294                             break;
295                     }
296                 }
297             } catch (ThreadAbortException e) {
298             } catch (Exception e) {
299                 Console.Error.WriteLine ("Error in avahi-sharp event loop: " + e);
300             }
301         }
302     }
303 }