/// <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; } } }
/// <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; } } }