Projekt

Obecné

Profil

Stáhnout (1.45 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6

    
7
namespace LDClient.detection {
8
    public class NetworkDetection : IDetection {
9

    
10
        private readonly uint _port;
11

    
12
        private bool _isRunning;
13
        
14
        private readonly uint _detectionPeriod;
15

    
16
        public NetworkDetection(uint port, uint detectionPeriod) {
17
            this._port = port;
18
            this._detectionPeriod = detectionPeriod;
19
        }
20

    
21
        public void DetectAsync() {
22

    
23
            var listeners = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
24

    
25
            Program.DefaultLogger.Debug($"NetworkDetection -> Checking all currently listening.");
26
            foreach (var listener in listeners) {
27
                //Program.DefaultLogger.Debug($"{listener.Address}:{listener.Port}");
28
                if (listener.Port == _port) {
29
                    Program.DefaultLogger.Info($"Found some process listening on {listener.Address}:{_port}");
30
                }
31
            }
32

    
33
        }
34

    
35

    
36
        public void RunPeriodicDetection() {
37
            Program.DefaultLogger.Info("Network periodic detector has started");
38
            _isRunning = true;
39
            while (_isRunning) {
40
                DetectAsync();
41
                Thread.Sleep((int)_detectionPeriod);
42
            }
43
        }
44

    
45
        public void StopPeriodicDetection() {
46
            _isRunning = false;
47
        }
48
    }
49
}
(2-2/3)