TransportListener.cs source code in C# .NET

Source code for the .NET framework in C#

                        

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

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK