]> git.meshlink.io Git - catta/blob - avahi-sharp/Client.cs
s/Host/HostName/ in a couple places, and keep references to callbacks to
[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.Collections;
25 using System.Runtime.InteropServices;
26
27 namespace Avahi
28 {
29     internal enum ResolverEvent {
30         Found,
31         Timeout
32     }
33     
34     internal enum BrowserEvent {
35         Added,
36         Removed
37     }
38     
39     internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
40
41     public delegate void ClientStateHandler (object o, ClientState state);
42
43     public enum Protocol {
44         Unspecified = 0,
45         IPv4 = 2,
46         IPv6 = 10
47     }
48     
49     public enum ClientState {
50         Invalid,
51         Registering,
52         Running,
53         Collision,
54         Disconnected = 100
55     }
56     
57     public class Client : IDisposable
58     {
59         private IntPtr handle;
60
61         [DllImport ("avahi-client")]
62         private static extern IntPtr avahi_client_new (IntPtr poll, ClientCallback handler,
63                                                        IntPtr userData, out int error);
64
65         [DllImport ("avahi-client")]
66         private static extern void avahi_client_free (IntPtr handle);
67
68         [DllImport ("avahi-client")]
69         private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
70
71         [DllImport ("avahi-client")]
72         private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
73
74         [DllImport ("avahi-client")]
75         private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
76
77         [DllImport ("avahi-client")]
78         private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
79
80         [DllImport ("avahi-client")]
81         private static extern ClientState avahi_client_get_state (IntPtr handle);
82
83         [DllImport ("avahi-client")]
84         private static extern int avahi_client_errno (IntPtr handle);
85         
86         [DllImport ("avahi-glib")]
87         private static extern IntPtr avahi_glib_poll_new (IntPtr context, int priority);
88
89         [DllImport ("avahi-glib")]
90         private static extern IntPtr avahi_glib_poll_get (IntPtr gpoll);
91
92         public event ClientStateHandler StateChanged;
93
94         internal IntPtr Handle
95         {
96             get { return handle; }
97         }
98         
99         public string Version
100         {
101             get { return Utility.PtrToString (avahi_client_get_version_string (handle)); }
102         }
103
104         public string HostName
105         {
106             get { return Utility.PtrToString (avahi_client_get_host_name (handle)); }
107         }
108
109         public string DomainName
110         {
111             get { return Utility.PtrToString (avahi_client_get_domain_name (handle)); }
112         }
113
114         public string HostNameFqdn
115         {
116             get { return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle)); }
117         }
118
119         public ClientState State
120         {
121             get { return (ClientState) avahi_client_get_state (handle); }
122         }
123
124         internal int LastError
125         {
126             get { return avahi_client_errno (handle); }
127         }
128
129         public Client ()
130         {
131             IntPtr gpoll = avahi_glib_poll_new (IntPtr.Zero, 0);
132             IntPtr poll = avahi_glib_poll_get (gpoll);
133
134             int error;
135             handle = avahi_client_new (poll, OnClientCallback, IntPtr.Zero, out error);
136             if (error != 0)
137                 throw new ClientException (error);
138         }
139
140         ~Client ()
141         {
142             Dispose ();
143         }
144
145         public void Dispose ()
146         {
147             if (handle != IntPtr.Zero) {
148                 avahi_client_free (handle);
149                 handle = IntPtr.Zero;
150             }
151         }
152
153         internal void CheckError ()
154         {
155             int error = LastError;
156
157             if (error != 0)
158                 throw new ClientException (error);
159         }
160         
161         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
162         {
163             if (StateChanged != null)
164                 StateChanged (this, state);
165         }
166     }
167 }