]> git.meshlink.io Git - catta/blob - avahi-sharp/Client.cs
integrate mono bindings into the build
[catta] / avahi-sharp / Client.cs
1 /***
2   This file is part of avahi.
3
4   avahi is free software; you can redistribute it and/or modify it
5   under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2.1 of the
7   License, or (at your option) any later version.
8
9   avahi is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12   Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with avahi; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA.
18 ***/
19
20
21 using System;
22 using System.Collections;
23 using System.Runtime.InteropServices;
24
25 namespace Avahi
26 {
27     internal enum ResolverEvent {
28         Found,
29         Timeout
30     }
31     
32     internal enum BrowserEvent {
33         Added,
34         Removed
35     }
36     
37     internal delegate void ClientHandler (IntPtr client, ClientState state, IntPtr userData);
38
39     public enum Protocol {
40         Unspecified = 0,
41         IPv4 = 2,
42         IPv6 = 10
43     }
44     
45     public enum ClientState {
46         Invalid,
47         Registering,
48         Running,
49         Collision,
50         Disconnected = 100
51     }
52     
53     public class Client : IDisposable
54     {
55         private IntPtr handle;
56
57         [DllImport ("avahi-client")]
58         private static extern IntPtr avahi_client_new (IntPtr poll, ClientHandler handler,
59                                                        IntPtr userData, out int error);
60
61         [DllImport ("avahi-client")]
62         private static extern void avahi_client_free (IntPtr handle);
63
64         [DllImport ("avahi-client")]
65         private static extern IntPtr avahi_client_get_version_string (IntPtr handle);
66
67         [DllImport ("avahi-client")]
68         private static extern IntPtr avahi_client_get_host_name (IntPtr handle);
69
70         [DllImport ("avahi-client")]
71         private static extern IntPtr avahi_client_get_domain_name (IntPtr handle);
72
73         [DllImport ("avahi-client")]
74         private static extern IntPtr avahi_client_get_host_name_fqdn (IntPtr handle);
75
76         [DllImport ("avahi-client")]
77         private static extern ClientState avahi_client_get_state (IntPtr handle);
78
79         [DllImport ("avahi-client")]
80         private static extern int avahi_client_errno (IntPtr handle);
81         
82         [DllImport ("avahi-glib")]
83         private static extern IntPtr avahi_glib_poll_new (IntPtr context, int priority);
84
85         [DllImport ("avahi-glib")]
86         private static extern IntPtr avahi_glib_poll_get (IntPtr gpoll);
87
88         internal IntPtr Handle
89         {
90             get { return handle; }
91         }
92         
93         public string Version
94         {
95             get { return Utility.PtrToString (avahi_client_get_version_string (handle)); }
96         }
97
98         public string HostName
99         {
100             get { return Utility.PtrToString (avahi_client_get_host_name (handle)); }
101         }
102
103         public string DomainName
104         {
105             get { return Utility.PtrToString (avahi_client_get_domain_name (handle)); }
106         }
107
108         public string HostNameFqdn
109         {
110             get { return Utility.PtrToString (avahi_client_get_host_name_fqdn (handle)); }
111         }
112
113         public ClientState State
114         {
115             get { return (ClientState) avahi_client_get_state (handle); }
116         }
117
118         internal int LastError
119         {
120             get { return avahi_client_errno (handle); }
121         }
122
123         public Client ()
124         {
125             IntPtr gpoll = avahi_glib_poll_new (IntPtr.Zero, 0);
126             IntPtr poll = avahi_glib_poll_get (gpoll);
127
128             int error;
129             handle = avahi_client_new (poll, OnClientCallback, IntPtr.Zero, out error);
130             if (error != 0)
131                 throw new ClientException (error);
132         }
133
134         ~Client ()
135         {
136             Dispose ();
137         }
138
139         public void Dispose ()
140         {
141             if (handle != IntPtr.Zero) {
142                 avahi_client_free (handle);
143                 handle = IntPtr.Zero;
144             }
145         }
146
147         internal void CheckError ()
148         {
149             int error = LastError;
150
151             if (error != 0)
152                 throw new ClientException (error);
153         }
154         
155         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
156         {
157             Console.WriteLine ("Got new state: " + state);
158         }
159     }
160 }