summaryrefslogtreecommitdiffstats
path: root/avahi-sharp/AddressResolver.cs
diff options
context:
space:
mode:
authorJames Willcox <snopr@snorp.net>2005-09-09 17:12:57 +0000
committerJames Willcox <snopr@snorp.net>2005-09-09 17:12:57 +0000
commitba12decc4413dedf22c06545d1ec5938efa8954a (patch)
tree6bd8edc20ffabbbd9300264677a983695787dd7f /avahi-sharp/AddressResolver.cs
parente1d06346134b3051878c8080694068517faf3f16 (diff)
add initial mono bindings
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@538 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
Diffstat (limited to 'avahi-sharp/AddressResolver.cs')
-rw-r--r--avahi-sharp/AddressResolver.cs132
1 files changed, 132 insertions, 0 deletions
diff --git a/avahi-sharp/AddressResolver.cs b/avahi-sharp/AddressResolver.cs
new file mode 100644
index 0000000..730645f
--- /dev/null
+++ b/avahi-sharp/AddressResolver.cs
@@ -0,0 +1,132 @@
+using System;
+using System.Collections;
+using System.Net;
+using System.Runtime.InteropServices;
+using Mono.Unix;
+
+namespace Avahi
+{
+
+ internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
+ ResolverEvent revent, Protocol aproto, IntPtr address,
+ IntPtr hostname, IntPtr userdata);
+
+ public delegate void HostAddressHandler (object o, string host, IPAddress address);
+
+ public class AddressResolver : IDisposable
+ {
+ private IntPtr handle;
+ private Client client;
+ private int iface;
+ private Protocol proto;
+ private IPAddress address;
+
+ private IPAddress currentAddress;
+ private string currentHost;
+
+ private ArrayList foundListeners = new ArrayList ();
+ private ArrayList timeoutListeners = new ArrayList ();
+
+ [DllImport ("avahi-client")]
+ private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
+ IntPtr address, AddressResolverCallback cb,
+ IntPtr userdata);
+
+ [DllImport ("avahi-client")]
+ private static extern void avahi_address_resolver_free (IntPtr handle);
+
+ public event HostAddressHandler Found
+ {
+ add {
+ foundListeners.Add (value);
+ Start ();
+ }
+ remove {
+ foundListeners.Remove (value);
+ Stop (false);
+ }
+ }
+
+ public event EventHandler Timeout
+ {
+ add {
+ timeoutListeners.Add (value);
+ Start ();
+ }
+ remove {
+ timeoutListeners.Remove (value);
+ Stop (false);
+ }
+ }
+
+ public IPAddress Address
+ {
+ get { return currentAddress; }
+ }
+
+ public string HostName
+ {
+ get { return currentHost; }
+ }
+
+ public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified, address)
+ {
+ }
+
+ public AddressResolver (Client client, int iface, Protocol proto, IPAddress address)
+ {
+ this.client = client;
+ this.iface = iface;
+ this.proto = proto;
+ this.address = address;
+ }
+
+ ~AddressResolver ()
+ {
+ Dispose ();
+ }
+
+ public void Dispose ()
+ {
+ Stop (true);
+ }
+
+ private void Start ()
+ {
+ if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
+ return;
+
+ IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
+ handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr,
+ OnAddressResolverCallback, IntPtr.Zero);
+ Utility.Free (addrPtr);
+ }
+
+ private void Stop (bool force)
+ {
+ if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
+ avahi_address_resolver_free (handle);
+ handle = IntPtr.Zero;
+ }
+ }
+
+ private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
+ ResolverEvent revent, Protocol aproto, IntPtr address,
+ IntPtr hostname, IntPtr userdata)
+ {
+ if (revent == ResolverEvent.Found) {
+ currentAddress = Utility.PtrToAddress (address);
+ currentHost = Utility.PtrToString (hostname);
+
+ foreach (HostAddressHandler handler in foundListeners)
+ handler (this, currentHost, currentAddress);
+ } else {
+ currentAddress = null;
+ currentHost = null;
+
+ foreach (EventHandler handler in timeoutListeners)
+ handler (this, new EventArgs ());
+ }
+ }
+ }
+}