/// <summary> /// Updates the arp item. /// </summary> /// <param name="physicalAddress">The physical address.</param> /// <param name="ipAddress">The ip address.</param> /// <param name="timestamp">The timestamp.</param> /// <returns>ArpItem.</returns> /// <autogeneratedoc /> // ReSharper disable once UnusedMethodReturnValue.Local private ArpItem _UpdateArpItem(PhysicalAddress physicalAddress, IPAddress ipAddress, DateTimeOffset timestamp) { lock (_updateLock) { if (_physical2ArpItem.TryGetValue(physicalAddress, out var arpItem)) { // Update IP Address if (!arpItem.IPAddress.Equals(ipAddress)) { // Remove old entry and add new _ipAddress2ArpItem.Remove(arpItem.IPAddress); arpItem.IPAddress = ipAddress; _ipAddress2ArpItem.Add(ipAddress, arpItem); } // Update cache value based on this session arpItem.CreatedTimestamp = timestamp; } else { // Create new entry arpItem = new ArpItem(physicalAddress, ipAddress, timestamp); // Add to both Dictionaries _physical2ArpItem.Add(physicalAddress, arpItem); _ipAddress2ArpItem.Add(ipAddress, arpItem); } return(arpItem); } }
/// <summary> /// get arp item as an asynchronous operation. /// </summary> /// <param name="ipAddress">The ip address.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Task<ArpItem>.</returns> /// <autogeneratedoc /> public async Task <ArpItem> GetArpItemAsync(IPAddress ipAddress, CancellationToken cancellationToken) { ArpItem arpItem = null; // TODO: Rename to be Sync try { lock (_updateLock) { if (_ipAddress2ArpItem.TryGetValue(ipAddress, out arpItem)) { return(arpItem); } } cancellationToken.ThrowIfCancellationRequested(); // OK if ping fails or times out at it will still populate the arp cache await _pingService.PingIpAddressAsync(ipAddress); cancellationToken.ThrowIfCancellationRequested(); await RefreshInternalAsync(Timeout, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); lock (_updateLock) { _ipAddress2ArpItem.TryGetValue(ipAddress, out arpItem); } } catch (OperationCanceledException ex) { Logger.LogDebug(ex, "GetArpItemAsync task cancelled"); } return(arpItem); }