Exemplo n.º 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);
            }
        }
Exemplo n.º 2
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);
        }