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
- ConfigurationConverterBase.cs
- Keywords.cs
- PerformanceCounterPermission.cs
- XmlSchemaProviderAttribute.cs
- AssertSection.cs
- Compiler.cs
- basevalidator.cs
- WebHeaderCollection.cs
- SqlLiftIndependentRowExpressions.cs
- ListViewInsertionMark.cs
- ValidatorUtils.cs
- PropertyChangeTracker.cs
- StorageTypeMapping.cs
- SafeBitVector32.cs
- TreeViewDesigner.cs
- ReceiveMessageRecord.cs
- ArrayWithOffset.cs
- Substitution.cs
- WebScriptClientGenerator.cs
- DateTime.cs
- EdmType.cs
- DataGridViewIntLinkedList.cs
- ListViewItemMouseHoverEvent.cs
- SqlInternalConnectionSmi.cs
- PropertyCondition.cs
- CookieProtection.cs
- DecimalConverter.cs
- HttpConfigurationContext.cs
- ImpersonateTokenRef.cs
- SqlStream.cs
- DefaultPropertyAttribute.cs
- CounterCreationDataCollection.cs
- EncryptedType.cs
- WindowsSecurityTokenAuthenticator.cs
- SmtpSection.cs
- ValidationHelper.cs
- ScriptIgnoreAttribute.cs
- SizeValueSerializer.cs
- WebConfigurationFileMap.cs
- SqlInternalConnectionTds.cs
- SplashScreenNativeMethods.cs
- Assign.cs
- TcpAppDomainProtocolHandler.cs
- ValidationErrorCollection.cs
- XmlWhitespace.cs
- HtmlHead.cs
- SyntaxCheck.cs
- _SecureChannel.cs
- TextEvent.cs
- MdiWindowListItemConverter.cs
- ToolStripSplitButton.cs
- PolygonHotSpot.cs
- Number.cs
- SByteStorage.cs
- ThemeInfoAttribute.cs
- EarlyBoundInfo.cs
- Peer.cs
- LinqDataSource.cs
- Ipv6Element.cs
- Qualifier.cs
- XhtmlTextWriter.cs
- DataControlImageButton.cs
- RNGCryptoServiceProvider.cs
- RequestCache.cs
- Renderer.cs
- CompositeFontInfo.cs
- XhtmlTextWriter.cs
- Stack.cs
- BehaviorEditorPart.cs
- Utilities.cs
- ReferencedType.cs
- ScrollItemProviderWrapper.cs
- CollectionDataContractAttribute.cs
- ReservationCollection.cs
- CriticalExceptions.cs
- _ListenerAsyncResult.cs
- __Error.cs
- SectionInformation.cs
- MailFileEditor.cs
- InputLanguage.cs
- FixedFindEngine.cs
- ClrProviderManifest.cs
- DataGridViewCheckBoxCell.cs
- WsdlInspector.cs
- PropVariant.cs
- ServiceConfigurationTraceRecord.cs
- DBDataPermissionAttribute.cs
- CompilerScopeManager.cs
- SafeFileHandle.cs
- Int32.cs
- ReflectTypeDescriptionProvider.cs
- BinaryMethodMessage.cs
- FixedElement.cs
- XmlRootAttribute.cs
- PageStatePersister.cs
- CanExecuteRoutedEventArgs.cs
- TiffBitmapDecoder.cs
- DynamicResourceExtensionConverter.cs
- BatchStream.cs
- WorkflowInstanceProvider.cs