7f2946925e59c319890ce00b65e21a54ce91bd33
[mono.git] / mcs / class / referencesource / System.ServiceModel.Discovery / System / ServiceModel / Discovery / DiscoveryProxy.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.ServiceModel.Discovery
6 {
7     using System;
8     using System.Collections.ObjectModel;
9     using System.Diagnostics.CodeAnalysis;
10     using System.Runtime;
11     using System.ServiceModel.Discovery.Version11;
12     using System.ServiceModel.Discovery.VersionApril2005;
13     using System.ServiceModel.Discovery.VersionCD1;
14     using System.Xml;
15
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
30     {
31         DiscoveryMessageSequenceGenerator messageSequenceGenerator;
32         DuplicateDetector<UniqueId> duplicateDetector;
33
34         protected DiscoveryProxy()
35             : this(new DiscoveryMessageSequenceGenerator())
36         {
37         }
38
39         protected DiscoveryProxy(DiscoveryMessageSequenceGenerator messageSequenceGenerator)
40             : this(messageSequenceGenerator, DiscoveryDefaults.DuplicateMessageHistoryLength)
41         {
42         }
43
44         protected DiscoveryProxy(
45             DiscoveryMessageSequenceGenerator messageSequenceGenerator,
46             int duplicateMessageHistoryLength)
47         {
48             if (messageSequenceGenerator == null)
49             {
50                 throw FxTrace.Exception.ArgumentNull("messageSequenceGenerator");
51             }
52             if (duplicateMessageHistoryLength < 0)
53             {
54                 throw FxTrace.Exception.ArgumentOutOfRange(
55                     "duplicateMessageHistoryLength",
56                     duplicateMessageHistoryLength,
57                     SR.DiscoveryNegativeDuplicateMessageHistoryLength);
58             }
59             if (duplicateMessageHistoryLength > 0)
60             {
61                 this.duplicateDetector = new DuplicateDetector<UniqueId>(duplicateMessageHistoryLength);
62             }
63             this.messageSequenceGenerator = messageSequenceGenerator;
64         }
65
66         void IAnnouncementContractApril2005.HelloOperation(HelloMessageApril2005 message)
67         {
68             Fx.Assert("The sync method IAnnouncementContractApril2005.HelloOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
69         }
70
71         IAsyncResult IAnnouncementContractApril2005.BeginHelloOperation(HelloMessageApril2005 message, AsyncCallback callback, object state)
72         {
73             return new HelloOperationApril2005AsyncResult(this, message, callback, state);
74         }
75
76         void IAnnouncementContractApril2005.EndHelloOperation(IAsyncResult result)
77         {
78             HelloOperationApril2005AsyncResult.End(result);
79         }
80
81         void IAnnouncementContractApril2005.ByeOperation(ByeMessageApril2005 message)
82         {
83             Fx.Assert("The sync method IAnnouncementContractApril2005.ByeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
84         }
85
86         IAsyncResult IAnnouncementContractApril2005.BeginByeOperation(ByeMessageApril2005 message, AsyncCallback callback, object state)
87         {
88             return new ByeOperationApril2005AsyncResult(this, message, callback, state);
89         }
90
91         void IAnnouncementContractApril2005.EndByeOperation(IAsyncResult result)
92         {
93             ByeOperationApril2005AsyncResult.End(result);
94         }
95
96         void IAnnouncementContract11.HelloOperation(HelloMessage11 message)
97         {
98             Fx.Assert("The sync method IAnnouncementContract11.HelloOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
99         }
100
101         IAsyncResult IAnnouncementContract11.BeginHelloOperation(HelloMessage11 message, AsyncCallback callback, object state)
102         {
103             return new HelloOperation11AsyncResult(this, message, callback, state);
104         }
105
106         void IAnnouncementContract11.EndHelloOperation(IAsyncResult result)
107         {
108             HelloOperation11AsyncResult.End(result);
109         }
110
111         void IAnnouncementContract11.ByeOperation(ByeMessage11 message)
112         {
113             Fx.Assert("The sync method IAnnouncementContract11.ByeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
114         }
115
116         IAsyncResult IAnnouncementContract11.BeginByeOperation(ByeMessage11 message, AsyncCallback callback, object state)
117         {
118             return new ByeOperation11AsyncResult(this, message, callback, state);
119         }
120
121         void IAnnouncementContract11.EndByeOperation(IAsyncResult result)
122         {
123             ByeOperation11AsyncResult.End(result);
124         }
125
126         void IAnnouncementContractCD1.HelloOperation(HelloMessageCD1 message)
127         {
128             Fx.Assert("The sync method IAnnouncementContractCD1.HelloOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
129         }
130
131         IAsyncResult IAnnouncementContractCD1.BeginHelloOperation(HelloMessageCD1 message, AsyncCallback callback, object state)
132         {
133             return new HelloOperationCD1AsyncResult(this, message, callback, state);
134         }
135
136         void IAnnouncementContractCD1.EndHelloOperation(IAsyncResult result)
137         {
138             HelloOperationCD1AsyncResult.End(result);
139         }
140
141         void IAnnouncementContractCD1.ByeOperation(ByeMessageCD1 message)
142         {
143             Fx.Assert("The sync method IAnnouncementContractCD1.ByeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
144         }
145
146         IAsyncResult IAnnouncementContractCD1.BeginByeOperation(ByeMessageCD1 message, AsyncCallback callback, object state)
147         {
148             return new ByeOperationCD1AsyncResult(this, message, callback, state);
149         }
150
151         void IAnnouncementContractCD1.EndByeOperation(IAsyncResult result)
152         {
153             ByeOperationCD1AsyncResult.End(result);
154         }
155
156         void IDiscoveryContractApril2005.ProbeOperation(ProbeMessageApril2005 request)
157         {
158             Fx.Assert("The sync method IDiscoveryContractApril2005.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
159         }
160
161         IAsyncResult IDiscoveryContractApril2005.BeginProbeOperation(ProbeMessageApril2005 request, AsyncCallback callback, object state)
162         {
163             return new ProbeDuplexApril2005AsyncResult(request, this, this, callback, state);
164         }
165
166         void IDiscoveryContractApril2005.EndProbeOperation(IAsyncResult result)
167         {
168             ProbeDuplexApril2005AsyncResult.End(result);
169         }
170
171         void IDiscoveryContractApril2005.ResolveOperation(ResolveMessageApril2005 request)
172         {
173             Fx.Assert("The sync method IDiscoveryContractApril2005.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
174         }
175
176         IAsyncResult IDiscoveryContractApril2005.BeginResolveOperation(ResolveMessageApril2005 request, AsyncCallback callback, object state)
177         {
178             return new ResolveDuplexApril2005AsyncResult(request, this, this, callback, state);
179         }
180
181         void IDiscoveryContractApril2005.EndResolveOperation(IAsyncResult result)
182         {
183             ResolveDuplexApril2005AsyncResult.End(result);
184         }
185
186         void IDiscoveryContractAdhoc11.ProbeOperation(ProbeMessage11 request)
187         {
188             Fx.Assert("The sync method IDiscoveryContractAdhoc11.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
189         }
190
191         IAsyncResult IDiscoveryContractAdhoc11.BeginProbeOperation(ProbeMessage11 request, AsyncCallback callback, object state)
192         {
193             return new ProbeDuplex11AsyncResult(request, this, this, callback, state);
194         }
195
196         void IDiscoveryContractAdhoc11.EndProbeOperation(IAsyncResult result)
197         {
198             ProbeDuplex11AsyncResult.End(result);
199         }
200
201         void IDiscoveryContractAdhoc11.ResolveOperation(ResolveMessage11 request)
202         {
203             Fx.Assert("The sync method IDiscoveryContractAdhoc11.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
204         }
205
206         IAsyncResult IDiscoveryContractAdhoc11.BeginResolveOperation(ResolveMessage11 request, AsyncCallback callback, object state)
207         {
208             return new ResolveDuplex11AsyncResult(request, this, this, callback, state);
209         }
210
211         void IDiscoveryContractAdhoc11.EndResolveOperation(IAsyncResult result)
212         {
213             ResolveDuplex11AsyncResult.End(result);
214         }
215
216         ProbeMatchesMessage11 IDiscoveryContractManaged11.ProbeOperation(ProbeMessage11 request)
217         {
218             Fx.Assert("The sync method IDiscoveryContractManaged11.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
219             return null;
220         }
221
222         IAsyncResult IDiscoveryContractManaged11.BeginProbeOperation(ProbeMessage11 request, AsyncCallback callback, object state)
223         {
224             return new ProbeRequestResponse11AsyncResult(request, this, callback, state);
225         }
226
227         ProbeMatchesMessage11 IDiscoveryContractManaged11.EndProbeOperation(IAsyncResult result)
228         {
229             return ProbeRequestResponse11AsyncResult.End(result);
230         }
231
232         ResolveMatchesMessage11 IDiscoveryContractManaged11.ResolveOperation(ResolveMessage11 request)
233         {
234             Fx.Assert("The sync method IDiscoveryContractManaged11.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
235             return null;
236         }
237
238         IAsyncResult IDiscoveryContractManaged11.BeginResolveOperation(ResolveMessage11 request, AsyncCallback callback, object state)
239         {
240             return new ResolveRequestResponse11AsyncResult(request, this, callback, state);
241         }
242
243         ResolveMatchesMessage11 IDiscoveryContractManaged11.EndResolveOperation(IAsyncResult result)
244         {
245             return ResolveRequestResponse11AsyncResult.End(result);
246         }
247
248         void IDiscoveryContractAdhocCD1.ProbeOperation(ProbeMessageCD1 request)
249         {
250             Fx.Assert("The sync method IDiscoveryContractAdhocCD1.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
251         }
252
253         IAsyncResult IDiscoveryContractAdhocCD1.BeginProbeOperation(ProbeMessageCD1 request, AsyncCallback callback, object state)
254         {
255             return new ProbeDuplexCD1AsyncResult(request, this, this, callback, state);
256         }
257
258         void IDiscoveryContractAdhocCD1.EndProbeOperation(IAsyncResult result)
259         {
260             ProbeDuplexCD1AsyncResult.End(result);
261         }
262
263         void IDiscoveryContractAdhocCD1.ResolveOperation(ResolveMessageCD1 request)
264         {
265             Fx.Assert("The sync method IDiscoveryContractAdhocCD1.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
266         }
267
268         IAsyncResult IDiscoveryContractAdhocCD1.BeginResolveOperation(ResolveMessageCD1 request, AsyncCallback callback, object state)
269         {
270             return new ResolveDuplexCD1AsyncResult(request, this, this, callback, state);
271         }
272
273         void IDiscoveryContractAdhocCD1.EndResolveOperation(IAsyncResult result)
274         {
275             ResolveDuplexCD1AsyncResult.End(result);
276         }
277
278         ProbeMatchesMessageCD1 IDiscoveryContractManagedCD1.ProbeOperation(ProbeMessageCD1 request)
279         {
280             Fx.Assert("The sync method IDiscoveryContractManagedCD1.ProbeOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
281             return null;
282         }
283
284         IAsyncResult IDiscoveryContractManagedCD1.BeginProbeOperation(ProbeMessageCD1 request, AsyncCallback callback, object state)
285         {
286             return new ProbeRequestResponseCD1AsyncResult(request, this, callback, state);
287         }
288
289         ProbeMatchesMessageCD1 IDiscoveryContractManagedCD1.EndProbeOperation(IAsyncResult result)
290         {
291             return ProbeRequestResponseCD1AsyncResult.End(result);
292         }
293
294         ResolveMatchesMessageCD1 IDiscoveryContractManagedCD1.ResolveOperation(ResolveMessageCD1 request)
295         {
296             Fx.Assert("The sync method IDiscoveryContractManagedCD1.ResolveOperation must not get invoked. It is marked with PreferAsyncInvocation flag.");
297             return null;
298         }
299
300         IAsyncResult IDiscoveryContractManagedCD1.BeginResolveOperation(ResolveMessageCD1 request, AsyncCallback callback, object state)
301         {
302             return new ResolveRequestResponseCD1AsyncResult(request, this, callback, state);
303         }
304
305         ResolveMatchesMessageCD1 IDiscoveryContractManagedCD1.EndResolveOperation(IAsyncResult result)
306         {
307             return ResolveRequestResponseCD1AsyncResult.End(result);
308         }
309
310         bool IAnnouncementServiceImplementation.IsDuplicate(UniqueId messageId)
311         {
312             return (this.duplicateDetector != null) && (!this.duplicateDetector.AddIfNotDuplicate(messageId));
313         }
314
315         IAsyncResult IAnnouncementServiceImplementation.OnBeginOnlineAnnouncement(
316             DiscoveryMessageSequence messageSequence,
317             EndpointDiscoveryMetadata endpointDiscoveryMetadata,
318             AsyncCallback callback,
319             object state)
320         {
321             return this.OnBeginOnlineAnnouncement(messageSequence, endpointDiscoveryMetadata, callback, state);
322         }
323
324         void IAnnouncementServiceImplementation.OnEndOnlineAnnouncement(IAsyncResult result)
325         {
326             this.OnEndOnlineAnnouncement(result);
327         }
328
329         IAsyncResult IAnnouncementServiceImplementation.OnBeginOfflineAnnouncement(
330             DiscoveryMessageSequence messageSequence,
331             EndpointDiscoveryMetadata endpointDiscoveryMetadata,
332             AsyncCallback callback,
333             object state)
334         {
335             return this.OnBeginOfflineAnnouncement(messageSequence, endpointDiscoveryMetadata, callback, state);
336         }
337
338         void IAnnouncementServiceImplementation.OnEndOfflineAnnouncement(IAsyncResult result)
339         {
340             this.OnEndOfflineAnnouncement(result);
341         }
342
343         bool IDiscoveryServiceImplementation.IsDuplicate(UniqueId messageId)
344         {
345             return (this.duplicateDetector != null) && (!this.duplicateDetector.AddIfNotDuplicate(messageId));
346         }
347
348         DiscoveryMessageSequence IDiscoveryServiceImplementation.GetNextMessageSequence()
349         {
350             return this.messageSequenceGenerator.Next();
351         }
352
353         IAsyncResult IDiscoveryServiceImplementation.BeginFind(FindRequestContext findRequestContext, AsyncCallback callback, object state)
354         {
355             return this.OnBeginFind(findRequestContext, callback, state);
356         }
357
358         void IDiscoveryServiceImplementation.EndFind(IAsyncResult result)
359         {
360             this.OnEndFind(result);
361         }
362
363         IAsyncResult IDiscoveryServiceImplementation.BeginResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state)
364         {
365             return this.OnBeginResolve(resolveCriteria, callback, state);
366         }
367
368         EndpointDiscoveryMetadata IDiscoveryServiceImplementation.EndResolve(IAsyncResult result)
369         {
370             return this.OnEndResolve(result);
371         }
372
373         IAsyncResult IMulticastSuppressionImplementation.BeginShouldRedirectFind(FindCriteria findCriteria, AsyncCallback callback, object state)
374         {
375             return this.BeginShouldRedirectFind(findCriteria, callback, state);
376         }
377
378         bool IMulticastSuppressionImplementation.EndShouldRedirectFind(IAsyncResult result, out Collection<EndpointDiscoveryMetadata> redirectionEndpoints)
379         {
380             return this.EndShouldRedirectFind(result, out redirectionEndpoints);
381         }
382
383         IAsyncResult IMulticastSuppressionImplementation.BeginShouldRedirectResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state)
384         {
385             return this.BeginShouldRedirectResolve(resolveCriteria, callback, state);
386         }
387
388         bool IMulticastSuppressionImplementation.EndShouldRedirectResolve(IAsyncResult result, out Collection<EndpointDiscoveryMetadata> redirectionEndpoints)
389         {
390             return this.EndShouldRedirectResolve(result, out redirectionEndpoints);
391         }
392
393         protected virtual IAsyncResult BeginShouldRedirectFind(FindCriteria resolveCriteria, AsyncCallback callback, object state)
394         {
395             return new CompletedAsyncResult<bool>(false, callback, state);
396         }
397
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)
400         {
401             redirectionEndpoints = null;
402             return CompletedAsyncResult<bool>.End(result);
403         }
404
405         protected virtual IAsyncResult BeginShouldRedirectResolve(ResolveCriteria findCriteria, AsyncCallback callback, object state)
406         {
407             return new CompletedAsyncResult<bool>(false, callback, state);
408         }
409
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)
412         {
413             redirectionEndpoints = null;
414             return CompletedAsyncResult<bool>.End(result);
415         }
416
417
418         protected abstract IAsyncResult OnBeginOnlineAnnouncement(
419             DiscoveryMessageSequence messageSequence,
420             EndpointDiscoveryMetadata endpointDiscoveryMetadata,
421             AsyncCallback callback,
422             object state);
423         protected abstract void OnEndOnlineAnnouncement(IAsyncResult result);
424
425         protected abstract IAsyncResult OnBeginOfflineAnnouncement(
426             DiscoveryMessageSequence messageSequence,
427             EndpointDiscoveryMetadata endpointDiscoveryMetadata,
428             AsyncCallback callback,
429             object state);
430         protected abstract void OnEndOfflineAnnouncement(IAsyncResult result);
431
432         protected abstract IAsyncResult OnBeginFind(FindRequestContext findRequestContext, AsyncCallback callback, object state);
433         protected abstract void OnEndFind(IAsyncResult result);
434
435         protected abstract IAsyncResult OnBeginResolve(ResolveCriteria resolveCriteria, AsyncCallback callback, object state);
436         protected abstract EndpointDiscoveryMetadata OnEndResolve(IAsyncResult result);
437     }
438 }