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
{
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);
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);
}
}
- 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)
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);
}
}
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,
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) {
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
{
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
{
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);
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
{
private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
{
if (StateChanged != null)
- StateChanged (this, state);
+ StateChanged (this, new EntryGroupStateArgs (state));
}
}
}
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);
$(srcdir)/DomainBrowser.cs \
$(srcdir)/EntryGroup.cs \
$(srcdir)/HostNameResolver.cs \
+ $(srcdir)/RecordBrowser.cs \
$(srcdir)/ResolverBase.cs \
$(srcdir)/ServiceBrowser.cs \
$(srcdir)/ServiceResolver.cs \
--- /dev/null
+/* $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;
+ }
+ }
+ }
+}
internal void EmitFailure (ErrorCode code)
{
if (Failed != null)
- Failed (this, code);
+ Failed (this, new ErrorCodeArgs (code));
}
}
}
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
{
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:
currentInfo = info;
foreach (ServiceInfoHandler handler in foundListeners)
- handler (this, info);
+ handler (this, new ServiceInfoArgs (info));
break;
case ResolverEvent.Failure:
EmitFailure (client.LastError);
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
{
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);