File: InternalDuplexBindingElement.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 (273 lines) | stat: -rw-r--r-- 10,644 bytes parent folder | download
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Channels
{
    using System.Runtime;
    using System.ServiceModel.Dispatcher;

    sealed class InternalDuplexBindingElement : BindingElement
    {
        InputChannelDemuxer clientChannelDemuxer;
        bool providesCorrelation;

        public InternalDuplexBindingElement()
            : this(false)
        {
        }

        // 


        internal InternalDuplexBindingElement(bool providesCorrelation)
            : base()
        {
            this.providesCorrelation = providesCorrelation;
        }

        InternalDuplexBindingElement(InternalDuplexBindingElement elementToBeCloned)
            : base(elementToBeCloned)
        {
            this.clientChannelDemuxer = elementToBeCloned.ClientChannelDemuxer;
            this.providesCorrelation = elementToBeCloned.ProvidesCorrelation;
        }

        internal InputChannelDemuxer ClientChannelDemuxer
        {
            get { return this.clientChannelDemuxer; }
        }

        internal bool ProvidesCorrelation
        {
            get { return this.providesCorrelation; }
        }

        public override BindingElement Clone()
        {
            return new InternalDuplexBindingElement(this);
        }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            if (context == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
            }

            if (!this.CanBuildChannelFactory<TChannel>(context))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(
                    "TChannel", SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
            }

            IChannelFactory<IOutputChannel> innerChannelFactory = context.Clone().BuildInnerChannelFactory<IOutputChannel>();

            if (this.clientChannelDemuxer == null)
            {
                this.clientChannelDemuxer = new InputChannelDemuxer(context);
            }
            else
            {
#pragma warning suppress 56506 // [....], context.RemainingBindingElements will never be null
                context.RemainingBindingElements.Clear();
            }
            LocalAddressProvider localAddressProvider = context.BindingParameters.Remove<LocalAddressProvider>();
            return (IChannelFactory<TChannel>)(object)
                new InternalDuplexChannelFactory(this, context, this.clientChannelDemuxer, innerChannelFactory, localAddressProvider);
        }

        public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
        {
            if (context == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
            }

            if (typeof(TChannel) != typeof(IDuplexChannel))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("TChannel",
                    SR.GetString(SR.ChannelTypeNotSupported, typeof(TChannel)));
            }

            return (IChannelListener<TChannel>)(object)new InternalDuplexChannelListener(this, context);
        }

        public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
        {
            if (context == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");

            return typeof(TChannel) == typeof(IDuplexChannel)
                && context.CanBuildInnerChannelFactory<IOutputChannel>()
                && context.CanBuildInnerChannelListener<IInputChannel>();
        }

        public override bool CanBuildChannelListener<TChannel>(BindingContext context)
        {
            if (context == null)
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");

            return typeof(TChannel) == typeof(IDuplexChannel)
                && context.CanBuildInnerChannelFactory<IOutputChannel>()
                && context.CanBuildInnerChannelListener<IInputChannel>();
        }

        internal static T GetSecurityCapabilities<T>(ISecurityCapabilities lowerCapabilities)
        {
            Fx.Assert(typeof(T) == typeof(ISecurityCapabilities), "Can only be used with ISecurityCapabilities");
            if (lowerCapabilities != null)
            {
                // composite duplex cannot ensure that messages it receives are from the part it sends
                // messages to. So it cannot offer server auth
                return (T)(object)(new SecurityCapabilities(lowerCapabilities.SupportsClientAuthentication,
                    false, lowerCapabilities.SupportsClientWindowsIdentity, lowerCapabilities.SupportedRequestProtectionLevel,
                    System.Net.Security.ProtectionLevel.None));
            }
            else
            {
                return (T)(object)null;
            }
        }

        public override T GetProperty<T>(BindingContext context)
        {
            if (typeof(T) == typeof(ISecurityCapabilities) && !this.ProvidesCorrelation)
            {
                return InternalDuplexBindingElement.GetSecurityCapabilities<T>(context.GetInnerProperty<ISecurityCapabilities>());
            }
            else
            {
                return context.GetInnerProperty<T>();
            }
        }

        internal override bool IsMatch(BindingElement b)
        {
            if (b == null)
                return false;
            return (b is InternalDuplexBindingElement);
        }

        public static void AddDuplexFactorySupport(BindingContext context, ref InternalDuplexBindingElement internalDuplexBindingElement)
        {
            if (context.CanBuildInnerChannelFactory<IDuplexChannel>())
                return;
            if (context.RemainingBindingElements.Find<CompositeDuplexBindingElement>() == null)
                return;

            if (context.CanBuildInnerChannelFactory<IOutputChannel>() &&
                context.CanBuildInnerChannelListener<IInputChannel>())
            {
                if (context.CanBuildInnerChannelFactory<IRequestChannel>())
                    return;
                if (context.CanBuildInnerChannelFactory<IRequestSessionChannel>())
                    return;
                if (context.CanBuildInnerChannelFactory<IOutputSessionChannel>())
                    return;
                if (context.CanBuildInnerChannelFactory<IDuplexSessionChannel>())
                    return;

                if (internalDuplexBindingElement == null)
                    internalDuplexBindingElement = new InternalDuplexBindingElement();
                context.RemainingBindingElements.Insert(0, internalDuplexBindingElement);
            }
        }

        public static void AddDuplexListenerSupport(BindingContext context, ref InternalDuplexBindingElement internalDuplexBindingElement)
        {
            if (context.CanBuildInnerChannelListener<IDuplexChannel>())
                return;
            if (context.RemainingBindingElements.Find<CompositeDuplexBindingElement>() == null)
                return;

            if (context.CanBuildInnerChannelFactory<IOutputChannel>() &&
                context.CanBuildInnerChannelListener<IInputChannel>())
            {
                if (context.CanBuildInnerChannelListener<IReplyChannel>())
                    return;
                if (context.CanBuildInnerChannelListener<IReplySessionChannel>())
                    return;
                if (context.CanBuildInnerChannelListener<IInputSessionChannel>())
                    return;
                if (context.CanBuildInnerChannelListener<IDuplexSessionChannel>())
                    return;

                if (internalDuplexBindingElement == null)
                    internalDuplexBindingElement = new InternalDuplexBindingElement();
                context.RemainingBindingElements.Insert(0, internalDuplexBindingElement);
            }
        }

        public static void AddDuplexListenerSupport(CustomBinding binding, ref InternalDuplexBindingElement internalDuplexBindingElement)
        {
            if (binding.CanBuildChannelListener<IDuplexChannel>())
                return;
            if (binding.Elements.Find<CompositeDuplexBindingElement>() == null)
                return;

            if (binding.CanBuildChannelFactory<IOutputChannel>() &&
                binding.CanBuildChannelListener<IInputChannel>())
            {
                if (binding.CanBuildChannelListener<IReplyChannel>())
                    return;
                if (binding.CanBuildChannelListener<IReplySessionChannel>())
                    return;
                if (binding.CanBuildChannelListener<IInputSessionChannel>())
                    return;
                if (binding.CanBuildChannelListener<IDuplexSessionChannel>())
                    return;

                if (internalDuplexBindingElement == null)
                    internalDuplexBindingElement = new InternalDuplexBindingElement();
                binding.Elements.Insert(0, internalDuplexBindingElement);
            }
        }
    }

    class LocalAddressProvider
    {
        EndpointAddress localAddress;
        MessageFilter filter;
        int priority;

        public LocalAddressProvider(EndpointAddress localAddress, MessageFilter filter)
        {
            if (localAddress == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("localAddress");
            }
            if (filter == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("filter");
            }
            this.localAddress = localAddress;
            this.filter = filter;

            if (localAddress.Headers.FindHeader(XD.UtilityDictionary.UniqueEndpointHeaderName.Value,
                    XD.UtilityDictionary.UniqueEndpointHeaderNamespace.Value) == null)
            {
                this.priority = Int32.MaxValue - 1;
            }
            else
            {
                this.priority = Int32.MaxValue;
            }
        }

        public EndpointAddress LocalAddress
        {
            get { return this.localAddress; }
        }

        public MessageFilter Filter
        {
            get { return this.filter; }
        }

        public int Priority
        {
            get { return this.priority; }
        }
    }
}