| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF |
| | | 12 | | { |
| | | 13 | | internal delegate void InstanceContextEmptyCallback(InstanceContext instanceContext); |
| | | 14 | | |
| | | 15 | | internal class ServiceChannelManager : LifetimeManager |
| | | 16 | | { |
| | | 17 | | private ICommunicationWaiter _activityWaiter; |
| | | 18 | | private int _activityWaiterCount; |
| | | 19 | | private readonly Action<InstanceContext> _emptyCallback; |
| | | 20 | | private IChannel _firstIncomingChannel; |
| | | 21 | | private ChannelCollection _incomingChannels; |
| | | 22 | | private ChannelCollection _outgoingChannels; |
| | | 23 | | private readonly InstanceContext _instanceContext; |
| | | 24 | | |
| | | 25 | | public ServiceChannelManager(InstanceContext instanceContext) |
| | 102 | 26 | | : this(instanceContext, null) |
| | | 27 | | { |
| | 102 | 28 | | } |
| | | 29 | | |
| | | 30 | | public ServiceChannelManager(InstanceContext instanceContext, Action<InstanceContext> emptyCallback) |
| | 2493 | 31 | | : base(instanceContext.ThisLock) |
| | | 32 | | { |
| | 2493 | 33 | | _instanceContext = instanceContext; |
| | 2493 | 34 | | _emptyCallback = emptyCallback; |
| | 2493 | 35 | | } |
| | | 36 | | |
| | 17344 | 37 | | public int ActivityCount { get; private set; } |
| | | 38 | | |
| | | 39 | | public ICollection<IChannel> IncomingChannels |
| | | 40 | | { |
| | | 41 | | get |
| | | 42 | | { |
| | 13 | 43 | | EnsureIncomingChannelCollection(); |
| | 13 | 44 | | return (ICollection<IChannel>)_incomingChannels; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public ICollection<IChannel> OutgoingChannels |
| | | 49 | | { |
| | | 50 | | get |
| | | 51 | | { |
| | 79 | 52 | | if (_outgoingChannels == null) |
| | | 53 | | { |
| | 68 | 54 | | lock (ThisLock) |
| | | 55 | | { |
| | 68 | 56 | | if (_outgoingChannels == null) |
| | | 57 | | { |
| | 68 | 58 | | _outgoingChannels = new ChannelCollection(this, ThisLock); |
| | | 59 | | } |
| | 68 | 60 | | } |
| | | 61 | | } |
| | 79 | 62 | | return _outgoingChannels; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public bool IsBusy |
| | | 67 | | { |
| | | 68 | | get |
| | | 69 | | { |
| | 2332 | 70 | | if (ActivityCount > 0) |
| | | 71 | | { |
| | 0 | 72 | | return true; |
| | | 73 | | } |
| | | 74 | | |
| | 2332 | 75 | | if (BusyCount > 0) |
| | | 76 | | { |
| | 0 | 77 | | return true; |
| | | 78 | | } |
| | | 79 | | |
| | 2332 | 80 | | ICollection<IChannel> outgoing = _outgoingChannels; |
| | 2332 | 81 | | if ((outgoing != null) && (outgoing.Count > 0)) |
| | | 82 | | { |
| | 0 | 83 | | return true; |
| | | 84 | | } |
| | | 85 | | |
| | 2332 | 86 | | return false; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public void AddIncomingChannel(IChannel channel) |
| | | 91 | | { |
| | 90 | 92 | | bool added = false; |
| | | 93 | | |
| | 90 | 94 | | lock (ThisLock) |
| | | 95 | | { |
| | 90 | 96 | | if (State == LifetimeState.Opened) |
| | | 97 | | { |
| | 90 | 98 | | if (_firstIncomingChannel == null) |
| | | 99 | | { |
| | 85 | 100 | | if (_incomingChannels == null) |
| | | 101 | | { |
| | 83 | 102 | | _firstIncomingChannel = channel; |
| | 83 | 103 | | ChannelAdded(channel); |
| | | 104 | | } |
| | | 105 | | else |
| | | 106 | | { |
| | 2 | 107 | | if (_incomingChannels.Contains(channel)) |
| | | 108 | | { |
| | 2 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | _incomingChannels.Add(channel); |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | 5 | 117 | | EnsureIncomingChannelCollection(); |
| | 5 | 118 | | if (_incomingChannels.Contains(channel)) |
| | | 119 | | { |
| | 5 | 120 | | return; |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | _incomingChannels.Add(channel); |
| | | 124 | | } |
| | 83 | 125 | | added = true; |
| | | 126 | | } |
| | 83 | 127 | | } |
| | | 128 | | |
| | 83 | 129 | | if (!added) |
| | | 130 | | { |
| | 0 | 131 | | channel.Abort(); |
| | 0 | 132 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().ToString |
| | | 133 | | } |
| | 90 | 134 | | } |
| | | 135 | | |
| | | 136 | | private void ChannelAdded(IChannel channel) |
| | | 137 | | { |
| | 88 | 138 | | base.IncrementBusyCount(); |
| | 88 | 139 | | channel.Closed += OnChannelClosed; |
| | 88 | 140 | | } |
| | | 141 | | |
| | | 142 | | private void ChannelRemoved(IChannel channel) |
| | | 143 | | { |
| | 84 | 144 | | channel.Closed -= OnChannelClosed; |
| | 84 | 145 | | DecrementBusyCount(); |
| | 84 | 146 | | } |
| | | 147 | | |
| | | 148 | | public async Task CloseInputAsync(CancellationToken token) |
| | | 149 | | { |
| | 2332 | 150 | | AsyncCommunicationWaiter activityWaiter = null; |
| | | 151 | | |
| | 2332 | 152 | | lock (ThisLock) |
| | | 153 | | { |
| | 2332 | 154 | | if (ActivityCount > 0) |
| | | 155 | | { |
| | 0 | 156 | | activityWaiter = new AsyncCommunicationWaiter(ThisLock); |
| | 0 | 157 | | if (!(_activityWaiter == null)) |
| | | 158 | | { |
| | | 159 | | Fx.Assert("ServiceChannelManager.CloseInput: (this.activityWaiter == null)"); |
| | | 160 | | } |
| | 0 | 161 | | _activityWaiter = activityWaiter; |
| | 0 | 162 | | Interlocked.Increment(ref _activityWaiterCount); |
| | | 163 | | } |
| | 2332 | 164 | | } |
| | | 165 | | |
| | 2332 | 166 | | if (activityWaiter != null) |
| | | 167 | | { |
| | 0 | 168 | | CommunicationWaitResult result = await activityWaiter.WaitAsync(false, token); |
| | 0 | 169 | | if (Interlocked.Decrement(ref _activityWaiterCount) == 0) |
| | | 170 | | { |
| | 0 | 171 | | activityWaiter.Dispose(); |
| | 0 | 172 | | _activityWaiter = null; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | switch (result) |
| | | 176 | | { |
| | | 177 | | case CommunicationWaitResult.Expired: |
| | 0 | 178 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.SfxCloseTimedO |
| | | 179 | | case CommunicationWaitResult.Aborted: |
| | 0 | 180 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType(). |
| | | 181 | | } |
| | | 182 | | } |
| | 2332 | 183 | | } |
| | | 184 | | |
| | | 185 | | public void DecrementActivityCount() |
| | | 186 | | { |
| | 2536 | 187 | | ICommunicationWaiter activityWaiter = null; |
| | 2536 | 188 | | bool empty = false; |
| | | 189 | | |
| | 2536 | 190 | | lock (ThisLock) |
| | | 191 | | { |
| | 2536 | 192 | | if (!(ActivityCount > 0)) |
| | | 193 | | { |
| | | 194 | | Fx.Assert("ServiceChannelManager.DecrementActivityCount: (this.activityCount > 0)"); |
| | | 195 | | } |
| | 2536 | 196 | | if (--ActivityCount == 0) |
| | | 197 | | { |
| | 2535 | 198 | | if (_activityWaiter != null) |
| | | 199 | | { |
| | 0 | 200 | | activityWaiter = _activityWaiter; |
| | 0 | 201 | | Interlocked.Increment(ref _activityWaiterCount); |
| | | 202 | | } |
| | 2535 | 203 | | if (BusyCount == 0) |
| | | 204 | | { |
| | 2430 | 205 | | empty = true; |
| | | 206 | | } |
| | | 207 | | } |
| | 2536 | 208 | | } |
| | | 209 | | |
| | 2536 | 210 | | if (activityWaiter != null) |
| | | 211 | | { |
| | 0 | 212 | | activityWaiter.Signal(); |
| | 0 | 213 | | if (Interlocked.Decrement(ref _activityWaiterCount) == 0) |
| | | 214 | | { |
| | 0 | 215 | | activityWaiter.Dispose(); |
| | 0 | 216 | | _activityWaiter = null; |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | 2536 | 220 | | if (empty && State == LifetimeState.Opened) |
| | | 221 | | { |
| | 2372 | 222 | | OnEmpty(); |
| | | 223 | | } |
| | 2536 | 224 | | } |
| | | 225 | | |
| | | 226 | | private void EnsureIncomingChannelCollection() |
| | | 227 | | { |
| | 18 | 228 | | lock (ThisLock) |
| | | 229 | | { |
| | 18 | 230 | | if (_incomingChannels == null) |
| | | 231 | | { |
| | 18 | 232 | | _incomingChannels = new ChannelCollection(this, ThisLock); |
| | 18 | 233 | | if (_firstIncomingChannel != null) |
| | | 234 | | { |
| | 5 | 235 | | _incomingChannels.Add(_firstIncomingChannel); |
| | 5 | 236 | | ChannelRemoved(_firstIncomingChannel); // Adding to collection called ChannelAdded, so call Chan |
| | 5 | 237 | | _firstIncomingChannel = null; |
| | | 238 | | } |
| | | 239 | | } |
| | 18 | 240 | | } |
| | 18 | 241 | | } |
| | | 242 | | |
| | | 243 | | public void IncrementActivityCount() |
| | | 244 | | { |
| | 2536 | 245 | | lock (ThisLock) |
| | | 246 | | { |
| | 2536 | 247 | | if (State == LifetimeState.Closed) |
| | | 248 | | { |
| | 0 | 249 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().ToSt |
| | | 250 | | } |
| | | 251 | | |
| | 2536 | 252 | | ActivityCount++; |
| | 2536 | 253 | | } |
| | 2536 | 254 | | } |
| | | 255 | | |
| | | 256 | | protected override void IncrementBusyCount() |
| | | 257 | | { |
| | 0 | 258 | | base.IncrementBusyCount(); |
| | 0 | 259 | | } |
| | | 260 | | |
| | | 261 | | protected override void OnAbort() |
| | | 262 | | { |
| | 58 | 263 | | IChannel[] channels = SnapshotChannels(); |
| | 116 | 264 | | for (int index = 0; index < channels.Length; index++) |
| | | 265 | | { |
| | 0 | 266 | | channels[index].Abort(); |
| | | 267 | | } |
| | | 268 | | |
| | 58 | 269 | | ICommunicationWaiter activityWaiter = null; |
| | | 270 | | |
| | 58 | 271 | | lock (ThisLock) |
| | | 272 | | { |
| | 58 | 273 | | if (_activityWaiter != null) |
| | | 274 | | { |
| | 0 | 275 | | activityWaiter = _activityWaiter; |
| | 0 | 276 | | Interlocked.Increment(ref _activityWaiterCount); |
| | | 277 | | } |
| | 58 | 278 | | } |
| | | 279 | | |
| | 58 | 280 | | if (activityWaiter != null) |
| | | 281 | | { |
| | 0 | 282 | | activityWaiter.Signal(); |
| | 0 | 283 | | if (Interlocked.Decrement(ref _activityWaiterCount) == 0) |
| | | 284 | | { |
| | 0 | 285 | | activityWaiter.Dispose(); |
| | 0 | 286 | | _activityWaiter = null; |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | |
| | 58 | 290 | | base.OnAbort(); |
| | 58 | 291 | | } |
| | | 292 | | |
| | | 293 | | protected override async Task OnCloseAsync(CancellationToken token) |
| | | 294 | | { |
| | 2332 | 295 | | await CloseInputAsync(token); |
| | 2332 | 296 | | await base.OnCloseAsync(token); |
| | 2332 | 297 | | } |
| | | 298 | | |
| | | 299 | | protected override void OnEmpty() |
| | | 300 | | { |
| | 2451 | 301 | | if (_emptyCallback != null) |
| | | 302 | | { |
| | 2332 | 303 | | _emptyCallback(_instanceContext); |
| | | 304 | | } |
| | 2451 | 305 | | } |
| | | 306 | | |
| | | 307 | | private void OnChannelClosed(object sender, EventArgs args) |
| | | 308 | | { |
| | 79 | 309 | | RemoveChannel((IChannel)sender); |
| | 79 | 310 | | } |
| | | 311 | | |
| | | 312 | | public bool RemoveChannel(IChannel channel) |
| | | 313 | | { |
| | 79 | 314 | | lock (ThisLock) |
| | | 315 | | { |
| | 79 | 316 | | if (_firstIncomingChannel == channel) |
| | | 317 | | { |
| | 74 | 318 | | _firstIncomingChannel = null; |
| | 74 | 319 | | ChannelRemoved(channel); |
| | 74 | 320 | | return true; |
| | | 321 | | } |
| | 5 | 322 | | else if (_incomingChannels != null && _incomingChannels.Contains(channel)) |
| | | 323 | | { |
| | 5 | 324 | | _incomingChannels.Remove(channel); |
| | 5 | 325 | | return true; |
| | | 326 | | } |
| | 0 | 327 | | else if (_outgoingChannels != null && _outgoingChannels.Contains(channel)) |
| | | 328 | | { |
| | 0 | 329 | | _outgoingChannels.Remove(channel); |
| | 0 | 330 | | return true; |
| | | 331 | | } |
| | 0 | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | return false; |
| | 79 | 335 | | } |
| | | 336 | | |
| | | 337 | | public IChannel[] SnapshotChannels() |
| | | 338 | | { |
| | 58 | 339 | | lock (ThisLock) |
| | | 340 | | { |
| | 58 | 341 | | int outgoingCount = (_outgoingChannels != null ? _outgoingChannels.Count : 0); |
| | | 342 | | |
| | 58 | 343 | | if (_firstIncomingChannel != null) |
| | | 344 | | { |
| | 0 | 345 | | IChannel[] channels = new IChannel[1 + outgoingCount]; |
| | 0 | 346 | | channels[0] = _firstIncomingChannel; |
| | 0 | 347 | | if (outgoingCount > 0) |
| | | 348 | | { |
| | 0 | 349 | | _outgoingChannels.CopyTo(channels, 1); |
| | | 350 | | } |
| | | 351 | | |
| | 0 | 352 | | return channels; |
| | | 353 | | } |
| | | 354 | | |
| | 58 | 355 | | if (_incomingChannels != null) |
| | | 356 | | { |
| | 0 | 357 | | IChannel[] channels = new IChannel[_incomingChannels.Count + outgoingCount]; |
| | 0 | 358 | | _incomingChannels.CopyTo(channels, 0); |
| | 0 | 359 | | if (outgoingCount > 0) |
| | | 360 | | { |
| | 0 | 361 | | _outgoingChannels.CopyTo(channels, _incomingChannels.Count); |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | return channels; |
| | | 365 | | } |
| | | 366 | | |
| | 58 | 367 | | if (outgoingCount > 0) |
| | | 368 | | { |
| | 0 | 369 | | IChannel[] channels = new IChannel[outgoingCount]; |
| | 0 | 370 | | _outgoingChannels.CopyTo(channels, 0); |
| | 0 | 371 | | return channels; |
| | | 372 | | } |
| | 58 | 373 | | } |
| | 58 | 374 | | return EmptyArray<IChannel>.Allocate(0); |
| | 0 | 375 | | } |
| | | 376 | | |
| | | 377 | | private class ChannelCollection : ICollection<IChannel> |
| | | 378 | | { |
| | | 379 | | private readonly ServiceChannelManager _channelManager; |
| | | 380 | | private readonly object _syncRoot; |
| | 86 | 381 | | private readonly HashSet<IChannel> _hashSet = new HashSet<IChannel>(); |
| | | 382 | | |
| | | 383 | | public bool IsReadOnly |
| | | 384 | | { |
| | 0 | 385 | | get { return false; } |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | public int Count |
| | | 389 | | { |
| | | 390 | | get |
| | | 391 | | { |
| | 35 | 392 | | lock (_syncRoot) |
| | | 393 | | { |
| | 35 | 394 | | return _hashSet.Count; |
| | | 395 | | } |
| | 35 | 396 | | } |
| | | 397 | | } |
| | | 398 | | |
| | 86 | 399 | | public ChannelCollection(ServiceChannelManager channelManager, object syncRoot) |
| | | 400 | | { |
| | 86 | 401 | | _channelManager = channelManager; |
| | 86 | 402 | | _syncRoot = syncRoot ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(syncRoot |
| | 86 | 403 | | } |
| | | 404 | | |
| | | 405 | | public void Add(IChannel channel) |
| | | 406 | | { |
| | 5 | 407 | | lock (_syncRoot) |
| | | 408 | | { |
| | 5 | 409 | | if (_hashSet.Add(channel)) |
| | | 410 | | { |
| | 5 | 411 | | _channelManager.ChannelAdded(channel); |
| | | 412 | | } |
| | 5 | 413 | | } |
| | 5 | 414 | | } |
| | | 415 | | |
| | | 416 | | public void Clear() |
| | | 417 | | { |
| | 0 | 418 | | lock (_syncRoot) |
| | | 419 | | { |
| | 0 | 420 | | foreach (IChannel channel in _hashSet) |
| | | 421 | | { |
| | 0 | 422 | | _channelManager.ChannelRemoved(channel); |
| | | 423 | | } |
| | | 424 | | |
| | 0 | 425 | | _hashSet.Clear(); |
| | 0 | 426 | | } |
| | 0 | 427 | | } |
| | | 428 | | |
| | | 429 | | public bool Contains(IChannel channel) |
| | | 430 | | { |
| | 25 | 431 | | lock (_syncRoot) |
| | | 432 | | { |
| | 25 | 433 | | if (channel != null) |
| | | 434 | | { |
| | 25 | 435 | | return _hashSet.Contains(channel); |
| | | 436 | | } |
| | 0 | 437 | | return false; |
| | | 438 | | } |
| | 25 | 439 | | } |
| | | 440 | | |
| | | 441 | | public void CopyTo(IChannel[] array, int arrayIndex) |
| | | 442 | | { |
| | 0 | 443 | | lock (_syncRoot) |
| | | 444 | | { |
| | 0 | 445 | | _hashSet.CopyTo(array, arrayIndex); |
| | 0 | 446 | | } |
| | 0 | 447 | | } |
| | | 448 | | |
| | | 449 | | public bool Remove(IChannel channel) |
| | | 450 | | { |
| | 84 | 451 | | lock (_syncRoot) |
| | | 452 | | { |
| | 84 | 453 | | bool ret = false; |
| | 84 | 454 | | if (channel != null) |
| | | 455 | | { |
| | 6 | 456 | | ret = _hashSet.Remove(channel); |
| | 6 | 457 | | if (ret) |
| | | 458 | | { |
| | 5 | 459 | | _channelManager.ChannelRemoved(channel); |
| | | 460 | | } |
| | | 461 | | } |
| | 84 | 462 | | return ret; |
| | | 463 | | } |
| | 84 | 464 | | } |
| | | 465 | | |
| | | 466 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| | | 467 | | { |
| | 0 | 468 | | lock (_syncRoot) |
| | | 469 | | { |
| | 0 | 470 | | return _hashSet.GetEnumerator(); |
| | | 471 | | } |
| | 0 | 472 | | } |
| | | 473 | | |
| | | 474 | | IEnumerator<IChannel> IEnumerable<IChannel>.GetEnumerator() |
| | | 475 | | { |
| | 0 | 476 | | lock (_syncRoot) |
| | | 477 | | { |
| | 0 | 478 | | return _hashSet.GetEnumerator(); |
| | | 479 | | } |
| | 0 | 480 | | } |
| | | 481 | | } |
| | | 482 | | } |
| | | 483 | | } |