public void Dispose() { if (_multi != null) { _multi.OnDataRecived -= MessageRecived; _multi.StopListening(); _multi.Dispose(); _multi = null; } if (_broad != null) { _broad.OnDataRecived -= MessageRecived; _broad.StopListening(); _broad.Dispose(); _broad = null; } }
public MasterNetworker(string moduleName, ILogger logger) { this.logger = logger; _moduleName = moduleName; if (_broad == null && _multi == null) { _broad = new BroadcastConnector(logger); _multi = new MulticastConnector(logger); _broad.Listen(); _multi.Listen(); } if (_broad != null) { _broad.OnDataRecived += MessageRecived; } if (_multi != null) { _multi.OnDataRecived += MessageRecived; } }
private void MessageRecived(object sender, TransmissionEventArgs e) { var localIps = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().SelectMany(x => x.GetIPProperties().UnicastAddresses).Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork); var isLocal = localIps.Any(x => x.Address.Equals(e.Ip)); if (isLocal) { return; } if (_broad != null && sender is MulticastConnector) { _broad.OnDataRecived -= MessageRecived; _broad.StopListening(); _broad.Dispose(); _broad = null; } else if (_multi != null && sender is BroadcastConnector) { if (_broadcastsAhead > 10) { _multi.OnDataRecived -= MessageRecived; _multi.StopListening(); _multi.Dispose(); _multi = null; } else { _broadcastsAhead++; } } var moduleName = e.Data.ModuleName.Value; var eventType = e.Data.Type.Value; if (!_knownModules.ContainsKey(moduleName)) { _knownModules.TryAdd(moduleName, e.Ip); OnChange?.Invoke(this, new ChangeDetectedEventArgs { ModuleIp = e.Ip, ModuleName = moduleName, Type = ChangeType.ModuleAddress }); } else if (!_knownModules[moduleName].Equals(e.Ip)) { _knownModules[moduleName] = e.Ip; OnChange?.Invoke(this, new ChangeDetectedEventArgs { ModuleIp = e.Ip, ModuleName = moduleName, Type = ChangeType.ModuleAddress }); } if (eventType == "PresetChange") { OnChange?.Invoke(this, new ChangeDetectedEventArgs { ModuleIp = e.Ip, ModuleName = moduleName, Type = e.Data.Deleted.Value ? ChangeType.PresetDeleted : ChangeType.PresetUpserted, PresetName = e.Data.PresetName.Value }); } else if (eventType == "ModuleChange") { OnChange?.Invoke(this, new ChangeDetectedEventArgs { ModuleIp = e.Ip, ModuleName = moduleName, Type = ChangeType.ModuleSettings }); } }