Exemplo n.º 1
0
 public PathListenerProviderBase(IHostListenerFactory hostListenerFactory, Uri uri, ISecurityFactory securityFactory)
 {
     using (EneterTrace.Entering())
     {
         myHostListenerFactory = hostListenerFactory;
         Address           = uri;
         mySecurityFactory = securityFactory;
     }
 }
        /// <summary>
        /// Starts listening for the given URI path.
        /// </summary>
        /// <remarks>
        /// The listening consists of two parts:
        /// => Host listener - TCP listening on an address and port.
        /// => Path listener - based on the above protocol (HTTP or WebSocket) listening to the path.
        ///
        /// If the URI contains hostname instead of the IP address then it resolves the host name.
        /// But the result can be multiple addresses. E.g. for localhost it can return IPV4: 127.0.0.1 and IPV6: [::1].
        /// In sach case it will try to start listening to all addresses associated with the host name.
        /// If start listening fails for one of those addresses then StartListening throws exception.
        ///
        /// </remarks>
        /// <param name="address"></param>
        /// <param name="hostListenerFactory"></param>
        /// <param name="connectionHandler"></param>
        /// <param name="serverSecurityFactory"></param>
        public static void StartListening(Uri address,
                                          IHostListenerFactory hostListenerFactory,
                                          object connectionHandler,
                                          ISecurityFactory serverSecurityFactory)
        {
            using (EneterTrace.Entering())
            {
                try
                {
                    using (ThreadLock.Lock(myListeners))
                    {
                        // Get all possible end points for the given hostname/address.
                        IEnumerable <IPEndPoint> anEndPoints = GetEndPoints(address);
                        foreach (IPEndPoint anEndPoint in anEndPoints)
                        {
                            // Try to get existing host listener for the endpoint.
                            HostListenerBase aHostListener = GetHostListener(anEndPoint);
                            if (aHostListener == null)
                            {
                                // The host listener does not exist so create it.
                                aHostListener = hostListenerFactory.CreateHostListener(anEndPoint, serverSecurityFactory);

                                // Register the path listener.
                                aHostListener.RegisterListener(address, connectionHandler);

                                myListeners.Add(aHostListener);
                            }
                            else
                            {
                                // If found listener is listening to another protocol.
                                // e.g. if I want to start listening to http but websocket listener is listening on
                                //      the given IP address and port.
                                if (aHostListener.GetType() != hostListenerFactory.ListenerType)
                                {
                                    string anErrorMessage = TracedObject + "failed to start " + hostListenerFactory.ListenerType + " because " + aHostListener.GetType() + " is already listening on IP address and port.";
                                    EneterTrace.Error(anErrorMessage);
                                    throw new InvalidOperationException(anErrorMessage);
                                }

                                // Register the path listener.
                                aHostListener.RegisterListener(address, connectionHandler);
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "failed to start listening.", err);
                    throw;
                }
            }
        }
Exemplo n.º 3
0
 public PathListenerProviderBase(IHostListenerFactory hostListenerFactory, Uri uri)
     : this(hostListenerFactory, uri, new NonSecurityFactory())
 {
 }