]> git.meshlink.io Git - catta/commitdiff
* Add RecordBrowser.cs
authorJames Willcox <snopr@snorp.net>
Thu, 17 Nov 2005 17:55:09 +0000 (17:55 +0000)
committerJames Willcox <snopr@snorp.net>
Thu, 17 Nov 2005 17:55:09 +0000 (17:55 +0000)
* Make all of the events like "standard" .NET ones

git-svn-id: file:///home/lennart/svn/public/avahi/trunk@996 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

13 files changed:
avahi-sharp/AddressResolver.cs
avahi-sharp/AvahiTest.cs
avahi-sharp/Client.cs
avahi-sharp/ClientException.cs
avahi-sharp/DomainBrowser.cs
avahi-sharp/EntryGroup.cs
avahi-sharp/HostNameResolver.cs
avahi-sharp/Makefile.am
avahi-sharp/RecordBrowser.cs [new file with mode: 0644]
avahi-sharp/ResolverBase.cs
avahi-sharp/ServiceBrowser.cs
avahi-sharp/ServiceResolver.cs
avahi-sharp/ServiceTypeBrowser.cs

index 3231cbf492efa6c6f404890af982adaadfe02e16..63dc33e74228932a6ae1ac8d88bdebe82cde7911 100644 (file)
@@ -32,7 +32,29 @@ namespace Avahi
                                                     ResolverEvent revent, IntPtr address,
                                                     IntPtr hostname, LookupResultFlags flags, IntPtr userdata);
 
