[Server] Invalidate session after a timeout

This commit is contained in:
lukeIam committed 2014-12-30 11:22:34 +01:00
1 parent f4583b830a
commit e4ce006b06
3 files changed
+43 -1

No files matched your search

+33 -1
View File
@@ -18,12 +18,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
namespace ServerLib
{
static class CSessionControl
{
private static readonly Dictionary<Guid, CSession> _ActiveSessions = new Dictionary<Guid, CSession>();
private static readonly Dictionary<Guid, CSession> _ActiveSessions;
private const int _UserTimeoutCheckIntervall = 120000;
private const int _UserTimeout = 120000;
static CSessionControl()
{
_ActiveSessions = new Dictionary<Guid, CSession>();
Timer timer = new Timer(_UserTimeoutCheckIntervall) {AutoReset = true, Enabled = true};
timer.Elapsed += _CheckForUserTimeouts;
timer.Start();
}
private static void _CheckForUserTimeouts(object sender, ElapsedEventArgs e)
{
var sessionIdsToRemove = _ActiveSessions.Where(pair => (DateTime.Now-pair.Value.LastSeen).TotalMilliseconds > _UserTimeout)
.Select(pair => pair.Key)
.ToList();
foreach (var sessionToRemove in sessionIdsToRemove)
{
InvalidateSessions(sessionToRemove);
}
}
public static Guid OpenSession(string userName, string password)
{
@@ -83,5 +106,14 @@ namespace ServerLib
return -1;
return _ActiveSessions[sessionId].ProfileId;
}
internal static void ResetSessionTimeout(Guid sessionId)
{
CSession value;
if (_ActiveSessions.TryGetValue(sessionId, out value))
{
value.LastSeen = DateTime.Now;
}
}
}
}