Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / LayeredChannelListener.cs / 1 / LayeredChannelListener.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Channels
{
using System.Collections.Generic;
using System.ServiceModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel.Diagnostics;
using System.Text;
using System.Threading;
abstract class LayeredChannelListener
: ChannelListenerBase
where TChannel : class, IChannel
{
IChannelListener innerChannelListener;
bool sharedInnerListener;
EventHandler onInnerListenerFaulted;
protected LayeredChannelListener(IDefaultCommunicationTimeouts timeouts, IChannelListener innerChannelListener)
: this(false, timeouts, innerChannelListener)
{
}
protected LayeredChannelListener(bool sharedInnerListener)
: this(sharedInnerListener, null, null)
{
}
protected LayeredChannelListener(bool sharedInnerListener, IDefaultCommunicationTimeouts timeouts)
: this(sharedInnerListener, timeouts, null)
{
}
protected LayeredChannelListener(bool sharedInnerListener, IDefaultCommunicationTimeouts timeouts, IChannelListener innerChannelListener)
: base(timeouts)
{
this.sharedInnerListener = sharedInnerListener;
this.innerChannelListener = innerChannelListener;
this.onInnerListenerFaulted = new EventHandler(OnInnerListenerFaulted);
if (this.innerChannelListener != null)
{
this.innerChannelListener.Faulted += onInnerListenerFaulted;
}
}
internal virtual IChannelListener InnerChannelListener
{
get
{
return innerChannelListener;
}
set
{
lock (ThisLock)
{
ThrowIfDisposedOrImmutable();
if (this.innerChannelListener != null)
{
this.innerChannelListener.Faulted -= onInnerListenerFaulted;
}
this.innerChannelListener = value;
if (this.innerChannelListener != null)
{
this.innerChannelListener.Faulted += onInnerListenerFaulted;
}
}
}
}
internal bool SharedInnerListener
{
get { return sharedInnerListener; }
}
public override Uri Uri
{
get { return GetInnerListenerSnapshot().Uri; }
}
public override T GetProperty()
{
T baseProperty = base.GetProperty();
if (baseProperty != null)
{
return baseProperty;
}
IChannelListener channelListener = this.InnerChannelListener;
if (channelListener != null)
{
return channelListener.GetProperty();
}
else
{
return default(T);
}
}
protected override void OnAbort()
{
lock (ThisLock)
{
this.OnCloseOrAbort();
}
IChannelListener channelListener = this.InnerChannelListener;
if (channelListener != null && !sharedInnerListener)
{
channelListener.Abort();
}
}
protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
{
this.OnCloseOrAbort();
return new CloseAsyncResult(InnerChannelListener, sharedInnerListener, timeout, callback, state);
}
protected override void OnEndClose(IAsyncResult result)
{
CloseAsyncResult.End(result);
}
protected override void OnClose(TimeSpan timeout)
{
this.OnCloseOrAbort();
if (InnerChannelListener != null && !sharedInnerListener)
{
InnerChannelListener.Close(timeout);
}
}
void OnCloseOrAbort()
{
IChannelListener channelListener = this.InnerChannelListener;
if (channelListener != null)
{
channelListener.Faulted -= onInnerListenerFaulted;
}
}
protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
{
return new OpenAsyncResult(InnerChannelListener, sharedInnerListener, timeout, callback, state);
}
protected override void OnEndOpen(IAsyncResult result)
{
OpenAsyncResult.End(result);
}
protected override void OnOpen(TimeSpan timeout)
{
if (InnerChannelListener != null && !sharedInnerListener)
InnerChannelListener.Open(timeout);
}
protected override void OnOpening()
{
base.OnOpening();
ThrowIfInnerListenerNotSet();
}
void OnInnerListenerFaulted(object sender, EventArgs e)
{
// if our inner listener faulted, we should fault as well
this.Fault();
}
internal void ThrowIfInnerListenerNotSet()
{
if (this.InnerChannelListener == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.InnerListenerFactoryNotSet, this.GetType().ToString())));
}
}
internal IChannelListener GetInnerListenerSnapshot()
{
IChannelListener innerChannelListener = this.InnerChannelListener;
if (innerChannelListener == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.InnerListenerFactoryNotSet, this.GetType().ToString())));
}
return innerChannelListener;
}
class OpenAsyncResult : AsyncResult
{
ICommunicationObject communicationObject;
static AsyncCallback onOpenComplete = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnOpenComplete));
public OpenAsyncResult(ICommunicationObject communicationObject, bool sharedInnerListener, TimeSpan timeout, AsyncCallback callback, object state)
: base(callback, state)
{
this.communicationObject = communicationObject;
if (this.communicationObject == null || sharedInnerListener)
{
this.Complete(true);
return;
}
IAsyncResult result = this.communicationObject.BeginOpen(timeout, onOpenComplete, this);
if (result.CompletedSynchronously)
{
this.communicationObject.EndOpen(result);
this.Complete(true);
}
}
static void OnOpenComplete(IAsyncResult result)
{
if (result.CompletedSynchronously)
return;
OpenAsyncResult thisPtr = (OpenAsyncResult)result.AsyncState;
Exception exception = null;
try
{
thisPtr.communicationObject.EndOpen(result);
}
#pragma warning suppress 56500 // covered by FxCOP
catch (Exception e)
{
if (DiagnosticUtility.IsFatal(e))
throw;
exception = e;
}
thisPtr.Complete(false, exception);
}
public static void End(IAsyncResult result)
{
AsyncResult.End(result);
}
}
class CloseAsyncResult : AsyncResult
{
ICommunicationObject communicationObject;
static AsyncCallback onCloseComplete = DiagnosticUtility.ThunkAsyncCallback(new AsyncCallback(OnCloseComplete));
public CloseAsyncResult(ICommunicationObject communicationObject, bool sharedInnerListener, TimeSpan timeout, AsyncCallback callback, object state)
: base(callback, state)
{
this.communicationObject = communicationObject;
if (this.communicationObject == null || sharedInnerListener)
{
this.Complete(true);
return;
}
IAsyncResult result = this.communicationObject.BeginClose(timeout, onCloseComplete, this);
if (result.CompletedSynchronously)
{
this.communicationObject.EndClose(result);
this.Complete(true);
}
}
static void OnCloseComplete(IAsyncResult result)
{
if (result.CompletedSynchronously)
return;
CloseAsyncResult thisPtr = (CloseAsyncResult)result.AsyncState;
Exception exception = null;
try
{
thisPtr.communicationObject.EndClose(result);
}
#pragma warning suppress 56500 // covered by FxCOP
catch (Exception e)
{
if (DiagnosticUtility.IsFatal(e))
throw;
exception = e;
}
thisPtr.Complete(false, exception);
}
public static void End(IAsyncResult result)
{
AsyncResult.End(result);
}
}
}
abstract class LayeredChannelAcceptor : ChannelAcceptor
where TChannel : class, IChannel
where TInnerChannel : class, IChannel
{
IChannelListener innerListener;
protected LayeredChannelAcceptor(ChannelManagerBase channelManager, IChannelListener innerListener)
: base(channelManager)
{
this.innerListener = innerListener;
}
protected abstract TChannel OnAcceptChannel(TInnerChannel innerChannel);
public override TChannel AcceptChannel(TimeSpan timeout)
{
TInnerChannel innerChannel = this.innerListener.AcceptChannel(timeout);
if (innerChannel == null)
return null;
else
return OnAcceptChannel(innerChannel);
}
public override IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
{
return this.innerListener.BeginAcceptChannel(timeout, callback, state);
}
public override TChannel EndAcceptChannel(IAsyncResult result)
{
TInnerChannel innerChannel = this.innerListener.EndAcceptChannel(result);
if (innerChannel == null)
return null;
else
return OnAcceptChannel(innerChannel);
}
public override bool WaitForChannel(TimeSpan timeout)
{
return this.innerListener.WaitForChannel(timeout);
}
public override IAsyncResult BeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state)
{
return this.innerListener.BeginWaitForChannel(timeout, callback, state);
}
public override bool EndWaitForChannel(IAsyncResult result)
{
return this.innerListener.EndWaitForChannel(result);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DriveNotFoundException.cs
- OleTxTransaction.cs
- RankException.cs
- CollectionConverter.cs
- TableLayoutRowStyleCollection.cs
- PropertyItem.cs
- ReferentialConstraint.cs
- Function.cs
- TimerElapsedEvenArgs.cs
- grammarelement.cs
- XmlQueryRuntime.cs
- ServicePointManager.cs
- TraceContextRecord.cs
- LinearGradientBrush.cs
- EntityConnectionStringBuilder.cs
- TextRangeAdaptor.cs
- ObjectDataSourceFilteringEventArgs.cs
- PrivilegeNotHeldException.cs
- MatrixKeyFrameCollection.cs
- milrender.cs
- AutomationElementIdentifiers.cs
- XmlNamespaceMappingCollection.cs
- DocumentAutomationPeer.cs
- ToolboxComponentsCreatedEventArgs.cs
- CodeLabeledStatement.cs
- CommunicationObjectAbortedException.cs
- ScriptManager.cs
- CrossAppDomainChannel.cs
- XmlIterators.cs
- RoutedPropertyChangedEventArgs.cs
- ChannelCacheDefaults.cs
- DesigntimeLicenseContextSerializer.cs
- Encoding.cs
- DependentTransaction.cs
- QilParameter.cs
- ActivityMarkupSerializer.cs
- HtmlInputCheckBox.cs
- MethodExpr.cs
- AnnotationObservableCollection.cs
- CorruptStoreException.cs
- SoapHeaderAttribute.cs
- IgnoreFileBuildProvider.cs
- QueueProcessor.cs
- PageCodeDomTreeGenerator.cs
- ConfigXmlAttribute.cs
- GraphicsContainer.cs
- MessageQueueCriteria.cs
- XmlValidatingReader.cs
- DecoratedNameAttribute.cs
- LinearGradientBrush.cs
- HttpCacheParams.cs
- FieldAccessException.cs
- DropShadowBitmapEffect.cs
- PolicyException.cs
- DataGridTextBoxColumn.cs
- EFDataModelProvider.cs
- WebMessageBodyStyleHelper.cs
- DoubleIndependentAnimationStorage.cs
- TranslateTransform3D.cs
- OleDbFactory.cs
- shaperfactoryquerycacheentry.cs
- ViewService.cs
- ScriptDescriptor.cs
- ProbeDuplex11AsyncResult.cs
- ObjectListGeneralPage.cs
- ControlBuilderAttribute.cs
- StylusCollection.cs
- AbstractSvcMapFileLoader.cs
- _LazyAsyncResult.cs
- ScriptControl.cs
- FixedSOMTableRow.cs
- TcpChannelHelper.cs
- IIS7UserPrincipal.cs
- CompositeTypefaceMetrics.cs
- PropertyChangeTracker.cs
- UIHelper.cs
- TextShapeableCharacters.cs
- _SslSessionsCache.cs
- HttpDictionary.cs
- CodeSnippetExpression.cs
- SerialPort.cs
- XmlNodeChangedEventManager.cs
- PenCursorManager.cs
- SwitchAttribute.cs
- ProtocolsConfigurationHandler.cs
- SimpleType.cs
- DataExchangeServiceBinder.cs
- WebErrorHandler.cs
- ConnectionPoint.cs
- CharacterHit.cs
- ServiceModelSecurityTokenRequirement.cs
- SchemaNamespaceManager.cs
- TextEffect.cs
- DataTableClearEvent.cs
- Triangle.cs
- DataStorage.cs
- WindowsToolbar.cs
- HandlerMappingMemo.cs
- ExpressionValueEditor.cs
- DataGridLinkButton.cs