RemoteStateServer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // System.Web.SessionState.RemoteStateServer
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. // Marek Habersack ([email protected])
  8. //
  9. // (C) 2003-2006 Novell, Inc (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Threading;
  34. using System.Web.Caching;
  35. namespace System.Web.SessionState {
  36. class LockableStateServerItem
  37. {
  38. public StateServerItem item;
  39. public ReaderWriterLock rwlock;
  40. public LockableStateServerItem (StateServerItem item)
  41. {
  42. this.item = item;
  43. this.rwlock = new ReaderWriterLock ();
  44. }
  45. }
  46. internal class RemoteStateServer : MarshalByRefObject {
  47. const Int32 lockAcquireTimeout = 30000;
  48. Cache cache;
  49. internal RemoteStateServer ()
  50. {
  51. cache = new Cache ();
  52. }
  53. void Insert (string id, LockableStateServerItem item)
  54. {
  55. cache.Insert (id, item, null, Cache.NoAbsoluteExpiration, new TimeSpan (0, item.item.Timeout, 0));
  56. }
  57. LockableStateServerItem Retrieve (string id)
  58. {
  59. return cache [id] as LockableStateServerItem;
  60. }
  61. internal void CreateUninitializedItem (string id, int timeout)
  62. {
  63. StateServerItem item = new StateServerItem (timeout);
  64. item.Action = SessionStateActions.InitializeItem;
  65. LockableStateServerItem cacheItem = new LockableStateServerItem (item);
  66. Insert (id, cacheItem);
  67. }
  68. internal StateServerItem GetItem (string id,
  69. out bool locked,
  70. out TimeSpan lockAge,
  71. out object lockId,
  72. out SessionStateActions actions,
  73. bool exclusive)
  74. {
  75. Console.WriteLine ("RemoteStateServer.GetItem");
  76. Console.WriteLine ("\tid == {0}", id);
  77. locked = false;
  78. lockAge = TimeSpan.MinValue;
  79. lockId = Int32.MinValue;
  80. actions = SessionStateActions.None;
  81. LockableStateServerItem item = Retrieve (id);
  82. if (item == null || item.item.IsAbandoned ()) {
  83. Console.WriteLine ("\tNo item for that id (abandoned == {0})", item != null ? item.item.IsAbandoned() : false);
  84. return null;
  85. }
  86. try {
  87. Console.WriteLine ("\tacquiring reader lock");
  88. item.rwlock.AcquireReaderLock (lockAcquireTimeout);
  89. if (item.item.Locked) {
  90. Console.WriteLine ("\titem is locked");
  91. locked = true;
  92. lockAge = DateTime.UtcNow.Subtract (item.item.LockedTime);
  93. lockId = item.item.LockId;
  94. return null;
  95. }
  96. Console.WriteLine ("\teleasing reader lock");
  97. item.rwlock.ReleaseReaderLock ();
  98. if (exclusive) {
  99. Console.WriteLine ("\tacquiring writer lock");
  100. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  101. Console.WriteLine ("\tlocking the item");
  102. item.item.Locked = true;
  103. item.item.LockedTime = DateTime.UtcNow;
  104. item.item.LockId++;
  105. Console.WriteLine ("\tnew lock id == {0}", item.item.LockId);
  106. lockId = item.item.LockId;
  107. }
  108. } catch {
  109. throw;
  110. } finally {
  111. if (item.rwlock.IsReaderLockHeld) {
  112. Console.WriteLine ("\treleasing reader lock [finally]");
  113. item.rwlock.ReleaseReaderLock ();
  114. }
  115. if (item.rwlock.IsWriterLockHeld) {
  116. Console.WriteLine ("\treleasing writer lock [finally]");
  117. item.rwlock.ReleaseWriterLock ();
  118. }
  119. }
  120. Console.WriteLine ("\treturning an item");
  121. actions = item.item.Action;
  122. return item.item;
  123. }
  124. internal void Remove (string id, object lockid)
  125. {
  126. cache.Remove (id);
  127. }
  128. internal void ResetItemTimeout (string id)
  129. {
  130. LockableStateServerItem item = Retrieve (id);
  131. if (item == null)
  132. return;
  133. item.item.Touch ();
  134. }
  135. internal void ReleaseItemExclusive (string id, object lockId)
  136. {
  137. LockableStateServerItem item = Retrieve (id);
  138. if (item == null || item.item.LockId != (Int32)lockId)
  139. return;
  140. try {
  141. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  142. item.item.Locked = false;
  143. } catch {
  144. throw;
  145. } finally {
  146. if (item.rwlock.IsWriterLockHeld)
  147. item.rwlock.ReleaseWriterLock ();
  148. }
  149. }
  150. internal void SetAndReleaseItemExclusive (string id, byte [] collection_data, byte [] sobjs_data,
  151. object lockId, int timeout, bool newItem)
  152. {
  153. Console.WriteLine ("RemoteStateServer.SetAndReleaseItemExclusive");
  154. Console.WriteLine ("\tid == {0}", id);
  155. LockableStateServerItem item = Retrieve (id);
  156. bool fresh = false;
  157. if (newItem || item == null) {
  158. Console.WriteLine ("\tnew item");
  159. item = new LockableStateServerItem (new StateServerItem (collection_data, sobjs_data, timeout));
  160. item.item.LockId = (Int32)lockId;
  161. fresh = true;
  162. } else {
  163. if (item.item.LockId != (Int32)lockId) {
  164. Console.WriteLine ("\tLockId mismatch ({0} != {1})", item.item.LockId, lockId);
  165. return;
  166. }
  167. Console.WriteLine ("\tremoving from cache");
  168. Remove (id, lockId);
  169. }
  170. try {
  171. Console.WriteLine ("\tacquiring writer lock");
  172. item.rwlock.AcquireWriterLock (lockAcquireTimeout);
  173. item.item.Locked = false;
  174. if (!fresh) {
  175. Console.WriteLine ("\tnot fresh = updating data");
  176. item.item.CollectionData = collection_data;
  177. item.item.StaticObjectsData = sobjs_data;
  178. }
  179. Console.WriteLine ("\tInserting in cache");
  180. Insert (id, item);
  181. } catch {
  182. throw;
  183. } finally {
  184. if (item.rwlock.IsWriterLockHeld) {
  185. Console.WriteLine ("\treleasing writer lock [finally]");
  186. item.rwlock.ReleaseWriterLock ();
  187. }
  188. }
  189. }
  190. public override object InitializeLifetimeService ()
  191. {
  192. return null; // just in case...
  193. }
  194. }
  195. }
  196. #endif