From: James Willcox Date: Thu, 17 Nov 2005 17:55:09 +0000 (+0000) Subject: * Add RecordBrowser.cs X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=84a9392791c574c56ca148157b8b951851208398;p=catta * Add RecordBrowser.cs * 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 --- diff --git a/avahi-sharp/AddressResolver.cs b/avahi-sharp/AddressResolver.cs index 3231cbf..63dc33e 100644 --- a/avahi-sharp/AddressResolver.cs +++ b/avahi-sharp/AddressResolver.cs @@ -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); diff --git a/avahi-sharp/AvahiTest.cs b/avahi-sharp/AvahiTest.cs index 9c4aeca..0fac04b 100644 --- a/avahi-sharp/AvahiTest.cs +++ b/avahi-sharp/AvahiTest.cs @@ -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); } } diff --git a/avahi-sharp/Client.cs b/avahi-sharp/Client.cs index 394efc5..01b08bd 100644 --- a/avahi-sharp/Client.cs +++ b/avahi-sharp/Client.cs @@ -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) { diff --git a/avahi-sharp/ClientException.cs b/avahi-sharp/ClientException.cs index 11c4e63..fadcce9 100644 --- a/avahi-sharp/ClientException.cs +++ b/avahi-sharp/ClientException.cs @@ -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 { diff --git a/avahi-sharp/DomainBrowser.cs b/avahi-sharp/DomainBrowser.cs index 08f2b35..50ac757 100644 --- a/avahi-sharp/DomainBrowser.cs +++ b/avahi-sharp/DomainBrowser.cs @@ -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); diff --git a/avahi-sharp/EntryGroup.cs b/avahi-sharp/EntryGroup.cs index a8744d0..443f198 100644 --- a/avahi-sharp/EntryGroup.cs +++ b/avahi-sharp/EntryGroup.cs @@ -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)); } } } diff --git a/avahi-sharp/HostNameResolver.cs b/avahi-sharp/HostNameResolver.cs index 9780811..0922aa7 100644 --- a/avahi-sharp/HostNameResolver.cs +++ b/avahi-sharp/HostNameResolver.cs @@ -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); diff --git a/avahi-sharp/Makefile.am b/avahi-sharp/Makefile.am index 80fd06e..778f882 100644 --- a/avahi-sharp/Makefile.am +++ b/avahi-sharp/Makefile.am @@ -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 index 0000000..5a736bd --- /dev/null +++ b/avahi-sharp/RecordBrowser.cs @@ -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; + } + } + } +} diff --git a/avahi-sharp/ResolverBase.cs b/avahi-sharp/ResolverBase.cs index 5d375b3..0a5a2a8 100644 --- a/avahi-sharp/ResolverBase.cs +++ b/avahi-sharp/ResolverBase.cs @@ -30,7 +30,7 @@ namespace Avahi internal void EmitFailure (ErrorCode code) { if (Failed != null) - Failed (this, code); + Failed (this, new ErrorCodeArgs (code)); } } } diff --git a/avahi-sharp/ServiceBrowser.cs b/avahi-sharp/ServiceBrowser.cs index a9c1726..639dd0a 100644 --- a/avahi-sharp/ServiceBrowser.cs +++ b/avahi-sharp/ServiceBrowser.cs @@ -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: diff --git a/avahi-sharp/ServiceResolver.cs b/avahi-sharp/ServiceResolver.cs index 4a6a8a0..8dfe520 100644 --- a/avahi-sharp/ServiceResolver.cs +++ b/avahi-sharp/ServiceResolver.cs @@ -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); diff --git a/avahi-sharp/ServiceTypeBrowser.cs b/avahi-sharp/ServiceTypeBrowser.cs index 754b9b1..dbfff73 100644 --- a/avahi-sharp/ServiceTypeBrowser.cs +++ b/avahi-sharp/ServiceTypeBrowser.cs @@ -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);