diff options
| author | James Willcox <snopr@snorp.net> | 2005-10-26 03:20:44 +0000 | 
|---|---|---|
| committer | James Willcox <snopr@snorp.net> | 2005-10-26 03:20:44 +0000 | 
| commit | 94d2753047cff1e9223e42736884e4d51348b45a (patch) | |
| tree | 5d4173121818486d3adaa3c3b357b2a6f893860c | |
| parent | 58dbdd3113ff75b7262ac2e7a8474550a2dacfd3 (diff) | |
first pass at updating to the 0.6 apis
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@876 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
| -rw-r--r-- | avahi-sharp/AddressResolver.cs | 17 | ||||
| -rw-r--r-- | avahi-sharp/AvahiTest.cs | 207 | ||||
| -rw-r--r-- | avahi-sharp/BrowserBase.cs | 4 | ||||
| -rw-r--r-- | avahi-sharp/Client.cs | 50 | ||||
| -rw-r--r-- | avahi-sharp/ClientException.cs | 64 | ||||
| -rw-r--r-- | avahi-sharp/EntryGroup.cs | 32 | ||||
| -rw-r--r-- | avahi-sharp/HostNameResolver.cs | 11 | ||||
| -rw-r--r-- | avahi-sharp/ResolverBase.cs | 19 | ||||
| -rw-r--r-- | avahi-sharp/ServiceResolver.cs | 10 | ||||
| -rw-r--r-- | avahi-sharp/Utility.cs | 13 | 
10 files changed, 231 insertions, 196 deletions
diff --git a/avahi-sharp/AddressResolver.cs b/avahi-sharp/AddressResolver.cs index 01211ae..3231cbf 100644 --- a/avahi-sharp/AddressResolver.cs +++ b/avahi-sharp/AddressResolver.cs @@ -29,7 +29,7 @@ namespace Avahi  {      internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto, -                                                    ResolverEvent revent, Protocol aproto, IntPtr address, +                                                    ResolverEvent revent, IntPtr address,                                                      IntPtr hostname, LookupResultFlags flags, IntPtr userdata);      public delegate void HostAddressHandler (object o, string host, IPAddress address); @@ -124,7 +124,7 @@ namespace Avahi                  (foundListeners.Count == 0 && timeoutListeners.Count == 0))                  return; -            IntPtr addrPtr = Utility.StringToPtr (address.ToString ()); +            IntPtr addrPtr = Utility.AddressToPtr (address);              lock (client) {                  handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr, flags, @@ -147,7 +147,7 @@ namespace Avahi          }          private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto, -                                                ResolverEvent revent, Protocol aproto, IntPtr address, +                                                ResolverEvent revent, IntPtr address,                                                  IntPtr hostname, LookupResultFlags flags, IntPtr userdata)          {              switch (revent) { @@ -158,15 +158,8 @@ namespace Avahi                  foreach (HostAddressHandler handler in foundListeners)                      handler (this, currentHost, currentAddress);                  break; -            case ResolverEvent.Timeout: -                currentAddress = null; -                currentHost = null; -                 -                foreach (EventHandler handler in timeoutListeners) -                    handler (this, new EventArgs ()); -                break; -            default: -                EmitResolverEvent (revent); +            case ResolverEvent.Failure: +                EmitFailure (client.LastError);                  break;              }          } diff --git a/avahi-sharp/AvahiTest.cs b/avahi-sharp/AvahiTest.cs index 395d5cb..9c4aeca 100644 --- a/avahi-sharp/AvahiTest.cs +++ b/avahi-sharp/AvahiTest.cs @@ -1,109 +1,122 @@  /* $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. +    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.Text;  using System.Net;  using Avahi;  public class AvahiTest { -	private static Client client; - -	public static void Main () { -		client = new Client (); - -		EntryGroup eg = new EntryGroup (client); -		eg.StateChanged += OnEntryGroupChanged; -		eg.AddService ("foobar2", "_daap._tcp", client.DomainName, -			       444, new string[] { "foo", "bar", "baz" }); -		eg.Commit (); -		Console.WriteLine ("Press enter to quit"); -		Console.ReadLine (); -	} - -	private static void OnEntryGroupChanged (object o, EntryGroupState state) -	{ -		Console.WriteLine ("Entry group status: " + state); - -		/* -		if (state == EntryGroupState.Established) { -			DomainBrowser browser = new DomainBrowser (client); -			browser.DomainAdded += OnDomainAdded; -		} -		*/ - -		BrowseServiceTypes ("dns-sd.org"); -	} - -	private static void OnDomainAdded (object o, DomainInfo info) -	{ -		Console.WriteLine ("Got domain added: " + info.Domain); -		BrowseServiceTypes (info.Domain); -	} - -	private static void BrowseServiceTypes (string domain) -	{ -		ServiceTypeBrowser stb = new ServiceTypeBrowser (client, domain); -		stb.CacheExhausted += OnCacheExhausted; -		stb.ServiceTypeAdded += OnServiceTypeAdded; -	} - -	private static void OnCacheExhausted (object o, EventArgs args) -	{ -		Console.WriteLine ("Cache is exhausted"); -	} - -	private static void OnServiceTypeAdded (object o, ServiceTypeInfo info) -	{ -		Console.WriteLine ("Got service type: " + info.ServiceType); -		ServiceBrowser sb = new ServiceBrowser (client, info.ServiceType, info.Domain); -		sb.ServiceAdded += OnServiceAdded; -	} - -	private static void OnServiceAdded (object o, ServiceInfo info) -	{ -		// Console.WriteLine ("Got service: " + info.Name); -		ServiceResolver resolver = new ServiceResolver (client, info); -		resolver.Found += OnServiceResolved; -	} - -	private static void OnServiceResolved (object o, ServiceInfo info) -	{ -		Console.WriteLine ("Service '{0}' at {1}:{2}", info.Name, info.HostName, info.Port); -		foreach (byte[] bytes in info.Text) { -			Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes)); -		} -		AddressResolver ar = new AddressResolver (client, info.Address); -		ar.Found += OnAddressResolved; -	} - -	private static void OnAddressResolved (object o, string host, IPAddress address) -	{ -		Console.WriteLine ("Resolved {0} to {1}", address, host); -		HostNameResolver hr = new HostNameResolver (client, host); -		hr.Found += OnHostNameResolved; -	} - -	private static void OnHostNameResolved (object o, string host, IPAddress address) -	{ -		Console.WriteLine ("Resolved {0} to {1}", host, address); -	} +    private static Client client; +    private static ArrayList objects = new ArrayList (); + +    public static void Main () { +        client = new Client (); + +        EntryGroup eg = new EntryGroup (client); +        eg.StateChanged += OnEntryGroupChanged; +        eg.AddService ("foobar2", "_daap._tcp", client.DomainName, +                       444, new string[] { "foo", "bar", "baz" }); +        eg.Commit (); +        Console.WriteLine ("Press enter to quit"); +        Console.ReadLine (); +    } + +    private static void OnEntryGroupChanged (object o, EntryGroupState state) +    { +        Console.WriteLine ("Entry group status: " + state); +        if (state == EntryGroupState.Established) { +            DomainBrowser browser = new DomainBrowser (client); +            objects.Add (browser); +             +            browser.DomainAdded += OnDomainAdded; +        } +    } + +    private static void OnDomainAdded (object o, DomainInfo info) +    { +        Console.WriteLine ("Got domain added: " + info.Domain); +        BrowseServiceTypes (info.Domain); +    } + +    private static void BrowseServiceTypes (string domain) +    { +        ServiceTypeBrowser stb = new ServiceTypeBrowser (client, domain); +        objects.Add (stb); +         +        stb.CacheExhausted += OnCacheExhausted; +        stb.ServiceTypeAdded += OnServiceTypeAdded; +    } + +    private static void OnCacheExhausted (object o, EventArgs args) +    { +        Console.WriteLine ("Cache is exhausted"); +    } + +    private static void OnServiceTypeAdded (object o, ServiceTypeInfo info) +    { +        Console.WriteLine ("Got service type: " + info.ServiceType); +        ServiceBrowser sb = new ServiceBrowser (client, info.ServiceType, info.Domain); +        objects.Add (sb); +         +        sb.ServiceAdded += OnServiceAdded; +    } + +    private static void OnServiceAdded (object o, ServiceInfo info) +    { +        // Console.WriteLine ("Got service: " + info.Name); +        ServiceResolver resolver = new ServiceResolver (client, info); +        objects.Add (resolver); +        resolver.Found += OnServiceResolved; +    } + +    private static void OnServiceResolved (object o, ServiceInfo info) +    { +        objects.Remove (o); +         +        Console.WriteLine ("Service '{0}' at {1}:{2}", info.Name, info.HostName, info.Port); +        foreach (byte[] bytes in info.Text) { +            Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes)); +        } +        AddressResolver ar = new AddressResolver (client, info.Address); +        objects.Add (ar); +         +        ar.Found += OnAddressResolved; +    } + +    private static void OnAddressResolved (object o, string host, IPAddress address) +    { +        objects.Remove (o); +         +        Console.WriteLine ("Resolved {0} to {1}", address, host); +        HostNameResolver hr = new HostNameResolver (client, host); +        objects.Add (hr); +         +        hr.Found += OnHostNameResolved; +    } + +    private static void OnHostNameResolved (object o, string host, IPAddress address) +    { +        objects.Remove (o); +        Console.WriteLine ("Resolved {0} to {1}", host, address); +    }  } diff --git a/avahi-sharp/BrowserBase.cs b/avahi-sharp/BrowserBase.cs index 2968114..1f1f550 100644 --- a/avahi-sharp/BrowserBase.cs +++ b/avahi-sharp/BrowserBase.cs @@ -41,10 +41,6 @@ namespace Avahi                  if (AllForNow != null)                      AllForNow (this, new EventArgs ());                  break; -            case BrowserEvent.NotFound: -                if (NotFound != null) -                    NotFound (this, new EventArgs ()); -                break;              case BrowserEvent.Failure:                  if (Failed != null)                      Failed (this, new EventArgs ()); diff --git a/avahi-sharp/Client.cs b/avahi-sharp/Client.cs index e148ff3..73b6f57 100644 --- a/avahi-sharp/Client.cs +++ b/avahi-sharp/Client.cs @@ -29,8 +29,6 @@ namespace Avahi  {      internal enum ResolverEvent {          Found, -        Timeout, -        NotFound,          Failure      } @@ -39,7 +37,6 @@ namespace Avahi          Removed,          CacheExhausted,          AllForNow, -        NotFound,          Failure      } @@ -66,8 +63,7 @@ namespace Avahi      public enum LookupFlags {          None = 0,          UseWideArea = 1, -        UseMulticast = 2, -        NoTxt = 4, +        UseMulticast = 4,          NoAddress = 8      } @@ -76,7 +72,9 @@ namespace Avahi          None = 0,          Cached = 1,          WideArea = 2, -        Multicast = 4 +        Multicast = 4, +        Local = 8, +        OurOwn = 16,      }      public class Client : IDisposable @@ -123,7 +121,7 @@ namespace Avahi          private static extern void avahi_simple_poll_free (IntPtr spoll);          [DllImport ("avahi-common")] -        private static extern int avahi_simple_poll_iterate (IntPtr spoll, int timeout); +        private static extern int avahi_simple_poll_loop (IntPtr spoll);          [DllImport ("avahi-common")]          private static extern void avahi_simple_poll_set_func (IntPtr spoll, PollCallback cb); @@ -134,10 +132,6 @@ namespace Avahi          [DllImport ("avahi-client")]          private static extern uint avahi_client_get_local_service_cookie (IntPtr client); -        [DllImport ("avahi-client")] -        private static extern int avahi_client_is_service_local (IntPtr client, int iface, Protocol proto, -                                                                 IntPtr name, IntPtr type, IntPtr domain); -          [DllImport ("libc")]          private static extern int poll(IntPtr ufds, uint nfds, int timeout); @@ -203,11 +197,11 @@ namespace Avahi              }          } -        internal int LastError +        internal ErrorCode LastError          {              get {                  lock (this) { -                    return avahi_client_errno (handle); +                    return (ErrorCode) avahi_client_errno (handle);                  }              }          } @@ -250,32 +244,11 @@ namespace Avahi              }          } -        public bool IsServiceLocal (ServiceInfo service) -        { -            return IsServiceLocal (service.NetworkInterface, service.Protocol, service.Name, -                                   service.ServiceType, service.Domain); -        } - -        public bool IsServiceLocal (int iface, Protocol proto, string name, string type, string domain) -        { -            IntPtr namePtr = Utility.StringToPtr (name); -            IntPtr typePtr = Utility.StringToPtr (type); -            IntPtr domainPtr = Utility.StringToPtr (domain); -             -            int result = avahi_client_is_service_local (handle, iface, proto, namePtr, typePtr, domainPtr); - -            Utility.Free (namePtr); -            Utility.Free (typePtr); -            Utility.Free (domainPtr); - -            return result == 1; -        } -          internal void CheckError ()          { -            int error = LastError; +            ErrorCode error = LastError; -            if (error != 0) +            if (error != ErrorCode.Ok)                  throw new ClientException (error);          } @@ -295,10 +268,7 @@ namespace Avahi          private void PollLoop () {              try {                  lock (this) { -                    while (true) { -                        if (avahi_simple_poll_iterate (spoll, -1) != 0) -                            break; -                    } +                    avahi_simple_poll_loop (spoll);                  }              } catch (ThreadAbortException e) {              } catch (Exception e) { diff --git a/avahi-sharp/ClientException.cs b/avahi-sharp/ClientException.cs index 2abc57c..3ee0cab 100644 --- a/avahi-sharp/ClientException.cs +++ b/avahi-sharp/ClientException.cs @@ -25,24 +25,78 @@ using System.Runtime.InteropServices;  namespace Avahi  { +    public enum ErrorCode { +        Ok = 0, +        Failure = -1, +        BadState = -2, +        InvalidHostName = - 3, +        InvalidDomainName = -4, +        NoNetwork = -5, +        InvalidTTL = -6, +        IsPattern = -7, +        Collision = -8, +        InvalidRecord = -9, +        InvalidServiceName = -10, +        InvalidServiceType = -11, +        InvalidPort = -12, +        InvalidKey = -13, +        InvalidAddress = -14, +        Timeout = -15, +        TooManyClients = -16, +        TooManyObjects = -17, +        TooManyEntries = -18, +        OS = -19, +        AccessDenied = -20, +        InvalidOperation = -21, +        DBusError = -22, +        NotConnected = -23, +        NoMemory = -24, +        InvalidObject = -25, +        NoDaemon = -26, +        InvalidInterface = -27, +        InvalidProtocol = -28, +        InvalidFlags = -29, +        NotFound = -30, +        InvalidConfig = -31, +        VersionMismatch = -32, +        InvalidServiceSubtype = -33, +        InvalidPacket = -34, +        InvalidDnsError = -35, +        DnsFormErr = -36, +        DnsServFail = -37, +        DnsNxDomain = -38, +        DnsNoTimp = -39, +        DnsRefused = -40, +        DnsYxDomain = -41, +        DnsYxRrSet = -42, +        DnsNxRrSet = -43, +        DnsNotAuth = -44, +        DnsNotZone = -45 +    } + +    public delegate void ErrorCodeHandler (object o, ErrorCode code); +          public class ClientException : ApplicationException      { -        private int code; +        private ErrorCode code;          [DllImport ("avahi-common")] -        private static extern IntPtr avahi_strerror (int code); +        private static extern IntPtr avahi_strerror (ErrorCode code); -        public int ErrorCode +        public ErrorCode ErrorCode          {              get { return code; }          } + +        internal ClientException (int code) : this ((ErrorCode) code) { +        } -        internal ClientException (int code) : base (GetErrorString (code)) +        internal ClientException (ErrorCode code) : base (GetErrorString (code))          {              this.code = code;          } -        private static string GetErrorString (int code) +        private static string GetErrorString (ErrorCode code)          {              IntPtr str = avahi_strerror (code);              return Utility.PtrToString (str); diff --git a/avahi-sharp/EntryGroup.cs b/avahi-sharp/EntryGroup.cs index 85c9a1d..4373178 100644 --- a/avahi-sharp/EntryGroup.cs +++ b/avahi-sharp/EntryGroup.cs @@ -26,11 +26,24 @@ using System.Runtime.InteropServices;  namespace Avahi  { +    [Flags] +    public enum PublishFlags { +        None = 0, +        Unique = 1, +        NoProbe = 2, +        NoAnnounce = 4, +        AllowMultiple = 8, +        NoReverse = 16, +        NoCookie = 32, +        Update = 64 +    } +          public enum EntryGroupState {          Uncommited,          Registering,          Established, -        Collision +        Collision, +        Failure      }      internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata); @@ -59,8 +72,9 @@ namespace Avahi          [DllImport ("avahi-client")]          private static extern void avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto, -                                                                         IntPtr name, IntPtr type, IntPtr domain, -                                                                         IntPtr host, UInt16 port, IntPtr strlst); +                                                                         PublishFlags flags, IntPtr name, IntPtr type, +                                                                         IntPtr domain, IntPtr host, UInt16 port, +                                                                         IntPtr strlst);          [DllImport ("avahi-client")]          private static extern void avahi_entry_group_free (IntPtr group); @@ -142,10 +156,16 @@ namespace Avahi          public void AddService (string name, string type, string domain,                                  UInt16 port, params string[] txt)          { -            AddService (-1, Protocol.Unspecified, name, type, domain, null, port, txt); +            AddService (PublishFlags.None, name, type, domain, port, txt); +        } + +        public void AddService (PublishFlags flags, string name, string type, string domain, +                                UInt16 port, params string[] txt) +        { +            AddService (-1, Protocol.Unspecified, flags, name, type, domain, null, port, txt);          } -        public void AddService (int iface, Protocol proto, string name, string type, string domain, +        public void AddService (int iface, Protocol proto, PublishFlags flags, string name, string type, string domain,                                  string host, UInt16 port, params string[] txt)          {              IntPtr list = avahi_string_list_new (IntPtr.Zero); @@ -164,7 +184,7 @@ namespace Avahi              IntPtr hostPtr = Utility.StringToPtr (host);              lock (client) { -                avahi_entry_group_add_service_strlst (handle, iface, proto, namePtr, typePtr, domainPtr, +                avahi_entry_group_add_service_strlst (handle, iface, proto, flags, namePtr, typePtr, domainPtr,                                                        hostPtr, port, list);              } diff --git a/avahi-sharp/HostNameResolver.cs b/avahi-sharp/HostNameResolver.cs index 44febcd..9780811 100644 --- a/avahi-sharp/HostNameResolver.cs +++ b/avahi-sharp/HostNameResolver.cs @@ -159,15 +159,8 @@ namespace Avahi                  foreach (HostAddressHandler handler in foundListeners)                      handler (this, currentHost, currentAddress);                  break; -            case ResolverEvent.Timeout: -                currentAddress = null; -                currentHost = null; -                 -                foreach (EventHandler handler in timeoutListeners) -                    handler (this, new EventArgs ()); -                break; -            default: -                EmitResolverEvent (revent); +            case ResolverEvent.Failure: +                EmitFailure (client.LastError);                  break;              }          } diff --git a/avahi-sharp/ResolverBase.cs b/avahi-sharp/ResolverBase.cs index 0fdd4a1..5d375b3 100644 --- a/avahi-sharp/ResolverBase.cs +++ b/avahi-sharp/ResolverBase.cs @@ -25,23 +25,12 @@ namespace Avahi  {      public abstract class ResolverBase      { -        public event EventHandler NotFound; -        public event EventHandler Failed; +        public event ErrorCodeHandler Failed; -        internal void EmitResolverEvent (ResolverEvent revent) +        internal void EmitFailure (ErrorCode code)          { -            switch (revent) { -            case ResolverEvent.NotFound: -                if (NotFound != null) -                    NotFound (this, new EventArgs ()); -                break; -            case ResolverEvent.Failure: -                if (Failed != null) -                    Failed (this, new EventArgs ()); -                break; -            default: -                break; -            } +            if (Failed != null) +                Failed (this, code);          }      }  } diff --git a/avahi-sharp/ServiceResolver.cs b/avahi-sharp/ServiceResolver.cs index 7527652..4a6a8a0 100644 --- a/avahi-sharp/ServiceResolver.cs +++ b/avahi-sharp/ServiceResolver.cs @@ -207,14 +207,8 @@ namespace Avahi                  foreach (ServiceInfoHandler handler in foundListeners)                      handler (this, info);                  break; -            case ResolverEvent.Timeout: -                currentInfo = ServiceInfo.Zero; -                 -                foreach (EventHandler handler in timeoutListeners) -                    handler (this, new EventArgs ()); -                break; -            default: -                EmitResolverEvent (revent); +            case ResolverEvent.Failure: +                EmitFailure (client.LastError);                  break;              }          } diff --git a/avahi-sharp/Utility.cs b/avahi-sharp/Utility.cs index 55254eb..f8b4ffd 100644 --- a/avahi-sharp/Utility.cs +++ b/avahi-sharp/Utility.cs @@ -36,6 +36,9 @@ namespace Avahi          [DllImport ("avahi-common")]          private static extern IntPtr avahi_address_snprint (IntPtr buf, int size, IntPtr address); +        [DllImport ("avahi-common")] +        private static extern IntPtr avahi_address_parse (IntPtr str, Protocol proto, IntPtr ret); +          public static string PtrToString (IntPtr ptr)          {              if (ptr == IntPtr.Zero) @@ -74,6 +77,16 @@ namespace Avahi              Stdlib.free (ptr);          } +        public static IntPtr AddressToPtr (IPAddress address) +        { +            IntPtr straddr = Utility.StringToPtr (address.ToString ()); +            IntPtr addrPtr = Stdlib.malloc (32); +            avahi_address_parse (straddr, Protocol.Unspecified, addrPtr); +            Utility.Free (straddr); + +            return addrPtr; +        } +          public static IPAddress PtrToAddress (IntPtr ptr)          {              IPAddress address = null;  | 
