]> git.meshlink.io Git - catta/blob - avahi-sharp/ServiceResolver.cs
* update to the latest avahi-client API (LookupFlags)
[catta] / avahi-sharp / ServiceResolver.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 using System;
23 using System.Collections;
24 using System.Net;
25 using System.Runtime.InteropServices;
26 using Mono.Unix;
27
28 namespace Avahi
29 {
30
31     internal delegate void ServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
32                                                     ResolverEvent revent, IntPtr name, IntPtr type,
33                                                     IntPtr domain, IntPtr host, IntPtr address,
34                                                     UInt16 port, IntPtr txt, LookupResultFlags flags,
35                                                     IntPtr userdata);
36
37     public class ServiceResolver : IDisposable
38     {
39         private IntPtr handle;
40         private ServiceInfo currentInfo;
41         private Client client;
42         private int iface;
43         private Protocol proto;
44         private string name;
45         private string type;
46         private string domain;
47         private Protocol aproto;
48         private LookupFlags flags;
49         private ServiceResolverCallback cb;
50
51         private ArrayList foundListeners = new ArrayList ();
52         private ArrayList timeoutListeners = new ArrayList ();
53         
54         [DllImport ("avahi-client")]
55         private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto,
56                                                                  IntPtr name, IntPtr type, IntPtr domain,
57                                                                  Protocol aproto, LookupFlags flags,
58                                                                  ServiceResolverCallback cb,
59                                                                  IntPtr userdata);
60
61         [DllImport ("avahi-common")]
62         private static extern IntPtr avahi_string_list_get_next (IntPtr list);
63
64         [DllImport ("avahi-common")]
65         private static extern IntPtr avahi_string_list_get_text (IntPtr list);
66
67         [DllImport ("avahi-common")]
68         private static extern int avahi_string_list_get_size (IntPtr list);
69
70         [DllImport ("avahi-client")]
71         private static extern void avahi_service_resolver_free (IntPtr handle);
72
73         public event ServiceInfoHandler Found
74         {
75             add {
76                 foundListeners.Add (value);
77                 Start ();
78             }
79             remove {
80                 foundListeners.Remove (value);
81                 Stop (false);
82             }
83         }
84         
85         public event EventHandler Timeout
86         {
87             add {
88                 timeoutListeners.Add (value);
89                 Start ();
90             }
91             remove {
92                 timeoutListeners.Remove (value);
93                 Stop (false);
94             }
95         }
96
97         public ServiceInfo Service
98         {
99             get { return currentInfo; }
100         }
101
102         public ServiceResolver (Client client, string name, string type, string domain) : this (client, -1,
103                                                                                                 Protocol.Unspecified,
104                                                                                                 name, type, domain,
105                                                                                                 Protocol.Unspecified)
106         {
107         }
108
109         public ServiceResolver (Client client, ServiceInfo service) : this (client, service.NetworkInterface,
110                                                                             service.Protocol, service.Name,
111                                                                             service.ServiceType, service.Domain,
112                                                                             Protocol.Unspecified)
113         {
114         }
115         
116         public ServiceResolver (Client client, int iface, Protocol proto, string name,
117                                 string type, string domain, Protocol aproto)
118         {
119             this.client = client;
120             this.iface = iface;
121             this.proto = proto;
122             this.name = name;
123             this.type = type;
124             this.domain = domain;
125             this.aproto = aproto;
126             cb = OnServiceResolverCallback;
127         }
128
129         ~ServiceResolver ()
130         {
131             Dispose ();
132         }
133
134         public void Dispose ()
135         {
136             Stop (true);
137         }
138
139         private void Start ()
140         {
141             if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
142                 (foundListeners.Count == 0 && timeoutListeners.Count == 0))
143                 return;
144
145             IntPtr namePtr = Utility.StringToPtr (name);
146             IntPtr typePtr = Utility.StringToPtr (type);
147             IntPtr domainPtr = Utility.StringToPtr (domain);
148
149             lock (client) {
150                 handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr,
151                                                      aproto, flags, cb, IntPtr.Zero);
152             }
153             
154             Utility.Free (namePtr);
155             Utility.Free (typePtr);
156             Utility.Free (domainPtr);
157         }
158
159         private void Stop (bool force)
160         {
161             if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
162                 (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
163
164                 lock (client) {
165                     avahi_service_resolver_free (handle);
166                     handle = IntPtr.Zero;
167                 }
168             }
169         }
170
171         private void OnServiceResolverCallback (IntPtr resolver, int iface, Protocol proto,
172                                                 ResolverEvent revent, IntPtr name, IntPtr type,
173                                                 IntPtr domain, IntPtr host, IntPtr address,
174                                                 UInt16 port, IntPtr txt, LookupResultFlags flags,
175                                                 IntPtr userdata)
176         {
177             ServiceInfo info;
178             info.NetworkInterface = iface;
179             info.Protocol = proto;
180             info.Domain = Utility.PtrToString (domain);
181             info.ServiceType = Utility.PtrToString (type);
182             info.Name = Utility.PtrToString (name);
183             info.HostName = Utility.PtrToString (host);
184             info.Address = Utility.PtrToAddress (address);
185             info.Port = port;
186
187             ArrayList txtlist = new ArrayList ();
188             for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) {
189                 IntPtr buf = avahi_string_list_get_text (l);
190                 int len = avahi_string_list_get_size (l);
191
192                 byte[] txtbuf = new byte[len];
193                 Marshal.Copy (buf, txtbuf, 0, len);
194                 txtlist.Add (txtbuf);
195             }
196
197             info.Text = (byte[][]) txtlist.ToArray (typeof (byte[]));
198             info.Flags = flags;
199             
200             if (revent == ResolverEvent.Found) {
201                 currentInfo = info;
202
203                 foreach (ServiceInfoHandler handler in foundListeners)
204                     handler (this, info);
205             } else {
206                 currentInfo = ServiceInfo.Zero;
207                 
208                 foreach (EventHandler handler in timeoutListeners)
209                     handler (this, new EventArgs ());
210             }
211         }
212     }
213 }