/// <summary> /// Determines whether the specified <see cref="System.Object" />, is equal to this instance. /// </summary> /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { ResonanceUdpDiscoveredService <TDiscoveryInfo> other = obj as ResonanceUdpDiscoveredService <TDiscoveryInfo>; if (obj == null || this == null || other == null) { return(false); } return(other.Address == this.Address); }
private void ReceiveThreadMethod() { while (IsStarted) { try { var clientEndPoint = new IPEndPoint(IPAddress.Any, Port); var data = _udpClient.Receive(ref clientEndPoint); Logger.LogDebug($"Data received ({data.ToFriendlyByteSize()}), decoding discovery information..."); TDiscoveryInfo discoveryInfo = Decoder.Decode <TDiscoveryInfo>(data); Logger.LogDebug($"Discovery information decoded:\n{discoveryInfo.ToJsonString()}"); string address = clientEndPoint.Address.ToString(); Logger.LogDebug($"Service host address: {address}."); ResonanceUdpDiscoveredService <TDiscoveryInfo> discoveredService = new ResonanceUdpDiscoveredService <TDiscoveryInfo>(discoveryInfo, address); //validate service existence using TCP connection. if (EnableTcpValidation) { Logger.LogDebug($"Validating service existence using TCP..."); try { TcpClient client = new TcpClient(); client.Connect(address, Port); client.Dispose(); Logger.LogDebug("Service validated."); } catch { var missingService = _discoveredServices.ToList().FirstOrDefault(existingService => _discoveredServiceCompareFunc(existingService, discoveredService)); if (missingService != null) { Logger.LogDebug("Service TCP validation failed. Reporting service lost..."); _discoveredServices.Remove(missingService); Logger.LogDebug($"Total discovered services: {_discoveredServices.Count}."); ServiceLost?.Invoke(this, new ResonanceDiscoveredServiceEventArgs <ResonanceUdpDiscoveredService <TDiscoveryInfo>, TDiscoveryInfo>(missingService)); } else { Logger.LogDebug("Service TCP validation failed."); } continue; } } if (!_discoveredServices.ToList().Exists(existingService => _discoveredServiceCompareFunc(existingService, discoveredService))) { Logger.LogInformation("New service discovered on address {Address}. Reporting service discovered...", discoveredService.Address); _discoveredServices.Add(discoveredService); Logger.LogDebug($"Total discovered services: {_discoveredServices.Count}."); ServiceDiscovered?.Invoke(this, new ResonanceDiscoveredServiceEventArgs <ResonanceUdpDiscoveredService <TDiscoveryInfo>, TDiscoveryInfo>(discoveredService)); } else { Logger.LogDebug("Service was already discovered."); } } catch (Exception ex) { if (ex.Message != "A blocking operation was interrupted by a call to WSACancelBlockingCall") { Logger.LogError(ex, "Error occurred on discovery method."); } } } }