X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;ds=sidebyside;f=avahi-sharp%2FServiceResolver.cs;h=8e8464be10f32bf36846408fedf08931e5be8e02;hb=dab307a583fcacdb0703e451f4f854f540fc8ec1;hp=af3bf5bcb67ff8c7b673e236222d06ace5edd8a4;hpb=ba12decc4413dedf22c06545d1ec5938efa8954a;p=catta diff --git a/avahi-sharp/ServiceResolver.cs b/avahi-sharp/ServiceResolver.cs index af3bf5b..8e8464b 100644 --- a/avahi-sharp/ServiceResolver.cs +++ b/avahi-sharp/ServiceResolver.cs @@ -1,3 +1,24 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + using System; using System.Collections; using System.Net; @@ -23,6 +44,7 @@ namespace Avahi private string type; private string domain; private Protocol aproto; + private ServiceResolverCallback cb; private ArrayList foundListeners = new ArrayList (); private ArrayList timeoutListeners = new ArrayList (); @@ -33,6 +55,15 @@ namespace Avahi Protocol aproto, ServiceResolverCallback cb, IntPtr userdata); + [DllImport ("avahi-common")] + private static extern IntPtr avahi_string_list_get_next (IntPtr list); + + [DllImport ("avahi-common")] + private static extern IntPtr avahi_string_list_get_text (IntPtr list); + + [DllImport ("avahi-common")] + private static extern int avahi_string_list_get_size (IntPtr list); + [DllImport ("avahi-client")] private static extern void avahi_service_resolver_free (IntPtr handle); @@ -89,8 +120,7 @@ namespace Avahi this.type = type; this.domain = domain; this.aproto = aproto; - - + cb = OnServiceResolverCallback; } ~ServiceResolver () @@ -105,14 +135,19 @@ namespace Avahi private void Start () { - if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0)) + if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero || + (foundListeners.Count == 0 && timeoutListeners.Count == 0)) return; IntPtr namePtr = Utility.StringToPtr (name); IntPtr typePtr = Utility.StringToPtr (type); IntPtr domainPtr = Utility.StringToPtr (domain); - handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr, - aproto, OnServiceResolverCallback, IntPtr.Zero); + + lock (client) { + handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr, + aproto, cb, IntPtr.Zero); + } + Utility.Free (namePtr); Utility.Free (typePtr); Utility.Free (domainPtr); @@ -120,9 +155,13 @@ namespace Avahi private void Stop (bool force) { - if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) { - avahi_service_resolver_free (handle); - handle = IntPtr.Zero; + if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero && + (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) { + + lock (client) { + avahi_service_resolver_free (handle); + handle = IntPtr.Zero; + } } } @@ -137,11 +176,22 @@ namespace Avahi info.Domain = Utility.PtrToString (domain); info.ServiceType = Utility.PtrToString (type); info.Name = Utility.PtrToString (name); - info.Host = Utility.PtrToString (host); + info.HostName = Utility.PtrToString (host); info.Address = Utility.PtrToAddress (address); info.Port = port; - info.Text = null; + ArrayList txtlist = new ArrayList (); + for (IntPtr l = txt; l != IntPtr.Zero; l = avahi_string_list_get_next (l)) { + IntPtr buf = avahi_string_list_get_text (l); + int len = avahi_string_list_get_size (l); + + byte[] txtbuf = new byte[len]; + Marshal.Copy (buf, txtbuf, 0, len); + txtlist.Add (txtbuf); + } + + info.Text = (byte[][]) txtlist.ToArray (typeof (byte[])); + if (revent == ResolverEvent.Found) { currentInfo = info;