diff options
Diffstat (limited to 'OpenSim/Framework/Communications/Cache')
-rw-r--r-- | OpenSim/Framework/Communications/Cache/AuthedSessionCache.cs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/OpenSim/Framework/Communications/Cache/AuthedSessionCache.cs b/OpenSim/Framework/Communications/Cache/AuthedSessionCache.cs new file mode 100644 index 0000000..b989997 --- /dev/null +++ b/OpenSim/Framework/Communications/Cache/AuthedSessionCache.cs | |||
@@ -0,0 +1,107 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | |||
5 | namespace OpenSim.Framework.Communications.Cache | ||
6 | { | ||
7 | public class AuthedSessionCache | ||
8 | { | ||
9 | public class CacheData | ||
10 | { | ||
11 | private static readonly DateTime UNIX_EPOCH = new DateTime(1970, 1, 1); | ||
12 | private string m_session_id; | ||
13 | private string m_agent_id; | ||
14 | private int m_expire; | ||
15 | |||
16 | private int get_current_unix_time() | ||
17 | { | ||
18 | return (int)(DateTime.UtcNow - UNIX_EPOCH).TotalSeconds; | ||
19 | } | ||
20 | |||
21 | public CacheData(string sid, string aid) | ||
22 | { | ||
23 | m_session_id = sid; | ||
24 | m_agent_id = aid; | ||
25 | m_expire = get_current_unix_time() + DEFAULT_LIFETIME; | ||
26 | } | ||
27 | |||
28 | public CacheData(string sid, string aid, int time_now) | ||
29 | { | ||
30 | m_session_id = sid; | ||
31 | m_agent_id = aid; | ||
32 | m_expire = time_now + DEFAULT_LIFETIME; | ||
33 | } | ||
34 | |||
35 | public string SessionID | ||
36 | { | ||
37 | get { return m_session_id; } | ||
38 | set { m_session_id = value; } | ||
39 | } | ||
40 | |||
41 | public string AgentID | ||
42 | { | ||
43 | get { return m_agent_id; } | ||
44 | set { m_agent_id = value; } | ||
45 | } | ||
46 | |||
47 | public bool isExpired | ||
48 | { | ||
49 | get { return m_expire < get_current_unix_time(); } | ||
50 | } | ||
51 | |||
52 | public void Renew() | ||
53 | { | ||
54 | m_expire = get_current_unix_time() + DEFAULT_LIFETIME; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | private static readonly int DEFAULT_LIFETIME = 30; | ||
59 | private Dictionary<string, CacheData> m_authed_sessions = new Dictionary<string,CacheData>(); | ||
60 | private int m_session_lifetime = DEFAULT_LIFETIME; | ||
61 | |||
62 | public AuthedSessionCache() | ||
63 | { | ||
64 | m_session_lifetime = DEFAULT_LIFETIME; | ||
65 | } | ||
66 | |||
67 | public AuthedSessionCache(int timeout) | ||
68 | { | ||
69 | m_session_lifetime = timeout; | ||
70 | } | ||
71 | |||
72 | public CacheData getCachedSession(string session_id, string agent_id) | ||
73 | { | ||
74 | CacheData ret = null; | ||
75 | lock (m_authed_sessions) | ||
76 | { | ||
77 | if (m_authed_sessions.ContainsKey(session_id)) | ||
78 | { | ||
79 | CacheData cached_session = m_authed_sessions[session_id]; | ||
80 | if (!cached_session.isExpired && cached_session.AgentID == agent_id) | ||
81 | { | ||
82 | ret = m_authed_sessions[session_id]; | ||
83 | // auto renew | ||
84 | m_authed_sessions[session_id].Renew(); | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | return ret; | ||
89 | } | ||
90 | |||
91 | public void Add(string session_id, string agent_id) | ||
92 | { | ||
93 | CacheData data = new CacheData(session_id, agent_id); | ||
94 | lock (m_authed_sessions) | ||
95 | { | ||
96 | if (m_authed_sessions.ContainsKey(session_id)) | ||
97 | { | ||
98 | m_authed_sessions[session_id] = data; | ||
99 | } | ||
100 | else | ||
101 | { | ||
102 | m_authed_sessions.Add(session_id, data); | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | } | ||