Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / SingletonChannelAcceptor.cs / 1 / SingletonChannelAcceptor.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.ServiceModel.Diagnostics; abstract class SingletonChannelAcceptor: InputQueueChannelAcceptor where ChannelInterfaceType : class, IChannel where TChannel : /*ChannelInterfaceType,*/ InputQueueChannel where QueueItemType : class, IDisposable { TChannel currentChannel; object currentChannelLock = new object(); static WaitCallback onInvokeDequeuedCallback; public SingletonChannelAcceptor(ChannelManagerBase channelManager) : base(channelManager) { } public override ChannelInterfaceType AcceptChannel(TimeSpan timeout) { EnsureChannelAvailable(); return base.AcceptChannel(timeout); } public override IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state) { EnsureChannelAvailable(); return base.BeginAcceptChannel(timeout, callback, state); } protected TChannel GetCurrentChannel() { return this.currentChannel; } TChannel EnsureChannelAvailable() { bool channelCreated = false; TChannel newChannel; if ((newChannel = currentChannel) == null) { lock (currentChannelLock) { if (IsDisposed) { return null; } if ((newChannel = currentChannel) == null) { newChannel = OnCreateChannel(); newChannel.Closed += OnChannelClosed; currentChannel = newChannel; channelCreated = true; } } } if (channelCreated) { EnqueueAndDispatch((ChannelInterfaceType)(object)newChannel); } return newChannel; } protected abstract TChannel OnCreateChannel(); protected abstract void OnTraceMessageReceived(QueueItemType item); public void DispatchItems() { TChannel channel = EnsureChannelAvailable(); if (channel != null) { channel.Dispatch(); } } public void Enqueue(QueueItemType item) { Enqueue(item, null); } public void Enqueue(QueueItemType item, ItemDequeuedCallback dequeuedCallback) { Enqueue(item, dequeuedCallback, true); } public void Enqueue(QueueItemType item, ItemDequeuedCallback dequeuedCallback, bool canDispatchOnThisThread) { TChannel channel = EnsureChannelAvailable(); if (DiagnosticUtility.ShouldTraceInformation) { OnTraceMessageReceived(item); } if (channel != null) { channel.EnqueueAndDispatch(item, dequeuedCallback, canDispatchOnThisThread); } else { InvokeDequeuedCallback(dequeuedCallback, canDispatchOnThisThread); item.Dispose(); } } public void Enqueue(Exception exception, ItemDequeuedCallback dequeuedCallback) { Enqueue(exception, dequeuedCallback, true); } public void Enqueue(Exception exception, ItemDequeuedCallback dequeuedCallback, bool canDispatchOnThisThread) { TChannel channel = EnsureChannelAvailable(); if (channel != null) { channel.EnqueueAndDispatch(exception, dequeuedCallback, canDispatchOnThisThread); } else { InvokeDequeuedCallback(dequeuedCallback, canDispatchOnThisThread); } } public bool EnqueueWithoutDispatch(QueueItemType item, ItemDequeuedCallback dequeuedCallback) { TChannel channel = EnsureChannelAvailable(); if (DiagnosticUtility.ShouldTraceInformation) { OnTraceMessageReceived(item); } if (channel != null) { return channel.EnqueueWithoutDispatch(item, dequeuedCallback); } else { InvokeDequeuedCallback(dequeuedCallback, false); item.Dispose(); return false; } } public override bool EnqueueWithoutDispatch(Exception exception, ItemDequeuedCallback dequeuedCallback) { TChannel channel = EnsureChannelAvailable(); if (channel != null) { return channel.EnqueueWithoutDispatch(exception, dequeuedCallback); } else { InvokeDequeuedCallback(dequeuedCallback, false); return false; } } public void EnqueueAndDispatch(QueueItemType item, ItemDequeuedCallback dequeuedCallback, bool canDispatchOnThisThread) { TChannel channel = EnsureChannelAvailable(); if (DiagnosticUtility.ShouldTraceInformation) { OnTraceMessageReceived(item); } if (channel != null) { channel.EnqueueAndDispatch(item, dequeuedCallback, canDispatchOnThisThread); } else { InvokeDequeuedCallback(dequeuedCallback, canDispatchOnThisThread); item.Dispose(); } } public override void EnqueueAndDispatch(Exception exception, ItemDequeuedCallback dequeuedCallback, bool canDispatchOnThisThread) { TChannel channel = EnsureChannelAvailable(); if (channel != null) { channel.EnqueueAndDispatch(exception, dequeuedCallback, canDispatchOnThisThread); } else { InvokeDequeuedCallback(dequeuedCallback, canDispatchOnThisThread); } } protected void OnChannelClosed(object sender, EventArgs args) { IChannel channel = (IChannel)sender; lock (currentChannelLock) { if (channel == currentChannel) { currentChannel = null; } } } static void InvokeDequeuedCallback(ItemDequeuedCallback dequeuedCallback, bool canDispatchOnThisThread) { if (dequeuedCallback != null) { if (canDispatchOnThisThread) { dequeuedCallback(); return; } if (onInvokeDequeuedCallback == null) { onInvokeDequeuedCallback = new WaitCallback(OnInvokeDequeuedCallback); } IOThreadScheduler.ScheduleCallback(onInvokeDequeuedCallback, dequeuedCallback); } } static void OnInvokeDequeuedCallback(object state) { DiagnosticUtility.DebugAssert(state != null, "SingletonChannelAcceptor.OnInvokeDequeuedCallback: (state != null)"); ItemDequeuedCallback dequeuedCallback = (ItemDequeuedCallback)state; dequeuedCallback(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
![Network programming in C#, Network Programming in VB.NET, Network Programming in .NET](/images/book.jpg)
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ConnectionStringsExpressionBuilder.cs
- TextElement.cs
- AnimationException.cs
- ColorIndependentAnimationStorage.cs
- WindowsRichEditRange.cs
- RuleSettingsCollection.cs
- BaseResourcesBuildProvider.cs
- CompositionAdorner.cs
- XPathItem.cs
- Vector3DCollection.cs
- FileRecordSequenceHelper.cs
- DefinitionUpdate.cs
- Helpers.cs
- BitStream.cs
- WebPartTransformer.cs
- ToolStripScrollButton.cs
- ActivityExecutorDelegateInfo.cs
- SerializationEventsCache.cs
- ScaleTransform3D.cs
- HttpConfigurationSystem.cs
- ObjectStorage.cs
- InsufficientMemoryException.cs
- ArrangedElementCollection.cs
- ServiceDescriptionImporter.cs
- OracleSqlParser.cs
- GreenMethods.cs
- CompressedStack.cs
- MethodExpr.cs
- PerformanceCounterManager.cs
- SolidColorBrush.cs
- ThicknessConverter.cs
- CorrelationResolver.cs
- MessageSmuggler.cs
- InvalidProgramException.cs
- XmlTextAttribute.cs
- AuthenticationModulesSection.cs
- MergablePropertyAttribute.cs
- SetIterators.cs
- NGCSerializer.cs
- NamedObject.cs
- AuthenticatingEventArgs.cs
- FigureParagraph.cs
- MonthChangedEventArgs.cs
- SqlLiftIndependentRowExpressions.cs
- SafeHandles.cs
- DesignBindingConverter.cs
- ExternalDataExchangeService.cs
- ServiceBehaviorElement.cs
- File.cs
- SqlCacheDependencyDatabaseCollection.cs
- WindowsStatusBar.cs
- LineBreak.cs
- CallbackHandler.cs
- MsmqHostedTransportManager.cs
- TextRangeProviderWrapper.cs
- AppDomain.cs
- DesignColumn.cs
- MemberNameValidator.cs
- Thread.cs
- RelationshipEndCollection.cs
- MetadataSerializer.cs
- StructuralComparisons.cs
- ImageEditor.cs
- CheckBox.cs
- ImageListStreamer.cs
- GuidelineCollection.cs
- AppDomainAttributes.cs
- DataSvcMapFile.cs
- XmlDataSourceNodeDescriptor.cs
- RoleManagerSection.cs
- Group.cs
- BoolLiteral.cs
- KeyboardNavigation.cs
- MessageSecurityOverMsmqElement.cs
- PopupControlService.cs
- GenericRootAutomationPeer.cs
- AttachedPropertyBrowsableForChildrenAttribute.cs
- EmbeddedMailObjectsCollection.cs
- BlurBitmapEffect.cs
- DataGridViewTextBoxEditingControl.cs
- TableLayoutPanel.cs
- TextElement.cs
- DataServiceException.cs
- XmlCustomFormatter.cs
- BaseValidator.cs
- RuntimeConfigLKG.cs
- PseudoWebRequest.cs
- LayoutEditorPart.cs
- X509Certificate2.cs
- DispatcherSynchronizationContext.cs
- BCLDebug.cs
- DispatcherHookEventArgs.cs
- TabPanel.cs
- PageParser.cs
- ComplexBindingPropertiesAttribute.cs
- OdbcConnectionString.cs
- SourceSwitch.cs
- AttributeQuery.cs
- SqlCachedBuffer.cs
- QueryOperationResponseOfT.cs