1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.ServiceModel.Discovery
8 using System.Collections.ObjectModel;
9 using System.Diagnostics.CodeAnalysis;
11 using System.ServiceModel.Discovery.Version11;
12 using System.ServiceModel.Discovery.VersionApril2005;
13 using System.ServiceModel.Discovery.VersionCD1;
16 [Fx.Tag.XamlVisible(false)]
17 public abstract class DiscoveryProxy :
18 IAnnouncementContractApril2005,
19 IAnnouncementContract11,
20 IAnnouncementContractCD1,
21 IDiscoveryContractAdhocApril2005,
22 IDiscoveryContractManagedApril2005,
23 IDiscoveryContractAdhoc11,
24 IDiscoveryContractManaged11,
25 IDiscoveryContractAdhocCD1,
26 IDiscoveryContractManagedCD1,
27 IAnnouncementServiceImplementation,
28 IDiscoveryServiceImplementation,
29 IMulticastSuppressionImplementation
31 DiscoveryMessageSequenceGenerator messageSequenceGenerator;
32 DuplicateDetector<UniqueId> duplicateDetector;
34 protected DiscoveryProxy()
35 : this(new DiscoveryMessageSequenceGenerator())
39 protected DiscoveryProxy(DiscoveryMessageSequenceGenerator messageSequenceGenerator)
40 : this(messageSequenceGenerator, DiscoveryDefaults.DuplicateMessageHistoryLength)
44 protected DiscoveryProxy(
45 DiscoveryMessageSequenceGenerator messageSequenceGenerator,
46 int duplicateMessageHistoryLength)
48 if (messageSequenceGenerator == null)
50 throw FxTrace.Exception.ArgumentNull("messageSequenceGenerator");
52 if (duplicateMessageHistoryLength < 0)
54 throw FxTrace.Exception.ArgumentOutOfRange(
55 "duplicateMessageHistoryLength",
56 duplicateMessageHistoryLength,
57 SR.DiscoveryNegativeDuplicateMessageHistoryLength);
59 if (duplicateMessageHistoryLength > 0)
61 this.duplicateDetector = new DuplicateDetector<UniqueId>(duplicateMessageHistoryLength);
63 this.messageSequenceGenerator = messageSequenceGenerator;
66 void IAnnouncementContractApril2005.HelloOperation(HelloMessageApril2005 message)
68 Fx.Assert("The sync method IAnnouncementContractApril2005.HelloOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
71 IAsyncResult IAnnouncementContractApril2005.BeginHelloOperation(HelloMessageApril2005 message, AsyncCallback callback, object state)
73 return new HelloOperationApril2005AsyncResult(this, message, callback, state);
76 void IAnnouncementContractApril2005.EndHelloOperation(IAsyncResult result)
78 HelloOperationApril2005AsyncResult.End(result);
81 void IAnnouncementContractApril2005.ByeOperation(ByeMessageApril2005 message)
83 Fx.Assert("The sync method IAnnouncementContractApril2005.ByeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
86 IAsyncResult IAnnouncementContractApril2005.BeginByeOperation(ByeMessageApril2005 message, AsyncCallback callback, object state)
88 return new ByeOperationApril2005AsyncResult(this, message, callback, state);
91 void IAnnouncementContractApril2005.EndByeOperation(IAsyncResult result)
93 ByeOperationApril2005AsyncResult.End(result);
96 void IAnnouncementContract11.HelloOperation(HelloMessage11 message)
98 Fx.Assert("The sync method IAnnouncementContract11.HelloOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
101 IAsyncResult IAnnouncementContract11.BeginHelloOperation(HelloMessage11 message, AsyncCallback callback, object state)
103 return new HelloOperation11AsyncResult(this, message, callback, state);
106 void IAnnouncementContract11.EndHelloOperation(IAsyncResult result)
108 HelloOperation11AsyncResult.End(result);
111 void IAnnouncementContract11.ByeOperation(ByeMessage11 message)
113 Fx.Assert("The sync method IAnnouncementContract11.ByeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
116 IAsyncResult IAnnouncementContract11.BeginByeOperation(ByeMessage11 message, AsyncCallback callback, object state)
118 return new ByeOperation11AsyncResult(this, message, callback, state);
121 void IAnnouncementContract11.EndByeOperation(IAsyncResult result)
123 ByeOperation11AsyncResult.End(result);
126 void IAnnouncementContractCD1.HelloOperation(HelloMessageCD1 message)
128 Fx.Assert("The sync method IAnnouncementContractCD1.HelloOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
131 IAsyncResult IAnnouncementContractCD1.BeginHelloOperation(HelloMessageCD1 message, AsyncCallback callback, object state)
133 return new HelloOperationCD1AsyncResult(this, message, callback, state);
136 void IAnnouncementContractCD1.EndHelloOperation(IAsyncResult result)
138 HelloOperationCD1AsyncResult.End(result);
141 void IAnnouncementContractCD1.ByeOperation(ByeMessageCD1 message)
143 Fx.Assert("The sync method IAnnouncementContractCD1.ByeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
146 IAsyncResult IAnnouncementContractCD1.BeginByeOperation(ByeMessageCD1 message, AsyncCallback callback, object state)
148 return new ByeOperationCD1AsyncResult(this, message, callback, state);
151 void IAnnouncementContractCD1.EndByeOperation(IAsyncResult result)
153 ByeOperationCD1AsyncResult.End(result);
156 void IDiscoveryContractApril2005.ProbeOperation(ProbeMessageApril2005 request)
158 Fx.Assert("The sync method IDiscoveryContractApril2005.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
161 IAsyncResult IDiscoveryContractApril2005.BeginProbeOperation(ProbeMessageApril2005 request, AsyncCallback callback, object state)
163 return new ProbeDuplexApril2005AsyncResult(request, this, this, callback, state);
166 void IDiscoveryContractApril2005.EndProbeOperation(IAsyncResult result)
168 ProbeDuplexApril2005AsyncResult.End(result);
171 void IDiscoveryContractApril2005.ResolveOperation(ResolveMessageApril2005 request)
173 Fx.Assert("The sync method IDiscoveryContractApril2005.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
176 IAsyncResult IDiscoveryContractApril2005.BeginResolveOperation(ResolveMessageApril2005 request, AsyncCallback callback, object state)
178 return new ResolveDuplexApril2005AsyncResult(request, this, this, callback, state);
181 void IDiscoveryContractApril2005.EndResolveOperation(IAsyncResult result)
183 ResolveDuplexApril2005AsyncResult.End(result);
186 void IDiscoveryContractAdhoc11.ProbeOperation(ProbeMessage11 request)
188 Fx.Assert("The sync method IDiscoveryContractAdhoc11.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
191 IAsyncResult IDiscoveryContractAdhoc11.BeginProbeOperation(ProbeMessage11 request, AsyncCallback callback, object state)
193 return new ProbeDuplex11AsyncResult(request, this, this, callback, state);
196 void IDiscoveryContractAdhoc11.EndProbeOperation(IAsyncResult result)
198 ProbeDuplex11AsyncResult.End(result);
201 void IDiscoveryContractAdhoc11.ResolveOperation(ResolveMessage11 request)
203 Fx.Assert("The sync method IDiscoveryContractAdhoc11.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
206 IAsyncResult IDiscoveryContractAdhoc11.BeginResolveOperation(ResolveMessage11 request, AsyncCallback callback, object state)
208 return new ResolveDuplex11AsyncResult(request, this, this, callback, state);
211 void IDiscoveryContractAdhoc11.EndResolveOperation(IAsyncResult result)
213 ResolveDuplex11AsyncResult.End(result);
216 ProbeMatchesMessage11 IDiscoveryContractManaged11.ProbeOperation(ProbeMessage11 request)
218 Fx.Assert("The sync method IDiscoveryContractManaged11.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
222 IAsyncResult IDiscoveryContractManaged11.BeginProbeOperation(ProbeMessage11 request, AsyncCallback callback, object state)
224 return new ProbeRequestResponse11AsyncResult(request, this, callback, state);
227 ProbeMatchesMessage11 IDiscoveryContractManaged11.EndProbeOperation(IAsyncResult result)
229 return ProbeRequestResponse11AsyncResult.End(result);
232 ResolveMatchesMessage11 IDiscoveryContractManaged11.ResolveOperation(ResolveMessage11 request)
234 Fx.Assert("The sync method IDiscoveryContractManaged11.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
238 IAsyncResult IDiscoveryContractManaged11.BeginResolveOperation(ResolveMessage11 request, AsyncCallback callback, object state)
240 return new ResolveRequestResponse11AsyncResult(request, this, callback, state);
243 ResolveMatchesMessage11 IDiscoveryContractManaged11.EndResolveOperation(IAsyncResult result)
245 return ResolveRequestResponse11AsyncResult.End(result);
248 void IDiscoveryContractAdhocCD1.ProbeOperation(ProbeMessageCD1 request)
250 Fx.Assert("The sync method IDiscoveryContractAdhocCD1.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
253 IAsyncResult IDiscoveryContractAdhocCD1.BeginProbeOperation(ProbeMessageCD1 request, AsyncCallback callback, object state)
255 return new ProbeDuplexCD1AsyncResult(request, this, this, callback, state);
258 void IDiscoveryContractAdhocCD1.EndProbeOperation(IAsyncResult result)
260 ProbeDuplexCD1AsyncResult.End(result);
263 void IDiscoveryContractAdhocCD1.ResolveOperation(ResolveMessageCD1 request)
265 Fx.Assert("The sync method IDiscoveryContractAdhocCD1.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
268 IAsyncResult IDiscoveryContractAdhocCD1.BeginResolveOperation(ResolveMessageCD1 request, AsyncCallback callback, object state)
270 return new ResolveDuplexCD1AsyncResult(request, this, this, callback, state);
273 void IDiscoveryContractAdhocCD1.EndResolveOperation(IAsyncResult result)
275 ResolveDuplexCD1AsyncResult.End(result);
278 ProbeMatchesMessageCD1 IDiscoveryContractManagedCD1.ProbeOperation(ProbeMessageCD1 request)
280 Fx.Assert("The sync method IDiscoveryContractManagedCD1.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
284 IAsyncResult IDiscoveryContractManagedCD1.BeginProbeOperation(ProbeMessageCD1 request, AsyncCallback callback, object state)
286 return new ProbeRequestResponseCD1AsyncResult(request, this, callback, state);
289 ProbeMatchesMessageCD1 IDiscoveryContractManagedCD1.EndProbeOperation(IAsyncResult result)
291 return ProbeRequestResponseCD1AsyncResult.End(result);
294 ResolveMatchesMessageCD1 IDiscoveryContractManagedCD1.ResolveOperation(ResolveMessageCD1 request)
296 Fx.Assert("The sync method IDiscoveryContractManagedCD1.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
300 IAsyncResult IDiscoveryContractManagedCD1.BeginResolveOperation(ResolveMessageCD1 request, AsyncCallback callback, object state)
302 return new ResolveRequestResponseCD1AsyncResult(request, this, callback, state);
305 ResolveMatchesMessageCD1 IDiscoveryContractManagedCD1.EndResolveOperation(IAsyncResult result)
307 return ResolveRequestResponseCD1AsyncResult.End(result);
310 bool IAnnouncementServiceImplementation.IsDuplicate(UniqueId messageId)
312 return (this.duplicateDetector != null) && (!this.duplicateDetector.AddIfNotDuplicate(messageId));
315 IAsyncResult IAnnouncementServiceImplementation.OnBeginOnlineAnnouncement(
316 DiscoveryMessageSequence messageSequence,
317 EndpointDiscoveryMetadata endpointDiscoveryMetadata,
318 AsyncCallback callback,
321 return this.OnBeginOnlineAnnouncement(messageSequence, endpointDiscoveryMetadata, callback, state);
324 void IAnnouncementServiceImplementation.OnEndOnlineAnnouncement(IAsyncResult result)
326 this.OnEndOnlineAnnouncement(result);
329 IAsyncResult IAnnouncementServiceImplementation.OnBeginOfflineAnnouncement(
330 DiscoveryMessageSequence messageSequence,
331 EndpointDiscoveryMetadata endpointDiscoveryMetadata,
332 AsyncCallback callback,
335 return this.OnBeginOfflineAnnouncement(messageSequence, endpointDiscoveryMetadata, callback, state);
338 void IAnnouncementServiceImplementation.OnEndOfflineAnnouncement(IAsyncResult result)
340 this.OnEndOfflineAnnouncement(result);
343 bool IDiscoveryServiceImplementation.IsDuplicate(UniqueId messageId)
345 return (this.duplicateDetector != null) && (!this.duplicateDetector.AddIfNotDuplicate(messageId));
348 DiscoveryMessageSequence IDiscoveryServiceImplementation.GetNextMessageSequence()
350 return this.messageSequenceGenerator.Next();
353 IAsyncResult IDiscoveryServiceImplementation.BeginFind(FindRequestContext findRequestContext, AsyncCallback callback, object state)
355 return this.OnBeginFind(findRequestContext, callback, state);
358 void IDiscoveryServiceImplementation.EndFind(IAsyncResult result)
360 this.OnEndFind(result);
363 IAsyncResult IDiscoveryServiceImplementation.BeginResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state)
365 return this.OnBeginResolve(resolveCriteria, callback, state);
368 EndpointDiscoveryMetadata IDiscoveryServiceImplementation.EndResolve(IAsyncResult result)
370 return this.OnEndResolve(result);
373 IAsyncResult IMulticastSuppressionImplementation.BeginShouldRedirectFind(FindCriteria findCriteria, AsyncCallback callback, object state)
375 return this.BeginShouldRedirectFind(findCriteria, callback, state);
378 bool IMulticastSuppressionImplementation.EndShouldRedirectFind(IAsyncResult result, out Collection<EndpointDiscoveryMetadata> redirectionEndpoints)
380 return this.EndShouldRedirectFind(result, out redirectionEndpoints);
383 IAsyncResult IMulticastSuppressionImplementation.BeginShouldRedirectResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state)
385 return this.BeginShouldRedirectResolve(resolveCriteria, callback, state);
388 bool IMulticastSuppressionImplementation.EndShouldRedirectResolve(IAsyncResult result, out Collection<EndpointDiscoveryMetadata> redirectionEndpoints)
390 return this.EndShouldRedirectResolve(result, out redirectionEndpoints);
393 protected virtual IAsyncResult BeginShouldRedirectFind(FindCriteria resolveCriteria, AsyncCallback callback, object state)
395 return new CompletedAsyncResult<bool>(false, callback, state);
398 [SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters, Justification = "This is a Try pattern that requires out parameter.")]
399 protected virtual bool EndShouldRedirectFind(IAsyncResult result, out Collection<EndpointDiscoveryMetadata> redirectionEndpoints)
401 redirectionEndpoints = null;
402 return CompletedAsyncResult<bool>.End(result);
405 protected virtual IAsyncResult BeginShouldRedirectResolve(ResolveCriteria findCriteria, AsyncCallback callback, object state)
407 return new CompletedAsyncResult<bool>(false, callback, state);
410 [SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters, Justification = "This is a Try pattern that requires out parameter.")]
411 protected virtual bool EndShouldRedirectResolve(IAsyncResult result, out Collection<EndpointDiscoveryMetadata> redirectionEndpoints)
413 redirectionEndpoints = null;
414 return CompletedAsyncResult<bool>.End(result);
418 protected abstract IAsyncResult OnBeginOnlineAnnouncement(
419 DiscoveryMessageSequence messageSequence,
420 EndpointDiscoveryMetadata endpointDiscoveryMetadata,
421 AsyncCallback callback,
423 protected abstract void OnEndOnlineAnnouncement(IAsyncResult result);
425 protected abstract IAsyncResult OnBeginOfflineAnnouncement(
426 DiscoveryMessageSequence messageSequence,
427 EndpointDiscoveryMetadata endpointDiscoveryMetadata,
428 AsyncCallback callback,
430 protected abstract void OnEndOfflineAnnouncement(IAsyncResult result);
432 protected abstract IAsyncResult OnBeginFind(FindRequestContext findRequestContext, AsyncCallback callback, object state);
433 protected abstract void OnEndFind(IAsyncResult result);
435 protected abstract IAsyncResult OnBeginResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state);
436 protected abstract EndpointDiscoveryMetadata OnEndResolve(IAsyncResult result);