Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / SMSvcHost / System / ServiceModel / Activation / ListenerConnectionDemuxer.cs / 1 / ListenerConnectionDemuxer.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Activation { using System; using System.Collections.Generic; using System.IO; using System.Diagnostics; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Activation.Diagnostics; using System.ServiceModel.Dispatcher; class ListenerConnectionDemuxer { ConnectionAcceptor acceptor; ListconnectionReaders; bool isDisposed; ListenerConnectionModeCallback onConnectionModeKnown; ConnectionClosedCallback onConnectionClosed; // this is the one provided by the caller ConnectionHandleDuplicated connectionHandleDuplicated; // this is the onw we use internally ViaDecodedCallback onViaDecoded; TransportType transportType; TimeSpan channelInitializationTimeout; public ListenerConnectionDemuxer(IConnectionListener listener, TransportType transportType, int maxAccepts, int initialMaxPendingConnections, TimeSpan channelInitializationTimeout, ConnectionHandleDuplicated connectionHandleDuplicated) { this.transportType = transportType; this.connectionReaders = new List (); this.connectionHandleDuplicated = connectionHandleDuplicated; this.acceptor = new ConnectionAcceptor(listener, maxAccepts, initialMaxPendingConnections, OnConnectionAvailable); this.channelInitializationTimeout = channelInitializationTimeout; this.onConnectionClosed = new ConnectionClosedCallback(OnConnectionClosed); this.onViaDecoded = new ViaDecodedCallback(OnViaDecoded); } object ThisLock { get { return this; } } public void Dispose() { lock (ThisLock) { if (isDisposed) return; isDisposed = true; } for (int i = 0; i < connectionReaders.Count; i++) { connectionReaders[i].Dispose(); } connectionReaders.Clear(); acceptor.Dispose(); } ListenerConnectionModeReader SetupModeReader(IConnection connection) { if (onConnectionModeKnown == null) { onConnectionModeKnown = new ListenerConnectionModeCallback(OnConnectionModeKnown); } ListenerConnectionModeReader modeReader = new ListenerConnectionModeReader(connection, onConnectionModeKnown, onConnectionClosed); lock (ThisLock) { if (isDisposed) { modeReader.Dispose(); return null; } else { connectionReaders.Add(modeReader); return modeReader; } } } void OnConnectionAvailable(IConnection connection, ItemDequeuedCallback connectionDequeuedCallback) { if (transportType == TransportType.Tcp) { ListenerPerfCounters.IncrementConnectionsAcceptedTcp(); } else { ListenerPerfCounters.IncrementConnectionsAcceptedNamedPipe(); } ListenerConnectionModeReader modeReader = SetupModeReader(connection); if (modeReader != null) { // StartReading() will never throw non-fatal exceptions; // it propagates all exceptions into the onConnectionModeKnown callback, // which is where we need our robust handling modeReader.StartReading(this.channelInitializationTimeout, connectionDequeuedCallback); } else { connectionDequeuedCallback(); } } void OnConnectionModeKnown(ListenerConnectionModeReader modeReader) { lock (ThisLock) { if (isDisposed) { return; } connectionReaders.Remove(modeReader); } try { FramingMode framingMode = modeReader.GetConnectionMode(); switch (framingMode) { case FramingMode.Duplex: OnDuplexConnection(modeReader); break; case FramingMode.Singleton: OnSingletonConnection(modeReader); break; default: { Exception inner = new InvalidDataException(SR.GetString( SR.FramingModeNotSupported, framingMode)); Exception exception = new ProtocolException(inner.Message, inner); FramingEncodingString.AddFaultString(exception, FramingEncodingString.UnsupportedModeFault); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception); } } } catch (ProtocolException exception) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information); } modeReader.Dispose(); } catch (Exception exception) { if (DiagnosticUtility.IsFatal(exception)) { throw; } if (DiagnosticUtility.ShouldTraceError) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Error); } // containment -- abort the errant reader modeReader.Dispose(); } } void OnViaDecoded(InitialServerConnectionReader connectionReader, ListenerSessionConnection session) { try { connectionHandleDuplicated(session); } finally { session.TriggerDequeuedCallback(); } lock (ThisLock) { if (isDisposed) { return; } connectionReaders.Remove(connectionReader); } } void OnConnectionClosed(InitialServerConnectionReader connectionReader) { lock (ThisLock) { if (isDisposed) { return; } connectionReaders.Remove(connectionReader); } } void OnSingletonConnection(ListenerConnectionModeReader modeReader) { ListenerSingletonConnectionReader singletonReader = new ListenerSingletonConnectionReader( modeReader.Connection, modeReader.GetConnectionDequeuedCallback(), transportType, modeReader.StreamPosition, modeReader.BufferOffset, modeReader.BufferSize, onConnectionClosed, onViaDecoded); lock (ThisLock) { if (isDisposed) { singletonReader.Dispose(); return; } connectionReaders.Add(singletonReader); } singletonReader.StartReading(modeReader.AccruedData, modeReader.GetRemainingTimeout()); } void OnDuplexConnection(ListenerConnectionModeReader modeReader) { ListenerSessionConnectionReader sessionReader = new ListenerSessionConnectionReader( modeReader.Connection, modeReader.GetConnectionDequeuedCallback(), transportType, modeReader.StreamPosition, modeReader.BufferOffset, modeReader.BufferSize, onConnectionClosed, onViaDecoded); lock (ThisLock) { if (isDisposed) { sessionReader.Dispose(); return; } connectionReaders.Add(sessionReader); } sessionReader.StartReading(modeReader.AccruedData, modeReader.GetRemainingTimeout()); } public void StartDemuxing() { acceptor.StartAccepting(); } } } // 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
- PageWrapper.cs
- ContextActivityUtils.cs
- ReadOnlyDictionary.cs
- SymLanguageType.cs
- RowsCopiedEventArgs.cs
- ComponentResourceKey.cs
- SqlServer2KCompatibilityAnnotation.cs
- FilteredDataSetHelper.cs
- updatecommandorderer.cs
- RootProjectionNode.cs
- ReadOnlyActivityGlyph.cs
- GPRECT.cs
- Column.cs
- CallbackDebugBehavior.cs
- ErrorHandler.cs
- PrintDialog.cs
- StrokeNodeData.cs
- GridViewColumnCollection.cs
- AttributeEmitter.cs
- DocumentXPathNavigator.cs
- CodeMemberMethod.cs
- DoubleIndependentAnimationStorage.cs
- KoreanLunisolarCalendar.cs
- TagMapCollection.cs
- TimelineClockCollection.cs
- BitmapEffectDrawingContextState.cs
- EntityContainerEntitySet.cs
- TimeSpanStorage.cs
- RowsCopiedEventArgs.cs
- ApplicationFileParser.cs
- RenderTargetBitmap.cs
- FileClassifier.cs
- _AutoWebProxyScriptWrapper.cs
- PropertyEmitterBase.cs
- Knowncolors.cs
- ReadOnlyDataSourceView.cs
- TabPage.cs
- Splitter.cs
- XPathScanner.cs
- XPathNavigatorException.cs
- PersonalizableAttribute.cs
- ContentPlaceHolder.cs
- CodeLinePragma.cs
- NetStream.cs
- MetafileHeader.cs
- CodeDefaultValueExpression.cs
- DecodeHelper.cs
- XslAst.cs
- CodeGenerationManager.cs
- SqlUtils.cs
- DrawToolTipEventArgs.cs
- FormatterServices.cs
- FontSource.cs
- DataGridColumnStyleMappingNameEditor.cs
- HtmlShimManager.cs
- RequestCachePolicy.cs
- ProfileParameter.cs
- SQLInt32Storage.cs
- DoubleAnimation.cs
- AttachmentCollection.cs
- XhtmlBasicTextViewAdapter.cs
- SafeViewOfFileHandle.cs
- Timer.cs
- Int32Animation.cs
- VirtualPath.cs
- HandlerBase.cs
- DropDownButton.cs
- DataPagerFieldCollection.cs
- PathSegment.cs
- FixedTextPointer.cs
- WebPartMenu.cs
- TextServicesDisplayAttribute.cs
- ObjectDataSourceFilteringEventArgs.cs
- OutputCacheSettingsSection.cs
- InputLanguageSource.cs
- AddInToken.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- SqlDataReaderSmi.cs
- State.cs
- NegationPusher.cs
- MemberCollection.cs
- FrameworkContentElement.cs
- PrintControllerWithStatusDialog.cs
- DomainLiteralReader.cs
- WMICapabilities.cs
- ArraySegment.cs
- BaseParagraph.cs
- InkPresenterAutomationPeer.cs
- EventKeyword.cs
- AddInStore.cs
- SafeViewOfFileHandle.cs
- SecurityTokenReferenceStyle.cs
- QilInvokeLateBound.cs
- WebDescriptionAttribute.cs
- NumberAction.cs
- AsyncCompletedEventArgs.cs
- PublisherMembershipCondition.cs
- LayoutTable.cs
- FontStretchConverter.cs
- ActiveXHelper.cs