예제 #1
0
파일: NoIIS.cs 프로젝트: 13465220927/NoIIS
        /// <summary>
        /// The main-thread of NoIIS where all client requests are arrives.
        /// </summary>
        public static void runner()
        {
            // Set the min. number of threads for the thread-pool:
            ThreadPool.SetMinThreads(1000, 600);

            // Start maintaining:
            Task.Factory.StartNew(() => NoIISServer.maintenanceThread(), TaskCreationOptions.LongRunning);

            // The HTTP listener:
            var listener = new HttpListener();

            // Add all hosts to the listener as end-points:
            NoIISServer.hosts.ToList().ForEach((n) => { listener.Prefixes.Add(n); Console.WriteLine(n); });

            // Start listening:
            listener.Start();

            // Loop forever:
            while (true)
            {
                try
                {
                    // Force a maximum number of concurrent clients:
                    NoIISServer.semaphoreClients.Wait();

                    // Start the new client thread:
                    Task.Factory.StartNew(() => NoIISServer.clientThread(listener));
                }
                catch (Exception e)
                {
                    Console.WriteLine("There was an error for starting a client thread: " + e.Message);
                    Console.WriteLine(e.StackTrace);
                    NoIISServer.semaphoreClients.Release();
                }
            }
        }