public void BeginMonitoringConnectionsAsync()
        {
            backgroundWorker         = new BackgroundWorker();
            backgroundWorker.DoWork += (s, e) =>
            {
                while (true)
                {
                    var newConnections = IpHlpApi.GetTcpTableEx();

                    // for each old entry that doesnt exist in new list, remove
                    for (int i = 0; i < TcpInfo.Count; i++)
                    {
                        var existingFound = newConnections.Any(x => TcpInfo[i].Equals(x));
                        if (!existingFound)
                        {
                            new Action(() => TcpInfo.RemoveAt(i)).ExecuteInSpecificThread(dispatcher);
                            i--;
                        }
                    }

                    // for each new entry that doesnt exist, add
                    for (int i = 0; i < newConnections.Length; i++)
                    {
                        bool existingFound = TcpInfo.Any(x => x.Equals(newConnections[i]));
                        if (!existingFound)
                        {
                            var entry = new TcpConnection(newConnections[i].LocalPort,
                                                          newConnections[i].LocalAddress.ToString(),
                                                          newConnections[i].RemotePort,
                                                          newConnections[i].RemoteAddress.ToString(),
                                                          newConnections[i].dwOwningPid,
                                                          newConnections[i].dwState,
                                                          AsyncPropertyPendingText,
                                                          AsyncPropertyLoadingText);
                            new Action(() => TcpInfo.Add(entry)).ExecuteInSpecificThread(dispatcher);
                        }
                    }
                    Thread.Sleep((int)RefreshInterval.TotalMilliseconds);
                }
            };
            backgroundWorker.RunWorkerAsync();
        }
Esempio n. 2
0
        public TcpConnection(ushort localPort,
                             string localAddress,
                             ushort remotePort,
                             string remoteAddress,
                             int processId,
                             TcpState state,
                             string pendingText,
                             string loadingText)
        {
            this.LocalPort     = localPort;
            this.LocalAddress  = localAddress;
            this.RemotePort    = remotePort;
            this.RemoteAddress = remoteAddress;
            this.ProcessId     = processId;
            this.State         = state;
            this.PendingText   = pendingText;
            this.LoadingText   = loadingText;

            RemoteMacAddressAsync             = PendingText;
            RemoteMacAddressManufacturerAsync = CreatePendingInstance <KnownMacManufacturer>();
            Task.Factory.StartNew(() =>
            {
                RemoteMacAddressAsync = LoadingText;
                var remoteMacAddress  = IpHlpApi.ResolvePhysicalAddress(RemoteAddress);
                RemoteMacAddressAsync = remoteMacAddress != null && !string.IsNullOrWhiteSpace(remoteMacAddress.ToString()) ? remoteMacAddress.ToString() : null;

                RemoteMacAddressManufacturerAsync = CreateLoadingInstance <KnownMacManufacturer>();
                RemoteMacAddressManufacturerAsync = KnownMacManufacturers.Instance.Lookup(RemoteMacAddressAsync);
            });

            LocalPortCommonServiceAsync = CreatePendingInstance <CommonService>();
            Task.Factory.StartNew(() =>
            {
                LocalPortCommonServiceAsync = CreateLoadingInstance <CommonService>();
                LocalPortCommonServiceAsync = CommonTcpServices.Instance.Lookup(LocalPort);
            });

            RemotePortCommonServiceAsync = CreatePendingInstance <CommonService>();
            Task.Factory.StartNew(() =>
            {
                RemotePortCommonServiceAsync = CreateLoadingInstance <CommonService>();
                RemotePortCommonServiceAsync = CommonTcpServices.Instance.Lookup(RemotePort);
            });

            RunningProcessAsync = CreatePendingInstance <RunningProcess>();
            Task.Factory.StartNew(() =>
            {
                RunningProcessAsync = CreateLoadingInstance <RunningProcess>();
                RunningProcessAsync = ProcessMonitor.Instance.GetProcessInfo(ProcessId);
            });

            RemoteHostnameAsync = PendingText;
            Task.Factory.StartNew(() =>
            {
                RemoteHostnameAsync = LoadingText;
                RemoteHostnameAsync = DnsHostCache.Instance.Resolve(IPAddress.Parse(RemoteAddress));
            });

            LocalHostnameAsync = PendingText;
            Task.Factory.StartNew(() =>
            {
                LocalHostnameAsync = LoadingText;
                LocalHostnameAsync = DnsHostCache.Instance.Resolve(IPAddress.Parse(LocalAddress));
            });

            RemoteGeoAsync = CreatePendingInstance <GeoIpEntry>();
            Task.Factory.StartNew(() =>
            {
                RemoteGeoAsync = CreateLoadingInstance <GeoIpEntry>();
                RemoteGeoAsync = GeoIpCache.Instance.Lookup(IPAddress.Parse(RemoteAddress));
            });
        }