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