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
- DetailsViewPageEventArgs.cs
- EntityConnectionStringBuilder.cs
- AccessedThroughPropertyAttribute.cs
- SByte.cs
- DocobjHost.cs
- ExpandSegmentCollection.cs
- MembershipAdapter.cs
- HandleInitializationContext.cs
- LineVisual.cs
- InternalCache.cs
- PixelShader.cs
- RegexCharClass.cs
- InboundActivityHelper.cs
- RuntimeConfig.cs
- httpapplicationstate.cs
- TableParagraph.cs
- DynamicScriptObject.cs
- XmlResolver.cs
- MonitoringDescriptionAttribute.cs
- RadioButtonFlatAdapter.cs
- DbConnectionInternal.cs
- DiffuseMaterial.cs
- DirectionalLight.cs
- ValidatedControlConverter.cs
- Item.cs
- RectConverter.cs
- DataGridRow.cs
- PixelFormats.cs
- CharConverter.cs
- GridViewUpdatedEventArgs.cs
- CheckBoxStandardAdapter.cs
- ModuleConfigurationInfo.cs
- VSDExceptions.cs
- IndexedGlyphRun.cs
- ReferenceService.cs
- ObjectAnimationUsingKeyFrames.cs
- IsolatedStorageException.cs
- Documentation.cs
- RuntimeConfigLKG.cs
- HostingEnvironmentWrapper.cs
- DoubleLink.cs
- DependencyObjectType.cs
- X509CertificateCollection.cs
- WmlFormAdapter.cs
- BulletDecorator.cs
- EventPrivateKey.cs
- XmlWrappingReader.cs
- columnmapfactory.cs
- StyleTypedPropertyAttribute.cs
- LogConverter.cs
- X509Certificate2.cs
- MissingSatelliteAssemblyException.cs
- StackOverflowException.cs
- RegexCaptureCollection.cs
- XmlQueryOutput.cs
- DefaultAsyncDataDispatcher.cs
- BuildManager.cs
- InputScope.cs
- OrderByQueryOptionExpression.cs
- DecimalConverter.cs
- LayoutEditorPart.cs
- UrlAuthFailedErrorFormatter.cs
- SubclassTypeValidator.cs
- DefaultTextStoreTextComposition.cs
- InvokeMethodDesigner.xaml.cs
- DocumentViewerAutomationPeer.cs
- ScriptComponentDescriptor.cs
- GenericPrincipal.cs
- ModuleBuilder.cs
- WindowsFormsDesignerOptionService.cs
- Inflater.cs
- WizardPanelChangingEventArgs.cs
- ToolboxItemAttribute.cs
- UdpChannelFactory.cs
- ContextMenu.cs
- BaseTransportHeaders.cs
- CodeDefaultValueExpression.cs
- ListViewContainer.cs
- MarginCollapsingState.cs
- SerializationFieldInfo.cs
- AssemblySettingAttributes.cs
- _IPv4Address.cs
- XmlHierarchyData.cs
- ParseHttpDate.cs
- IntegerValidator.cs
- SendMailErrorEventArgs.cs
- QuaternionIndependentAnimationStorage.cs
- BaseParser.cs
- DateTimeUtil.cs
- QilCloneVisitor.cs
- CookieProtection.cs
- CompositionDesigner.cs
- HtmlInputPassword.cs
- ControlOperationBehavior.cs
- ReflectionTypeLoadException.cs
- UpdatePanelTriggerCollection.cs
- LinqMaximalSubtreeNominator.cs
- EntryWrittenEventArgs.cs
- CancelEventArgs.cs
- ThreadStaticAttribute.cs