From: James Willcox Date: Thu, 17 Nov 2005 21:11:13 +0000 (+0000) Subject: * add byte[] overloads for EntryGroup methods X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=9a589e1f5221d1bd995b6c4fa73e3a074d4c2da4;p=catta * add byte[] overloads for EntryGroup methods * cleanup string marshalling git-svn-id: file:///home/lennart/svn/public/avahi/trunk@999 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-sharp/AddressResolver.cs b/avahi-sharp/AddressResolver.cs index 63dc33e..3025102 100644 --- a/avahi-sharp/AddressResolver.cs +++ b/avahi-sharp/AddressResolver.cs @@ -151,6 +151,9 @@ namespace Avahi lock (client) { handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags, cb, IntPtr.Zero); + + if (handle == IntPtr.Zero) + client.ThrowError (); } Utility.Free (addrPtr); diff --git a/avahi-sharp/AvahiTest.cs b/avahi-sharp/AvahiTest.cs index df95b90..99cd6e2 100644 --- a/avahi-sharp/AvahiTest.cs +++ b/avahi-sharp/AvahiTest.cs @@ -37,7 +37,7 @@ public class AvahiTest { EntryGroup eg = new EntryGroup (client); eg.StateChanged += OnEntryGroupChanged; eg.AddService ("foobar2", "_daap._tcp", client.DomainName, - 444, new string[] { "foo", "bar", "baz" }); + 444, new string[] { "foo=stuff", "bar=stuff2", "baz=stuff3" }); eg.Commit (); Console.WriteLine ("Press enter to quit"); Console.ReadLine (); @@ -99,10 +99,17 @@ public class AvahiTest { foreach (byte[] bytes in args.Service.Text) { Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes)); } + AddressResolver ar = new AddressResolver (client, args.Service.Address); objects.Add (ar); - + ar.Found += OnAddressResolved; + ar.Failed += OnAddressResolverFailed; + } + + private static void OnAddressResolverFailed (object o, ErrorCodeArgs args) + { + Console.WriteLine ("Failed to resolve '{0}': {1}", (o as AddressResolver).Address, args.ErrorCode); } private static void OnAddressResolved (object o, HostAddressArgs args) diff --git a/avahi-sharp/DomainBrowser.cs b/avahi-sharp/DomainBrowser.cs index 50ac757..50da54e 100644 --- a/avahi-sharp/DomainBrowser.cs +++ b/avahi-sharp/DomainBrowser.cs @@ -22,6 +22,7 @@ using System; using System.Collections; using System.Runtime.InteropServices; +using System.Text; namespace Avahi { @@ -78,7 +79,7 @@ namespace Avahi [DllImport ("avahi-client")] private static extern IntPtr avahi_domain_browser_new (IntPtr client, int iface, int proto, - IntPtr domain, int btype, LookupFlags flags, + byte[] domain, int btype, LookupFlags flags, DomainBrowserCallback cb, IntPtr userdata); @@ -147,10 +148,12 @@ namespace Avahi return; lock (client) { - IntPtr domainPtr = Utility.StringToPtr (domain); - handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, domainPtr, (int) btype, flags, + handle = avahi_domain_browser_new (client.Handle, iface, (int) proto, + Utility.StringToBytes (domain), (int) btype, flags, cb, IntPtr.Zero); - Utility.Free (domainPtr); + + if (handle == IntPtr.Zero) + client.ThrowError (); } } diff --git a/avahi-sharp/EntryGroup.cs b/avahi-sharp/EntryGroup.cs index eb7d9e2..2ebecc2 100644 --- a/avahi-sharp/EntryGroup.cs +++ b/avahi-sharp/EntryGroup.cs @@ -22,6 +22,7 @@ using System; using System.Net; using System.Runtime.InteropServices; +using System.Text; using Mono.Unix; namespace Avahi @@ -90,28 +91,28 @@ namespace Avahi [DllImport ("avahi-client")] private static extern int avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto, - PublishFlags flags, IntPtr name, IntPtr type, - IntPtr domain, IntPtr host, UInt16 port, + PublishFlags flags, byte[] name, byte[] type, + byte[] domain, byte[] host, UInt16 port, IntPtr strlst); [DllImport ("avahi-client")] private static extern int avahi_entry_group_update_service_strlst (IntPtr group, int iface, Protocol proto, - PublishFlags flags, IntPtr name, - IntPtr type, IntPtr domain, IntPtr strlst); + PublishFlags flags, byte[] name, + byte[] type, byte[] domain, IntPtr strlst); [DllImport ("avahi-client")] private static extern int avahi_entry_group_add_service_subtype (IntPtr group, int iface, Protocol proto, - PublishFlags flags, IntPtr name, IntPtr type, - IntPtr domain, IntPtr subtype); + PublishFlags flags, byte[] name, byte[] type, + byte[] domain, byte[] subtype); [DllImport ("avahi-client")] private static extern int avahi_entry_group_add_address (IntPtr group, int iface, Protocol proto, - PublishFlags flags, IntPtr name, IntPtr address); + PublishFlags flags, byte[] name, IntPtr address); [DllImport ("avahi-client")] private static extern int avahi_entry_group_add_record (IntPtr group, int iface, Protocol proto, - PublishFlags flags, IntPtr name, RecordClass clazz, + PublishFlags flags, byte[] name, RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int size); [DllImport ("avahi-client")] @@ -121,23 +122,23 @@ namespace Avahi private static extern IntPtr avahi_string_list_new (IntPtr txt); [DllImport ("avahi-common")] - private static extern IntPtr avahi_string_list_add (IntPtr list, IntPtr txt); + private static extern IntPtr avahi_string_list_add (IntPtr list, byte[] txt); [DllImport ("avahi-common")] private static extern void avahi_string_list_free (IntPtr list); [DllImport ("avahi-common")] - private static extern int avahi_service_name_join (IntPtr buf, int len, IntPtr name, IntPtr type, - IntPtr domain); + private static extern int avahi_service_name_join (IntPtr buf, int len, byte[] name, byte[] type, + byte[] domain); [DllImport ("avahi-common")] - private static extern int avahi_service_name_split (IntPtr service, IntPtr name, int name_len, + private static extern int avahi_service_name_split (byte[] service, IntPtr name, int name_len, IntPtr type, int type_len, IntPtr domain, int domain_len); [DllImport ("avahi-common")] - private static extern IntPtr avahi_alternative_service_name (IntPtr name); + private static extern IntPtr avahi_alternative_service_name (byte[] name); public event EntryGroupStateHandler StateChanged; @@ -221,32 +222,41 @@ namespace Avahi if (txt != null) { foreach (string item in txt) { - IntPtr itemPtr = Utility.StringToPtr (item); - list = avahi_string_list_add (list, itemPtr); - Utility.Free (itemPtr); + list = avahi_string_list_add (list, Utility.StringToBytes (item)); } } - IntPtr namePtr = Utility.StringToPtr (name); - IntPtr typePtr = Utility.StringToPtr (type); - IntPtr domainPtr = Utility.StringToPtr (domain); - IntPtr hostPtr = Utility.StringToPtr (host); - - lock (client) { - int ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr, - hostPtr, port, list); + AddService (iface, proto, flags, name, type, domain, host, port, list); + } - Utility.Free (namePtr); - Utility.Free (typePtr); - Utility.Free (domainPtr); - Utility.Free (hostPtr); - avahi_string_list_free (list); + public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain, + string host, UInt16 port, params byte[][] txt) + { + IntPtr list = avahi_string_list_new (IntPtr.Zero); - if (ret < 0) { - client.ThrowError (); + if (txt != null) { + foreach (byte[] item in txt) { + list = avahi_string_list_add (list, item); } } + + AddService (iface, proto, flags, name, type, domain, host, port, list); + } + + private void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, + string domain, string host, UInt16 port, IntPtr list) + { + int ret = avahi_entry_group_add_service_strlst (handle, iface, proto, flags, + Utility.StringToBytes (name), + Utility.StringToBytes (type), + Utility.StringToBytes (domain), + Utility.StringToBytes (host), port, list); + + avahi_string_list_free (list); + if (ret < 0) { + client.ThrowError (); + } } public void UpdateService (string name, string type, string domain, params string[] txt) @@ -261,23 +271,17 @@ namespace Avahi if (txt != null) { foreach (string item in txt) { - IntPtr itemPtr = Utility.StringToPtr (item); - list = avahi_string_list_add (list, itemPtr); - Utility.Free (itemPtr); + list = avahi_string_list_add (list, Utility.StringToBytes (item)); } } - IntPtr namePtr = Utility.StringToPtr (name); - IntPtr typePtr = Utility.StringToPtr (type); - IntPtr domainPtr = Utility.StringToPtr (domain); - lock (client) { - int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr, list); + int ret = avahi_entry_group_update_service_strlst (handle, iface, proto, flags, + Utility.StringToBytes (name), + Utility.StringToBytes (type), + Utility.StringToBytes (domain), + list); - Utility.Free (namePtr); - Utility.Free (typePtr); - Utility.Free (domainPtr); - avahi_string_list_free (list); if (ret < 0) { @@ -294,20 +298,13 @@ namespace Avahi public void AddServiceSubtype (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain, string subtype) { - IntPtr namePtr = Utility.StringToPtr (name); - IntPtr typePtr = Utility.StringToPtr (type); - IntPtr domainPtr = Utility.StringToPtr (domain); - IntPtr subtypePtr = Utility.StringToPtr (subtype); - lock (client) { - int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags, namePtr, - typePtr, domainPtr, subtypePtr); + int ret = avahi_entry_group_add_service_subtype (handle, iface, proto, flags, + Utility.StringToBytes (name), + Utility.StringToBytes (type), + Utility.StringToBytes (domain), + Utility.StringToBytes (subtype)); - Utility.Free (namePtr); - Utility.Free (typePtr); - Utility.Free (domainPtr); - Utility.Free (subtypePtr); - if (ret < 0) { client.ThrowError (); } @@ -321,13 +318,12 @@ namespace Avahi public void AddAddress (int iface, Protocol proto, PublishFlags flags, string name, IPAddress address) { - IntPtr namePtr = Utility.StringToPtr (name); IntPtr addressPtr = Utility.AddressToPtr (address); lock (client) { - int ret = avahi_entry_group_add_address (handle, iface, proto, flags, namePtr, addressPtr); + int ret = avahi_entry_group_add_address (handle, iface, proto, flags, + Utility.StringToBytes (name), addressPtr); - Utility.Free (namePtr); Utility.Free (addressPtr); if (ret < 0) { @@ -344,14 +340,11 @@ namespace Avahi public void AddRecord (int iface, Protocol proto, PublishFlags flags, string name, RecordClass clazz, RecordType type, uint ttl, byte[] rdata, int length) { - IntPtr namePtr = Utility.StringToPtr (name); - lock (client) { - int ret = avahi_entry_group_add_record (handle, iface, proto, flags, namePtr, + int ret = avahi_entry_group_add_record (handle, iface, proto, flags, + Utility.StringToBytes (name), clazz, type, ttl, rdata, length); - Utility.Free (namePtr); - if (ret < 0) { client.ThrowError (); } @@ -363,15 +356,10 @@ namespace Avahi int len = 4 * (name.Length + type.Length + domain.Length) + 4; IntPtr buf = Stdlib.malloc ((ulong) len); - IntPtr namePtr = Utility.StringToPtr (name); - IntPtr typePtr = Utility.StringToPtr (type); - IntPtr domainPtr = Utility.StringToPtr (domain); - - int ret = avahi_service_name_join (buf, len, namePtr, typePtr, domainPtr); - - Utility.Free (namePtr); - Utility.Free (typePtr); - Utility.Free (domainPtr); + int ret = avahi_service_name_join (buf, len, + Utility.StringToBytes (name), + Utility.StringToBytes (type), + Utility.StringToBytes (domain)); if (ret < 0) { Utility.Free (buf); @@ -388,15 +376,13 @@ namespace Avahi { int len = 1024; - IntPtr servicePtr = Utility.StringToPtr (service); IntPtr namePtr = Stdlib.malloc ((ulong) len); IntPtr typePtr = Stdlib.malloc ((ulong) len); IntPtr domainPtr = Stdlib.malloc ((ulong) len); - int ret = avahi_service_name_split (servicePtr, namePtr, len, typePtr, len, domainPtr, len); + int ret = avahi_service_name_split (Utility.StringToBytes (service), namePtr, len, typePtr, len, + domainPtr, len); - Utility.Free (servicePtr); - if (ret < 0) { Utility.Free (namePtr); Utility.Free (typePtr); @@ -418,11 +404,7 @@ namespace Avahi } public static string GetAlternativeServiceName (string name) { - IntPtr namePtr = Utility.StringToPtr (name); - IntPtr result = avahi_alternative_service_name (namePtr); - Utility.Free (namePtr); - - return Utility.PtrToStringFree (result); + return Utility.PtrToStringFree (avahi_alternative_service_name (Utility.StringToBytes (name))); } private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata) diff --git a/avahi-sharp/HostNameResolver.cs b/avahi-sharp/HostNameResolver.cs index 0922aa7..81305fd 100644 --- a/avahi-sharp/HostNameResolver.cs +++ b/avahi-sharp/HostNameResolver.cs @@ -23,6 +23,7 @@ using System; using System.Collections; using System.Net; using System.Runtime.InteropServices; +using System.Text; using Mono.Unix; namespace Avahi @@ -51,7 +52,7 @@ namespace Avahi [DllImport ("avahi-client")] private static extern IntPtr avahi_host_name_resolver_new (IntPtr client, int iface, Protocol proto, - IntPtr hostname, Protocol aproto, LookupFlags flags, + byte[] hostname, Protocol aproto, LookupFlags flags, HostNameResolverCallback cb, IntPtr userdata); [DllImport ("avahi-client")] @@ -125,14 +126,14 @@ namespace Avahi (foundListeners.Count == 0 && timeoutListeners.Count == 0)) return; - IntPtr hostPtr = Utility.StringToPtr (hostname); - lock (client) { - handle = avahi_host_name_resolver_new (client.Handle, iface, proto, hostPtr, aproto, flags, + handle = avahi_host_name_resolver_new (client.Handle, iface, proto, + Utility.StringToBytes (hostname), aproto, flags, cb, IntPtr.Zero); + + if (handle == IntPtr.Zero) + client.ThrowError (); } - - Utility.Free (hostPtr); } private void Stop (bool force) diff --git a/avahi-sharp/RecordBrowser.cs b/avahi-sharp/RecordBrowser.cs index 5a736bd..f65f8ac 100644 --- a/avahi-sharp/RecordBrowser.cs +++ b/avahi-sharp/RecordBrowser.cs @@ -23,6 +23,7 @@ using System; using System.Net; using System.Collections; using System.Runtime.InteropServices; +using System.Text; namespace Avahi { @@ -93,7 +94,7 @@ namespace Avahi [DllImport ("avahi-client")] private static extern IntPtr avahi_record_browser_new (IntPtr client, int iface, Protocol proto, - IntPtr name, ushort clazz, ushort type, + byte[] name, ushort clazz, ushort type, LookupFlags flags, RecordBrowserCallback cb, IntPtr userdata); @@ -164,14 +165,13 @@ namespace Avahi (addListeners.Count == 0 && removeListeners.Count == 0)) return; - IntPtr namePtr = Utility.StringToPtr (name); - lock (client) { - handle = avahi_record_browser_new (client.Handle, iface, proto, namePtr, (ushort) clazz, (ushort) type, - flags, cb, IntPtr.Zero); - } + handle = avahi_record_browser_new (client.Handle, iface, proto, Utility.StringToBytes (name), + (ushort) clazz, (ushort) type, flags, cb, IntPtr.Zero); - Utility.Free (namePtr); + if (handle == IntPtr.Zero) + client.ThrowError (); + } } private void Stop (bool force) diff --git a/avahi-sharp/ServiceBrowser.cs b/avahi-sharp/ServiceBrowser.cs index 639dd0a..520e339 100644 --- a/avahi-sharp/ServiceBrowser.cs +++ b/avahi-sharp/ServiceBrowser.cs @@ -23,6 +23,7 @@ using System; using System.Net; using System.Collections; using System.Runtime.InteropServices; +using System.Text; namespace Avahi { @@ -79,8 +80,8 @@ namespace Avahi private ArrayList removeListeners = new ArrayList (); [DllImport ("avahi-client")] - private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, IntPtr type, - IntPtr domain, LookupFlags flags, + private static extern IntPtr avahi_service_browser_new (IntPtr client, int iface, int proto, byte[] type, + byte[] domain, LookupFlags flags, ServiceBrowserCallback cb, IntPtr userdata); @@ -152,15 +153,14 @@ namespace Avahi (addListeners.Count == 0 && removeListeners.Count == 0)) return; - IntPtr domainPtr = Utility.StringToPtr (domain); - IntPtr typePtr = Utility.StringToPtr (type); - lock (client) { - handle = avahi_service_browser_new (client.Handle, iface, (int) proto, typePtr, domainPtr, flags, - cb, IntPtr.Zero); + handle = avahi_service_browser_new (client.Handle, iface, (int) proto, + Utility.StringToBytes (type), Utility.StringToBytes (domain), + flags, cb, IntPtr.Zero); + + if (handle == IntPtr.Zero) + client.ThrowError (); } - Utility.Free (domainPtr); - Utility.Free (typePtr); } private void Stop (bool force) diff --git a/avahi-sharp/ServiceResolver.cs b/avahi-sharp/ServiceResolver.cs index 8dfe520..945372b 100644 --- a/avahi-sharp/ServiceResolver.cs +++ b/avahi-sharp/ServiceResolver.cs @@ -23,6 +23,7 @@ using System; using System.Collections; using System.Net; using System.Runtime.InteropServices; +using System.Text; using Mono.Unix; namespace Avahi @@ -53,7 +54,7 @@ namespace Avahi [DllImport ("avahi-client")] private static extern IntPtr avahi_service_resolver_new (IntPtr client, int iface, Protocol proto, - IntPtr name, IntPtr type, IntPtr domain, + byte[] name, byte[] type, byte[] domain, Protocol aproto, LookupFlags flags, ServiceResolverCallback cb, IntPtr userdata); @@ -145,18 +146,14 @@ namespace Avahi (foundListeners.Count == 0 && timeoutListeners.Count == 0)) return; - IntPtr namePtr = Utility.StringToPtr (name); - IntPtr typePtr = Utility.StringToPtr (type); - IntPtr domainPtr = Utility.StringToPtr (domain); - lock (client) { - handle = avahi_service_resolver_new (client.Handle, iface, proto, namePtr, typePtr, domainPtr, - aproto, flags, cb, IntPtr.Zero); + handle = avahi_service_resolver_new (client.Handle, iface, proto, + Utility.StringToBytes (name), Utility.StringToBytes (type), + Utility.StringToBytes (domain), aproto, flags, cb, IntPtr.Zero); + + if (handle == IntPtr.Zero) + client.ThrowError (); } - - Utility.Free (namePtr); - Utility.Free (typePtr); - Utility.Free (domainPtr); } private void Stop (bool force) diff --git a/avahi-sharp/ServiceTypeBrowser.cs b/avahi-sharp/ServiceTypeBrowser.cs index dbfff73..3796d60 100644 --- a/avahi-sharp/ServiceTypeBrowser.cs +++ b/avahi-sharp/ServiceTypeBrowser.cs @@ -22,6 +22,7 @@ using System; using System.Collections; using System.Runtime.InteropServices; +using System.Text; namespace Avahi { @@ -71,7 +72,7 @@ namespace Avahi [DllImport ("avahi-client")] private static extern IntPtr avahi_service_type_browser_new (IntPtr client, int iface, int proto, - IntPtr domain, LookupFlags flags, + byte[] domain, LookupFlags flags, ServiceTypeBrowserCallback cb, IntPtr userdata); @@ -143,10 +144,12 @@ namespace Avahi return; lock (client) { - IntPtr domainPtr = Utility.StringToPtr (domain); - handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, domainPtr, flags, + handle = avahi_service_type_browser_new (client.Handle, iface, (int) proto, + Utility.StringToBytes (domain), flags, cb, IntPtr.Zero); - Utility.Free (domainPtr); + + if (handle == IntPtr.Zero) + client.ThrowError (); } } diff --git a/avahi-sharp/Utility.cs b/avahi-sharp/Utility.cs index f8b4ffd..b15161c 100644 --- a/avahi-sharp/Utility.cs +++ b/avahi-sharp/Utility.cs @@ -60,12 +60,20 @@ namespace Avahi return ret; } - public static IntPtr StringToPtr (string str) + public static byte[] StringToBytes (string str) + { + if (str == null) + return null; + + return Encoding.UTF8.GetBytes (str); + } + + private static IntPtr StringToPtr (string str) { if (str == null) return IntPtr.Zero; - byte[] bytes = Encoding.UTF8.GetBytes (str); + byte[] bytes = Utility.StringToBytes (str); IntPtr buf = Stdlib.malloc ((uint) bytes.Length + 1); Marshal.Copy (bytes, 0, buf, bytes.Length); Marshal.WriteByte (buf, bytes.Length, 0);