]> git.meshlink.io Git - catta/blob - avahi-sharp/EntryGroup.cs
74a127cfe64b0c94558beca6e2ee1b04e8bc8b9b
[catta] / avahi-sharp / EntryGroup.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4
5 namespace Avahi
6 {
7
8     public enum EntryGroupState {
9         Uncommited,
10         Registering,
11         Established,
12         Collision
13     }
14
15     internal delegate void EntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata);
16     public delegate void EntryGroupStateHandler (object o, EntryGroupState state);
17     
18     public class EntryGroup : IDisposable
19     {
20         private Client client;
21         private IntPtr handle;
22         
23         [DllImport ("avahi-client")]
24         private static extern IntPtr avahi_entry_group_new (IntPtr client, EntryGroupCallback cb, IntPtr userdata);
25
26         [DllImport ("avahi-client")]
27         private static extern void avahi_entry_group_commit (IntPtr group);
28
29         [DllImport ("avahi-client")]
30         private static extern void avahi_entry_group_reset (IntPtr group);
31
32         [DllImport ("avahi-client")]
33         private static extern EntryGroupState avahi_entry_group_get_state (IntPtr group);
34
35         [DllImport ("avahi-client")]
36         private static extern bool avahi_entry_group_is_empty (IntPtr group);
37
38         [DllImport ("avahi-client")]
39         private static extern void avahi_entry_group_add_service_strlst (IntPtr group, int iface, Protocol proto,
40                                                                          IntPtr name, IntPtr type, IntPtr domain,
41                                                                          IntPtr host, UInt16 port, IntPtr strlst);
42         
43         [DllImport ("avahi-client")]
44         private static extern void avahi_entry_group_free (IntPtr group);
45
46         [DllImport ("avahi-common")]
47         private static extern IntPtr avahi_string_list_new (IntPtr txt);
48
49         [DllImport ("avahi-common")]
50         private static extern void avahi_string_list_add (IntPtr list, IntPtr txt);
51
52         [DllImport ("avahi-common")]
53         private static extern void avahi_string_list_free (IntPtr list);
54
55         public event EntryGroupStateHandler StateChanged;
56         
57         public EntryGroupState State
58         {
59             get { return avahi_entry_group_get_state (handle); }
60         }
61
62         public bool IsEmpty
63         {
64             get { return avahi_entry_group_is_empty (handle); }
65         }
66         
67         public EntryGroup (Client client)
68         {
69             this.client = client;
70             handle = avahi_entry_group_new (client.Handle, OnEntryGroupCallback, IntPtr.Zero);
71             client.CheckError ();
72         }
73
74         ~EntryGroup ()
75         {
76             Dispose ();
77         }
78
79         public void Dispose ()
80         {
81             if (handle != IntPtr.Zero) {
82                 avahi_entry_group_free (handle);
83                 handle = IntPtr.Zero;
84             }
85         }
86
87         public void Commit ()
88         {
89             avahi_entry_group_commit (handle);
90             client.CheckError ();
91         }
92
93         public void Reset ()
94         {
95             avahi_entry_group_reset (handle);
96             client.CheckError ();
97         }
98
99         public void AddService (string name, string type, string domain,
100                                 UInt16 port, string[] txt)
101         {
102             AddService (-1, Protocol.Unspecified, name, type, domain, null, port, txt);
103         }
104
105         public void AddService (int iface, Protocol proto, string name, string type, string domain,
106                                 string host, UInt16 port, string[] txt)
107         {
108             IntPtr list = avahi_string_list_new (IntPtr.Zero);
109
110             if (txt != null) {
111                 foreach (string item in txt) {
112                     IntPtr itemPtr = Utility.StringToPtr (item);
113                     avahi_string_list_add (list, itemPtr);
114                     Utility.Free (itemPtr);
115                 }
116             }
117
118             IntPtr namePtr = Utility.StringToPtr (name);
119             IntPtr typePtr = Utility.StringToPtr (type);
120             IntPtr domainPtr = Utility.StringToPtr (domain);
121             IntPtr hostPtr = Utility.StringToPtr (host);
122             avahi_entry_group_add_service_strlst (handle, iface, proto, namePtr, typePtr, domainPtr,
123                                                   hostPtr, port, list);
124             avahi_string_list_free (list);
125
126             client.CheckError ();
127             
128             Console.WriteLine ("Added service: {0}, {1}, {2}, {3}, {4}", name, type, domain, host, port);
129         }
130
131         private void OnEntryGroupCallback (IntPtr group, EntryGroupState state, IntPtr userdata)
132         {
133             if (StateChanged != null)
134                 StateChanged (this, state);
135         }
136     }
137 }