private async Task DiscoveryLoop() { try { while (_shouldContinueDiscoveryTask) { try { // Blocks until a message returns on this socket from a remote host. var udpReceiveResult = await _udpServer.ReceiveAsync(); if (IsDiscoveryEnabled && Protocol.ValidateDiscoveryRequest(udpReceiveResult.Buffer)) { var response = Protocol.CreateDiscoveryResponseMessage(_port); await _udpServer.SendAsync(response, response.Length, udpReceiveResult.RemoteEndPoint); var discoveryRequestEventArgs = new DiscoveryRequestReceivedEventArgs(udpReceiveResult.RemoteEndPoint.ToString()); OnDiscoveryRequestReceived(discoveryRequestEventArgs); } } catch (Exception e) { Console.WriteLine(e.ToString()); } await Task.Delay(DEFAULT_DELAY); } } catch (Exception e) { throw new HubFailureException($"The {nameof(Hub)} found an unrecoverable problem with the discovery system and it has been disabled. {nameof(Hub)} should still work, but channels are not going to be able to discover this {nameof(Hub)} anymore. Error was: {e.Message}", e); } }
/// <summary> /// Triggers a <see cref="DiscoveryRequestReceived"/> event. /// </summary> /// <param name="e">Event arguments.</param> protected virtual void OnDiscoveryRequestReceived(DiscoveryRequestReceivedEventArgs e) { DiscoveryRequestReceived?.Invoke(this, e); }