-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBonjourSandbox.linq
More file actions
232 lines (197 loc) · 4.59 KB
/
BonjourSandbox.linq
File metadata and controls
232 lines (197 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<Query Kind="Program">
<Reference><ProgramFilesX64>\Bonjour SDK\Samples\CS\DNSServiceBrowser\bin\Debug\Interop.Bonjour.dll</Reference>
<Namespace>Bonjour</Namespace>
</Query>
void Main()
{
eventManager = new DNSSDEventManager();
eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(ServiceFound);
eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(ServiceLost);
eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(ServiceResolved);
eventManager.OperationFailed += new _IDNSSDEvents_OperationFailedEventHandler(OperationFailed);
service = new DNSSDService();
TryBrowse();
//signal.WaitOne(5000);
// service.Stop();
}
// You can define other methods, fields, classes and namespaces here
private Bonjour.DNSSDEventManager eventManager = null;
private Bonjour.DNSSDService service = null;
private Bonjour.DNSSDService browser = null;
private Bonjour.DNSSDService resolver = null;
private List<object> browseList = new List<object>();
private AutoResetEvent signal = new AutoResetEvent(false);
private const string BrowseType = "_ipp._tcp";
private bool TryBrowse()
{
try
{
if (browser != null) {
browser.Stop();
}
//
// Selecting a service type will start a new browse operation.
//
browser = service.Browse(0, 0, BrowseType, null, eventManager);
return true;
}
catch (Exception e)
{
e?.Dump();
return false;
}
}
private bool TryResolve(BrowseData data)
{
try
{
resolver?.Stop();
resolver = service.Resolve(0, data.InterfaceIndex, data.Name, data.Type, data.Domain, eventManager);
return true;
}
catch(Exception e)
{
e?.Dump();
return false;
}
}
public void ServiceFound(DNSSDService sref,
DNSSDFlags flags,
uint ifIndex,
String serviceName,
String regType,
String domain)
{
int index = browseList.IndexOf(serviceName);
//
// Check to see if we've seen this service before. If the machine has multiple
// interfaces, we could potentially get called back multiple times for the
// same service. Implementing a simple reference counting scheme will address
// the problem of the same service showing up more than once in the browse list.
//
if (index == -1)
{
BrowseData data = new BrowseData();
data.InterfaceIndex = ifIndex;
data.Name = serviceName;
data.Type = regType;
data.Domain = domain;
data.Refs = 1;
browseList.Add(data);
}
else
{
BrowseData data = (BrowseData)browseList[index];
data.Refs++;
}
browseList.Dump();
TryResolve((BrowseData)browseList[0]);
}
public void ServiceLost
(
DNSSDService sref,
DNSSDFlags flags,
uint ifIndex,
String serviceName,
String regType,
String domain
)
{}
public void ServiceResolved
(
DNSSDService sref,
DNSSDFlags flags,
uint ifIndex,
String fullName,
String hostName,
ushort port,
TXTRecord txtRecord
)
{
ResolveData data = new ResolveData();
data.InterfaceIndex = ifIndex;
data.FullName = fullName;
data.HostName = hostName;
data.Port = port;
data.TxtRecord = txtRecord;
//
// Don't forget to stop the resolver. This eases the burden on the network
//
resolver.Stop();
resolver = null;
data.Dump();
RenderTxtRecord(data);
}
public void OperationFailed
(
DNSSDService sref,
DNSSDError error
)
{
Console.WriteLine("Operation failed: error code: " + error, "Error");
}
public void RenderTxtRecord(ResolveData resolveData)
{
var sb = new System.Text.StringBuilder();
if (resolveData.TxtRecord != null)
{
for (uint idx = 0; idx < resolveData.TxtRecord.GetCount(); idx++)
{
String key;
Byte[] bytes;
key = resolveData.TxtRecord.GetKeyAtIndex(idx);
bytes = (Byte[])resolveData.TxtRecord.GetValueAtIndex(idx);
if (key.Length > 0)
{
String val = "";
if (bytes != null)
{
val = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
}
sb.AppendLine(key + "=" + val);
}
}
sb.ToString().Dump();
}
}
public class BrowseData
{
public uint InterfaceIndex;
public String Name;
public String Type;
public String Domain;
public int Refs;
public override String
ToString()
{
return Name;
}
public override bool
Equals(object other)
{
bool result = false;
if (other != null)
{
result = (this.Name == other.ToString());
}
return result;
}
public override int
GetHashCode()
{
return Name.GetHashCode();
}
};
public class ResolveData
{
public uint InterfaceIndex;
public String FullName;
public String HostName;
public int Port;
public TXTRecord TxtRecord;
public override String
ToString()
{
return FullName;
}
};