[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

+2
View File
@@ -39,6 +39,7 @@ namespace ServerLib
} }
catch (Exception) catch (Exception)
{ } { }
CSessionControl.ResetSessionTimeout(sessionKey);
return sessionKey; return sessionKey;
} }
@@ -296,6 +297,7 @@ namespace ServerLib
public bool IsServerOnline() public bool IsServerOnline()
{ {
_GetSession();
return true; return true;
} }
#endregion #endregion
+8
View File
@@ -38,12 +38,20 @@ namespace ServerLib
{ {
get { return _Roles; } get { return _Roles; }
} }
private DateTime _LastSeen;
public DateTime LastSeen
{
get { return _LastSeen; }
internal set { _LastSeen = value; }
}
public CSession(Guid id, int profileId, EUserRoles roles) public CSession(Guid id, int profileId, EUserRoles roles)
{ {
_ID = id; _ID = id;
_ProfileId = profileId; _ProfileId = profileId;
_Roles = roles; _Roles = roles;
_LastSeen = DateTime.Now;
} }
} }
} }
+33 -1
View File
@@ -18,12 +18,35 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Timers;
namespace ServerLib namespace ServerLib
{ {
static class CSessionControl 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) public static Guid OpenSession(string userName, string password)
{ {
@@ -83,5 +106,14 @@ namespace ServerLib
return -1; return -1;
return _ActiveSessions[sessionId].ProfileId; return _ActiveSessions[sessionId].ProfileId;
} }
internal static void ResetSessionTimeout(Guid sessionId)
{
CSession value;
if (_ActiveSessions.TryGetValue(sessionId, out value))
{
value.LastSeen = DateTime.Now;
}
}
} }
} }