Пример #1
0
        private HttpListenerWorkerRequest DequeueRequest(bool localOnly)
        {
            HttpListenerWorkerRequest wr = null;

            while (_count > 0)
            {
                lock (this)
                {
                    if (_localQueue.Count > 0)
                    {
                        wr = (HttpListenerWorkerRequest)_localQueue.Dequeue();
                        _count--;
                    }
                    else if (!localOnly && (_externQueue.Count > 0))
                    {
                        wr = (HttpListenerWorkerRequest)_externQueue.Dequeue();
                        _count--;
                    }
                }
                if (wr == null)
                {
                    return(wr);
                }
                if (CheckClientConnected(wr))
                {
                    return(wr);
                }
                _parent.RejectRequestNow(wr, true);
                wr = null;
            }
            return(wr);
        }
Пример #2
0
 private void QueueRequest(HttpListenerWorkerRequest wr, bool isLocal)
 {
     lock (this)
     {
         if (isLocal)
         {
             _localQueue.Enqueue(wr);
         }
         else
         {
             _externQueue.Enqueue(wr);
         }
         _count++;
     }
 }
Пример #3
0
        internal void Remove(HttpListenerWorkerRequest wr)
        {
            var timeoutLink = (RequestTimeoutEntry)wr.TimeoutLink;

            if (timeoutLink != null)
            {
                if (timeoutLink.DecrementCount() != 0)
                {
                    return;
                }
                timeoutLink.RemoveFromList();
                Interlocked.Decrement(ref _requestCount);
            }
            wr.TimeoutLink = null;
        }
Пример #4
0
        private static bool IsLocal(HttpListenerWorkerRequest wr)
        {
            string remoteAddress = wr.GetRemoteAddress();

            switch (remoteAddress)
            {
            case "127.0.0.1":
            case "::1":
                return(true);
            }
            if (string.IsNullOrEmpty(remoteAddress))
            {
                return(false);
            }
            return(remoteAddress == wr.GetLocalAddress());
        }
Пример #5
0
        internal void ProcessRequestNoDemand(HttpListenerWorkerRequest wr)
        {
            if (!UseThreading)
            {
                Parallel.Invoke(() => ProcessRequestNow(wr));
                return;
            }
            var queue = _requestQueue;

            if (queue != null)
            {
                wr = queue.GetRequestToExecute(wr);
            }
            if (wr != null)
            {
                ProcessRequestNow(wr);
            }
        }
Пример #6
0
 internal void Add(HttpListenerWorkerRequest wr)
 {
     if (wr.TimeoutLink != null)
     {
         ((RequestTimeoutEntry)wr.TimeoutLink).IncrementCount();
     }
     else
     {
         var entry = new RequestTimeoutEntry(wr);
         int index = _currentList++;
         if (index >= _lists.Length)
         {
             index        = 0;
             _currentList = 0;
         }
         entry.AddToList(_lists[index]);
         Interlocked.Increment(ref _requestCount);
         wr.TimeoutLink = entry;
     }
 }
Пример #7
0
        internal HttpListenerWorkerRequest GetRequestToExecute(HttpListenerWorkerRequest wr)
        {
            int workerThreads;
            int completionPortThreads;

            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            int threads = (completionPortThreads > workerThreads ? workerThreads : completionPortThreads);

            if ((threads < _minExternFreeThreads) || (_count != 0))
            {
                bool isLocal = IsLocal(wr);
                if (isLocal && (threads >= _minLocalFreeThreads) && (_count == 0))
                {
                    return(wr);
                }
                if (_count >= _queueLimit)
                {
                    _parent.RejectRequestNow(wr, false);
                    return(null);
                }
                QueueRequest(wr, isLocal);
                if (threads >= _minExternFreeThreads)
                {
                    wr = DequeueRequest(false);
                    return(wr);
                }
                if (threads >= _minLocalFreeThreads)
                {
                    wr = DequeueRequest(true);
                    return(wr);
                }
                wr = null;
                ScheduleMoreWorkIfNeeded();
            }
            return(wr);
        }
Пример #8
0
 internal virtual void RejectRequestNow(HttpListenerWorkerRequest wr, bool silent)
 {
 }
Пример #9
0
 internal virtual void ProcessRequestNow(HttpListenerWorkerRequest wr)
 {
 }
Пример #10
0
 private bool CheckClientConnected(HttpListenerWorkerRequest wr)
 {
     return((DateTime.UtcNow - wr.GetStartTime()) > _clientConnectedTime ? wr.IsClientConnected() : true);
 }
Пример #11
0
 internal RequestTimeoutEntry(HttpListenerWorkerRequest wr)
 {
     _wr    = wr;
     _count = 1;
 }