Beispiel #1
0
        internal void RegisterContext(HttpListenerContext context)
        {
            lock (_ctxRegistrySync)
                _ctxRegistry[context] = context;

            HttpListenerAsyncResult ares = null;

            lock (_waitQueueSync) {
                if (_waitQueue.Count == 0)
                {
                    lock (_ctxQueueSync)
                        _ctxQueue.Add(context);
                }
                else
                {
                    ares = _waitQueue[0];
                    _waitQueue.RemoveAt(0);
                }
            }

            if (ares != null)
            {
                ares.Complete(context);
            }
        }
Beispiel #2
0
        private static void complete(HttpListenerAsyncResult asyncResult)
        {
            asyncResult._completed = true;

            var waitHandle = asyncResult._waitHandle;

            if (waitHandle != null)
            {
                waitHandle.Set();
            }

            var callback = asyncResult._callback;

            if (callback != null)
            {
                ThreadPool.UnsafeQueueUserWorkItem(
                    state => {
                    try {
                        callback(asyncResult);
                    }
                    catch {
                    }
                },
                    null);
            }
        }
Beispiel #3
0
        internal HttpListenerAsyncResult BeginGetContext(HttpListenerAsyncResult asyncResult)
        {
            CheckDisposed();
            if (_prefixes.Count == 0)
            {
                throw new InvalidOperationException("The listener has no URI prefix on which listens.");
            }

            if (!_listening)
            {
                throw new InvalidOperationException("The listener hasn't been started.");
            }

            // Lock _waitQueue early to avoid race conditions.
            lock (_waitQueueSync) {
                lock (_ctxQueueSync) {
                    var ctx = getContextFromQueue();
                    if (ctx != null)
                    {
                        asyncResult.Complete(ctx, true);
                        return(asyncResult);
                    }
                }

                _waitQueue.Add(asyncResult);
            }

            return(asyncResult);
        }
    private static void complete (HttpListenerAsyncResult asyncResult)
    {
      asyncResult._completed = true;

      var waitHandle = asyncResult._waitHandle;
      if (waitHandle != null)
        waitHandle.Set ();

      var callback = asyncResult._callback;
      if (callback != null)
        ThreadPool.UnsafeQueueUserWorkItem (
          state => {
            try {
              callback (asyncResult);
            }
            catch {
            }
          },
          null);
    }
Beispiel #5
0
    internal HttpListenerAsyncResult BeginGetContext (HttpListenerAsyncResult asyncResult)
    {
      CheckDisposed ();
      if (_prefixes.Count == 0)
        throw new InvalidOperationException ("The listener has no URI prefix on which listens.");

      if (!_listening)
        throw new InvalidOperationException ("The listener hasn't been started.");

      // Lock _waitQueue early to avoid race conditions.
      lock (_waitQueueSync) {
        lock (_ctxQueueSync) {
          var ctx = getContextFromQueue ();
          if (ctx != null) {
            asyncResult.Complete (ctx, true);
            return asyncResult;
          }
        }

        _waitQueue.Add (asyncResult);
      }

      return asyncResult;
    }