-    public delegate void HostAddressHandler (object o, string host, IPAddress address);
+    public delegate void HostAddressHandler (object o, HostAddressArgs args);
+
+    public class HostAddressArgs : EventArgs
+    {
+        private string host;
+        private IPAddress address;
+
+        public string Host
+        {
+            get { return host; }
+        }
+
+        public IPAddress Address
+        {
+            get { return address; }
+        }
+
+        public HostAddressArgs (string host, IPAddress address)
+        {
+            this.host = host;
+            this.address = address;
+        }
+    }
     
     public class AddressResolver : ResolverBase, IDisposable
     {
@@ -156,7 +178,7 @@ namespace Avahi
                 currentHost = Utility.PtrToString (hostname);
 
                 foreach (HostAddressHandler handler in foundListeners)
-                    handler (this, currentHost, currentAddress);
+                    handler (this, new HostAddressArgs (currentHost, currentAddress));
                 break;
             case ResolverEvent.Failure:
                 EmitFailure (client.LastError);
index 9c4aeca50fbcc96bd9353bf39ef7d7d9e43d06a2..0fac04b09c320a709b544bb1725e04999dd1f15f 100644 (file)
@@ -41,10 +41,10 @@ public class AvahiTest {
         Console.ReadLine ();
     }
 
-    private static void OnEntryGroupChanged (object o, EntryGroupState state)
+    private static void OnEntryGroupChanged (object o, EntryGroupStateArgs args)
     {
-        Console.WriteLine ("Entry group status: " + state);
-        if (state == EntryGroupState.Established) {
+        Console.WriteLine ("Entry group status: " + args.State);
+        if (args.State == EntryGroupState.Established) {
             DomainBrowser browser = new DomainBrowser (client);
             objects.Add (browser);
             
@@ -52,10 +52,10 @@ public class AvahiTest {
         }
     }
 
-    private static void OnDomainAdded (object o, DomainInfo info)
+    private static void OnDomainAdded (object o, DomainInfoArgs args)
     {
-        Console.WriteLine ("Got domain added: " + info.Domain);
-        BrowseServiceTypes (info.Domain);
+        Console.WriteLine ("Got domain added: " + args.Domain.Domain);
+        BrowseServiceTypes (args.Domain.Domain);
     }
 
     private static void BrowseServiceTypes (string domain)
@@ -72,51 +72,51 @@ public class AvahiTest {
         Console.WriteLine ("Cache is exhausted");
     }
 
-    private static void OnServiceTypeAdded (object o, ServiceTypeInfo info)
+    private static void OnServiceTypeAdded (object o, ServiceTypeInfoArgs args)
     {
-        Console.WriteLine ("Got service type: " + info.ServiceType);
-        ServiceBrowser sb = new ServiceBrowser (client, info.ServiceType, info.Domain);
+        Console.WriteLine ("Got service type: " + args.ServiceType.ServiceType);
+        ServiceBrowser sb = new ServiceBrowser (client, args.ServiceType.ServiceType, args.ServiceType.Domain);
         objects.Add (sb);
         
         sb.ServiceAdded += OnServiceAdded;
     }
 
-    private static void OnServiceAdded (object o, ServiceInfo info)
+    private static void OnServiceAdded (object o, ServiceInfoArgs args)
     {
         // Console.WriteLine ("Got service: " + info.Name);
-        ServiceResolver resolver = new ServiceResolver (client, info);
+        ServiceResolver resolver = new ServiceResolver (client, args.Service);
         objects.Add (resolver);
         resolver.Found += OnServiceResolved;
     }
 
-    private static void OnServiceResolved (object o, ServiceInfo info)
+    private static void OnServiceResolved (object o, ServiceInfoArgs args)
     {
         objects.Remove (o);
         
-        Console.WriteLine ("Service '{0}' at {1}:{2}", info.Name, info.HostName, info.Port);
-        foreach (byte[] bytes in info.Text) {
+        Console.WriteLine ("Service '{0}' at {1}:{2}", args.Service.Name, args.Service.HostName, args.Service.Port);
+        foreach (byte[] bytes in args.Service.Text) {
             Console.WriteLine ("Text: " + Encoding.UTF8.GetString (bytes));
         }
-        AddressResolver ar = new AddressResolver (client, info.Address);
+        AddressResolver ar = new AddressResolver (client, args.Service.Address);
         objects.Add (ar);
         
         ar.Found += OnAddressResolved;
     }
 
-    private static void OnAddressResolved (object o, string host, IPAddress address)
+    private static void OnAddressResolved (object o, HostAddressArgs args)
     {
         objects.Remove (o);
         
-        Console.WriteLine ("Resolved {0} to {1}", address, host);
-        HostNameResolver hr = new HostNameResolver (client, host);
+        Console.WriteLine ("Resolved {0} to {1}", args.Address, args.Host);
+        HostNameResolver hr = new HostNameResolver (client, args.Host);
         objects.Add (hr);
         
         hr.Found += OnHostNameResolved;
     }
 
-    private static void OnHostNameResolved (object o, string host, IPAddress address)
+    private static void OnHostNameResolved (object o, HostAddressArgs args)
     {
         objects.Remove (o);
-        Console.WriteLine ("Resolved {0} to {1}", host, address);
+        Console.WriteLine ("Resolved {0} to {1}", args.Host, args.Address);
     }
 }
index 394efc58e22100db7790d83782927e1815a923f2..01b08bd5934c9e8eae4c126548151ed579a80f82 100644 (file)
@@ -43,8 +43,23 @@ namespace Avahi
     internal delegate int PollCallback (IntPtr ufds, uint nfds, int timeout);
     internal delegate void ClientCallback (IntPtr client, ClientState state, IntPtr userData);
 
-    public delegate void ClientStateHandler (object o, ClientState state);
+    public delegate void ClientStateHandler (object o, ClientStateArgs state);
 
+    public class ClientStateArgs : EventArgs
+    {
+        private ClientState state;
+
+        public ClientState State
+        {
+            get { return state; }
+        }
+
+        public ClientStateArgs (ClientState state)
+        {
+            this.state = state;
+        }
+    }
+    
     public enum Protocol {
         Unspecified = -1,
         IPv4 = 0,
@@ -268,7 +283,7 @@ namespace Avahi
         private void OnClientCallback (IntPtr client, ClientState state, IntPtr userData)
         {
             if (StateChanged != null)
-                StateChanged (this, state);
+                StateChanged (this, new ClientStateArgs (state));
         }
 
         private int OnPollCallback (IntPtr ufds, uint nfds, int timeout) {
index 11c4e63b9c849ea778d47866446e0b78f3a13579..fadcce91f2a18241b7718eb6cc638fd09304ba29 100644 (file)
@@ -79,7 +79,22 @@ namespace Avahi
         NotPermitted = -50
     }
 
-    public delegate void ErrorCodeHandler (object o, ErrorCode code);
+    public delegate void ErrorCodeHandler (object o, ErrorCodeArgs args);
+
+    public class ErrorCodeArgs : EventArgs
+    {
+        private ErrorCode code;
+
+        public ErrorCode ErrorCode
+        {
+            get { return code; }
+        }
+
+        public ErrorCodeArgs (ErrorCode code)
+        {
+            this.code = code;
+        }
+    }
     
     public class ClientException : ApplicationException
     {
index 08f2b35a08219aca95f6af21d499e19afada55ee..50ac75778db363ac347cac0881b4add0aaaf4ee4 100644 (file)
@@ -44,7 +44,22 @@ namespace Avahi
         public LookupResultFlags Flags;
     }
 
-    public delegate void DomainInfoHandler (object o, DomainInfo info);
+    public class DomainInfoArgs : EventArgs
+    {
+        private DomainInfo domain;
+
+        public DomainInfo Domain
+        {
+            get { return domain; }
+        }
+
+        public DomainInfoArgs (DomainInfo domain)
+        {
+            this.domain = domain;
+        }
+    }
+
+    public delegate void DomainInfoHandler (object o, DomainInfoArgs args);
     
     public class DomainBrowser : BrowserBase, IDisposable
     {
@@ -160,20 +175,18 @@ namespace Avahi
             info.Domain = Utility.PtrToString (domain);
             info.Flags = flags;
 
-            infos.Add (info);
-
             switch (bevent) {
             case BrowserEvent.Added:
                 infos.Add (info);
 
                 foreach (DomainInfoHandler handler in addListeners)
-                    handler (this, info);
+                    handler (this, new DomainInfoArgs (info));
                 break;
             case BrowserEvent.Removed:
                 infos.Remove (info);
 
                 foreach (DomainInfoHandler handler in removeListeners)
-                    handler (this, info);
+                    handler (this, new DomainInfoArgs (info));
                 break;
             default:
                 EmitBrowserEvent (bevent);
index a8744d08ea687470948c77c7aea440feda062c4c..443f198c0d0b0fa60e5caf4c5c21b9a934bc72c6 100644 (file)
@@ -48,8 +48,23 @@ namespace Avahi
         Failure
     }
 
+    public class EntryGroupStateArgs : EventArgs
+    {
+        private EntryGroupState state;
+
+        public EntryGroupState State
+        {
+            get { return state; }
+        }
+        
+        public EntryGroupStateArgs (EntryGroupState state)
+        {
+            this.state = state;
+        }
+    }
+
     internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
-    public delegate void EntryGroupStateHandler (object o, EntryGroupState state);
+    public delegate void EntryGroupStateHandler (object o, EntryGroupStateArgs args);
     
     public class EntryGroup : IDisposable
     {
@@ -208,7 +223,7 @@ namespace Avahi
         private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
         {
             if (StateChanged != null)
-                StateChanged (this, state);
+                StateChanged (this, new EntryGroupStateArgs (state));
         }
     }
 }
index 9780811408a1fb3b64567a189e31c8cffd4163a0..0922aa787c1339b34c9f1014fd1c3703b6413904 100644 (file)
@@ -157,7 +157,7 @@ namespace Avahi
                 currentHost = Utility.PtrToString (hostname);
 
                 foreach (HostAddressHandler handler in foundListeners)
-                    handler (this, currentHost, currentAddress);
+                    handler (this, new HostAddressArgs (currentHost, currentAddress));
                 break;
             case ResolverEvent.Failure:
                 EmitFailure (client.LastError);
index 80fd06e9a8ab657b26c8e461b8b266cfa99a14e9..778f8826b923aec5e69a3f89eba0f994b47b079f 100644 (file)
@@ -30,6 +30,7 @@ AVAHISOURCES =                                \
        $(srcdir)/DomainBrowser.cs      \
        $(srcdir)/EntryGroup.cs         \
        $(srcdir)/HostNameResolver.cs   \
+       $(srcdir)/RecordBrowser.cs      \
        $(srcdir)/ResolverBase.cs       \
        $(srcdir)/ServiceBrowser.cs     \
        $(srcdir)/ServiceResolver.cs    \
diff --git a/avahi-sharp/RecordBrowser.cs b/avahi-sharp/RecordBrowser.cs
new file mode 100644 (file)
index 0000000..5a736bd
--- /dev/null
@@ -0,0 +1,227 @@
+/* $Id: ServiceBrowser.cs 635 2005-09-26 03:57:30Z snorp $ */
+
+/***
+  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.Net;
+using System.Collections;
+using System.Runtime.InteropServices;
+
+namespace Avahi
+{
+
+    public delegate void RecordInfoHandler (object o, RecordInfoArgs args);
+    
+    internal delegate void RecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
+                                                  IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
+                                                  LookupResultFlags flags, IntPtr userdata);
+
+    public enum RecordClass {
+        In = 1
+    }
+
+    public enum RecordType {
+        A = 1,
+        Ns = 2,
+        Cname = 5,
+        Soa = 6,
+        Ptr = 12,
+        Hinfo = 13,
+        Mx = 15,
+        Txt = 16,
+        Aaa = 28,
+        Srv = 33
+    }
+    
+    public struct RecordInfo
+    {
+        public int NetworkInterface;
+        public Protocol Protocol;
+        public string Name;
+        public RecordClass Class;
+        public RecordType Type;
+        public byte[] Data;
+        public LookupResultFlags Flags;
+    }
+
+    public class RecordInfoArgs : EventArgs
+    {
+        private RecordInfo record;
+
+        public RecordInfo Record {
+            get { return record; }
+        }
+
+        public RecordInfoArgs (RecordInfo record)
+        {
+            this.record = record;
+        }
+    }
+
+    public class RecordBrowser : BrowserBase, IDisposable
+    {
+        private IntPtr handle;
+        private ArrayList infos = new ArrayList ();
+        private Client client;
+        private int iface;
+        private Protocol proto;
+        private string name;
+        private RecordClass clazz;
+        private RecordType type;
+        private LookupFlags flags;
+        private RecordBrowserCallback cb;
+
+        private ArrayList addListeners = new ArrayList ();
+        private ArrayList removeListeners = new ArrayList ();
+        
+        [DllImport ("avahi-client")]
+        private static extern IntPtr avahi_record_browser_new (IntPtr client, int iface, Protocol proto,
+                                                               IntPtr name, ushort clazz, ushort type,
+                                                               LookupFlags flags, RecordBrowserCallback cb,
+                                                               IntPtr userdata);
+
+
+        [DllImport ("avahi-client")]
+        private static extern void avahi_record_browser_free (IntPtr handle);
+
+        public event RecordInfoHandler RecordAdded
+        {
+            add {
+                addListeners.Add (value);
+                Start ();
+            }
+            remove {
+                addListeners.Remove (value);
+                Stop (false);
+            }
+        }
+        
+        public event RecordInfoHandler RecordRemoved
+        {
+            add {
+                removeListeners.Add (value);
+                Start ();
+            }
+            remove {
+                removeListeners.Remove (value);
+                Stop (false);
+            }
+        }
+
+        public RecordInfo[] Records
+        {
+            get { return (RecordInfo[]) infos.ToArray (typeof (RecordInfo)); }
+        }
+
+        public RecordBrowser (Client client, string name, RecordType type) :
+            this (client, -1, Protocol.Unspecified, name, RecordClass.In, type, LookupFlags.None)
+        {
+        }
+        
+        public RecordBrowser (Client client, int iface, Protocol proto, string name, RecordClass clazz,
+                              RecordType type, LookupFlags flags)
+        {
+            this.client = client;
+            this.iface = iface;
+            this.proto = proto;
+            this.name = name;
+            this.clazz = clazz;
+            this.type = type;
+            this.flags = flags;
+            cb = OnRecordBrowserCallback;
+        }
+
+        ~RecordBrowser ()
+        {
+            Dispose ();
+        }
+
+        public void Dispose ()
+        {
+            Stop (true);
+        }
+
+        private void Start ()
+        {
+            if (client.Handle == IntPtr.Zero || handle != IntPtr.Zero ||
+                (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);
+            }
+
+            Utility.Free (namePtr);
+        }
+
+        private void Stop (bool force)
+        {
+            if (client.Handle != IntPtr.Zero && handle != IntPtr.Zero &&
+                (force || (addListeners.Count == 0 && removeListeners.Count == 0))) {
+                
+                lock (client) {
+                    avahi_record_browser_free (handle);
+                    handle = IntPtr.Zero;
+                }
+            }
+        }
+
+        private void OnRecordBrowserCallback (IntPtr browser, int iface, Protocol proto, BrowserEvent bevent,
+                                              IntPtr name, ushort clazz, ushort type, IntPtr rdata, int size,
+                                              LookupResultFlags flags, IntPtr userdata)
+        {
+            RecordInfo info;
+            info.NetworkInterface = iface;
+            info.Protocol = proto;
+            info.Name = Utility.PtrToString (name);
+            info.Class = (RecordClass) clazz;
+            info.Type = (RecordType) type;
+            info.Flags = flags;
+            info.Data = new byte[size];
+
+            if (rdata != IntPtr.Zero) {
+                Marshal.Copy (rdata, info.Data, 0, size);
+            }
+
+            switch (bevent) {
+            case BrowserEvent.Added:
+                infos.Add (info);
+
+                foreach (RecordInfoHandler handler in addListeners)
+                    handler (this, new RecordInfoArgs (info));
+
+                break;
+            case BrowserEvent.Removed:
+                infos.Remove (info);
+
+                foreach (RecordInfoHandler handler in removeListeners)
+                    handler (this, new RecordInfoArgs (info));
+
+                break;
+            default:
+                EmitBrowserEvent (bevent);
+                break;
+            }
+        }
+    }
+}
index 5d375b3bb7a23b220f23382217aabfef614784c4..0a5a2a8d4449b31511b57ef8f69965cf9e889ac9 100644 (file)
@@ -30,7 +30,7 @@ namespace Avahi
         internal void EmitFailure (ErrorCode code)
         {
             if (Failed != null)
-                Failed (this, code);
+                Failed (this, new ErrorCodeArgs (code));
         }
     }
 }
index a9c1726c20fa6565f343a38c0c70e51d1d41912b..639dd0a3bd5e466a988e542e6ae1b9d9d4008ae8 100644 (file)
@@ -47,7 +47,21 @@ namespace Avahi
         public static ServiceInfo Zero = new ServiceInfo ();
     }
 
-    public delegate void ServiceInfoHandler (object o, ServiceInfo info);
+    public class ServiceInfoArgs : EventArgs
+    {
+        private ServiceInfo service;
+
+        public ServiceInfo Service {
+            get { return service; }
+        }
+
+        public ServiceInfoArgs (ServiceInfo service)
+        {
+            this.service = service;
+        }
+    }
+
+    public delegate void ServiceInfoHandler (object o, ServiceInfoArgs args);
     
     public class ServiceBrowser : BrowserBase, IDisposable
     {
@@ -178,21 +192,19 @@ namespace Avahi
             info.Text = null;
             info.Flags = flags;
 
-            infos.Add (info);
-
             switch (bevent) {
             case BrowserEvent.Added:
                 infos.Add (info);
 
                 foreach (ServiceInfoHandler handler in addListeners)
-                    handler (this, info);
+                    handler (this, new ServiceInfoArgs (info));
 
                 break;
             case BrowserEvent.Removed:
                 infos.Remove (info);
 
                 foreach (ServiceInfoHandler handler in removeListeners)
-                    handler (this, info);
+                    handler (this, new ServiceInfoArgs (info));
 
                 break;
             default:
index 4a6a8a068f3bff7ad57867b25b59823e06d06da9..8dfe5209ee5b7779550071862cef52bb1608b982 100644 (file)
@@ -205,7 +205,7 @@ namespace Avahi
                 currentInfo = info;
 
                 foreach (ServiceInfoHandler handler in foundListeners)
-                    handler (this, info);
+                    handler (this, new ServiceInfoArgs (info));
                 break;
             case ResolverEvent.Failure:
                 EmitFailure (client.LastError);
index 754b9b151066e7769711583a7805e95b9949fd26..dbfff7378058dd9fd12247ff05fd03e78def0426 100644 (file)
@@ -38,7 +38,22 @@ namespace Avahi
         public LookupResultFlags Flags;
     }
 
-    public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfo info);
+    public class ServiceTypeInfoArgs : EventArgs
+    {
+        private ServiceTypeInfo type;
+
+        public ServiceTypeInfo ServiceType
+        {
+            get { return type; }
+        }
+
+        public ServiceTypeInfoArgs (ServiceTypeInfo type)
+        {
+            this.type = type;
+        }
+    }
+
+    public delegate void ServiceTypeInfoHandler (object o, ServiceTypeInfoArgs args);
     
     public class ServiceTypeBrowser : BrowserBase, IDisposable
     {
@@ -159,20 +174,18 @@ namespace Avahi
             info.ServiceType = Utility.PtrToString (type);
             info.Flags = flags;
 
-            infos.Add (info);
-
             switch (bevent) {
             case BrowserEvent.Added:
                 infos.Add (info);
 
                 foreach (ServiceTypeInfoHandler handler in addListeners)
-                    handler (this, info);
+                    handler (this, new ServiceTypeInfoArgs (info));
                 break;
             case BrowserEvent.Removed:
                 infos.Remove (info);
 
                 foreach (ServiceTypeInfoHandler handler in removeListeners)
-                    handler (this, info);
+                    handler (this, new ServiceTypeInfoArgs (info));
                 break;
             default:
                 EmitBrowserEvent (bevent);