]> git.meshlink.io Git - catta/blob - avahi-sharp/AddressResolver.cs
add initial mono bindings
[catta] / avahi-sharp / AddressResolver.cs
1 using System;
2 using System.Collections;
3 using System.Net;
4 using System.Runtime.InteropServices;
5 using Mono.Unix;
6
7 namespace Avahi
8 {
9
10     internal delegate void AddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
11                                                     ResolverEvent revent, Protocol aproto, IntPtr address,
12                                                     IntPtr hostname, IntPtr userdata);
13
14     public delegate void HostAddressHandler (object o, string host, IPAddress address);
15     
16     public class AddressResolver : IDisposable
17     {
18         private IntPtr handle;
19         private Client client;
20         private int iface;
21         private Protocol proto;
22         private IPAddress address;
23
24         private IPAddress currentAddress;
25         private string currentHost;
26
27         private ArrayList foundListeners = new ArrayList ();
28         private ArrayList timeoutListeners = new ArrayList ();
29         
30         [DllImport ("avahi-client")]
31         private static extern IntPtr avahi_address_resolver_new (IntPtr client, int iface, Protocol proto,
32                                                                  IntPtr address, AddressResolverCallback cb,
33                                                                  IntPtr userdata);
34
35         [DllImport ("avahi-client")]
36         private static extern void avahi_address_resolver_free (IntPtr handle);
37
38         public event HostAddressHandler Found
39         {
40             add {
41                 foundListeners.Add (value);
42                 Start ();
43             }
44             remove {
45                 foundListeners.Remove (value);
46                 Stop (false);
47             }
48         }
49         
50         public event EventHandler Timeout
51         {
52             add {
53                 timeoutListeners.Add (value);
54                 Start ();
55             }
56             remove {
57                 timeoutListeners.Remove (value);
58                 Stop (false);
59             }
60         }
61
62         public IPAddress Address
63         {
64             get { return currentAddress; }
65         }
66
67         public string HostName
68         {
69             get { return currentHost; }
70         }
71
72         public AddressResolver (Client client, IPAddress address) : this (client, -1, Protocol.Unspecified, address)
73         {
74         }
75
76         public AddressResolver (Client client, int iface, Protocol proto, IPAddress address)
77         {
78             this.client = client;
79             this.iface = iface;
80             this.proto = proto;
81             this.address = address;
82         }
83
84         ~AddressResolver ()
85         {
86             Dispose ();
87         }
88
89         public void Dispose ()
90         {
91             Stop (true);
92         }
93
94         private void Start ()
95         {
96             if (handle != IntPtr.Zero || (foundListeners.Count == 0 && timeoutListeners.Count == 0))
97                 return;
98
99             IntPtr addrPtr = Utility.StringToPtr (address.ToString ());
100             handle = avahi_address_resolver_new (client.Handle, iface, proto, addrPtr,
101                                                  OnAddressResolverCallback, IntPtr.Zero);
102             Utility.Free (addrPtr);
103         }
104
105         private void Stop (bool force)
106         {
107             if (handle != IntPtr.Zero && (force || (foundListeners.Count == 0 && timeoutListeners.Count == 0))) {
108                 avahi_address_resolver_free (handle);
109                 handle = IntPtr.Zero;
110             }
111         }
112
113         private void OnAddressResolverCallback (IntPtr resolver, int iface, Protocol proto,
114                                                 ResolverEvent revent, Protocol aproto, IntPtr address,
115                                                 IntPtr hostname, IntPtr userdata)
116         {
117             if (revent == ResolverEvent.Found) {
118                 currentAddress = Utility.PtrToAddress (address);
119                 currentHost = Utility.PtrToString (hostname);
120
121                 foreach (HostAddressHandler handler in foundListeners)
122                     handler (this, currentHost, currentAddress);
123             } else {
124                 currentAddress = null;
125                 currentHost = null;
126                 
127                 foreach (EventHandler handler in timeoutListeners)
128                     handler (this, new EventArgs ());
129             }
130         }
131     }
132 }