| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.ServiceModel.Channels
- {
- using System.Collections.Generic;
- using System.Runtime;
- using System.ServiceModel.Diagnostics;
- abstract class InputQueueChannel<TDisposable> : ChannelBase
- where TDisposable : class, IDisposable
- {
- InputQueue<TDisposable> inputQueue;
- protected InputQueueChannel(ChannelManagerBase channelManager)
- : base(channelManager)
- {
- this.inputQueue = TraceUtility.CreateInputQueue<TDisposable>();
- }
- public int InternalPendingItems
- {
- get
- {
- return this.inputQueue.PendingCount;
- }
- }
- public int PendingItems
- {
- get
- {
- ThrowIfDisposedOrNotOpen();
- return InternalPendingItems;
- }
- }
- public void EnqueueAndDispatch(TDisposable item)
- {
- EnqueueAndDispatch(item, null);
- }
- public void EnqueueAndDispatch(TDisposable item, Action dequeuedCallback, bool canDispatchOnThisThread)
- {
- OnEnqueueItem(item);
- // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
- inputQueue.EnqueueAndDispatch(item, dequeuedCallback, canDispatchOnThisThread);
- }
- public void EnqueueAndDispatch(Exception exception, Action dequeuedCallback, bool canDispatchOnThisThread)
- {
- // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
- inputQueue.EnqueueAndDispatch(exception, dequeuedCallback, canDispatchOnThisThread);
- }
- public void EnqueueAndDispatch(TDisposable item, Action dequeuedCallback)
- {
- OnEnqueueItem(item);
- // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
- inputQueue.EnqueueAndDispatch(item, dequeuedCallback);
- }
- public bool EnqueueWithoutDispatch(Exception exception, Action dequeuedCallback)
- {
- // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
- return inputQueue.EnqueueWithoutDispatch(exception, dequeuedCallback);
- }
- public bool EnqueueWithoutDispatch(TDisposable item, Action dequeuedCallback)
- {
- OnEnqueueItem(item);
- // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
- return inputQueue.EnqueueWithoutDispatch(item, dequeuedCallback);
- }
- public void Dispatch()
- {
- // NOTE: don't need to check IsDisposed here: InputQueue will handle dispose
- inputQueue.Dispatch();
- }
- public void Shutdown()
- {
- inputQueue.Shutdown();
- }
- protected override void OnFaulted()
- {
- base.OnFaulted();
- inputQueue.Shutdown(() => this.GetPendingException());
- }
- protected virtual void OnEnqueueItem(TDisposable item)
- {
- }
- protected IAsyncResult BeginDequeue(TimeSpan timeout, AsyncCallback callback, object state)
- {
- this.ThrowIfNotOpened();
- return inputQueue.BeginDequeue(timeout, callback, state);
- }
- protected bool EndDequeue(IAsyncResult result, out TDisposable item)
- {
- bool dequeued = inputQueue.EndDequeue(result, out item);
- if (item == null)
- {
- this.ThrowIfFaulted();
- this.ThrowIfAborted();
- }
- return dequeued;
- }
- protected bool Dequeue(TimeSpan timeout, out TDisposable item)
- {
- this.ThrowIfNotOpened();
- bool dequeued = inputQueue.Dequeue(timeout, out item);
- if (item == null)
- {
- this.ThrowIfFaulted();
- this.ThrowIfAborted();
- }
- return dequeued;
- }
- protected bool WaitForItem(TimeSpan timeout)
- {
- this.ThrowIfNotOpened();
- bool dequeued = inputQueue.WaitForItem(timeout);
- this.ThrowIfFaulted();
- this.ThrowIfAborted();
- return dequeued;
- }
- protected IAsyncResult BeginWaitForItem(TimeSpan timeout, AsyncCallback callback, object state)
- {
- this.ThrowIfNotOpened();
- return inputQueue.BeginWaitForItem(timeout, callback, state);
- }
- protected bool EndWaitForItem(IAsyncResult result)
- {
- bool dequeued = inputQueue.EndWaitForItem(result);
- this.ThrowIfFaulted();
- this.ThrowIfAborted();
- return dequeued;
- }
- protected override void OnClosing()
- {
- base.OnClosing();
- inputQueue.Shutdown(() => this.GetPendingException());
- }
- protected override void OnAbort()
- {
- inputQueue.Close();
- }
- protected override void OnClose(TimeSpan timeout)
- {
- inputQueue.Close();
- }
- protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
- {
- inputQueue.Close();
- return new CompletedAsyncResult(callback, state);
- }
- protected override void OnEndClose(IAsyncResult result)
- {
- CompletedAsyncResult.End(result);
- }
- }
- }
|