Exemplo n.º 1
0
        public Task StopAsync(CancellationToken cancellationToken)
        {
            void RegisterCancelation()
            {
                cancellationToken.Register(() =>
                {
                    if (Interlocked.Exchange(ref _shutdownSignalCompleted, 1) == 0)
                    {
                        LogHelper.LogInfo(_logger, "Canceled, terminating " + _outstandingRequests + " request(s).");
                        _shutdownSignal.TrySetResult(null);
                    }
                });
            }

            if (Interlocked.Exchange(ref _stopping, 1) == 1)
            {
                RegisterCancelation();

                return(_shutdownSignal.Task);
            }

            try
            {
                // Wait for active requests to drain
                if (_outstandingRequests > 0)
                {
                    LogHelper.LogInfo(_logger, "Stopping, waiting for " + _outstandingRequests + " request(s) to drain.");
                    RegisterCancelation();
                }
                else
                {
                    _shutdownSignal.TrySetResult(null);
                }
            }
            catch (Exception ex)
            {
                _shutdownSignal.TrySetException(ex);
            }

            return(_shutdownSignal.Task);
        }
        /// <summary>
        /// Start accepting incoming requests.
        /// </summary>
        public void Start()
        {
            CheckDisposed();

            LogHelper.LogInfo(Logger, nameof(Start));

            // Make sure there are no race conditions between Start/Stop/Abort/Close/Dispose.
            // Start needs to setup all resources. Abort/Stop must not interfere while Start is
            // allocating those resources.
            lock (_internalLock)
            {
                try
                {
                    CheckDisposed();
                    if (_state == State.Started)
                    {
                        return;
                    }

                    try
                    {
                        foreach (var urlPrefix in Options.UrlPrefixes)
                        {
                            var rcb = new RelayConnectionStringBuilder();

                            var tokenProvider = urlPrefix.TokenProvider != null ? urlPrefix.TokenProvider : Options.TokenProvider;
                            if (tokenProvider == null)
                            {
                                throw new InvalidOperationException("No relay token provider defined.");
                            }
                            var relayListener = new HybridConnectionListener(
                                new UriBuilder(urlPrefix.FullPrefix)
                            {
                                Scheme = "sb", Port = -1
                            }.Uri, tokenProvider);

                            relayListener.RequestHandler = (ctx) => requestHandler(new RequestContext(ctx, new Uri(urlPrefix.FullPrefix)));
                            // TODO: CR: An accept handler which simply returns true is the same as no handler at all.
                            // Would returning false and rejecting relayed connection requests be better?
                            relayListener.AcceptHandler = WebSocketAcceptHandler;
                            _relayListeners.Add(relayListener);
                        }
                    }
                    catch (Exception exception)
                    {
                        LogHelper.LogException(Logger, ".Ctor", exception);
                        throw;
                    }
                    foreach (var listener in _relayListeners)
                    {
                        listener.OpenAsync().GetAwaiter().GetResult();
                    }
                    _state = State.Started;
                }
                catch (Exception exception)
                {
                    // Make sure the HttpListener instance can't be used if Start() failed.
                    _state = State.Disposed;
                    LogHelper.LogException(Logger, nameof(Start), exception);
                    throw;
                }
            }
        }
Exemplo n.º 3
0
 internal bool UnregisterPrefix(string uriPrefix)
 {
     LogHelper.LogInfo(_logger, "Stop listening on prefix: " + uriPrefix);
     CheckDisposed();
     return(true);
 }
Exemplo n.º 4
0
        /// <summary>
        /// Start accepting incoming requests.
        /// </summary>
        public void Start()
        {
            CheckDisposed();

            LogHelper.LogInfo(Logger, "Start");

            // Make sure there are no race conditions between Start/Stop/Abort/Close/Dispose.
            // Start needs to setup all resources. Abort/Stop must not interfere while Start is
            // allocating those resources.
            lock (_internalLock)
            {
                try
                {
                    CheckDisposed();
                    if (_state == State.Started)
                    {
                        return;
                    }

                    try
                    {
                        foreach (var urlPrefix in Options.UrlPrefixes)
                        {
                            RelayConnectionStringBuilder rcb = new RelayConnectionStringBuilder();

                            var relayListener = new HybridConnectionListener(
                                new UriBuilder(urlPrefix.FullPrefix)
                            {
                                Scheme = "sb", Port = -1
                            }.Uri,
                                urlPrefix.TokenProvider != null ? urlPrefix.TokenProvider : Options.TokenProvider);

                            relayListener.RequestHandler = (ctx) => requestHandler(new RequestContext(ctx, new Uri(urlPrefix.FullPrefix)));
                            relayListener.AcceptHandler  = WebSocketAcceptHandler;
                            _relayListeners.Add(relayListener);
                        }
                    }
                    catch (Exception exception)
                    {
                        LogHelper.LogException(Logger, ".Ctor", exception);
                        throw;
                    }


                    foreach (var listener in _relayListeners)
                    {
                        listener.OpenAsync().GetAwaiter().GetResult();
                        //listener.AcceptConnectionAsync().ContinueWith((t) => {
                        //    Console.WriteLine(t);
                        //});
                    }
                    _state = State.Started;
                }
                catch (Exception exception)
                {
                    // Make sure the HttpListener instance can't be used if Start() failed.
                    _state = State.Disposed;
                    LogHelper.LogException(Logger, "Start", exception);
                    throw;
                }
            }
        }
Exemplo n.º 5
0
 internal void RegisterPrefix(string uriPrefix, int contextId)
 {
     LogHelper.LogInfo(_logger, "Listening on prefix: " + uriPrefix);
     CheckDisposed();
 }