File: ClientReliableChannelBinder.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (729 lines) | stat: -rw-r--r-- 26,180 bytes parent folder | download | duplicates (9)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Channels
{
    using System;
    using System.Runtime;
    using System.ServiceModel;
    using System.ServiceModel.Security;
    using System.ServiceModel.Diagnostics;

    abstract class ClientReliableChannelBinder<TChannel> : ReliableChannelBinder<TChannel>,
        IClientReliableChannelBinder
        where TChannel : class, IChannel
    {
        ChannelParameterCollection channelParameters;
        IChannelFactory<TChannel> factory;
        EndpointAddress to;
        Uri via;

        protected ClientReliableChannelBinder(EndpointAddress to, Uri via, IChannelFactory<TChannel> factory,
            MaskingMode maskingMode, TolerateFaultsMode faultMode, ChannelParameterCollection channelParameters,
            TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
            : base(factory.CreateChannel(to, via), maskingMode, faultMode,
            defaultCloseTimeout, defaultSendTimeout)
        {
            if (channelParameters == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("channelParameters");
            }

            this.to = to;
            this.via = via;
            this.factory = factory;
            this.channelParameters = channelParameters;
        }

        // The server side must get a message to determine where the channel should go, thus it is
        // pointless to create a channel for the sake of receiving on the client side. Also, since
        // the client side can create channels there receive may enter an infinite loop if open
        // persistently throws.
        protected override bool CanGetChannelForReceive
        {
            get
            {
                return false;
            }
        }

        public override bool CanSendAsynchronously
        {
            get
            {
                return true;
            }
        }

        public override ChannelParameterCollection ChannelParameters
        {
            get
            {
                return this.channelParameters;
            }
        }

        protected override bool MustCloseChannel
        {
            get
            {
                return true;
            }
        }

        protected override bool MustOpenChannel
        {
            get
            {
                return true;
            }
        }

        public Uri Via
        {
            get
            {
                return this.via;
            }
        }

        public IAsyncResult BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback,
            object state)
        {
            return this.BeginRequest(message, timeout, this.DefaultMaskingMode, callback, state);
        }

        public IAsyncResult BeginRequest(Message message, TimeSpan timeout, MaskingMode maskingMode,
            AsyncCallback callback, object state)
        {
            RequestAsyncResult result = new RequestAsyncResult(this, callback, state);
            result.Start(message, timeout, maskingMode);
            return result;
        }

        protected override IAsyncResult BeginTryGetChannel(TimeSpan timeout,
            AsyncCallback callback, object state)
        {
            CommunicationState currentState = this.State;
            TChannel channel;

            if ((currentState == CommunicationState.Created)
               || (currentState == CommunicationState.Opening)
               || (currentState == CommunicationState.Opened))
            {
                channel = this.factory.CreateChannel(this.to, this.via);
            }
            else
            {
                channel = null;
            }

            return new CompletedAsyncResult<TChannel>(channel, callback, state);
        }

        public static IClientReliableChannelBinder CreateBinder(EndpointAddress to, Uri via,
            IChannelFactory<TChannel> factory, MaskingMode maskingMode, TolerateFaultsMode faultMode,
            ChannelParameterCollection channelParameters,
            TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
        {
            Type type = typeof(TChannel);

            if (type == typeof(IDuplexChannel))
            {
                return new DuplexClientReliableChannelBinder(to, via, (IChannelFactory<IDuplexChannel>)(object)factory, maskingMode,
                    channelParameters, defaultCloseTimeout, defaultSendTimeout);
            }
            else if (type == typeof(IDuplexSessionChannel))
            {
                return new DuplexSessionClientReliableChannelBinder(to, via, (IChannelFactory<IDuplexSessionChannel>)(object)factory, maskingMode,
                    faultMode, channelParameters, defaultCloseTimeout, defaultSendTimeout);
            }
            else if (type == typeof(IRequestChannel))
            {
                return new RequestClientReliableChannelBinder(to, via, (IChannelFactory<IRequestChannel>)(object)factory, maskingMode,
                    channelParameters, defaultCloseTimeout, defaultSendTimeout);
            }
            else if (type == typeof(IRequestSessionChannel))
            {
                return new RequestSessionClientReliableChannelBinder(to, via, (IChannelFactory<IRequestSessionChannel>)(object)factory, maskingMode,
                    faultMode, channelParameters, defaultCloseTimeout, defaultSendTimeout);
            }
            else
            {
                throw Fx.AssertAndThrow("ClientReliableChannelBinder supports creation of IDuplexChannel, IDuplexSessionChannel, IRequestChannel, and IRequestSessionChannel only.");
            }
        }

        public Message EndRequest(IAsyncResult result)
        {
            return RequestAsyncResult.End(result);
        }

        protected override bool EndTryGetChannel(IAsyncResult result)
        {
            TChannel channel = CompletedAsyncResult<TChannel>.End(result);

            if (channel != null && !this.Synchronizer.SetChannel(channel))
            {
                channel.Abort();
            }

            return true;
        }

        public bool EnsureChannelForRequest()
        {
            return this.Synchronizer.EnsureChannel();
        }

        protected override void OnAbort()
        {
        }

        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback,
            object state)
        {
            return new CompletedAsyncResult(callback, state);
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback,
            object state)
        {
            return new CompletedAsyncResult(callback, state);
        }

        protected virtual IAsyncResult OnBeginRequest(TChannel channel, Message message,
            TimeSpan timeout, MaskingMode maskingMode, AsyncCallback callback, object state)
        {
            throw Fx.AssertAndThrow("The derived class does not support the OnBeginRequest operation.");
        }

        protected override void OnClose(TimeSpan timeout)
        {
        }

        protected override void OnEndClose(IAsyncResult result)
        {
            CompletedAsyncResult.End(result);
        }

        protected override void OnEndOpen(IAsyncResult result)
        {
            CompletedAsyncResult.End(result);
        }

        protected virtual Message OnEndRequest(TChannel channel, MaskingMode maskingMode,
            IAsyncResult result)
        {
            throw Fx.AssertAndThrow("The derived class does not support the OnEndRequest operation.");
        }

        protected override void OnOpen(TimeSpan timeout)
        {
        }

        protected virtual Message OnRequest(TChannel channel, Message message, TimeSpan timeout,
            MaskingMode maskingMode)
        {
            throw Fx.AssertAndThrow("The derived class does not support the OnRequest operation.");
        }

        public Message Request(Message message, TimeSpan timeout)
        {
            return this.Request(message, timeout, this.DefaultMaskingMode);
        }

        public Message Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
        {
            if (!this.ValidateOutputOperation(message, timeout, maskingMode))
            {
                return null;
            }

            bool autoAborted = false;

            try
            {
                TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
                TChannel channel;

                if (!this.Synchronizer.TryGetChannelForOutput(timeoutHelper.RemainingTime(), maskingMode,
                    out channel))
                {
                    if (!ReliableChannelBinderHelper.MaskHandled(maskingMode))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                            new TimeoutException(SR.GetString(SR.TimeoutOnRequest, timeout)));
                    }

                    return null;
                }

                if (channel == null)
                {
                    return null;
                }

                try
                {
                    return this.OnRequest(channel, message, timeoutHelper.RemainingTime(),
                        maskingMode);
                }
                finally
                {
                    autoAborted = this.Synchronizer.Aborting;
                    this.Synchronizer.ReturnChannel();
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                    throw;

                if (!this.HandleException(e, maskingMode, autoAborted))
                {
                    throw;
                }
                else
                {
                    return null;
                }
            }
        }

        protected override bool TryGetChannel(TimeSpan timeout)
        {
            CommunicationState currentState = this.State;
            TChannel channel = null;

            if ((currentState == CommunicationState.Created)
               || (currentState == CommunicationState.Opening)
               || (currentState == CommunicationState.Opened))
            {
                channel = this.factory.CreateChannel(this.to, this.via);
                if (!this.Synchronizer.SetChannel(channel))
                {
                    channel.Abort();
                }
            }
            else
            {
                channel = null;
            }

            return true;
        }

        abstract class DuplexClientReliableChannelBinder<TDuplexChannel>
            : ClientReliableChannelBinder<TDuplexChannel>
            where TDuplexChannel : class, IDuplexChannel
        {
            public DuplexClientReliableChannelBinder(EndpointAddress to, Uri via,
                IChannelFactory<TDuplexChannel> factory, MaskingMode maskingMode, TolerateFaultsMode faultMode,
                ChannelParameterCollection channelParameters,
                TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
                : base(to, via, factory, maskingMode, faultMode, channelParameters, defaultCloseTimeout,
                defaultSendTimeout)
            {
            }

            public override EndpointAddress LocalAddress
            {
                get
                {
                    IDuplexChannel channel = this.Synchronizer.CurrentChannel;
                    if (channel == null)
                        return null;
                    else
                        return channel.LocalAddress;
                }
            }

            public override EndpointAddress RemoteAddress
            {
                get
                {
                    IDuplexChannel channel = this.Synchronizer.CurrentChannel;
                    if (channel == null)
                        return null;
                    else
                        return channel.RemoteAddress;
                }
            }

            protected override IAsyncResult OnBeginSend(TDuplexChannel channel, Message message,
                TimeSpan timeout, AsyncCallback callback, object state)
            {
                return channel.BeginSend(message, timeout, callback, state);
            }

            protected override IAsyncResult OnBeginTryReceive(TDuplexChannel channel,
                TimeSpan timeout, AsyncCallback callback, object state)
            {
                return channel.BeginTryReceive(timeout, callback, state);
            }

            protected override void OnEndSend(TDuplexChannel channel, IAsyncResult result)
            {
                channel.EndSend(result);
            }

            protected override bool OnEndTryReceive(TDuplexChannel channel, IAsyncResult result,
                out RequestContext requestContext)
            {
                Message message;
                bool success = channel.EndTryReceive(result, out message);
                if (success && message == null)
                {
                    this.OnReadNullMessage();
                }
                requestContext = this.WrapMessage(message);
                return success;
            }

            protected virtual void OnReadNullMessage()
            {
            }

            protected override void OnSend(TDuplexChannel channel, Message message,
                TimeSpan timeout)
            {
                channel.Send(message, timeout);
            }

            protected override bool OnTryReceive(TDuplexChannel channel, TimeSpan timeout,
                out RequestContext requestContext)
            {
                Message message;
                bool success = channel.TryReceive(timeout, out message);
                if (success && message == null)
                {
                    this.OnReadNullMessage();
                }
                requestContext = this.WrapMessage(message);
                return success;
            }
        }

        sealed class DuplexClientReliableChannelBinder
            : DuplexClientReliableChannelBinder<IDuplexChannel>
        {
            public DuplexClientReliableChannelBinder(EndpointAddress to, Uri via,
                IChannelFactory<IDuplexChannel> factory, MaskingMode maskingMode,
                ChannelParameterCollection channelParameters,
                TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
                : base(to, via, factory, maskingMode, TolerateFaultsMode.Never, channelParameters,
                defaultCloseTimeout, defaultSendTimeout)
            {
            }

            public override bool HasSession
            {
                get
                {
                    return false;
                }
            }

            public override ISession GetInnerSession()
            {
                return null;
            }

            protected override bool HasSecuritySession(IDuplexChannel channel)
            {
                return false;
            }
        }

        sealed class DuplexSessionClientReliableChannelBinder
            : DuplexClientReliableChannelBinder<IDuplexSessionChannel>
        {
            public DuplexSessionClientReliableChannelBinder(EndpointAddress to, Uri via,
                IChannelFactory<IDuplexSessionChannel> factory, MaskingMode maskingMode, TolerateFaultsMode faultMode,
                ChannelParameterCollection channelParameters,
                TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
                : base(to, via, factory, maskingMode, faultMode, channelParameters, defaultCloseTimeout,
                defaultSendTimeout)
            {
            }

            public override bool HasSession
            {
                get
                {
                    return true;
                }
            }

            public override ISession GetInnerSession()
            {
                return this.Synchronizer.CurrentChannel.Session;
            }

            protected override IAsyncResult BeginCloseChannel(IDuplexSessionChannel channel,
                TimeSpan timeout, AsyncCallback callback, object state)
            {
                return ReliableChannelBinderHelper.BeginCloseDuplexSessionChannel(this, channel,
                    timeout, callback, state);
            }

            protected override void CloseChannel(IDuplexSessionChannel channel, TimeSpan timeout)
            {
                ReliableChannelBinderHelper.CloseDuplexSessionChannel(this, channel, timeout);
            }

            protected override void EndCloseChannel(IDuplexSessionChannel channel,
                IAsyncResult result)
            {
                ReliableChannelBinderHelper.EndCloseDuplexSessionChannel(channel, result);
            }

            protected override bool HasSecuritySession(IDuplexSessionChannel channel)
            {
                return channel.Session is ISecuritySession;
            }

            protected override void OnReadNullMessage()
            {
                this.Synchronizer.OnReadEof();
            }
        }

        abstract class RequestClientReliableChannelBinder<TRequestChannel>
            : ClientReliableChannelBinder<TRequestChannel>
            where TRequestChannel : class, IRequestChannel
        {
            InputQueue<Message> inputMessages;

            public RequestClientReliableChannelBinder(EndpointAddress to, Uri via,
                IChannelFactory<TRequestChannel> factory, MaskingMode maskingMode, TolerateFaultsMode faultMode,
                ChannelParameterCollection channelParameters,
                TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
                : base(to, via, factory, maskingMode, faultMode, channelParameters, defaultCloseTimeout,
                defaultSendTimeout)
            {
            }

            public override IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback,
                object state)
            {
                return this.GetInputMessages().BeginDequeue(timeout, callback, state);
            }

            public override bool EndTryReceive(IAsyncResult result,
                out RequestContext requestContext)
            {
                Message message;
                bool success = this.GetInputMessages().EndDequeue(result, out message);
                requestContext = this.WrapMessage(message);
                return success;
            }

            protected void EnqueueMessageIfNotNull(Message message)
            {
                if (message != null)
                {
                    this.GetInputMessages().EnqueueAndDispatch(message);
                }
            }

            InputQueue<Message> GetInputMessages()
            {
                lock (this.ThisLock)
                {
                    if (this.State == CommunicationState.Created)
                    {
                        throw Fx.AssertAndThrow("The method GetInputMessages() cannot be called when the binder is in the Created state.");
                    }

                    if (this.State == CommunicationState.Opening)
                    {
                        throw Fx.AssertAndThrow("The method GetInputMessages() cannot be called when the binder is in the Opening state.");
                    }

                    if (this.inputMessages == null)
                    {
                        this.inputMessages = TraceUtility.CreateInputQueue<Message>();
                    }
                }

                return this.inputMessages;
            }

            public override EndpointAddress LocalAddress
            {
                get
                {
                    return EndpointAddress.AnonymousAddress;
                }
            }

            public override EndpointAddress RemoteAddress
            {
                get
                {
                    IRequestChannel channel = this.Synchronizer.CurrentChannel;
                    if (channel == null)
                        return null;
                    else
                        return channel.RemoteAddress;
                }
            }

            protected override IAsyncResult OnBeginRequest(TRequestChannel channel,
                Message message, TimeSpan timeout, MaskingMode maskingMode,
                AsyncCallback callback, object state)
            {
                return channel.BeginRequest(message, timeout, callback, state);
            }

            protected override IAsyncResult OnBeginSend(TRequestChannel channel, Message message,
                TimeSpan timeout, AsyncCallback callback, object state)
            {
                return channel.BeginRequest(message, timeout, callback, state);
            }

            protected override Message OnEndRequest(TRequestChannel channel,
                MaskingMode maskingMode, IAsyncResult result)
            {
                return channel.EndRequest(result);
            }

            protected override void OnEndSend(TRequestChannel channel, IAsyncResult result)
            {
                Message message = channel.EndRequest(result);
                this.EnqueueMessageIfNotNull(message);
            }

            protected override Message OnRequest(TRequestChannel channel, Message message,
                TimeSpan timeout, MaskingMode maskingMode)
            {
                return channel.Request(message, timeout);
            }

            protected override void OnSend(TRequestChannel channel, Message message,
                TimeSpan timeout)
            {
                message = channel.Request(message, timeout);
                this.EnqueueMessageIfNotNull(message);
            }

            protected override void OnShutdown()
            {
                if (this.inputMessages != null)
                {
                    this.inputMessages.Close();
                }
            }

            public override bool TryReceive(TimeSpan timeout, out RequestContext requestContext)
            {
                Message message;
                bool success = this.GetInputMessages().Dequeue(timeout, out message);
                requestContext = this.WrapMessage(message);
                return success;
            }
        }

        sealed class RequestAsyncResult
            : ReliableChannelBinder<TChannel>.OutputAsyncResult<ClientReliableChannelBinder<TChannel>>
        {
            Message reply;

            public RequestAsyncResult(ClientReliableChannelBinder<TChannel> binder,
                AsyncCallback callback, object state)
                : base(binder, callback, state)
            {
            }

            protected override IAsyncResult BeginOutput(
                ClientReliableChannelBinder<TChannel> binder, TChannel channel, Message message,
                TimeSpan timeout, MaskingMode maskingMode, AsyncCallback callback, object state)
            {
                return binder.OnBeginRequest(channel, message, timeout, maskingMode, callback,
                    state);
            }

            public static Message End(IAsyncResult result)
            {
                RequestAsyncResult requestResult = AsyncResult.End<RequestAsyncResult>(result);
                return requestResult.reply;
            }

            protected override void EndOutput(ClientReliableChannelBinder<TChannel> binder,
                TChannel channel, MaskingMode maskingMode, IAsyncResult result)
            {
                this.reply = binder.OnEndRequest(channel, maskingMode, result);
            }

            protected override string GetTimeoutString(TimeSpan timeout)
            {
                return SR.GetString(SR.TimeoutOnRequest, timeout);
            }
        }

        sealed class RequestClientReliableChannelBinder
           : RequestClientReliableChannelBinder<IRequestChannel>
        {
            public RequestClientReliableChannelBinder(EndpointAddress to, Uri via,
                IChannelFactory<IRequestChannel> factory, MaskingMode maskingMode,
                ChannelParameterCollection channelParameters,
                TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
                : base(to, via, factory, maskingMode, TolerateFaultsMode.Never, channelParameters,
                defaultCloseTimeout, defaultSendTimeout)
            {
            }

            public override bool HasSession
            {
                get
                {
                    return false;
                }
            }

            public override ISession GetInnerSession()
            {
                return null;
            }

            protected override bool HasSecuritySession(IRequestChannel channel)
            {
                return false;
            }
        }

        sealed class RequestSessionClientReliableChannelBinder
            : RequestClientReliableChannelBinder<IRequestSessionChannel>
        {
            public RequestSessionClientReliableChannelBinder(EndpointAddress to, Uri via,
                IChannelFactory<IRequestSessionChannel> factory, MaskingMode maskingMode, TolerateFaultsMode faultMode,
                ChannelParameterCollection channelParameters,
                TimeSpan defaultCloseTimeout, TimeSpan defaultSendTimeout)
                : base(to, via, factory, maskingMode, faultMode, channelParameters, defaultCloseTimeout,
                defaultSendTimeout)
            {
            }

            public override bool HasSession
            {
                get
                {
                    return true;
                }
            }

            public override ISession GetInnerSession()
            {
                return this.Synchronizer.CurrentChannel.Session;
            }

            protected override bool HasSecuritySession(IRequestSessionChannel channel)
            {
                return channel.Session is ISecuritySession;
            }
        }
    }
}