| | | 1 | | namespace LOCKnet.Core.Security; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Implementierung von <see cref="IActivityMonitor"/>. |
| | | 5 | | /// Verwendet einen <see cref="System.Threading.Timer"/> — kein UI-Thread nötig. |
| | | 6 | | /// Bei Timeout wird <see cref="ISessionManager.Lock"/> aufgerufen. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class ActivityMonitor : IActivityMonitor |
| | | 9 | | { |
| | | 10 | | private readonly ISessionManager _session; |
| | | 11 | | private System.Threading.Timer? _timer; |
| | 32 | 12 | | private TimeSpan _timeout = TimeSpan.FromSeconds(60); |
| | | 13 | | private volatile bool _running; |
| | | 14 | | private bool _disposed; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initialisiert eine neue Instanz von <see cref="ActivityMonitor"/>. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="session">Die Sitzung, die bei Timeout gesperrt wird.</param> |
| | 32 | 20 | | public ActivityMonitor(ISessionManager session) |
| | 32 | 21 | | { |
| | 32 | 22 | | ArgumentNullException.ThrowIfNull(session); |
| | 32 | 23 | | _session = session; |
| | 32 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | | 27 | | public TimeSpan Timeout |
| | | 28 | | { |
| | 3 | 29 | | get => _timeout; |
| | | 30 | | set |
| | 29 | 31 | | { |
| | 29 | 32 | | if (value <= TimeSpan.Zero) |
| | 2 | 33 | | throw new ArgumentOutOfRangeException(nameof(value), "Timeout muss positiv sein."); |
| | 27 | 34 | | _timeout = value; |
| | 27 | 35 | | if (_running) |
| | 1 | 36 | | ResetTimer(); |
| | 27 | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | 11 | 41 | | public bool IsRunning => _running; |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | 51 | 44 | | public DateTimeOffset LastActivity { get; private set; } = DateTimeOffset.UtcNow; |
| | | 45 | | |
| | | 46 | | /// <inheritdoc/> |
| | | 47 | | public void Start() |
| | 14 | 48 | | { |
| | 14 | 49 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 14 | 50 | | if (_running) return; |
| | | 51 | | |
| | 12 | 52 | | LastActivity = DateTimeOffset.UtcNow; |
| | 12 | 53 | | _running = true; |
| | 12 | 54 | | _timer = new System.Threading.Timer(OnTimeout, null, _timeout, System.Threading.Timeout.InfiniteTimeSpan); |
| | 13 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc/> |
| | | 58 | | public void Stop() |
| | 35 | 59 | | { |
| | 35 | 60 | | _running = false; |
| | 35 | 61 | | _timer?.Change(System.Threading.Timeout.InfiniteTimeSpan, System.Threading.Timeout.InfiniteTimeSpan); |
| | 35 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc/> |
| | | 65 | | public void RecordActivity() |
| | 5 | 66 | | { |
| | 5 | 67 | | if (_running) |
| | 2 | 68 | | { |
| | 2 | 69 | | LastActivity = DateTimeOffset.UtcNow; |
| | 2 | 70 | | ResetTimer(); |
| | 2 | 71 | | } |
| | 5 | 72 | | } |
| | | 73 | | |
| | | 74 | | private void ResetTimer() |
| | 3 | 75 | | { |
| | 3 | 76 | | _timer?.Change(_timeout, System.Threading.Timeout.InfiniteTimeSpan); |
| | 3 | 77 | | } |
| | | 78 | | |
| | | 79 | | private void OnTimeout(object? state) |
| | 1 | 80 | | { |
| | 1 | 81 | | if (!_running) return; |
| | 1 | 82 | | _session.Lock(); |
| | 1 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <inheritdoc/> |
| | | 86 | | public void Dispose() |
| | 14 | 87 | | { |
| | 15 | 88 | | if (_disposed) return; |
| | 13 | 89 | | _disposed = true; |
| | 13 | 90 | | Stop(); |
| | 13 | 91 | | _timer?.Dispose(); |
| | 13 | 92 | | _timer = null; |
| | 14 | 93 | | } |
| | | 94 | | } |