| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using CoreWCF.Channels; |
| | | 6 | | using CoreWCF.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Dispatcher |
| | | 9 | | { |
| | | 10 | | internal class EndpointDispatcherTable |
| | | 11 | | { |
| | | 12 | | private MessageFilterTable<EndpointDispatcher> _filters; |
| | | 13 | | private const int optimizationThreshold = 2; |
| | | 14 | | private List<EndpointDispatcher> _cachedEndpoints; |
| | | 15 | | |
| | 663 | 16 | | public EndpointDispatcherTable(object thisLock) |
| | | 17 | | { |
| | 663 | 18 | | ThisLock = thisLock; |
| | 663 | 19 | | } |
| | | 20 | | |
| | | 21 | | public int Count |
| | | 22 | | { |
| | | 23 | | get |
| | | 24 | | { |
| | 0 | 25 | | return ((_cachedEndpoints != null) ? _cachedEndpoints.Count : 0) + |
| | 0 | 26 | | ((_filters != null) ? _filters.Count : 0); |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | 663 | 30 | | private object ThisLock { get; } |
| | | 31 | | |
| | | 32 | | public void AddEndpoint(EndpointDispatcher endpoint) |
| | | 33 | | { |
| | 661 | 34 | | lock (ThisLock) |
| | | 35 | | { |
| | 661 | 36 | | MessageFilter filter = endpoint.EndpointFilter; |
| | 661 | 37 | | int priority = endpoint.FilterPriority; |
| | | 38 | | |
| | 661 | 39 | | if (_filters == null) |
| | | 40 | | { |
| | 661 | 41 | | if (_cachedEndpoints == null) |
| | | 42 | | { |
| | 660 | 43 | | _cachedEndpoints = new List<EndpointDispatcher>(optimizationThreshold); |
| | | 44 | | } |
| | | 45 | | |
| | 661 | 46 | | if (_cachedEndpoints.Count < optimizationThreshold) |
| | | 47 | | { |
| | 661 | 48 | | _cachedEndpoints.Add(endpoint); |
| | | 49 | | } |
| | | 50 | | else |
| | | 51 | | { |
| | 0 | 52 | | _filters = new MessageFilterTable<EndpointDispatcher>(); |
| | 0 | 53 | | for (int i = 0; i < _cachedEndpoints.Count; i++) |
| | | 54 | | { |
| | 0 | 55 | | int cachedPriority = _cachedEndpoints[i].FilterPriority; |
| | 0 | 56 | | MessageFilter cachedFilter = _cachedEndpoints[i].EndpointFilter; |
| | 0 | 57 | | _filters.Add(cachedFilter, _cachedEndpoints[i], cachedPriority); |
| | | 58 | | } |
| | 0 | 59 | | _filters.Add(filter, endpoint, priority); |
| | 0 | 60 | | _cachedEndpoints = null; |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | else |
| | | 64 | | { |
| | 0 | 65 | | _filters.Add(filter, endpoint, priority); |
| | | 66 | | } |
| | 0 | 67 | | } |
| | 661 | 68 | | } |
| | | 69 | | |
| | | 70 | | public void RemoveEndpoint(EndpointDispatcher endpoint) |
| | | 71 | | { |
| | 0 | 72 | | lock (ThisLock) |
| | | 73 | | { |
| | 0 | 74 | | if (_filters == null) |
| | | 75 | | { |
| | 0 | 76 | | if (_cachedEndpoints != null && _cachedEndpoints.Contains(endpoint)) |
| | | 77 | | { |
| | 0 | 78 | | _cachedEndpoints.Remove(endpoint); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | else |
| | | 82 | | { |
| | 0 | 83 | | MessageFilter filter = endpoint.EndpointFilter; |
| | 0 | 84 | | _filters.Remove(filter); |
| | | 85 | | } |
| | 0 | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | private EndpointDispatcher LookupInCache(Message message, out bool addressMatched) |
| | | 90 | | { |
| | 2520 | 91 | | EndpointDispatcher result = null; |
| | 2520 | 92 | | int priority = int.MinValue; |
| | 2520 | 93 | | bool duplicatePriority = false; |
| | 2520 | 94 | | addressMatched = false; |
| | | 95 | | |
| | 2520 | 96 | | if (_cachedEndpoints != null && _cachedEndpoints.Count > 0) |
| | | 97 | | { |
| | 10080 | 98 | | for (int i = 0; i < _cachedEndpoints.Count; i++) |
| | | 99 | | { |
| | 2520 | 100 | | EndpointDispatcher cachedEndpoint = _cachedEndpoints[i]; |
| | 2520 | 101 | | int cachedPriority = cachedEndpoint.FilterPriority; |
| | 2520 | 102 | | MessageFilter cachedFilter = cachedEndpoint.EndpointFilter; |
| | | 103 | | |
| | | 104 | | bool matchResult; |
| | 2520 | 105 | | if (cachedFilter is AndMessageFilter andFilter) |
| | | 106 | | { |
| | 2475 | 107 | | matchResult = andFilter.Match(message, out bool addressResult); |
| | 2475 | 108 | | addressMatched |= addressResult; |
| | | 109 | | } |
| | | 110 | | else |
| | | 111 | | { |
| | 45 | 112 | | matchResult = cachedFilter.Match(message); |
| | | 113 | | } |
| | | 114 | | |
| | 2520 | 115 | | if (matchResult) |
| | | 116 | | { |
| | 2516 | 117 | | addressMatched = true; |
| | 2516 | 118 | | if (cachedPriority > priority || result == null) |
| | | 119 | | { |
| | 2516 | 120 | | result = cachedEndpoint; |
| | 2516 | 121 | | priority = cachedPriority; |
| | 2516 | 122 | | duplicatePriority = false; |
| | | 123 | | } |
| | 0 | 124 | | else if (cachedPriority == priority && result != null) |
| | | 125 | | { |
| | 0 | 126 | | duplicatePriority = true; |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | 2520 | 132 | | if (duplicatePriority) |
| | | 133 | | { |
| | 0 | 134 | | throw TraceUtility.ThrowHelperError(new MultipleFilterMatchesException(SR.FilterMultipleMatches), messag |
| | | 135 | | } |
| | | 136 | | |
| | 2520 | 137 | | return result; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | public EndpointDispatcher Lookup(Message message, out bool addressMatched) |
| | | 141 | | { |
| | 2518 | 142 | | EndpointDispatcher data = LookupInCache(message, out addressMatched); |
| | 2518 | 143 | | if (data == null) |
| | | 144 | | { |
| | 2 | 145 | | lock (ThisLock) |
| | | 146 | | { |
| | 2 | 147 | | data = LookupInCache(message, out addressMatched); |
| | | 148 | | |
| | 2 | 149 | | if (data == null && _filters != null) |
| | | 150 | | { |
| | 0 | 151 | | _filters.GetMatchingValue(message, out data, out addressMatched); |
| | | 152 | | } |
| | 2 | 153 | | } |
| | | 154 | | } |
| | | 155 | | |
| | 2518 | 156 | | return data; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | } |