File: EndpointDispatcher.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 (339 lines) | stat: -rw-r--r-- 11,351 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
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.ServiceModel.Dispatcher
{
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Diagnostics;

    public class EndpointDispatcher
    {
        MessageFilter addressFilter;
        bool addressFilterSetExplicit;
        ChannelDispatcher channelDispatcher;
        MessageFilter contractFilter;
        string contractName;
        string contractNamespace;
        ServiceChannel datagramChannel;
        DispatchRuntime dispatchRuntime;
        MessageFilter endpointFilter;
        int filterPriority;
        Uri listenUri;
        EndpointAddress originalAddress;
        string perfCounterId;
        string perfCounterBaseId;
        string id; // for ServiceMetadataBehavior, to help get EndpointIdentity of ServiceEndpoint from EndpointDispatcher
        bool isSystemEndpoint;

        internal EndpointDispatcher(EndpointAddress address, string contractName, string contractNamespace, string id, bool isSystemEndpoint)
            : this(address, contractName, contractNamespace)
        {
            this.id = id;
            this.isSystemEndpoint = isSystemEndpoint;
        }

        public EndpointDispatcher(EndpointAddress address, string contractName, string contractNamespace)
            : this(address, contractName, contractNamespace, false)
        {
        }

        public EndpointDispatcher(EndpointAddress address, string contractName, string contractNamespace, bool isSystemEndpoint)
        {
            this.originalAddress = address;
            this.contractName = contractName;
            this.contractNamespace = contractNamespace;

            if (address != null)
            {
                this.addressFilter = new EndpointAddressMessageFilter(address);
            }
            else
            {
                this.addressFilter = new MatchAllMessageFilter();
            }

            this.contractFilter = new MatchAllMessageFilter();
            this.dispatchRuntime = new DispatchRuntime(this);
            this.filterPriority = 0;
            this.isSystemEndpoint = isSystemEndpoint;
        }

        EndpointDispatcher(EndpointDispatcher baseEndpoint, IEnumerable<AddressHeader> headers)
        {
            EndpointAddressBuilder builder = new EndpointAddressBuilder(baseEndpoint.EndpointAddress);
            foreach (AddressHeader h in headers)
            {
                builder.Headers.Add(h);
            }
            EndpointAddress address = builder.ToEndpointAddress();

            this.addressFilter = new EndpointAddressMessageFilter(address);
            // channelDispatcher is Attached
            this.contractFilter = baseEndpoint.ContractFilter;
            this.contractName = baseEndpoint.ContractName;
            this.contractNamespace = baseEndpoint.ContractNamespace;
            this.dispatchRuntime = baseEndpoint.DispatchRuntime;
            // endpointFilter is lazy
            this.filterPriority = baseEndpoint.FilterPriority + 1;
            this.originalAddress = address;
            if (PerformanceCounters.PerformanceCountersEnabled)
            {
                this.perfCounterId = baseEndpoint.perfCounterId;
                this.perfCounterBaseId = baseEndpoint.perfCounterBaseId;
            }
            this.id = baseEndpoint.id;
        }

        public MessageFilter AddressFilter
        {
            get { return this.addressFilter; }
            set
            {
                if (value == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
                }
                this.ThrowIfDisposedOrImmutable();
                this.addressFilter = value;
                this.addressFilterSetExplicit = true;
            }
        }

        internal bool AddressFilterSetExplicit
        {
            get { return this.addressFilterSetExplicit; }
        }

        public ChannelDispatcher ChannelDispatcher
        {
            get { return this.channelDispatcher; }
        }

        public MessageFilter ContractFilter
        {
            get { return this.contractFilter; }
            set
            {
                if (value == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
                }
                this.ThrowIfDisposedOrImmutable();
                this.contractFilter = value;
            }
        }

        public string ContractName
        {
            get { return this.contractName; }
        }

        public string ContractNamespace
        {
            get { return this.contractNamespace; }
        }

        internal ServiceChannel DatagramChannel
        {
            get { return this.datagramChannel; }
            set { this.datagramChannel = value; }
        }

        public DispatchRuntime DispatchRuntime
        {
            get { return this.dispatchRuntime; }
        }

        internal Uri ListenUri
        {
            get { return this.listenUri; }
        }

        internal EndpointAddress OriginalAddress
        {
            get { return this.originalAddress; }
        }

        public EndpointAddress EndpointAddress
        {
            get
            {
                if (this.channelDispatcher == null)
                {
                    return this.originalAddress;
                }

                if ((this.originalAddress != null) && (this.originalAddress.Identity != null))
                {
                    return this.originalAddress;
                }

                IChannelListener listener = this.channelDispatcher.Listener;
                EndpointIdentity identity = listener.GetProperty<EndpointIdentity>();
                if ((this.originalAddress != null) && (identity == null))
                {
                    return this.originalAddress;
                }

                EndpointAddressBuilder builder;
                if (this.originalAddress != null)
                {
                    builder = new EndpointAddressBuilder(this.originalAddress);
                }
                else
                {
                    builder = new EndpointAddressBuilder();
                    builder.Uri = listener.Uri;
                }
                builder.Identity = identity;
                return builder.ToEndpointAddress();
            }
        }

        public bool IsSystemEndpoint
        {
            get { return this.isSystemEndpoint; }
        }

        internal MessageFilter EndpointFilter
        {
            get
            {
                if (this.endpointFilter == null)
                {
                    MessageFilter addressFilter = this.addressFilter;
                    MessageFilter contractFilter = this.contractFilter;

                    // Can't optimize addressFilter similarly.
                    // AndMessageFilter tracks when the address filter matched so the correct
                    // fault can be sent back.
                    if (contractFilter is MatchAllMessageFilter)
                    {
                        this.endpointFilter = addressFilter;
                    }
                    else
                    {
                        this.endpointFilter = new AndMessageFilter(addressFilter, contractFilter);
                    }
                }
                return this.endpointFilter;
            }
        }

        public int FilterPriority
        {
            get { return this.filterPriority; }
            set { this.filterPriority = value; }
        }

        internal string Id
        {
            get { return this.id; }
            set { this.id = value; }
        }

        internal string PerfCounterId
        {
            get { return this.perfCounterId; }
        }

        internal string PerfCounterBaseId
        {
            get { return this.perfCounterBaseId; }
        }

        internal int PerfCounterInstanceId { get; set; }

        static internal EndpointDispatcher AddEndpointDispatcher(EndpointDispatcher baseEndpoint,
                                                                 IEnumerable<AddressHeader> headers)
        {
            EndpointDispatcher endpoint = new EndpointDispatcher(baseEndpoint, headers);
            baseEndpoint.ChannelDispatcher.Endpoints.Add(endpoint);
            return endpoint;
        }

        internal void Attach(ChannelDispatcher channelDispatcher)
        {
            if (channelDispatcher == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("channelDispatcher");
            }

            if (this.channelDispatcher != null)
            {
                Exception error = new InvalidOperationException(SR.GetString(SR.SFxEndpointDispatcherMultipleChannelDispatcher0));
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
            }

            this.channelDispatcher = channelDispatcher;
            this.listenUri = channelDispatcher.Listener.Uri;
        }

        internal void Detach(ChannelDispatcher channelDispatcher)
        {
            if (channelDispatcher == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("channelDispatcher");
            }

            if (this.channelDispatcher != channelDispatcher)
            {
                Exception error = new InvalidOperationException(SR.GetString(SR.SFxEndpointDispatcherDifferentChannelDispatcher0));
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
            }

            this.ReleasePerformanceCounters();
            this.channelDispatcher = null;
        }

        internal void ReleasePerformanceCounters()
        {
            if (PerformanceCounters.PerformanceCountersEnabled)
            {
                PerformanceCounters.ReleasePerformanceCountersForEndpoint(this.perfCounterId, this.perfCounterBaseId);
            }
        }

        internal bool SetPerfCounterId()
        {
            Uri keyUri = null;
            if (null != this.ListenUri)
            {
                keyUri = this.ListenUri;
            }
            else
            {
                EndpointAddress endpointAddress = this.EndpointAddress;
                if (null != endpointAddress)
                {
                    keyUri = endpointAddress.Uri;
                }
            }

            if (null != keyUri)
            {
                this.perfCounterBaseId = keyUri.AbsoluteUri.ToUpperInvariant();
                this.perfCounterId = this.perfCounterBaseId + "/" + contractName.ToUpperInvariant();

                return true;
            }
            else
            {
                return false;
            }
        }

        void ThrowIfDisposedOrImmutable()
        {
            ChannelDispatcher channelDispatcher = this.channelDispatcher;
            if (channelDispatcher != null)
            {
                channelDispatcher.ThrowIfDisposedOrImmutable();
            }
        }
    }
}