File: RemoteStateServer.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (190 lines) | stat: -rw-r--r-- 5,242 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// System.Web.SessionState.RemoteStateServer
//
// Author(s):
//  Jackson Harper (jackson@ximian.com)
//  Gonzalo Paniagua (gonzalo@ximian.com)
//  Marek Habersack (grendello@gmail.com)
//
// (C) 2003-2006 Novell, Inc (http://www.novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Threading;
using System.Web.Caching;

namespace System.Web.SessionState {
	class LockableStateServerItem
	{
		public StateServerItem item;
		public ReaderWriterLock rwlock;

		public LockableStateServerItem (StateServerItem item)
		{
			this.item = item;
			this.rwlock = new ReaderWriterLock ();
		}
	}
	
	internal class RemoteStateServer : MarshalByRefObject {
		const Int32 lockAcquireTimeout = 30000;
		Cache cache;
		
		internal RemoteStateServer ()
		{
			cache = new Cache ();
		}

		void Insert (string id, LockableStateServerItem item)
		{
			cache.Insert (id, item, null, Cache.NoAbsoluteExpiration, new TimeSpan (0, item.item.Timeout, 0));
		}

		LockableStateServerItem Retrieve (string id)
		{
			return cache [id] as LockableStateServerItem;
		}

		internal void CreateUninitializedItem (string id, int timeout)
		{
			StateServerItem item = new StateServerItem (timeout);
			item.Action = SessionStateActions.InitializeItem;
			LockableStateServerItem cacheItem = new LockableStateServerItem (item);
			Insert (id, cacheItem);
		}
		
		internal StateServerItem GetItem (string id,
						  out bool locked,
						  out TimeSpan lockAge,
						  out object lockId,
						  out SessionStateActions actions,
						  bool exclusive)
		{
			locked = false;
			lockAge = TimeSpan.MinValue;
			lockId = Int32.MinValue;
			actions = SessionStateActions.None;
			
			LockableStateServerItem item = Retrieve (id);
			if (item == null || item.item.IsAbandoned ())
				return null;
			
			try {
				item.rwlock.AcquireReaderLock (lockAcquireTimeout);
				if (item.item.Locked) {
					locked = true;
					lockAge = DateTime.UtcNow.Subtract (item.item.LockedTime);
					lockId = item.item.LockId;
					return null;
				}

				item.rwlock.ReleaseReaderLock ();
				if (exclusive) {
					item.rwlock.AcquireWriterLock (lockAcquireTimeout);
					item.item.Locked = true;
					item.item.LockedTime = DateTime.UtcNow;
					item.item.LockId++;
					lockId = item.item.LockId;
				}
			} catch {
				throw;
			} finally {
				if (item.rwlock.IsReaderLockHeld)
					item.rwlock.ReleaseReaderLock ();
				
				if (item.rwlock.IsWriterLockHeld)
					item.rwlock.ReleaseWriterLock ();
			}
			
			actions = item.item.Action;
			return item.item;
		}

		internal void Remove (string id, object lockid)
		{
			cache.Remove (id);
		}

		internal void ResetItemTimeout (string id)
		{
			LockableStateServerItem item = Retrieve (id);
			if (item == null)
				return;
			item.item.Touch ();
		}

		internal void ReleaseItemExclusive (string id, object lockId)
		{
			LockableStateServerItem item = Retrieve (id);
			if (item == null || item.item.LockId != (Int32)lockId)
				return;
			
			try {
				item.rwlock.AcquireWriterLock (lockAcquireTimeout);
				item.item.Locked = false;
			} catch {
				throw;
			} finally {
				if (item.rwlock.IsWriterLockHeld)
					item.rwlock.ReleaseWriterLock ();
			}
		}
		
		internal void SetAndReleaseItemExclusive (string id, byte [] collection_data, byte [] sobjs_data,
							  object lockId, int timeout, bool newItem)
		{
			LockableStateServerItem item = Retrieve (id);
			bool fresh = false;
			
			if (newItem || item == null) {
				item = new LockableStateServerItem (new StateServerItem (collection_data, sobjs_data, timeout));
				item.item.LockId = (Int32)lockId;
				fresh = true;
			} else {
				if (item.item.LockId != (Int32)lockId)
					return;
				Remove (id, lockId);
			}

			try {
				item.rwlock.AcquireWriterLock (lockAcquireTimeout);
				item.item.Locked = false;
				if (!fresh) {
					item.item.CollectionData = collection_data;
					item.item.StaticObjectsData = sobjs_data;
				}
				Insert (id, item);
			} catch {
				throw;
			} finally {
				if (item.rwlock.IsWriterLockHeld)
					item.rwlock.ReleaseWriterLock ();
			}
		}
		
		public override object InitializeLifetimeService ()
		{
			return null; // just in case...
		}
	}
}