Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / SMSvcHost / System / ServiceModel / Activation / TransportListener.cs / 1 / TransportListener.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Activation { using System; using System.Threading; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Diagnostics; using System.Security; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Diagnostics; using System.ServiceModel.Activation.Diagnostics; class TransportListener { static byte[] drainBuffer; static Hashtable namedPipeInstances = new Hashtable(); static Hashtable tcpInstances = new Hashtable(); int count; ListenerConnectionDemuxer demuxer; ListenerConnectionDemuxer demuxerV6; TransportType transportType; TransportListener(IPEndPoint endPoint) { transportType = TransportType.Tcp; SocketSettings socketSettings = new SocketSettings(); IConnectionListener connectionListener = null; if (endPoint.Address.Equals(IPAddress.Broadcast)) { if (Socket.SupportsIPv4) { connectionListener = new SocketConnectionListener(new IPEndPoint(IPAddress.Any, endPoint.Port), socketSettings, true); demuxer = Go(connectionListener); } if (Socket.OSSupportsIPv6) { connectionListener = new SocketConnectionListener(new IPEndPoint(IPAddress.IPv6Any, endPoint.Port), socketSettings, true); demuxerV6 = Go(connectionListener); } } else { connectionListener = new SocketConnectionListener(endPoint, socketSettings, true); demuxer = Go(connectionListener); } } TransportListener(BaseUriWithWildcard pipeUri) { transportType = TransportType.NamedPipe; IConnectionListener connectionListener = new PipeConnectionListener(pipeUri.BaseAddress, pipeUri.HostNameComparisonMode, ListenerConstants.SharedConnectionBufferSize, null, false, int.MaxValue); demuxer = Go(connectionListener); } void AddRef() { ++count; } int DelRef() { return --count; } internal ListenerConnectionDemuxer Go(IConnectionListener connectionListener) { if (DiagnosticUtility.ShouldTraceInformation) { ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.TransportListenerListenRequest, this); } ConnectionHandleDuplicated onDupHandle = new ConnectionHandleDuplicated(OnDupHandle); ListenerConnectionDemuxer connectionDemuxer = null; if (transportType == TransportType.Tcp) { connectionDemuxer = new ListenerConnectionDemuxer(connectionListener, transportType, ListenerConfig.NetTcp.MaxPendingAccepts, ListenerConfig.NetTcp.MaxPendingConnections, ListenerConfig.NetTcp.ReceiveTimeout, onDupHandle); } else if (transportType == TransportType.NamedPipe) { connectionDemuxer = new ListenerConnectionDemuxer(connectionListener, transportType, ListenerConfig.NetPipe.MaxPendingAccepts, ListenerConfig.NetPipe.MaxPendingConnections, ListenerConfig.NetPipe.ReceiveTimeout, onDupHandle); } if (ExecutionContext.IsFlowSuppressed()) { if (SecurityContext.IsFlowSuppressed()) { connectionDemuxer.StartDemuxing(); } else { using (SecurityContext.SuppressFlow()) { connectionDemuxer.StartDemuxing(); } } } else { using (ExecutionContext.SuppressFlow()) { if (SecurityContext.IsFlowSuppressed()) { connectionDemuxer.StartDemuxing(); } else { using (SecurityContext.SuppressFlow()) { connectionDemuxer.StartDemuxing(); } } } } if (DiagnosticUtility.ShouldTraceInformation) { ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.TransportListenerListening, this); } return connectionDemuxer; } internal static void Listen(IPEndPoint endPoint) { lock (tcpInstances) { TransportListener t = tcpInstances[endPoint] as TransportListener; if (t != null) { // We use the shared TransportListener that is created earlier. t.AddRef(); } else { t = new TransportListener(endPoint); tcpInstances.Add(endPoint, t); t.AddRef(); } } } internal static void Listen(BaseUriWithWildcard pipeUri) { lock (namedPipeInstances) { if (namedPipeInstances.ContainsKey(pipeUri)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.GetString(SR.PipeAddressAlreadyUsed))); } else { TransportListener t = new TransportListener(pipeUri); namedPipeInstances.Add(pipeUri, t); } } } static TransportType GetTransportTypeAndAddress(IConnection connection, out IPAddress address, out int port) { Socket socket = connection.GetCoreTransport() as Socket; address = null; port = -1; TransportType transportType = TransportType.NamedPipe; if (socket != null) { address = (socket.LocalEndPoint as IPEndPoint).Address; port = (socket.LocalEndPoint as IPEndPoint).Port; transportType = TransportType.Tcp; } return transportType; } internal void OnDupHandle(ListenerSessionConnection session) { if (DiagnosticUtility.ShouldTraceInformation) { ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.TransportListenerSessionsReceived, this); } IPAddress address; int port; TransportType transportType = GetTransportTypeAndAddress(session.Connection, out address, out port); Debug.Print("TransportListener.OnDupHandle() via: " + session.Via.ToString() + " transportType: " + transportType); MessageQueue messageQueue = RoutingTable.Lookup(session.Via, address, port); if (messageQueue != null) { messageQueue.EnqueueSessionAndDispatch(session); } else { TransportListener.SendFault(session.Connection, FramingEncodingString.EndpointNotFoundFault); MessageQueue.OnDispatchFailure(transportType); } } static object ThisStaticLock { get { return tcpInstances; } } internal static void SendFault(IConnection connection, string fault) { if (drainBuffer == null) { lock (ThisStaticLock) { if (drainBuffer == null) { drainBuffer = new byte[1024]; } } } try { InitialServerConnectionReader.SendFault(connection, fault, drainBuffer, ListenerConstants.SharedSendTimeout, ListenerConstants.SharedMaxDrainSize); } catch (Exception exception) { if (DiagnosticUtility.IsFatal(exception)) { throw; } // We don't care the error when sending a fault. if (DiagnosticUtility.ShouldTraceWarning) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Warning); } } } void Stop() { demuxer.Dispose(); if (demuxerV6 != null) { demuxerV6.Dispose(); } } internal static void Stop(IPEndPoint endPoint) { lock (tcpInstances) { TransportListener t = tcpInstances[endPoint] as TransportListener; if (t != null) { if (t.DelRef() == 0) { if (DiagnosticUtility.ShouldTraceInformation) { ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.TransportListenerStop, t); } try { t.Stop(); } finally { tcpInstances.Remove(endPoint); } } } } } internal static void Stop(BaseUriWithWildcard pipeUri) { lock (namedPipeInstances) { TransportListener t = namedPipeInstances[pipeUri] as TransportListener; if (t != null) { if (DiagnosticUtility.ShouldTraceInformation) { ListenerTraceUtility.TraceEvent(TraceEventType.Information, TraceCode.TransportListenerStop, t); } try { t.Stop(); } finally { namedPipeInstances.Remove(pipeUri); } } } } } } // 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
- TableCellAutomationPeer.cs
- BatchParser.cs
- TextTreeUndo.cs
- LogStream.cs
- TableLayoutPanel.cs
- ObjectToIdCache.cs
- ClientData.cs
- StrokeCollection.cs
- ProcessModelInfo.cs
- ImageDrawing.cs
- DecimalConstantAttribute.cs
- EntityCommandExecutionException.cs
- TagPrefixCollection.cs
- SmtpNetworkElement.cs
- XmlSchemaSimpleTypeRestriction.cs
- Guid.cs
- SrgsDocumentParser.cs
- FrameworkObject.cs
- dsa.cs
- MenuBase.cs
- ColumnWidthChangingEvent.cs
- SortQuery.cs
- CustomWebEventKey.cs
- _NtlmClient.cs
- TextServicesPropertyRanges.cs
- AlgoModule.cs
- StorageComplexPropertyMapping.cs
- UIPropertyMetadata.cs
- SQLBoolean.cs
- Schema.cs
- ArraySubsetEnumerator.cs
- ElementMarkupObject.cs
- HandlerMappingMemo.cs
- VirtualPathProvider.cs
- Configuration.cs
- CodeAccessSecurityEngine.cs
- ControlPaint.cs
- ValidationPropertyAttribute.cs
- DoubleConverter.cs
- Misc.cs
- pingexception.cs
- WebBrowserNavigatingEventHandler.cs
- WorkflowRuntime.cs
- OnOperation.cs
- AdCreatedEventArgs.cs
- CreateRefExpr.cs
- SQLDecimalStorage.cs
- PropertyChangedEventManager.cs
- TriggerCollection.cs
- COAUTHIDENTITY.cs
- StringConverter.cs
- BaseCodeDomTreeGenerator.cs
- BamlLocalizerErrorNotifyEventArgs.cs
- LabelLiteral.cs
- HostVisual.cs
- RealProxy.cs
- CookieHandler.cs
- SessionEndingCancelEventArgs.cs
- RefreshPropertiesAttribute.cs
- ObjectFullSpanRewriter.cs
- ReadOnlyTernaryTree.cs
- StringSorter.cs
- MiniAssembly.cs
- DelegatingTypeDescriptionProvider.cs
- MasterPageParser.cs
- AuthenticationService.cs
- PageWrapper.cs
- Schema.cs
- ProtectedConfiguration.cs
- CallbackCorrelationInitializer.cs
- EventSinkHelperWriter.cs
- ComponentResourceKeyConverter.cs
- FormDocumentDesigner.cs
- TreeViewCancelEvent.cs
- BaseCodePageEncoding.cs
- StaticDataManager.cs
- FragmentNavigationEventArgs.cs
- OracleTransaction.cs
- DTCTransactionManager.cs
- WindowsGraphics.cs
- WebPartCancelEventArgs.cs
- HttpHeaderCollection.cs
- AttachInfo.cs
- DbConnectionStringCommon.cs
- DecodeHelper.cs
- X509CertificateCollection.cs
- WebSysDefaultValueAttribute.cs
- QueryModel.cs
- WindowsTab.cs
- MetadataItem_Static.cs
- RuleDefinitions.cs
- HtmlTableRow.cs
- FormClosingEvent.cs
- SoapFormatExtensions.cs
- CodeCommentStatementCollection.cs
- PersonalizationProvider.cs
- CodeDOMUtility.cs
- ChangeDirector.cs
- XmlSchemaDocumentation.cs
- XmlExtensionFunction.cs