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
- TiffBitmapDecoder.cs
- _SecureChannel.cs
- TranslateTransform.cs
- PerfCounterSection.cs
- UnmanagedMemoryStreamWrapper.cs
- DescendantBaseQuery.cs
- StyleHelper.cs
- PropertyChangeTracker.cs
- PocoPropertyAccessorStrategy.cs
- CodeIterationStatement.cs
- FlowLayoutPanelDesigner.cs
- DateTimeOffset.cs
- Soap12ProtocolReflector.cs
- BitmapSource.cs
- CancelRequestedRecord.cs
- InvalidDataException.cs
- ColorTransformHelper.cs
- PanelDesigner.cs
- SafeArrayRankMismatchException.cs
- OleDbInfoMessageEvent.cs
- Odbc32.cs
- Models.cs
- SqlParameterCollection.cs
- MessageSecurityException.cs
- InternalDuplexBindingElement.cs
- BaseUriHelper.cs
- VirtualizingStackPanel.cs
- RichTextBoxConstants.cs
- PasswordTextContainer.cs
- TreeView.cs
- PenLineCapValidation.cs
- ServiceBehaviorAttribute.cs
- EncoderBestFitFallback.cs
- SafeSecurityHelper.cs
- SqlUDTStorage.cs
- BackgroundWorker.cs
- IArgumentProvider.cs
- ellipse.cs
- ScriptingAuthenticationServiceSection.cs
- ModuleBuilder.cs
- RoutedPropertyChangedEventArgs.cs
- ConnectionsZone.cs
- Environment.cs
- Underline.cs
- X509CertificateTrustedIssuerElementCollection.cs
- StringDictionary.cs
- TreeView.cs
- EntityClassGenerator.cs
- DataGridViewSelectedCellCollection.cs
- FormsAuthenticationEventArgs.cs
- WmpBitmapDecoder.cs
- SmiEventSink_DeferedProcessing.cs
- PrintDocument.cs
- NavigateEvent.cs
- RepeaterItemEventArgs.cs
- CommandID.cs
- DelegateBodyWriter.cs
- ImpersonationContext.cs
- PasswordDeriveBytes.cs
- DelayLoadType.cs
- ReferenceEqualityComparer.cs
- HttpModulesSection.cs
- CounterSample.cs
- GeometryGroup.cs
- DispatchWrapper.cs
- ParamArrayAttribute.cs
- ClientSettingsStore.cs
- DateTimeFormat.cs
- RuntimeConfigurationRecord.cs
- DataRowCollection.cs
- ClientRoleProvider.cs
- DataErrorValidationRule.cs
- Symbol.cs
- DbUpdateCommandTree.cs
- ExpressionVisitorHelpers.cs
- ResXBuildProvider.cs
- ViewStateException.cs
- UnionExpr.cs
- PolicyFactory.cs
- x509utils.cs
- ListDesigner.cs
- DateTimeConstantAttribute.cs
- VScrollBar.cs
- TryCatchDesigner.xaml.cs
- FileLoadException.cs
- MessageQueueInstaller.cs
- PipelineModuleStepContainer.cs
- UserNameSecurityTokenAuthenticator.cs
- UriParserTemplates.cs
- QuaternionConverter.cs
- NavigationWindowAutomationPeer.cs
- listitem.cs
- WpfPayload.cs
- HttpInputStream.cs
- GlyphRunDrawing.cs
- sitestring.cs
- PolyLineSegmentFigureLogic.cs
- ByteStreamBufferedMessageData.cs
- HandlerMappingMemo.cs
- DoubleSumAggregationOperator.cs